#!/usr/bin/perl

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

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

use Test::More tests => 28;

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
    my $uri = $neb->create_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
    my $uri = $neb->create_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
    my $uri = $neb->create_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
    my $uri = $neb->create_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
    my $uri = $neb->create_object("foo/bar");

    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/bar', 'user.nebulous_key xattr');
}
}

Test::Nebulous->setup;

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

    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/bar', 'user.nebulous_key xattr');
}
}

Test::Nebulous->setup;

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

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

Test::Nebulous->setup;

eval {
    $neb->create_object("foo");
    $neb->create_object("foo");
};
like($@, qr/Duplicate entry/, "object already exists");

Test::Nebulous->setup;

eval {
    $neb->create_object("foo", 'node03');
};
like($@, qr/node03 is not available/, "request volume this is full");

Test::Nebulous->setup;

eval {
    $neb->create_object("foo", 'node04');
};
like($@, qr/node04 is not available/, "request volume with allocate = FALSE, available = FALSE");

Test::Nebulous->setup;

eval {
    $neb->create_object("foo", 'node05');
};
like($@, qr/node05 is not available/, "request volume with allocate = FALSE , available = TRUE");

Test::Nebulous->setup;

eval {
    $neb->create_object("foo", 'node06');
};
like($@, qr/node06 is not available/, "request volume with allocate = TRUE, available = FALSE");

Test::Nebulous->setup;

eval {
    $neb->create_object("foo", 99);
};
like($@, qr/did not pass the \'is valid volume name\'/, "volume name doesn't exist");

Test::Nebulous->setup;

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

Test::Nebulous->setup;

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

Test::Nebulous->cleanup;
