Skip to content

Commit

Permalink
Add txn and full test support.
Browse files Browse the repository at this point in the history
  • Loading branch information
hexfusion committed Jun 16, 2017
1 parent d1274f1 commit edc7c3b
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 25 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Revision history for Net::Etcd
0.011
[ ENHANCEMENTS ]
* Add full support for Txn with tests. Recommended for use with etcd 3.2.0+

0.010
[ ENHANCEMENTS ]
* Add intial support for snapshot
Expand Down
27 changes: 27 additions & 0 deletions examples/watch.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Net::Etcd;
use Data::Dumper;
my ($host, $port);

if ( $ENV{ETCD_TEST_HOST} and $ENV{ETCD_TEST_PORT}) {
$host = $ENV{ETCD_TEST_HOST};
$port = $ENV{ETCD_TEST_PORT};
}

my ($watch,$key);
my $etcd = Net::Etcd->new( { host => $host, port => $port } );

# create watch with callback and store events
$watch = $etcd->watch( { key => 'foo'}, sub {
my ($result) = @_;
print STDERR Dumper($result);
})->create;

$etcd->put({ key => 'foo', value => 'bar' });

$etcd->range({ key => 'foo' });

1;
12 changes: 8 additions & 4 deletions lib/Net/Etcd/KV.pm
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ Key Value role providing easy access to Put and Range classes
Range gets the keys in the range from the key-value store.
# get range
$etcd->range({key =>'test0', range_end => 'test100'})
# delete range
$etcd->range({key =>'test0', range_end => 'test100'})->delete
=cut

sub range {
Expand All @@ -51,8 +55,8 @@ sub range {
cb => $cb,
( $options ? %$options : () ),
);
$range->request;
return $range;
$range->request unless $range->hold;
return $range
}

=head2 put
Expand All @@ -61,7 +65,7 @@ Put puts the given key into the key-value store. A put request increments
the revision of the key-value store and generates one event in the event
history.
$etcd->range({key =>'test0', range_end => 'test100'})
$etcd->put({key =>'test0', value=> 'bar'})
=cut

Expand Down Expand Up @@ -125,7 +129,7 @@ sub compare {
%$self,
( $options ? %$options : () ),
);
return $cmp;
return $cmp->json_args;
}

1;
7 changes: 4 additions & 3 deletions lib/Net/Etcd/KV/Compare.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Net::Etcd::KV::Compare
=cut

our $VERSION = '0.009';
our $VERSION = '0.010';

=head1 DESCRIPTION
Expand Down Expand Up @@ -57,7 +57,7 @@ key is the subject key for the comparison operation.

has key => (
is => 'ro',
coerce => sub { return encode_base64( $_[0], '' ) },
coerce => sub { return encode_base64( $_[0], '' ) if $_[0] },
);


Expand All @@ -68,7 +68,7 @@ version is the version of the given key
=cut

has version => (
is => 'ro',
is => 'ro',
);

=head2 create_revision
Expand Down Expand Up @@ -99,6 +99,7 @@ value is the value of the given key, in bytes.

has value => (
is => 'ro',
coerce => sub { return encode_base64( $_[0], '' ) if $_[0] },
);

1;
8 changes: 5 additions & 3 deletions lib/Net/Etcd/KV/Op.pm
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,17 @@ create op
=cut

#TODO this dirty hack should be a perl data object and then make json.

sub create {
my $self = shift;
my @op;
my $put = $self->request_put;
my $range = $self->request_range;
my $delete_range = $self->request_delete_range;
push @op, '{"RequestPut":' . $put->json_args . '}' if defined $put;
push @op, '{"RequestRange":' . $range->json_args . '}' if defined $range;
push @op, '{"RequestDeleteRange":' . $delete_range->json_args . '}' if defined $delete_range;
push @op, '{"requestPut":' . $put->json_args . '}' if defined $put;
push @op, '{"requestRange":' . $range->json_args . '}' if defined $range;
push @op, '{"requestDeleteRange":' . $delete_range->json_args . '}' if defined $delete_range;
return @op;
}

Expand Down
21 changes: 11 additions & 10 deletions lib/Net/Etcd/KV/Txn.pm
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ responses in order.

has compare => (
is => 'ro',
isa => ArrayRef,
required => 1,
isa => ArrayRef,
required => 1,
);

=head2 success
Expand All @@ -83,7 +83,7 @@ success is a list of requests which will be applied when compare evaluates to tr

has success => (
is => 'ro',
isa => ArrayRef,
isa => ArrayRef,
);

=head2 failure
Expand All @@ -94,7 +94,7 @@ failure is a list of requests which will be applied when compare evaluates to fa

has failure => (
is => 'ro',
isa => ArrayRef,
isa => ArrayRef,
);

=head1 PUBLIC METHODS
Expand All @@ -110,15 +110,16 @@ create txn
sub create {
my $self = shift;
my $compare = $self->compare;
my $success = $self->success;
my $failure = $self->failure;
my $txn ='"compare":[' . join(',', map {$_->json_args} @$compare) . '],';
$txn .= '"success":[' . join(',', @$success) . ']' if defined $success;
my $success = $self->success;
my $failure = $self->failure;

my $txn ='"compare":[' . join(',',@$compare) . '],';
$txn .= '"success":[' . join(',', @$success) . ']' if defined $success;
$txn .= ',' if defined $success and defined $failure;
$txn .= '"failure":[ ' . join(',', @$failure) . ']' if defined $failure;
$self->{json_args} = '{' . $txn . '}';
# print STDERR Dumper($self);
$self->request;
# print STDERR Dumper($self);
$self->request;
return $self;
}

Expand Down
76 changes: 71 additions & 5 deletions t/txn.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,41 @@ my ($host, $port);
if ( $ENV{ETCD_TEST_HOST} and $ENV{ETCD_TEST_PORT}) {
$host = $ENV{ETCD_TEST_HOST};
$port = $ENV{ETCD_TEST_PORT};
plan tests => 5;
plan tests => 14;
}
else {
plan skip_all => "Please set environment variable ETCD_TEST_HOST and ETCD_TEST_PORT.";
}

my ($put, @op, @compare, $txn);
my ($put, $comp, $range, @op, @compare, $txn);
my $etcd = Net::Etcd->new( { host => $host, port => $port } );

my @chars = ("A".."Z", "a".."z");

# gen random key so we can kee[ it realz
my $rand_key;
$rand_key .= $chars[rand @chars] for 1..8;

lives_ok(
sub {
$put = $etcd->put( { key => $rand_key , value => 'randy' } );
},
"put random key"
);

cmp_ok( $put->{response}{success}, '==', 1, "create static key success" );

lives_ok(
sub {
$put = $etcd->put( { key => 'foozilla', value => 'baz' } );
},
"put key"
);

cmp_ok( $put->{response}{success}, '==', 1, "put key success" );

#print STDERR Dumper($put);

lives_ok(
sub {
$put = $etcd->put( { key => 'foo', value => 'bar', hold => 1 } );
Expand All @@ -41,7 +67,7 @@ lives_ok(

lives_ok(
sub {
push @compare, $etcd->compare( { key => 'foo', target => 'CREATE', create_revision => '0' });
push @compare, $etcd->compare( { key => 'foozilla', result => 'EQUAL', target => 'VALUE', value => 'baz' });
},
"compare create"
);
Expand All @@ -52,11 +78,51 @@ lives_ok(
sub {
$txn = $etcd->txn( { compare => \@compare, success => \@op } );
},
"compare create"
"txn create"
);

cmp_ok( $txn->{response}{success}, '==', 1, "txn create success" );

print STDERR Dumper($txn);
#print STDERR Dumper($txn);

# make a cleanup txn
undef @op;
undef @compare;
undef $txn;

lives_ok(
sub {
$comp = $etcd->compare( { key => $rand_key, target => 'CREATE', result => 'NOT_EQUAL', create_revision => '0' });
push @compare, $comp;
},
"compare create"
);

#print STDERR Dumper($comp);


lives_ok(
sub {
$range = $etcd->range( { key => 'foozilla', hold => 1 } );
},
"range hold create"
);

lives_ok(
sub {
push @op, $etcd->op( { request_delete_range => $range } );
},
"op request_delete create"
);

lives_ok(
sub {
$txn = $etcd->txn( { compare => \@compare, success => \@op } );
},
"compare create"
);

cmp_ok( $txn->{response}{success}, '==', 1, "txn create cleanup success" );
#print STDERR Dumper($txn);

1;

0 comments on commit edc7c3b

Please sign in to comment.