#!/usr/bin/perl

# Copryight (C) 2004-2005  Joshua Hoblitt
#
# $Id: 56_client_find_instances.t,v 1.5 2008-09-10 23:50:26 bills Exp $

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

use Apache::Test qw( -withtestmore );

plan tests => 8;

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

use Nebulous::Client;
use Nebulous::Util qw( :standard );
use Test::Nebulous;

my $hostport = Apache::Test->config->{ 'hostport' };

Test::Nebulous->setup;

{
    my $key = "foo";
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );
    $neb->create($key);
    $neb->replicate($key,);

    is($neb->there_can_be_only_one($key), 1, "there can be only one!");

    my $locations = $neb->find_instances( "foo" );

    is( scalar @$locations, 1, "found 1" );
}

Test::Nebulous->setup;

{
    my $key = "foo";
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );
    $neb->create($key, "node01");
    my $uri = $neb->replicate($key, "node02");

    is($neb->there_can_be_only_one($key, "node02"), 1, "there can be only one!");

    my $locations = $neb->find_instances( "foo" );

    is( scalar @$locations, 1, "found 1" );
    is($locations->[0], $uri, "instance on correct volume" );
}

Test::Nebulous->setup;

{
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );

    is( $neb->there_can_be_only_one( "foo" ), undef, "storage object does not exist" );
}

Test::Nebulous->setup;

eval {
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );

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

Test::Nebulous->setup;

eval {
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );

    $neb->there_can_be_only_one( "foo", 'read', 3 );
};
like( $@, qr/1 - 2 were expected/, "too many params" );

Test::Nebulous->cleanup;
