forked from test-kitchen/test-kitchen
/
ssh_base_spec.rb
644 lines (483 loc) · 17.4 KB
/
ssh_base_spec.rb
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
# -*- encoding: utf-8 -*-
#
# Author:: Fletcher Nichol (<fnichol@nichol.ca>)
#
# Copyright (C) 2014, Fletcher Nichol
#
# 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_relative "../../spec_helper"
require "kitchen"
describe Kitchen::Driver::SSHBase do
let(:logged_output) { StringIO.new }
let(:logger) { Logger.new(logged_output) }
let(:config) { Hash.new }
let(:state) { Hash.new }
let(:busser) do
stub(
:setup_cmd => "setup",
:sync_cmd => "sync",
:run_cmd => "run"
)
end
let(:provisioner) do
stub(
:install_command => "install",
:init_command => "init",
:prepare_command => "prepare",
:run_command => "run",
:create_sandbox => true,
:cleanup_sandbox => true,
:sandbox_path => "/tmp/sandbox"
)
end
let(:instance) do
stub(
:name => "coolbeans",
:logger => logger,
:busser => busser,
:provisioners => [provisioner],
:to_str => "instance"
)
end
let(:driver) do
Kitchen::Driver::SSHBase.new(config).finalize_config!(instance)
end
describe "configuration" do
it ":sudo defaults to true" do
driver[:sudo].must_equal true
end
it ":port defaults to 22" do
driver[:port].must_equal 22
end
end
it "#create raises a ClientError" do
proc { driver.create(state) }.must_raise Kitchen::ClientError
end
it "#destroy raises a ClientError" do
proc { driver.destroy(state) }.must_raise Kitchen::ClientError
end
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
def self.constructs_an_ssh_object
describe "constructs an SSH object" do
it "with hostname set from state" do
Kitchen::SSH.expects(:new).with { |hostname, _username, _opts|
hostname.must_equal "fizzy"
}.returns(stub(:login_command => stub))
cmd
end
it "with username set from state" do
Kitchen::SSH.expects(:new).with { |_hostname, username, _opts|
username.must_equal "bork"
}.returns(stub(:login_command => stub))
cmd
end
it "with :user_known_hosts_file option set to /dev/null" do
Kitchen::SSH.expects(:new).with { |_hostname, _username, opts|
opts[:user_known_hosts_file].must_equal "/dev/null"
}.returns(stub(:login_command => stub))
cmd
end
it "with :paranoid option set to false" do
Kitchen::SSH.expects(:new).with { |_hostname, _username, opts|
opts[:paranoid].must_equal false
}.returns(stub(:login_command => stub))
cmd
end
it "with :keys_only option set to falsey by default" do
Kitchen::SSH.expects(:new).with { |_hostname, _username, opts|
opts[:keys_only].nil?
}.returns(stub(:login_command => stub))
cmd
end
it "with :keys_only option set to true if :ssh_key is set in config" do
config[:ssh_key] = "wicked"
Kitchen::SSH.expects(:new).with { |_hostname, _username, opts|
opts[:keys_only].must_equal true
}.returns(stub(:login_command => stub))
cmd
end
it "with :keys_only option set to true if :ssh_key is set in state" do
state[:ssh_key] = "wicked"
Kitchen::SSH.expects(:new).with { |_hostname, _username, opts|
opts[:keys_only].must_equal true
}.returns(stub(:login_command => stub))
cmd
end
it "with :keys option set to falsey by default" do
Kitchen::SSH.expects(:new).with { |_hostname, _username, opts|
opts[:keys].nil?
}.returns(stub(:login_command => stub))
cmd
end
it "with :keys option set to an array if :ssh_key is set in config" do
config[:ssh_key] = "wicked"
Kitchen::SSH.expects(:new).with { |_hostname, _username, opts|
opts[:keys].must_equal ["wicked"]
}.returns(stub(:login_command => stub))
cmd
end
it "with :keys option set to an array if :ssh_key is set in state" do
state[:ssh_key] = "wicked"
Kitchen::SSH.expects(:new).with { |_hostname, _username, opts|
opts[:keys].must_equal ["wicked"]
}.returns(stub(:login_command => stub))
cmd
end
it "with :password option set to falsey by default" do
Kitchen::SSH.expects(:new).with { |_hostname, _username, opts|
opts[:password].nil?
}.returns(stub(:login_command => stub))
cmd
end
it "with :password option set if given in config" do
config[:password] = "psst"
Kitchen::SSH.expects(:new).with { |_hostname, _username, opts|
opts[:password].must_equal "psst"
}.returns(stub(:login_command => stub))
cmd
end
it "with :password option set if given in state" do
state[:password] = "psst"
Kitchen::SSH.expects(:new).with { |_hostname, _username, opts|
opts[:password].must_equal "psst"
}.returns(stub(:login_command => stub))
cmd
end
it "with :forward_agent option set to falsey by default" do
Kitchen::SSH.expects(:new).with { |_hostname, _username, opts|
opts[:forward_agent].nil?
}.returns(stub(:login_command => stub))
cmd
end
it "with :forward_agent option set if given in config" do
config[:forward_agent] = "yeah?"
Kitchen::SSH.expects(:new).with { |_hostname, _username, opts|
opts[:forward_agent].must_equal "yeah?"
}.returns(stub(:login_command => stub))
cmd
end
it "with :forward_agent option set if given in state" do
state[:forward_agent] = "yeah?"
Kitchen::SSH.expects(:new).with { |_hostname, _username, opts|
opts[:forward_agent].must_equal "yeah?"
}.returns(stub(:login_command => stub))
cmd
end
it "with :port option set to 22 by default" do
Kitchen::SSH.expects(:new).with { |_hostname, _username, opts|
opts[:port].must_equal 22
}.returns(stub(:login_command => stub))
cmd
end
it "with :port option set if customized in config" do
config[:port] = 1234
Kitchen::SSH.expects(:new).with { |_hostname, _username, opts|
opts[:port].must_equal 1234
}.returns(stub(:login_command => stub))
cmd
end
it "with :port option set if customized in state" do
state[:port] = 9999
Kitchen::SSH.expects(:new).with { |_hostname, _username, opts|
opts[:port].must_equal 9999
}.returns(stub(:login_command => stub))
cmd
end
it "with :logger option set to driver's logger" do
Kitchen::SSH.expects(:new).with { |_hostname, _username, opts|
opts[:logger].must_equal logger
}.returns(stub(:login_command => stub))
cmd
end
end
end
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
describe "#login_command" do
let(:cmd) { driver.login_command(state) }
before do
state[:hostname] = "fizzy"
state[:username] = "bork"
end
it "returns a LoginCommand" do
cmd.must_be_instance_of Kitchen::LoginCommand
end
constructs_an_ssh_object
end
describe "#converge" do
let(:cmd) { driver.converge(state) }
let(:connection) { stub(:exec => true, :wait => true) }
before do
state[:hostname] = "fizzy"
state[:username] = "bork"
provisioner.stubs(:[]).with(:root_path).returns("/rooty")
FakeFS.activate!
FileUtils.mkdir_p("/tmp")
end
after do
FakeFS.deactivate!
FakeFS::FileSystem.clear
end
constructs_an_ssh_object
it "creates the sandbox" do
Kitchen::SSH.stubs(:new).yields(connection)
provisioner.expects(:create_sandbox)
cmd
end
it "ensures that the sandbox is cleaned up" do
Kitchen::SSH.stubs(:new).raises
provisioner.expects(:cleanup_sandbox)
begin
cmd
rescue # rubocop:disable Lint/HandleExceptions
end
end
it "invokes the provisioner commands over ssh" do
Kitchen::SSH.stubs(:new).yields(connection)
order = sequence("order")
connection.expects(:exec).with("install").in_sequence(order)
connection.expects(:exec).with("init").in_sequence(order)
connection.expects(:exec).with("prepare").in_sequence(order)
connection.expects(:exec).with("run").in_sequence(order)
cmd
end
it "invokes the #install_command with :http_proxy set in config" do
config[:http_proxy] = "http://proxy"
Kitchen::SSH.stubs(:new).yields(connection)
connection.expects(:exec).with("env http_proxy=http://proxy install")
cmd
end
it "invokes the #install_command with :https_proxy set in config" do
config[:https_proxy] = "https://proxy"
Kitchen::SSH.stubs(:new).yields(connection)
connection.expects(:exec).with("env https_proxy=https://proxy install")
cmd
end
it "invokes the #install_command with :http_proxy & :https_proxy set" do
config[:http_proxy] = "http://proxy"
config[:https_proxy] = "https://proxy"
Kitchen::SSH.stubs(:new).yields(connection)
connection.expects(:exec).with(
"env http_proxy=http://proxy https_proxy=https://proxy install")
cmd
end
describe "transferring files" do
before do
Kitchen::SSH.stubs(:new).yields(connection)
connection.stubs(:upload_path!)
FileUtils.mkdir_p "/tmp/sandbox/stuff"
end
it "uploads files" do
connection.expects(:upload_path!).with("/tmp/sandbox/stuff", "/rooty")
cmd
end
it "logs to info" do
cmd
logged_output.string.
must_match(/INFO -- : Transferring files to instance$/)
end
it "logs to debug" do
cmd
logged_output.string.must_match(/DEBUG -- : Transfer complete$/)
end
it "raises an ActionFailed on transfer when SSHFailed is raised" do
connection.stubs(:upload_path!).raises(Kitchen::SSHFailed.new("dang"))
proc { cmd }.must_raise Kitchen::ActionFailed
end
it "raises an ActionFailed on exec when Net::SSH:Exception is raised" do
connection.stubs(:upload_path!).raises(Net::SSH::Exception.new("dang"))
proc { cmd }.must_raise Kitchen::ActionFailed
end
end
it "raises an ActionFailed on exec when SSHFailed is raised" do
Kitchen::SSH.stubs(:new).yields(connection)
connection.stubs(:exec).raises(Kitchen::SSHFailed.new("dang"))
proc { cmd }.must_raise Kitchen::ActionFailed
end
it "raises an ActionFailed on exec when Net::SSH:Exception is raised" do
Kitchen::SSH.stubs(:new).yields(connection)
connection.stubs(:exec).raises(Net::SSH::Exception.new("dang"))
proc { cmd }.must_raise Kitchen::ActionFailed
end
end
describe "#setup" do
let(:cmd) { driver.setup(state) }
let(:connection) { mock }
before do
state[:hostname] = "fizzy"
state[:username] = "bork"
end
constructs_an_ssh_object
it "doesn't invoke an ssh command if busser#setup_cmd is nil" do
busser.stubs(:setup_cmd).returns(nil)
Kitchen::SSH.stubs(:new).yields(connection)
cmd
end
it "invokes the busser#setup_cmd over ssh" do
Kitchen::SSH.stubs(:new).yields(connection)
connection.expects(:exec).with("setup")
cmd
end
it "invokes the busser#setup_cmd with :http_proxy set in config" do
config[:http_proxy] = "http://proxy"
Kitchen::SSH.stubs(:new).yields(connection)
connection.expects(:exec).with("env http_proxy=http://proxy setup")
cmd
end
it "invokes the busser#setup_cmd with :https_proxy set in config" do
config[:https_proxy] = "https://proxy"
Kitchen::SSH.stubs(:new).yields(connection)
connection.expects(:exec).with("env https_proxy=https://proxy setup")
cmd
end
it "invokes the busser#setup_cmd with :http_proxy & :https_proxy set" do
config[:http_proxy] = "http://proxy"
config[:https_proxy] = "https://proxy"
Kitchen::SSH.stubs(:new).yields(connection)
connection.expects(:exec).with(
"env http_proxy=http://proxy https_proxy=https://proxy setup")
cmd
end
it "raises an ActionFailed when SSHFailed is raised" do
Kitchen::SSH.stubs(:new).yields(connection)
connection.stubs(:exec).raises(Kitchen::SSHFailed.new("dang"))
proc { cmd }.must_raise Kitchen::ActionFailed
end
it "raises an ActionFailed when Net::SSH:Exception is raised" do
Kitchen::SSH.stubs(:new).yields(connection)
connection.stubs(:exec).raises(Net::SSH::Exception.new("dang"))
proc { cmd }.must_raise Kitchen::ActionFailed
end
end
describe "#verify" do
let(:cmd) { driver.verify(state) }
let(:connection) { mock }
before do
state[:hostname] = "fizzy"
state[:username] = "bork"
end
constructs_an_ssh_object
it "doesn't invoke an ssh command if busser#sync_cmd & #run_cmd are nil" do
busser.stubs(:sync_cmd).returns(nil)
busser.stubs(:run_cmd).returns(nil)
Kitchen::SSH.stubs(:new).yields(connection)
cmd
end
it "doesn't invoke an ssh command for busser#sync_cmd if nil" do
busser.stubs(:sync_cmd).returns(nil)
Kitchen::SSH.stubs(:new).yields(connection)
connection.expects(:exec).with("run")
cmd
end
it "doesn't invoke an ssh command for busser#run_cmd if nil" do
busser.stubs(:run_cmd).returns(nil)
Kitchen::SSH.stubs(:new).yields(connection)
connection.expects(:exec).with("sync")
cmd
end
it "invokes the busser#sync_cmd & #run_cmd over ssh" do
Kitchen::SSH.stubs(:new).yields(connection)
connection.expects(:exec).with("sync")
connection.expects(:exec).with("run")
cmd
end
it "invokes the busser#setup_cmd with :http_proxy set in config" do
busser.stubs(:run_cmd).returns(nil)
config[:http_proxy] = "http://proxy"
Kitchen::SSH.stubs(:new).yields(connection)
connection.expects(:exec).with("env http_proxy=http://proxy sync")
cmd
end
it "invokes the busser#setup_cmd with :https_proxy set in config" do
busser.stubs(:run_cmd).returns(nil)
config[:https_proxy] = "https://proxy"
Kitchen::SSH.stubs(:new).yields(connection)
connection.expects(:exec).with("env https_proxy=https://proxy sync")
cmd
end
it "invokes the busser#setup_cmd with :http_proxy & :https_proxy set" do
busser.stubs(:run_cmd).returns(nil)
config[:http_proxy] = "http://proxy"
config[:https_proxy] = "https://proxy"
Kitchen::SSH.stubs(:new).yields(connection)
connection.expects(:exec).with(
"env http_proxy=http://proxy https_proxy=https://proxy sync")
cmd
end
it "raises an ActionFailed when SSHFailed is raised" do
Kitchen::SSH.stubs(:new).yields(connection)
connection.stubs(:exec).raises(Kitchen::SSHFailed.new("dang"))
proc { cmd }.must_raise Kitchen::ActionFailed
end
it "raises an ActionFailed when Net::SSH:Exception is raised" do
Kitchen::SSH.stubs(:new).yields(connection)
connection.stubs(:exec).raises(Net::SSH::Exception.new("dang"))
proc { cmd }.must_raise Kitchen::ActionFailed
end
end
describe "#ssh" do
let(:cmd) { driver.ssh(["host", "user", { :one => "two" }], "go") }
let(:connection) { mock }
it "creates an SSH object" do
Kitchen::SSH.expects(:new).with { |hostname, username, opts|
hostname.must_equal "host"
username.must_equal "user"
opts.must_equal(:one => "two")
}
cmd
end
it "invokes the command over ssh" do
Kitchen::SSH.stubs(:new).yields(connection)
connection.expects(:exec).with("go")
cmd
end
end
describe "#remote_command" do
let(:cmd) { driver.remote_command(state, "shipit") }
let(:connection) { mock }
before do
state[:hostname] = "fizzy"
state[:username] = "bork"
end
it "creates an SSH object" do
Kitchen::SSH.expects(:new).with { |hostname, username, _opts|
hostname.must_equal "fizzy"
username.must_equal "bork"
}
cmd
end
it "invokes the command over ssh" do
Kitchen::SSH.stubs(:new).yields(connection)
connection.expects(:exec).with("shipit")
cmd
end
end
describe "#wait_for_sshd" do
let(:cmd) do
driver.send(:wait_for_sshd, "host", "user", :one => "two")
end
it "creates an SSH object with merged options" do
Kitchen::SSH.expects(:new).with { |hostname, username, opts|
hostname.must_equal "host"
username.must_equal "user"
opts.must_equal(:one => "two", :logger => logger)
}.returns(stub(:wait => true))
cmd
end
it "calls wait on the SSH object" do
ssh = mock
Kitchen::SSH.stubs(:new).returns(ssh)
ssh.expects(:wait)
cmd
end
end
end