#!/usr/bin/perl

# Copryight (C) 2004-2005  Joshua Hoblitt
#
# $Id: 04_server_replicate_object.t,v 1.10 2007-05-08 02:27:42 jhoblitt Exp $

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

use Test::More tests => 16;

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

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

my $test_xattr = undef;

my $neb = Nebulous::Server->new(
    dsn         => "DBI:mysql:database=test:host=localhost",
    dbuser      => "test",
    dbpasswd    => "",
);

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
    $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;

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

Test::Nebulous->setup;

eval {
    $neb->create_object('foo');
    $neb->replicate_object('foo', 'bar');
};
like($@, qr/is 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;
