1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
#
# Author:: Bryan McLellan <btm@opscode.com>
# Copyright:: Copyright 2012-2016, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require "spec_helper"
require "net/ssh"
require "net/ssh/multi"
describe Chef::Knife::Ssh do
before(:each) do
Chef::Config[:client_key] = CHEF_SPEC_DATA + "/ssl/private_key.pem"
end
before do
@knife = Chef::Knife::Ssh.new
@knife.merge_configs
@node_foo = Chef::Node.new
@node_foo.automatic_attrs[:fqdn] = "foo.example.org"
@node_foo.automatic_attrs[:ipaddress] = "10.0.0.1"
@node_bar = Chef::Node.new
@node_bar.automatic_attrs[:fqdn] = "bar.example.org"
@node_bar.automatic_attrs[:ipaddress] = "10.0.0.2"
end
describe "#configure_session" do
context "manual is set to false (default)" do
before do
@knife.config[:manual] = false
@query = Chef::Search::Query.new
end
def configure_query(node_array)
allow(@query).to receive(:search).and_return([node_array])
allow(Chef::Search::Query).to receive(:new).and_return(@query)
end
def self.should_return_specified_attributes
it "returns an array of the attributes specified on the command line OR config file, if only one is set" do
@knife.config[:attribute] = "ipaddress"
Chef::Config[:knife][:ssh_attribute] = "ipaddress" # this value will be in the config file
configure_query([@node_foo, @node_bar])
expect(@knife).to receive(:session_from_list).with([["10.0.0.1", nil], ["10.0.0.2", nil]])
@knife.configure_session
end
it "returns an array of the attributes specified on the command line even when a config value is set" do
Chef::Config[:knife][:ssh_attribute] = "config_file" # this value will be in the config file
@knife.config[:attribute] = "ipaddress" # this is the value of the command line via #configure_attribute
configure_query([@node_foo, @node_bar])
expect(@knife).to receive(:session_from_list).with([["10.0.0.1", nil], ["10.0.0.2", nil]])
@knife.configure_session
end
end
it "searchs for and returns an array of fqdns" do
configure_query([@node_foo, @node_bar])
expect(@knife).to receive(:session_from_list).with([
["foo.example.org", nil],
["bar.example.org", nil],
])
@knife.configure_session
end
should_return_specified_attributes
context "when cloud hostnames are available" do
before do
@node_foo.automatic_attrs[:cloud][:public_hostname] = "ec2-10-0-0-1.compute-1.amazonaws.com"
@node_bar.automatic_attrs[:cloud][:public_hostname] = "ec2-10-0-0-2.compute-1.amazonaws.com"
end
it "returns an array of cloud public hostnames" do
configure_query([@node_foo, @node_bar])
expect(@knife).to receive(:session_from_list).with([
["ec2-10-0-0-1.compute-1.amazonaws.com", nil],
["ec2-10-0-0-2.compute-1.amazonaws.com", nil],
])
@knife.configure_session
end
should_return_specified_attributes
end
context "when cloud hostnames are available but empty" do
before do
@node_foo.automatic_attrs[:cloud][:public_hostname] = ""
@node_bar.automatic_attrs[:cloud][:public_hostname] = ""
end
it "returns an array of fqdns" do
configure_query([@node_foo, @node_bar])
expect(@knife).to receive(:session_from_list).with([
["foo.example.org", nil],
["bar.example.org", nil],
])
@knife.configure_session
end
should_return_specified_attributes
end
it "should raise an error if no host are found" do
configure_query([ ])
expect(@knife.ui).to receive(:fatal)
expect(@knife).to receive(:exit).with(10)
@knife.configure_session
end
context "when there are some hosts found but they do not have an attribute to connect with" do
before do
allow(@query).to receive(:search).and_return([[@node_foo, @node_bar]])
@node_foo.automatic_attrs[:fqdn] = nil
@node_bar.automatic_attrs[:fqdn] = nil
allow(Chef::Search::Query).to receive(:new).and_return(@query)
end
it "should raise a specific error (CHEF-3402)" do
expect(@knife.ui).to receive(:fatal).with(/^2 nodes found/)
expect(@knife).to receive(:exit).with(10)
@knife.configure_session
end
end
end
context "manual is set to true" do
before do
@knife.config[:manual] = true
end
it "returns an array of provided values" do
@knife.instance_variable_set(:@name_args, ["foo.example.org bar.example.org"])
expect(@knife).to receive(:session_from_list).with(["foo.example.org", "bar.example.org"])
@knife.configure_session
end
end
end
describe "#get_ssh_attribute" do
# Order of precedence for ssh target
# 1) command line attribute
# 2) configuration file
# 3) cloud attribute
# 4) fqdn
before do
Chef::Config[:knife][:ssh_attribute] = nil
@knife.config[:attribute] = nil
@node_foo.automatic_attrs[:cloud][:public_hostname] = "ec2-10-0-0-1.compute-1.amazonaws.com"
@node_bar.automatic_attrs[:cloud][:public_hostname] = ""
end
it "should return fqdn by default" do
expect(@knife.get_ssh_attribute(Chef::Node.new)).to eq("fqdn")
end
it "should return cloud.public_hostname attribute if available" do
expect(@knife.get_ssh_attribute(@node_foo)).to eq("cloud.public_hostname")
end
it "should favor to attribute_from_cli over config file and cloud" do
@knife.config[:attribute] = "command_line"
Chef::Config[:knife][:ssh_attribute] = "config_file"
expect( @knife.get_ssh_attribute(@node_foo)).to eq("command_line")
end
it "should favor config file over cloud and default" do
Chef::Config[:knife][:ssh_attribute] = "config_file"
expect( @knife.get_ssh_attribute(@node_foo)).to eq("config_file")
end
it "should return fqdn if cloud.hostname is empty" do
expect( @knife.get_ssh_attribute(@node_bar)).to eq("fqdn")
end
end
describe "#session_from_list" do
before :each do
@knife.instance_variable_set(:@longest, 0)
ssh_config = {:timeout => 50, :user => "locutus", :port => 23 }
allow(Net::SSH).to receive(:configuration_for).with("the.b.org").and_return(ssh_config)
end
it "uses the port from an ssh config file" do
@knife.session_from_list([["the.b.org", nil]])
expect(@knife.session.servers[0].port).to eq(23)
end
it "uses the port from a cloud attr" do
@knife.session_from_list([["the.b.org", 123]])
expect(@knife.session.servers[0].port).to eq(123)
end
it "uses the user from an ssh config file" do
@knife.session_from_list([["the.b.org", 123]])
expect(@knife.session.servers[0].user).to eq("locutus")
end
end
describe "#ssh_command" do
let(:execution_channel) { double(:execution_channel, :on_data => nil) }
let(:session_channel) { double(:session_channel, :request_pty => nil)}
let(:execution_channel2) { double(:execution_channel, :on_data => nil) }
let(:session_channel2) { double(:session_channel, :request_pty => nil)}
let(:session) { double(:session, :loop => nil) }
let(:command) { "false" }
before do
expect(execution_channel).
to receive(:on_request).
and_yield(nil, double(:data_stream, :read_long => exit_status))
expect(session_channel).
to receive(:exec).
with(command).
and_yield(execution_channel, true)
expect(execution_channel2).
to receive(:on_request).
and_yield(nil, double(:data_stream, :read_long => exit_status2))
expect(session_channel2).
to receive(:exec).
with(command).
and_yield(execution_channel2, true)
expect(session).
to receive(:open_channel).
and_yield(session_channel).
and_yield(session_channel2)
end
context "both connections return 0" do
let(:exit_status) { 0 }
let(:exit_status2) { 0 }
it "returns a 0 exit code" do
expect(@knife.ssh_command(command, session)).to eq(0)
end
end
context "the first connection returns 1 and the second returns 0" do
let(:exit_status) { 1 }
let(:exit_status2) { 0 }
it "returns a non-zero exit code" do
expect(@knife.ssh_command(command, session)).to eq(1)
end
end
context "the first connection returns 1 and the second returns 2" do
let(:exit_status) { 1 }
let(:exit_status2) { 2 }
it "returns a non-zero exit code" do
expect(@knife.ssh_command(command, session)).to eq(2)
end
end
end
describe "#run" do
before do
@query = Chef::Search::Query.new
expect(@query).to receive(:search).and_return([[@node_foo]])
allow(Chef::Search::Query).to receive(:new).and_return(@query)
allow(@knife).to receive(:ssh_command).and_return(exit_code)
@knife.name_args = ["*:*", "false"]
end
context "with an error" do
let(:exit_code) { 1 }
it "should exit with a non-zero exit code" do
expect(@knife).to receive(:exit).with(exit_code)
@knife.run
end
end
context "with no error" do
let(:exit_code) { 0 }
it "should not exit" do
expect(@knife).not_to receive(:exit)
@knife.run
end
end
end
describe "#configure_password" do
before do
@knife.config.delete(:ssh_password_ng)
@knife.config.delete(:ssh_password)
end
context "when setting ssh_password_ng from knife ssh" do
# in this case ssh_password_ng exists, but ssh_password does not
it "should prompt for a password when ssh_passsword_ng is nil" do
@knife.config[:ssh_password_ng] = nil
expect(@knife).to receive(:get_password).and_return("mysekretpassw0rd")
@knife.configure_password
expect(@knife.config[:ssh_password]).to eq("mysekretpassw0rd")
end
it "should set ssh_password to false if ssh_password_ng is false" do
@knife.config[:ssh_password_ng] = false
expect(@knife).not_to receive(:get_password)
@knife.configure_password
expect(@knife.config[:ssh_password]).to be_falsey
end
it "should set ssh_password to ssh_password_ng if we set a password" do
@knife.config[:ssh_password_ng] = "mysekretpassw0rd"
expect(@knife).not_to receive(:get_password)
@knife.configure_password
expect(@knife.config[:ssh_password]).to eq("mysekretpassw0rd")
end
end
context "when setting ssh_password from knife bootstrap / knife * server create" do
# in this case ssh_password exists, but ssh_password_ng does not
it "should set ssh_password to nil when ssh_password is nil" do
@knife.config[:ssh_password] = nil
expect(@knife).not_to receive(:get_password)
@knife.configure_password
expect(@knife.config[:ssh_password]).to be_nil
end
it "should set ssh_password to false when ssh_password is false" do
@knife.config[:ssh_password] = false
expect(@knife).not_to receive(:get_password)
@knife.configure_password
expect(@knife.config[:ssh_password]).to be_falsey
end
it "should set ssh_password to ssh_password if we set a password" do
@knife.config[:ssh_password] = "mysekretpassw0rd"
expect(@knife).not_to receive(:get_password)
@knife.configure_password
expect(@knife.config[:ssh_password]).to eq("mysekretpassw0rd")
end
end
context "when setting ssh_password in the config variable" do
before(:each) do
Chef::Config[:knife][:ssh_password] = "my_knife_passw0rd"
end
context "when setting ssh_password_ng from knife ssh" do
# in this case ssh_password_ng exists, but ssh_password does not
it "should prompt for a password when ssh_passsword_ng is nil" do
@knife.config[:ssh_password_ng] = nil
expect(@knife).to receive(:get_password).and_return("mysekretpassw0rd")
@knife.configure_password
expect(@knife.config[:ssh_password]).to eq("mysekretpassw0rd")
end
it "should set ssh_password to the configured knife.rb value if ssh_password_ng is false" do
@knife.config[:ssh_password_ng] = false
expect(@knife).not_to receive(:get_password)
@knife.configure_password
expect(@knife.config[:ssh_password]).to eq("my_knife_passw0rd")
end
it "should set ssh_password to ssh_password_ng if we set a password" do
@knife.config[:ssh_password_ng] = "mysekretpassw0rd"
expect(@knife).not_to receive(:get_password)
@knife.configure_password
expect(@knife.config[:ssh_password]).to eq("mysekretpassw0rd")
end
end
context "when setting ssh_password from knife bootstrap / knife * server create" do
# in this case ssh_password exists, but ssh_password_ng does not
it "should set ssh_password to the configured knife.rb value when ssh_password is nil" do
@knife.config[:ssh_password] = nil
expect(@knife).not_to receive(:get_password)
@knife.configure_password
expect(@knife.config[:ssh_password]).to eq("my_knife_passw0rd")
end
it "should set ssh_password to the configured knife.rb value when ssh_password is false" do
@knife.config[:ssh_password] = false
expect(@knife).not_to receive(:get_password)
@knife.configure_password
expect(@knife.config[:ssh_password]).to eq("my_knife_passw0rd")
end
it "should set ssh_password to ssh_password if we set a password" do
@knife.config[:ssh_password] = "mysekretpassw0rd"
expect(@knife).not_to receive(:get_password)
@knife.configure_password
expect(@knife.config[:ssh_password]).to eq("mysekretpassw0rd")
end
end
end
end
end
|