Skip to content

Commit 5ff4943

Browse files
authored
Merge pull request #52 from davissp14/bug-fix
Bug fixes + specs
2 parents add10c0 + ad32eb0 commit 5ff4943

File tree

2 files changed

+222
-11
lines changed

2 files changed

+222
-11
lines changed

lib/etcdv3.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,24 @@ def leader_id
6565
request.handle(:maintenance, 'member_status').leader
6666
end
6767

68+
# List active alarms
69+
def alarm_list
70+
request.handle(:maintenance, 'alarms', [:get, leader_id])
71+
end
72+
73+
# Disarm alarms on a specified member.
74+
def deactivate_alarms
75+
request.handle(:maintenance, 'alarms', [:deactivate, leader_id])
76+
end
77+
6878
# Inserts a new key.
6979
def put(key, value, lease_id: nil)
7080
request.handle(:kv, 'put', [key, value, lease_id])
7181
end
7282

7383
# Fetches key(s).
7484
def get(key, range_end='')
75-
request.handle(:get, 'get', [key, range_end])
85+
request.handle(:kv, 'get', [key, range_end])
7686
end
7787

7888
# Grant a lease with a speified TTL
@@ -154,16 +164,6 @@ def revoke_permission_from_role(name, permission, key, range_end='')
154164
request.handle(:auth, 'revoke_permission_from_role', [name, permission, key, range_end])
155165
end
156166

157-
# List active alarms
158-
def alarm_list
159-
request.handle(:maintenance, 'alarms', [:get, leader_id])
160-
end
161-
162-
# Disarm alarms on a specified member.
163-
def deactivate_alarms
164-
request.handle(:maintenance, 'alarms', [leader_id])
165-
end
166-
167167
# Enables authentication.
168168
def enable_auth
169169
request.handle(:auth, 'enable_auth')

spec/etcdv3_spec.rb

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
require 'spec_helper'
2+
3+
describe Etcd do
4+
context 'Insecure connection without Auth' do
5+
6+
let(:conn) { local_connection }
7+
8+
describe '#initialize' do
9+
subject { conn }
10+
it { is_expected.to have_attributes(scheme: 'http') }
11+
it { is_expected.to have_attributes(hostname: '127.0.0.1') }
12+
it { is_expected.to have_attributes(credentials: :this_channel_is_insecure) }
13+
it { is_expected.to have_attributes(token: nil) }
14+
it { is_expected.to have_attributes(user: nil) }
15+
it { is_expected.to have_attributes(password: nil) }
16+
end
17+
18+
describe '#version' do
19+
subject { conn.version }
20+
it { is_expected.to be_an_instance_of(String) }
21+
end
22+
23+
describe '#db_size' do
24+
subject { conn.db_size }
25+
it { is_expected.to_not be_nil }
26+
end
27+
28+
describe '#leader_id' do
29+
subject { conn.leader_id }
30+
it { is_expected.to_not be_nil }
31+
end
32+
33+
describe '#alarm_list' do
34+
subject { conn.alarm_list }
35+
it { is_expected.to_not be_nil }
36+
end
37+
38+
describe '#deactivate_alarms' do
39+
subject { conn.deactivate_alarms }
40+
it { is_expected.to_not be_nil }
41+
end
42+
43+
describe '#get' do
44+
subject { conn.get('test') }
45+
it { is_expected.to_not be_nil }
46+
end
47+
48+
describe '#put' do
49+
subject { conn.put('test', 'value') }
50+
it { is_expected.to_not be_nil }
51+
end
52+
53+
describe '#grant_lease' do
54+
subject { conn.grant_lease(2) }
55+
it { is_expected.to_not be_nil }
56+
end
57+
58+
describe '#revoke_lease' do
59+
let!(:lease_id) { conn.grant_lease(2)['ID'] }
60+
subject { conn.revoke_lease(lease_id) }
61+
it { is_expected.to_not be_nil }
62+
end
63+
64+
describe '#lease_ttl' do
65+
let!(:lease_id) { conn.grant_lease(2)['ID'] }
66+
subject { conn.lease_ttl(lease_id) }
67+
it { is_expected.to_not be_nil }
68+
end
69+
70+
describe '#add_user' do
71+
after { conn.delete_user('test') }
72+
subject { conn.add_user('test', 'user') }
73+
it { is_expected.to_not be_nil }
74+
end
75+
76+
describe '#delete_user' do
77+
before { conn.add_user('test', 'user') }
78+
subject { conn.delete_user('test') }
79+
it { is_expected.to_not be_nil }
80+
end
81+
82+
describe '#change_user_password' do
83+
before { conn.add_user('change_user', 'pass') }
84+
after { conn.delete_user('change_user') }
85+
subject { conn.change_user_password('change_user', 'new_pass') }
86+
it { is_expected.to_not be_nil }
87+
end
88+
89+
describe '#user_list' do
90+
subject { conn.user_list }
91+
it { is_expected.to_not be_nil }
92+
end
93+
94+
describe '#role_list' do
95+
subject { conn.role_list }
96+
it { is_expected.to_not be_nil }
97+
end
98+
99+
describe '#add_role' do
100+
subject { conn.add_role('add_role') }
101+
it { is_expected.to_not be_nil }
102+
end
103+
104+
describe '#delete_role' do
105+
before { conn.add_role('delete_role') }
106+
subject { conn.delete_role('delete_role') }
107+
it { is_expected.to_not be_nil }
108+
end
109+
110+
describe '#grant_role_to_user' do
111+
before { conn.add_user('grant_me', 'pass') }
112+
subject { conn.grant_role_to_user('grant_me', 'root') }
113+
it { is_expected.to_not be_nil }
114+
end
115+
116+
describe '#revoke_role_from_user' do
117+
subject { conn.revoke_role_from_user('grant_me', 'root') }
118+
it { is_expected.to_not be_nil }
119+
end
120+
121+
describe '#grant_permission_to_role' do
122+
before { conn.add_role('grant') }
123+
subject { conn.grant_permission_to_role('grant', 'readwrite', 'a', 'Z') }
124+
it { is_expected.to_not be_nil }
125+
end
126+
127+
describe '#revoke_permission_to_role' do
128+
subject { conn.revoke_permission_from_role('grant', 'readwrite', 'a', 'Z') }
129+
it { is_expected.to_not be_nil }
130+
end
131+
132+
describe '#disable_auth' do
133+
before do
134+
conn.add_user('root', 'test')
135+
conn.grant_role_to_user('root', 'root')
136+
conn.enable_auth
137+
conn.authenticate('root', 'test')
138+
end
139+
after { conn.delete_user('root') }
140+
subject { conn.disable_auth }
141+
it { is_expected.to be_an_instance_of(Etcdserverpb::AuthDisableResponse) }
142+
end
143+
144+
describe '#enable_auth' do
145+
before do
146+
conn.add_user('root', 'test')
147+
conn.grant_role_to_user('root', 'root')
148+
end
149+
after do
150+
conn.authenticate('root', 'test')
151+
conn.disable_auth
152+
conn.delete_user('root')
153+
end
154+
subject { conn.enable_auth }
155+
it { is_expected.to be_an_instance_of(Etcdserverpb::AuthEnableResponse) }
156+
end
157+
158+
describe "#authenticate" do
159+
context "auth enabled" do
160+
before do
161+
conn.add_user('root', 'test')
162+
conn.grant_role_to_user('root', 'root')
163+
conn.enable_auth
164+
conn.authenticate('root', 'test')
165+
end
166+
after do
167+
conn.disable_auth
168+
conn.delete_user('root')
169+
end
170+
it 'properly reconfigures auth and token' do
171+
expect(conn.token).to_not be_nil
172+
expect(conn.user).to eq('root')
173+
expect(conn.password).to eq('test')
174+
end
175+
end
176+
177+
context 'auth disabled' do
178+
it 'raises error' do
179+
expect { conn.authenticate('root', 'root') }.to raise_error(GRPC::InvalidArgument)
180+
end
181+
end
182+
end
183+
184+
describe '#metacache' do
185+
context 'uses cached request object' do
186+
let!(:object_id) { conn.send(:request).object_id }
187+
before { conn.add_user('root', 'test') }
188+
after { conn.delete_user('root') }
189+
subject { conn.send(:request).object_id }
190+
it { is_expected.to eq(object_id) }
191+
end
192+
context 'resets cache on auth' do
193+
let!(:object_id) { conn.send(:request).object_id }
194+
before do
195+
conn.add_user('root', 'test')
196+
conn.grant_role_to_user('root', 'root')
197+
conn.enable_auth
198+
conn.authenticate('root', 'test')
199+
conn.add_user('boom', 'password')
200+
end
201+
after do
202+
conn.disable_auth
203+
conn.delete_user('root')
204+
conn.delete_user('boom')
205+
end
206+
subject { conn.send(:request).object_id }
207+
it { is_expected.to_not eq(object_id) }
208+
end
209+
end
210+
end
211+
end

0 commit comments

Comments
 (0)