#!/usr/bin/perl

# Copryight (C) 2004-2005  Joshua Hoblitt
#
# $Id: 56_client_find_instances.t,v 1.1 2005-12-03 02:52:31 jhoblitt Exp $

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

use Apache::Test qw( -withtestmore );

plan tests => 19;

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;

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

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

    is( scalar @$locations, 1, "found 1" );
    like( @$locations[0], qr/file:/, "URIs match" );
    ok( -e _get_file_path( @$locations[0] ), "URI matches file" );
}

Test::Nebulous->setup;

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

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

    is( scalar @$locations, 2, "found 2" );
    like( @$locations[0], qr/file:/, "URIs match" );
    like( @$locations[1], qr/file:/, "URIs match" );
    ok( -e _get_file_path( @$locations[0] ), "URI matches file" );
    ok( -e _get_file_path( @$locations[1] ), "URI matches file" );
}

Test::Nebulous->setup;

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

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

    is( scalar @$locations, 1, "found 1" );
    like( @$locations[0], qr/file:/, "URIs match" );
    ok( -e _get_file_path( @$locations[0] ), "URI matches file" );
}

Test::Nebulous->setup;

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

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

    is( scalar @$locations, 2, "found 2" );
    like( @$locations[0], qr/file:/, "URIs match" );
    like( @$locations[1], qr/file:/, "URIs match" );
    ok( -e _get_file_path( @$locations[0] ), "URI matches file" );
    ok( -e _get_file_path( @$locations[1] ), "URI matches file" );
}

Test::Nebulous->setup;

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

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

Test::Nebulous->setup;

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

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

Test::Nebulous->setup;

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

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

Test::Nebulous->cleanup;
