From 2c4beeedf717eb5eb44904750a5e0bf6ec037689 Mon Sep 17 00:00:00 2001 From: Shaun Davis Date: Sun, 1 Aug 2021 21:38:42 -0500 Subject: [PATCH] Cleaning up specs --- lib/etcdv3/version.rb | 2 +- spec/etcdv3_spec.rb | 122 +++++++++++++++++++++++------------------- 2 files changed, 69 insertions(+), 55 deletions(-) diff --git a/lib/etcdv3/version.rb b/lib/etcdv3/version.rb index 9aed002..4bc49b9 100644 --- a/lib/etcdv3/version.rb +++ b/lib/etcdv3/version.rb @@ -1,3 +1,3 @@ class Etcdv3 - VERSION = '0.11.2'.freeze + VERSION = '0.11.3'.freeze end diff --git a/spec/etcdv3_spec.rb b/spec/etcdv3_spec.rb index 85404b7..2ec4d27 100644 --- a/spec/etcdv3_spec.rb +++ b/spec/etcdv3_spec.rb @@ -523,31 +523,38 @@ end describe "namespace" do - let(:ns_conn) { local_connection_with_namespace("/namespace/") } describe '#get' do + let(:get_conn) { local_connection_with_namespace("/namespace-get/") } + before do conn.put('/apples/', 'app') - conn.put('/namespace/apple', 'apple') - conn.put('/namespace/apples', 'apples') - conn.put('/namespace/appless', 'appless') + conn.put('/namespace-get/apple', 'apple') + conn.put('/namespace-get/apples', 'apples') + conn.put('/namespace-get/appless', 'appless') end it 'returns key w/o namespace' do - expect(ns_conn.get("apple").kvs.last.value).to eq('apple') + expect(get_conn.get("apple").kvs.last.value).to eq('apple') end it 'returns keys w/o namespace' do - expect(ns_conn.get("apple", range_end: 'applf').kvs.size).to eq(3) + expect(get_conn.get("apple", range_end: 'applf').kvs.size).to eq(3) + end + + it 'returns all keys under namespace' do + expect(get_conn.get("", range_end: "\0").kvs.size).to eq(3) end end describe '#put' do + let(:put_conn) { local_connection_with_namespace("/namespace-put/") } + before do - ns_conn.put('apple_put', 'test') + put_conn.put('apple_put', 'test') end it 'returns key with namespace' do - expect(conn.get("/namespace/apple_put").kvs.last.value).to eq('test') + expect(conn.get("/namespace-put/apple_put").kvs.last.value).to eq('test') end end @@ -559,10 +566,13 @@ del_conn.put('test', "key") del_conn.put('test2', "key2") conn.put('wall', 'zzzz') + conn.put('walzz', 'adsfas') end + it 'deleting all keys should be scoped to namespace' do - resp = del_conn.del('', range_end: "\0") + resp = del_conn.del("", range_end: "\0") expect(resp.deleted).to eq(2) + expect(conn.get("wall").kvs.last.value).to eq('zzzz') end end @@ -583,24 +593,26 @@ end describe '#transaction' do + let(:trans_conn) { local_connection_with_namespace("/namespace/") } + describe 'txn.value' do - before { ns_conn.put('txn', 'value') } - after { ns_conn.del('txn') } + before { trans_conn.put('txn', 'value') } + after { trans_conn.del('txn') } context 'success' do subject! do - ns_conn.transaction do |txn| + trans_conn.transaction do |txn| txn.compare = [ txn.value('txn', :equal, 'value') ] txn.success = [ txn.put('txn-test', 'success') ] txn.failure = [ txn.put('txn-test', 'failed') ] end end it 'sets correct key' do - expect(ns_conn.get('txn-test').kvs.first.value).to eq('success') + expect(trans_conn.get('txn-test').kvs.first.value).to eq('success') expect(conn.get("/namespace/txn-test").kvs.first.value).to eq('success') end it "raises a GRPC::DeadlineExceeded exception when it takes too long" do expect do - ns_conn.transaction(timeout: 0) do |txn| + trans_conn.transaction(timeout: 0) do |txn| txn.compare = [ txn.value('txn', :equal, 'value') ] txn.success = [ txn.put('txn-test', 'success') ] txn.failure = [ txn.put('txn-test', 'failed') ] @@ -609,7 +621,7 @@ end it "accepts a timeout" do expect do - ns_conn.transaction(timeout: 1) do |txn| + trans_conn.transaction(timeout: 1) do |txn| txn.compare = [ txn.value('txn', :equal, 'value') ] txn.success = [ txn.put('txn-test', 'success') ] txn.failure = [ txn.put('txn-test', 'failed') ] @@ -618,89 +630,89 @@ end end context "success, value with lease" do - let!(:lease_id) { ns_conn.lease_grant(2)['ID'] } + let!(:lease_id) { trans_conn.lease_grant(2)['ID'] } subject! do - ns_conn.transaction do |txn| + trans_conn.transaction do |txn| txn.compare = [ txn.value('txn', :equal, 'value') ] txn.success = [ txn.put('txn-test', 'success', lease_id) ] txn.failure = [ txn.put('txn-test', 'failed', lease_id) ] end end it 'sets correct key, with a lease' do - expect(ns_conn.get('txn-test').kvs.first.value).to eq('success') - expect(ns_conn.get('txn-test').kvs.first.lease).to eq(lease_id) + expect(trans_conn.get('txn-test').kvs.first.value).to eq('success') + expect(trans_conn.get('txn-test').kvs.first.lease).to eq(lease_id) end end context 'failure' do subject! do - ns_conn.transaction do |txn| + trans_conn.transaction do |txn| txn.compare = [ txn.value('txn', :equal, 'notright') ] txn.success = [ txn.put('txn-test', 'success') ] txn.failure = [ txn.put('txn-test', 'failed') ] end end it 'sets correct key' do - expect(ns_conn.get('txn-test').kvs.first.value).to eq('failed') + expect(trans_conn.get('txn-test').kvs.first.value).to eq('failed') end end end describe 'txn.create_revision' do - before { ns_conn.put('txn', 'value') } - after { ns_conn.del('txn') } + before { trans_conn.put('txn', 'value') } + after { trans_conn.del('txn') } context 'success' do subject! do - ns_conn.transaction do |txn| + trans_conn.transaction do |txn| txn.compare = [ txn.create_revision('txn', :greater, 1) ] txn.success = [ txn.put('txn-test', 'success') ] txn.failure = [ txn.put('txn-test', 'failed') ] end end it 'sets correct key' do - expect(ns_conn.get('txn-test').kvs.first.value).to eq('success') + expect(trans_conn.get('txn-test').kvs.first.value).to eq('success') end end context 'failure' do subject! do - ns_conn.transaction do |txn| + trans_conn.transaction do |txn| txn.compare = [ txn.create_revision('txn', :equal, 1) ] txn.success = [ txn.put('txn-test', 'success') ] txn.failure = [ txn.put('txn-test', 'failed') ] end end it 'sets correct key' do - expect(ns_conn.get('txn-test').kvs.first.value).to eq('failed') + expect(trans_conn.get('txn-test').kvs.first.value).to eq('failed') expect(conn.get('/namespace/txn-test').kvs.first.value).to eq('failed') end end end describe 'txn.mod_revision' do - before { ns_conn.put('txn', 'value') } - after { ns_conn.del('txn') } + before { trans_conn.put('txn', 'value') } + after { trans_conn.del('txn') } context 'success' do subject! do - ns_conn.transaction do |txn| + trans_conn.transaction do |txn| txn.compare = [ txn.mod_revision('txn', :less, 1000) ] txn.success = [ txn.put('txn-test', 'success') ] txn.failure = [ txn.put('txn-test', 'failed') ] end end it 'sets correct key' do - expect(ns_conn.get('txn-test').kvs.first.value).to eq('success') + expect(trans_conn.get('txn-test').kvs.first.value).to eq('success') expect(conn.get('/namespace/txn-test').kvs.first.value).to eq('success') end end context 'failure' do subject! do - ns_conn.transaction do |txn| + trans_conn.transaction do |txn| txn.compare = [ txn.mod_revision('txn', :greater, 1000) ] txn.success = [ txn.put('txn-test', 'success') ] txn.failure = [ txn.put('txn-test', 'failed') ] end end it 'sets correct key' do - expect(ns_conn.get('txn-test').kvs.first.value).to eq('failed') + expect(trans_conn.get('txn-test').kvs.first.value).to eq('failed') expect(conn.get('/namespace/txn-test').kvs.first.value).to eq('failed') end @@ -708,31 +720,31 @@ end describe 'txn.version' do - before { ns_conn.put('txn-version', 'value') } - after { ns_conn.del('txn-version') } + before { trans_conn.put('txn-version', 'value') } + after { trans_conn.del('txn-version') } context 'success' do subject! do - ns_conn.transaction do |txn| + trans_conn.transaction do |txn| txn.compare = [ txn.version('txn-version', :equal, 1) ] txn.success = [ txn.put('txn-test', 'success') ] txn.failure = [ txn.put('txn-test', 'failed') ] end end it 'sets correct key' do - expect(ns_conn.get('txn-test').kvs.first.value).to eq('success') + expect(trans_conn.get('txn-test').kvs.first.value).to eq('success') expect(conn.get('/namespace/txn-test').kvs.first.value).to eq('success') end end context 'failure' do subject! do - ns_conn.transaction do |txn| + trans_conn.transaction do |txn| txn.compare = [ txn.version('txn', :equal, 100)] txn.success = [ txn.put('txn-test', 'success') ] txn.failure = [ txn.put('txn-test', 'failed') ] end end it 'sets correct key' do - expect(ns_conn.get('txn-test').kvs.first.value).to eq('failed') + expect(trans_conn.get('txn-test').kvs.first.value).to eq('failed') expect(conn.get('/namespace/txn-test').kvs.first.value).to eq('failed') end end @@ -741,27 +753,29 @@ # Locking is not implemented in etcd v3.1.X unless $instance.version < Gem::Version.new("3.2.0") - describe '#lock' do - let(:lease_id) { lease_stub.lease_grant(10)['ID'] } - subject { ns_conn.lock('mylocklock', lease_id) } - it 'should lock key under specified namespace' do - expect(conn.get("/namespace/#{subject.key}").kvs).to_not be_empty + describe "locking" do + let(:ns_conn) { local_connection_with_namespace("/namespace/") } + + describe '#lock' do + let(:lease_id) { lease_stub.lease_grant(10)['ID'] } + subject { ns_conn.lock('mylocklock', lease_id) } + it 'should lock key under specified namespace' do + expect(conn.get("/namespace/#{subject.key}").kvs).to_not be_empty + end end - end - describe '#with_lock' do - let(:lease_id) { lease_stub.lease_grant(10)['ID'] } - let(:lease_id_2) { lease_stub.lease_grant(15)['ID'] } - it 'enforces lock' do - ns_conn.with_lock('mylock', lease_id) do - expect { ns_conn.lock('mylock', lease_id_2, timeout: 0.1) } - .to raise_error(GRPC::DeadlineExceeded) + describe '#with_lock' do + let(:lease_id) { lease_stub.lease_grant(10)['ID'] } + let(:lease_id_2) { lease_stub.lease_grant(15)['ID'] } + it 'enforces lock' do + ns_conn.with_lock('mylock', lease_id) do + expect { ns_conn.lock('mylock', lease_id_2, timeout: 0.1) } + .to raise_error(GRPC::DeadlineExceeded) + end end end end end - - end end end