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
|
#
# Author:: Daniel DeLeo (<dan@chef.io>)
# Copyright:: Copyright 2010-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 "tiny_server"
describe Chef::Knife::Ssh do
before(:all) do
Chef::Knife::Ssh.load_deps
@server = TinyServer::Manager.new
@server.start
end
after(:all) do
@server.stop
end
let(:ssh_config) { Hash.new }
before do
allow(Net::SSH).to receive(:configuration_for).and_return(ssh_config)
end
# Force log level to info.
around do |ex|
old_level = Chef::Log.level
begin
Chef::Log.level = :info
ex.run
ensure
Chef::Log.level = old_level
end
end
describe "identity file" do
context "when knife[:ssh_identity_file] is set" do
before do
setup_knife(["*:*", "uptime"])
Chef::Config[:knife][:ssh_identity_file] = "~/.ssh/aws.rsa"
end
it "uses the ssh_identity_file" do
@knife.run
expect(@knife.config[:ssh_identity_file]).to eq("~/.ssh/aws.rsa")
end
end
context "when knife[:ssh_identity_file] is set and frozen" do
before do
setup_knife(["*:*", "uptime"])
Chef::Config[:knife][:ssh_identity_file] = "~/.ssh/aws.rsa".freeze
end
it "uses the ssh_identity_file" do
@knife.run
expect(@knife.config[:ssh_identity_file]).to eq("~/.ssh/aws.rsa")
end
end
context "when -i is provided" do
before do
setup_knife(["-i ~/.ssh/aws.rsa", "*:*", "uptime"])
Chef::Config[:knife][:ssh_identity_file] = nil
end
it "should use the value on the command line" do
@knife.run
expect(@knife.config[:ssh_identity_file]).to eq("~/.ssh/aws.rsa")
end
it "should override what is set in knife.rb" do
Chef::Config[:knife][:ssh_identity_file] = "~/.ssh/other.rsa"
@knife.run
expect(@knife.config[:ssh_identity_file]).to eq("~/.ssh/aws.rsa")
end
end
context "when knife[:ssh_identity_file] is not provided]" do
before do
setup_knife(["*:*", "uptime"])
Chef::Config[:knife][:ssh_identity_file] = nil
end
it "uses the default" do
@knife.run
expect(@knife.config[:ssh_identity_file]).to eq(nil)
end
end
end
describe "port" do
context "when -p 31337 is provided" do
before do
setup_knife(["-p 31337", "*:*", "uptime"])
end
it "uses the ssh_port" do
@knife.run
expect(@knife.config[:ssh_port]).to eq("31337")
end
end
end
describe "user" do
context "when knife[:ssh_user] is set" do
before do
setup_knife(["*:*", "uptime"])
Chef::Config[:knife][:ssh_user] = "ubuntu"
end
it "uses the ssh_user" do
@knife.run
expect(@knife.config[:ssh_user]).to eq("ubuntu")
end
end
context "when knife[:ssh_user] is set and frozen" do
before do
setup_knife(["*:*", "uptime"])
Chef::Config[:knife][:ssh_user] = "ubuntu".freeze
end
it "uses the ssh_user" do
@knife.run
expect(@knife.config[:ssh_user]).to eq("ubuntu")
end
end
context "when -x is provided" do
before do
setup_knife(["-x ubuntu", "*:*", "uptime"])
Chef::Config[:knife][:ssh_user] = nil
end
it "should use the value on the command line" do
@knife.run
expect(@knife.config[:ssh_user]).to eq("ubuntu")
end
it "should override what is set in knife.rb" do
Chef::Config[:knife][:ssh_user] = "root"
@knife.run
expect(@knife.config[:ssh_user]).to eq("ubuntu")
end
end
context "when knife[:ssh_user] is not provided]" do
before do
setup_knife(["*:*", "uptime"])
Chef::Config[:knife][:ssh_user] = nil
end
it "uses the default (current user)" do
@knife.run
expect(@knife.config[:ssh_user]).to eq(nil)
end
end
end
describe "attribute" do
context "when knife[:ssh_attribute] is set" do
before do
setup_knife(["*:*", "uptime"])
Chef::Config[:knife][:ssh_attribute] = "ec2.public_hostname"
end
it "uses the ssh_attribute" do
@knife.run
expect(@knife.get_ssh_attribute(Chef::Node.new)).to eq("ec2.public_hostname")
end
end
context "when knife[:ssh_attribute] is not provided]" do
before do
setup_knife(["*:*", "uptime"])
Chef::Config[:knife][:ssh_attribute] = nil
end
it "uses the default" do
@knife.run
expect(@knife.get_ssh_attribute(Chef::Node.new)).to eq("fqdn")
end
end
context "when -a ec2.public_ipv4 is provided" do
before do
setup_knife(["-a ec2.public_hostname", "*:*", "uptime"])
Chef::Config[:knife][:ssh_attribute] = nil
end
it "should use the value on the command line" do
@knife.run
expect(@knife.config[:attribute]).to eq("ec2.public_hostname")
end
it "should override what is set in knife.rb" do
# This is the setting imported from knife.rb
Chef::Config[:knife][:ssh_attribute] = "fqdn"
# Then we run knife with the -a flag, which sets the above variable
setup_knife(["-a ec2.public_hostname", "*:*", "uptime"])
@knife.run
expect(@knife.config[:attribute]).to eq("ec2.public_hostname")
end
end
end
describe "gateway" do
context "when knife[:ssh_gateway] is set" do
before do
setup_knife(["*:*", "uptime"])
Chef::Config[:knife][:ssh_gateway] = "user@ec2.public_hostname"
end
it "uses the ssh_gateway" do
expect(@knife.session).to receive(:via).with("ec2.public_hostname", "user", {})
@knife.run
expect(@knife.config[:ssh_gateway]).to eq("user@ec2.public_hostname")
end
end
context "when -G user@ec2.public_hostname is provided" do
before do
setup_knife(["-G user@ec2.public_hostname", "*:*", "uptime"])
Chef::Config[:knife][:ssh_gateway] = nil
end
it "uses the ssh_gateway" do
expect(@knife.session).to receive(:via).with("ec2.public_hostname", "user", {})
@knife.run
expect(@knife.config[:ssh_gateway]).to eq("user@ec2.public_hostname")
end
end
context "when the gateway requires a password" do
before do
setup_knife(["-G user@ec2.public_hostname", "*:*", "uptime"])
Chef::Config[:knife][:ssh_gateway] = nil
allow(@knife.session).to receive(:via) do |host, user, options|
raise Net::SSH::AuthenticationFailed unless options[:password]
end
end
it "should prompt the user for a password" do
expect(@knife.ui).to receive(:ask).with("Enter the password for user@ec2.public_hostname: ").and_return("password")
@knife.run
end
end
end
def setup_knife(params = [])
@knife = Chef::Knife::Ssh.new(params)
# We explicitly avoid running #configure_chef, which would read a knife.rb
# if available, but #merge_configs (which is called by #configure_chef) is
# necessary to have default options merged in.
@knife.merge_configs
allow(@knife).to receive(:ssh_command) { 0 }
@api = TinyServer::API.instance
@api.clear
Chef::Config[:node_name] = nil
Chef::Config[:client_key] = nil
Chef::Config[:chef_server_url] = "http://localhost:9000"
@api.get("/search/node?q=*:*&sort=X_CHEF_id_CHEF_X%20asc&start=0", 200) do
%({"total":1, "start":0, "rows":[{"name":"i-xxxxxxxx", "json_class":"Chef::Node", "automatic":{"fqdn":"the.fqdn", "ec2":{"public_hostname":"the_public_hostname"}},"recipes":[]}]})
end
end
end
|