#!/usr/bin/perl

# Copryight (C) 2004-2005  Joshua Hoblitt
#
# $Id: 56_client_find_instances.t,v 1.3 2008-07-10 02:38:24 jhoblitt Exp $

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

use Apache::Test qw( -withtestmore );

plan tests => 29;

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, volume == undef
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );
    $neb->create( "foo" );

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

    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;

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

    my $locations = $neb->find_instances( "neb://node01/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;

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

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

    is($locations, undef, "no instances on specified volume" );
}

Test::Nebulous->setup;

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

    my $locations = $neb->find_instances( "neb://node02/foo", "any" );

    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", "node01" );
    $neb->replicate( "foo", "node01" );

    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;
