#!/usr/bin/perl

# Copryight (C) 2004-2005  Joshua Hoblitt
#
# $Id: 04_server_replicate_object.t,v 1.16 2008-09-11 22:35:52 jhoblitt Exp $

use strict;
use warnings FATAL => qw( all );

use Test::More tests => 25;

use lib qw( ./t ./lib );

use File::ExtAttr qw( getfattr );
use Nebulous::Server;
use Test::Nebulous;
use Test::URI;
use URI::Split qw( uri_split );

my $test_xattr = undef;

my $neb = Nebulous::Server->new(
    dsn         => $NEB_DB,
    dbuser      => $NEB_USER,
    dbpasswd    => $NEB_PASS,
    memcached_servers => $NEB_MEMCACHED_SERVERS,
);

Test::Nebulous->setup;

{
    # key
    $neb->create_object("foo");
    my $uri = $neb->replicate_object("foo");

    my ($scheme, $auth, $path, $query, $frag) = uri_split($uri);
    ok(-e $path, "file exists");
    uri_scheme_ok($uri, 'file');

SKIP: {
    skip "requires xattr support", 1 unless $test_xattr;
    is(getfattr($path, 'user.nebulous_key'), 'foo', 'user.nebulous_key xattr');
}
}

Test::Nebulous->setup;

{
    # key, $volume
    $neb->create_object("foo");
    my $uri = $neb->replicate_object("foo", "node01");

    my ($scheme, $auth, $path, $query, $frag) = uri_split($uri);
    ok(-e $path, "file exists");
    uri_scheme_ok($uri, 'file');

SKIP: {
    skip "requires xattr support", 1 unless $test_xattr;
    is(getfattr($path, 'user.nebulous_key'), 'foo', 'user.nebulous_key xattr');
}
}

Test::Nebulous->setup;

{
    # key, $volume is undef
    $neb->create_object("foo");
    my $uri = $neb->replicate_object("foo", undef);

    my ($scheme, $auth, $path, $query, $frag) = uri_split($uri);
    ok(-e $path, "file exists");
    uri_scheme_ok($uri, 'file');

SKIP: {
    skip "requires xattr support", 1 unless $test_xattr;
    is(getfattr($path, 'user.nebulous_key'), 'foo', 'user.nebulous_key xattr');
}
}

Test::Nebulous->setup;

{
    # key, $volume
    $neb->create_object("foo");
    my $uri1 = $neb->replicate_object("foo", "~node01");
    my $uri2 = $neb->replicate_object("foo", "~node02");

    {
        my ($scheme, $auth, $path, $query, $frag) = uri_split($uri1);
        ok(-e $path, "file exists");
        uri_scheme_ok($uri1, 'file');

SKIP: {
    skip "requires xattr support", 1 unless $test_xattr;
        is(getfattr($path, 'user.nebulous_key'), 'foo', 'user.nebulous_key xattr');
}
    }

    {
        my ($scheme, $auth, $path, $query, $frag) = uri_split($uri2);
        ok(-e $path, "file exists");
        uri_scheme_ok($uri2, 'file');

SKIP: {
    skip "requires xattr support", 1 unless $test_xattr;
        is(getfattr($path, 'user.nebulous_key'), 'foo', 'user.nebulous_key xattr');
}
    }
}

Test::Nebulous->setup;

{
    # key, $volume
    $neb->create_object("foo");
    my $uri1 = $neb->replicate_object("foo", "any");

    {
        my ($scheme, $auth, $path, $query, $frag) = uri_split($uri1);
        ok(-e $path, "file exists");
        uri_scheme_ok($uri1, 'file');

SKIP: {
    skip "requires xattr support", 1 unless $test_xattr;
        is(getfattr($path, 'user.nebulous_key'), 'foo', 'user.nebulous_key xattr');
}
    }
}

Test::Nebulous->setup;

{
    # key, $volume
    $neb->create_object("foo");
    my $uri1 = $neb->replicate_object("foo", "~any");

    {
        my ($scheme, $auth, $path, $query, $frag) = uri_split($uri1);
        ok(-e $path, "file exists");
        uri_scheme_ok($uri1, 'file');

SKIP: {
    skip "requires xattr support", 1 unless $test_xattr;
        is(getfattr($path, 'user.nebulous_key'), 'foo', 'user.nebulous_key xattr');
}
    }
}

Test::Nebulous->setup;

eval {
    $neb->replicate_object('foo');
};
like($@, qr/is valid object key/, 'storage object does not exist');

Test::Nebulous->setup;

eval {
    $neb->create_object('foo');
    ok($neb->replicate_object('foo', 'bar'),'soft fake storage volume');
};

Test::Nebulous->setup;

eval {
    $neb->create_object('foo');
    $neb->replicate_object('foo', '~bar');
};
like($@, qr/is not a valid volume name/, 'storage volume does not exist');

Test::Nebulous->setup;

eval {
    $neb->replicate_object();
};
like($@, qr/1 - 2 were expected/, 'no params');

Test::Nebulous->setup;

eval {
    $neb->create_object('foo');
    $neb->replicate_object('foo', 'node01', 3);
};
like($@, qr/1 - 2 were expected/, 'too many params');

Test::Nebulous->cleanup;
