From 73f7c8cdd0ab944513382e837806e8f5d8e4ff99 Mon Sep 17 00:00:00 2001 From: Sam Batschelet Date: Fri, 16 Jun 2017 18:40:22 -0400 Subject: [PATCH] Add txn and full test support. --- lib/Net/Etcd/KV.pm | 12 ++++-- lib/Net/Etcd/KV/Compare.pm | 5 ++- lib/Net/Etcd/KV/Op.pm | 8 ++-- lib/Net/Etcd/KV/Txn.pm | 21 ++++++----- t/txn.t | 76 +++++++++++++++++++++++++++++++++++--- 5 files changed, 98 insertions(+), 24 deletions(-) diff --git a/lib/Net/Etcd/KV.pm b/lib/Net/Etcd/KV.pm index 357697b..38e1c09 100644 --- a/lib/Net/Etcd/KV.pm +++ b/lib/Net/Etcd/KV.pm @@ -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 { @@ -51,8 +55,8 @@ sub range { cb => $cb, ( $options ? %$options : () ), ); - $range->request; - return $range; + $range->request unless $range->hold; + return $range } =head2 put @@ -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 @@ -125,7 +129,7 @@ sub compare { %$self, ( $options ? %$options : () ), ); - return $cmp; + return $cmp->json_args; } 1; diff --git a/lib/Net/Etcd/KV/Compare.pm b/lib/Net/Etcd/KV/Compare.pm index c5f9362..54d9bd9 100644 --- a/lib/Net/Etcd/KV/Compare.pm +++ b/lib/Net/Etcd/KV/Compare.pm @@ -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] }, ); @@ -68,7 +68,7 @@ version is the version of the given key =cut has version => ( - is => 'ro', + is => 'ro', ); =head2 create_revision @@ -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; diff --git a/lib/Net/Etcd/KV/Op.pm b/lib/Net/Etcd/KV/Op.pm index 2332658..49580e6 100644 --- a/lib/Net/Etcd/KV/Op.pm +++ b/lib/Net/Etcd/KV/Op.pm @@ -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; } diff --git a/lib/Net/Etcd/KV/Txn.pm b/lib/Net/Etcd/KV/Txn.pm index 140f0b8..96aa882 100644 --- a/lib/Net/Etcd/KV/Txn.pm +++ b/lib/Net/Etcd/KV/Txn.pm @@ -71,8 +71,8 @@ responses in order. has compare => ( is => 'ro', - isa => ArrayRef, - required => 1, + isa => ArrayRef, + required => 1, ); =head2 success @@ -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 @@ -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 @@ -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; } diff --git a/t/txn.t b/t/txn.t index 5e6271e..634f709 100644 --- a/t/txn.t +++ b/t/txn.t @@ -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 } ); @@ -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" ); @@ -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;