-
Notifications
You must be signed in to change notification settings - Fork 189
/
vagrant_spec.rb
2099 lines (1672 loc) · 56.8 KB
/
vagrant_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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Author:: Fletcher Nichol (<[email protected]>)
#
# Copyright (C) 2015, 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 "logger"
require "stringio" unless defined?(StringIO)
require "kitchen/driver/vagrant"
require "kitchen/provisioner/dummy"
require "kitchen/transport/dummy"
require "kitchen/verifier/dummy"
describe Kitchen::Driver::Vagrant do
let(:logged_output) { StringIO.new }
let(:logger) { Logger.new(logged_output) }
let(:config) { { kitchen_root: "/kroot" } }
let(:platform) { Kitchen::Platform.new(name: "fooos-99") }
let(:suite) { Kitchen::Suite.new(name: "suitey") }
let(:verifier) { Kitchen::Verifier::Dummy.new }
let(:provisioner) { Kitchen::Provisioner::Dummy.new }
let(:state) { {} }
let(:lifecycle_hooks) { Kitchen::LifecycleHooks.new(config, state) }
let(:transport) { Kitchen::Transport::Dummy.new }
let(:state_file) { double("state_file") }
let(:env) { {} }
let(:driver_object) { Kitchen::Driver::Vagrant.new(config) }
let(:driver) do
d = driver_object
instance
d
end
let(:driver_with_no_instance) do
driver_object
end
let(:instance) do
Kitchen::Instance.new(
verifier: verifier,
driver: driver_object,
logger: logger,
suite: suite,
platform: platform,
provisioner: provisioner,
lifecycle_hooks: lifecycle_hooks,
transport: transport,
state_file: state_file
)
end
module RunCommandStub
def run_command(_cmd, options = {})
options
end
end
before(:all) do
Kitchen::Driver::Vagrant.instance_eval { include RunCommandStub }
end
before(:each) { stub_const("ENV", env) }
after do
driver_object.class.send(:vagrant_version=, nil)
end
it "driver api_version is 2" do
expect(driver.diagnose_plugin[:api_version]).to eq(2)
end
it "plugin_version is set to Kitchen::Vagrant::VERSION" do
expect(driver.diagnose_plugin[:version]).to eq(
Kitchen::Driver::VAGRANT_VERSION
)
end
describe "#run_command" do
context "when invoked from a clean environment" do
it "passes through environment variables" do
options = driver.send(
:run_command,
"cmd",
environment: { "EV1" => "Val1", "EV2" => "Val2" }
)
expect(options[:environment]["EV1"]).to eq("Val1")
expect(options[:environment]["EV2"]).to eq("Val2")
end
it "leaves path alone" do
path = "/foo/#{File::PATH_SEPARATOR}/bar"
options = driver.send(
:run_command,
"cmd",
environment: { "PATH" => path }
)
expect(options[:environment]["PATH"]).to eq(path)
end
end
context "when invoked from a bundler[:environment]" do
let(:bundler_env) do
{
"BUNDLE_BIN_PATH" => "bundle_bin_path",
"BUNDLE_GEMFILE" => "bundle_gem_file",
"GEM_HOME" => "gem_home",
"GEM_PATH" => "gem_path",
"GEM_ROOT" => "gem_root",
"RUBYLIB" => "ruby_lib",
"RUBYOPT" => "ruby_opt",
"_ORIGINAL_GEM_PATH" => "original_gem_path",
}
end
it "removes all bundler related variables" do
env.merge!(bundler_env)
options = driver.send(:run_command, "cmd")
bundler_env.each do |k, _v|
expect(options[:environment]).to include(k)
expect(options[:environment][k]).to eq(nil)
end
end
it "fixes path if it notices gem_home in it" do
allow(RbConfig::CONFIG).to receive(:[]).with("host_os")
.and_return("linux")
env.merge!(bundler_env)
env["PATH"] = "gem_home/bin#{File::PATH_SEPARATOR}/something/else"
options = driver.send(:run_command, "cmd")
puts(options)
expect(options[:environment]["PATH"]).to eq("/something/else")
end
end
end
describe "configuration" do
let(:cache_directory_array) do
[
File.expand_path("~/.kitchen/cache"),
"/tmp/omnibus/cache",
"create: true",
]
end
%w{centos debian fedora opensuse ubuntu oracle freebsd hardenedbsd}.each do |name|
context "for known bento platform names starting with #{name}" do
before { allow(platform).to receive(:name) { "#{name}-99.04" } }
it "sets :box based on the platform name by default" do
expect(driver[:box]).to eq("bento/#{name}-99.04")
end
it "sets :box to a custom value" do
config[:box] = "booya"
expect(driver[:box]).to eq("booya")
end
it "sets :box_url to nil" do
config[:provider] = "the-next-coolness"
expect(driver[:box_url]).to eq(nil)
end
end
end
context "for unknown bento platform names" do
before { allow(platform).to receive(:name) { "slackware-14.1" } }
it "sets :box based on the platform name by default" do
expect(driver[:box]).to eq("slackware-14.1")
end
it "sets :box to a custom value" do
config[:box] = "booya"
expect(driver[:box]).to eq("booya")
end
it "sets :box_url to nil" do
expect(driver[:box_url]).to eq(nil)
end
end
it "sets :box_check_update to nil by default" do
expect(driver[:box_check_update]).to eq(nil)
end
it "sets :box_check_update to a custom value" do
config[:box_check_update] = true
expect(driver[:box_check_update]).to eq(true)
end
it "sets :box_download_ca_cert to nil by default" do
expect(driver[:box_download_ca_cert]).to eq(nil)
end
it "sets :box_download_ca_cert to a custom value" do
config[:box_download_ca_cert] = "cacert.pem"
expect(driver[:box_download_ca_cert]).to eq("/kroot/cacert.pem")
end
it "sets :box_download_insecure to nil by default" do
expect(driver[:box_download_insecure]).to eq(nil)
end
it "sets :box_download_insecure to a custom value" do
config[:box_download_insecure] = true
expect(driver[:box_download_insecure]).to eq(true)
end
it "sets :box_version to nil by default" do
expect(driver[:box_version]).to eq(nil)
end
it "sets :box_version to a custom value" do
config[:box_version] = "1.2.3"
expect(driver[:box_version]).to eq("1.2.3")
end
it "sets :boot_timeout to nil by default" do
expect(driver[:boot_timeout]).to eq(nil)
end
it "sets :boot_timeout to a custom value" do
config[:boot_timeout] = 600
expect(driver[:boot_timeout]).to eq(600)
end
it "sets :customize to an empty hash by default" do
expect(driver[:customize]).to eq({})
end
it "sets :customize to a custom value" do
config[:customize] = { a: "b", c: { d: "e" } }
expect(driver[:customize]).to eq(a: "b", c: { d: "e" })
end
it "sets :gui to nil by default" do
expect(driver[:gui]).to eq(nil)
end
it "sets :linked_clone to nil by default" do
expect(driver[:linked_clone]).to eq(nil)
end
it "sets :network to an empty array by default" do
expect(driver[:network]).to eq([])
end
it "sets :network to a custom value" do
config[:network] = [
["forwarded_port", guest: 80, host: 8080],
]
expect(driver[:network]).to eq([
["forwarded_port", guest: 80, host: 8080],
])
end
it "sets :pre_create_command to nil by default" do
expect(driver[:pre_create_command]).to eq(nil)
end
it "sets :pre_create_command to a custom value" do
config[:pre_create_command] = "execute yo"
expect(driver[:pre_create_command]).to eq("execute yo")
end
it "replaces {{vagrant_root}} in :pre_create_command" do
config[:pre_create_command] = "{{vagrant_root}}/candy"
expect(driver[:pre_create_command]).to eq(
"/kroot/.kitchen/kitchen-vagrant/suitey-fooos-99/candy"
)
end
it "sets :provision to false by default" do
expect(driver[:provision]).to eq(false)
end
it "sets :provision to a custom value" do
config[:provision] = true
expect(driver[:provision]).to eq(true)
end
it "sets :provider to virtualbox by default" do
expect(driver[:provider]).to eq("virtualbox")
end
it "sets :provider to the value of VAGRANT_DEFAULT_PROVIDER from ENV" do
env["VAGRANT_DEFAULT_PROVIDER"] = "vcool"
expect(driver[:provider]).to eq("vcool")
end
it "sets :provider to a custom value" do
config[:provider] = "mything"
expect(driver[:provider]).to eq("mything")
end
it "sets :ssh to an empty hash by default" do
expect(driver[:ssh]).to eq({})
end
it "sets :ssh to a custom value" do
config[:ssh] = { a: "b", c: { d: "e" } }
expect(driver[:ssh]).to eq(a: "b", c: { d: "e" })
end
it "sets :synced_folders with the cache_directory for select bento boxes" do
config[:box] = "bento/centos-99"
expect(driver[:synced_folders]).to eq([cache_directory_array])
end
it "does not set :synced_folders when cache_directory is false" do
config[:box] = "bento/centos-99"
config[:cache_directory] = false
expect(driver[:synced_folders]).to eq([])
end
it "sets :synced_folders with the cache_directory when :use_cached_chef_client is `true`" do
config[:box] = "some_owner/centos-99"
config[:use_cached_chef_client] = true
expect(driver[:synced_folders]).to eq([cache_directory_array])
end
it "does not set :synced_folders to cache_directory on freebsd systems" do
allow(platform).to receive(:name).and_return("freebsd-99")
expect(driver[:synced_folders]).to eq([])
end
it "sets :synced_folders to a custom value" do
config[:synced_folders] = [
["/host_path", "/vm_path", "create: true, type: :nfs"],
]
expect(driver[:synced_folders]).to eq([
[
File.expand_path("/host_path"),
"/vm_path", "create: true, type: :nfs"
],
])
end
it "replaces %{instance_name} with instance name in :synced_folders" do
config[:synced_folders] = [
["/root/%{instance_name}", "/vm_path", "stuff"],
]
expect(driver[:synced_folders]).to eq([
[File.expand_path("/root/suitey-fooos-99"), "/vm_path", "stuff"],
])
end
it "expands source paths relative to :kitchen_root in :synced_folders" do
config[:synced_folders] = [
["./a", "/vm_path", "stuff"],
]
expect(driver[:synced_folders]).to eq([
[File.expand_path("/kroot/a"), "/vm_path", "stuff"],
])
end
it "sets options to 'nil' if not set in :synced_folders entry" do
config[:synced_folders] = [
["/host_path", "/vm_path", nil],
]
expect(driver[:synced_folders]).to eq([
[File.expand_path("/host_path"), "/vm_path", "nil"],
])
end
it "sets :vagrant_binary to 'vagrant' by default" do
expect(driver[:vagrant_binary]).to eq("vagrant")
end
it "sets :vagrant_binary to a custom value" do
config[:vagrant_binary] = "vagrant.cmd"
expect(driver[:vagrant_binary]).to eq("vagrant.cmd")
end
it "sets :vagrantfile_erb to a default" do
expect(driver[:vagrantfile_erb]).to match(
%r{/kitchen-vagrant/templates/Vagrantfile\.erb$}
)
end
it "sets :vagrantfile_erb to a default value" do
config[:vagrantfile_erb] = "/a/Vagrantfile.erb"
expect(driver[:vagrantfile_erb]).to eq(
File.expand_path("/a/Vagrantfile.erb")
)
end
it "expands path for :vagrantfile_erb" do
config[:vagrantfile_erb] = "Yep.erb"
expect(driver[:vagrantfile_erb]).to eq(
File.expand_path("/kroot/Yep.erb")
)
end
it "sets :vagrantfiles to an empty array by default" do
expect(driver[:vagrantfiles]).to eq([])
end
it "sets and expands paths in :vagrantfiles" do
config[:vagrantfiles] = %w{one two three}
expect(driver[:vagrantfiles]).to eq(
%w{/kroot/one /kroot/two /kroot/three}.map { |f| File.expand_path(f) }
)
end
context "for unix os_types" do
before { allow(platform).to receive(:os_type).and_return("unix") }
it "sets :vm_hostname to the instance name by default" do
expect(driver[:vm_hostname]).to eq("suitey-fooos-99.vagrantup.com")
end
it "sets :vm_hostname to a custom value" do
config[:vm_hostname] = "okay"
expect(driver[:vm_hostname]).to eq("okay")
end
end
context "for windows os_types" do
before { allow(platform).to receive(:os_type).and_return("windows") }
it "sets :vm_hostname to nil by default" do
expect(driver[:vm_hostname]).to eq(nil)
end
it "sets :vm_hostname to a custom value, truncated to 15 chars" do
config[:vm_hostname] = "this-is-a-pretty-long-name-ya-think"
expect(driver[:vm_hostname]).to eq("this-is-a-pr-k")
end
it "replaces %{instance_name} with instance name in :synced_folders" do
config[:synced_folders] = [
["/root/%{instance_name}", "/vm_path", "stuff"],
]
expect(driver[:synced_folders]).to eq([
[File.expand_path("/root/suitey-fooos-99"), "/vm_path", "stuff"],
])
end
end
context "when cache_directory is customized" do
let(:custom_cache_directory_array) do
[
File.expand_path("~/.kitchen/cache"),
"Z:\\awesome\\cache",
"create: true",
]
end
before do
config[:box] = "bento/centos-99"
config[:cache_directory] = "Z:\\awesome\\cache"
end
it "sets :synced_folders with the custom cache_directory" do
expect(driver[:synced_folders]).to eq([custom_cache_directory_array])
end
it "replaces %{instance_name} with instance name in :synced_folders" do
config[:synced_folders] = [
["/root/%{instance_name}", "/vm_path", "stuff"],
]
expect(driver[:synced_folders]).to eq([
[File.expand_path("/root/suitey-fooos-99"), "/vm_path", "stuff"],
custom_cache_directory_array,
])
end
end
end
describe "#verify_dependencies" do
it "passes for supported versions of Vagrant" do
with_modern_vagrant
driver.verify_dependencies
end
it "passes for supported versions of Vagrant when it has no instances" do
with_modern_vagrant
driver_with_no_instance.verify_dependencies
end
it "raises a UserError for unsupported versions of Vagrant" do
with_unsupported_vagrant
expect { driver.verify_dependencies }.to raise_error(
Kitchen::UserError, /Please upgrade to version 2.4.0 or higher/
)
end
it "raises a UserError for a missing Vagrant command" do
allow(driver_object).to receive(:run_command)
.with("vagrant --version", any_args).and_raise(Errno::ENOENT)
expect { driver.verify_dependencies }.to raise_error(
Kitchen::UserError, /Vagrant 2.4.0 or higher is not installed/
)
end
end
describe "#create" do
let(:cmd) { driver.create(state) }
let(:vagrant_root) do
File.join(%W{
#{@dir} .kitchen kitchen-vagrant suitey-fooos-99
})
end
before do
@dir = Dir.mktmpdir("kitchen_root")
config[:kitchen_root] = @dir
allow(driver).to receive(:run_command).and_return("")
with_modern_vagrant
end
after do
FileUtils.remove_entry_secure(@dir)
end
it "logs a message on debug level for creating the Vagrantfile" do
cmd
expect(logged_output.string).to match(
/^D, .+ DEBUG -- : Creating Vagrantfile for \<suitey-fooos-99\> /
)
end
it "creates a Vagrantfile in the vagrant root directory" do
cmd
expect(File.exist?(File.join(vagrant_root, "Vagrantfile"))).to eq(true)
end
it "calls Transport's #wait_until_ready" do
conn = double("connection")
allow(transport).to receive(:connection).with(state).and_return(conn)
expect(conn).to receive(:wait_until_ready)
cmd
end
it "logs the Vagrantfile contents on debug level" do
cmd
expect(debug_lines).to match(Regexp.new(<<-REGEXP.gsub(/^ {8}/, "")))
------------
Vagrant.configure\("2"\) do \|c\|
.*
end
------------
REGEXP
end
it "raises ActionFailed if a custom Vagrantfile template was not found" do
config[:vagrantfile_erb] = "/a/bunch/of/nope"
expect { cmd }.to raise_error(
Kitchen::ActionFailed, /^Could not find Vagrantfile template/
)
end
it "runs the pre create command, if set" do
config[:pre_create_command] = "echo heya"
expect(driver).to receive(:run_command).with("echo heya", any_args)
cmd
end
it "runs vagrant up with --no-provision if :provision is falsey" do
config[:provision] = false
expect(driver).to receive(:run_command)
.with("vagrant up --no-provision --provider virtualbox", any_args)
cmd
end
it "runs vagrant up without --no-provision if :provision is truthy" do
config[:provision] = true
expect(driver).to receive(:run_command)
.with("vagrant up --provider virtualbox", any_args)
cmd
end
it "runs vagrant up with a custom provider if :provider is set" do
config[:provider] = "bananas"
expect(driver).to receive(:run_command)
.with("vagrant up --no-provision --provider bananas", any_args)
cmd
end
describe "for state" do
context "for non-WinRM-based transports" do
let(:output) do
<<-OUTPUT.gsub(/^ {10}/, "")
Host hehe
HostName 192.168.32.64
User vagrant
Port 2022
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /path/to/private_key
IdentitiesOnly yes
LogLevel FATAL
OUTPUT
end
before do
allow(transport).to receive(:name).and_return("Coolness")
allow(driver).to receive(:run_command)
.with("vagrant ssh-config", any_args).and_return(output)
end
it "sets :hostname from ssh-config" do
cmd
expect(state).to include(hostname: "192.168.32.64")
end
it "sets :port from ssh-config" do
cmd
expect(state).to include(port: "2022")
end
it "sets :username from ssh-config" do
cmd
expect(state).to include(username: "vagrant")
end
it "does not set :password by default" do
cmd
expect(state.keys).to_not include(:password)
end
it "sets :password if Password is in ssh-config" do
output.concat(" Password yep\n")
cmd
expect(state).to include(password: "yep")
end
it "sets :ssh_key from ssh-config" do
cmd
expect(state).to include(ssh_key: "/path/to/private_key")
end
it "does not set :proxy_command by default" do
cmd
expect(state.keys).to_not include(:proxy_command)
end
it "sets :proxy_command if ProxyCommand is in ssh-config" do
output.concat(" ProxyCommand echo proxy\n")
cmd
expect(state).to include(proxy_command: "echo proxy")
end
end
context "for WinRM-based transports" do
let(:output) do
<<-OUTPUT.gsub(/^ {10}/, "")
Host hehe
HostName 192.168.32.64
User vagrant
Password yep
Port 9999
RDPPort 5555
OUTPUT
end
before do
allow(transport).to receive(:name).and_return("WinRM")
allow(driver).to receive(:run_command)
.with("vagrant winrm-config", any_args).and_return(output)
end
it "sets :hostname from winrm-config" do
cmd
expect(state).to include(hostname: "192.168.32.64")
end
it "sets :port from winrm-config" do
cmd
expect(state).to include(port: "9999")
end
it "sets :username from winrm-config" do
cmd
expect(state).to include(username: "vagrant")
end
it "sets :password from winrm-config" do
cmd
expect(state).to include(password: "yep")
end
it "sets :rdp_port from winrm-config" do
cmd
expect(state).to include(rdp_port: "5555")
end
end
end
it "logs a message on info level" do
cmd
expect(logged_output.string).to match(
/I, .+ INFO -- : Vagrant instance \<suitey-fooos-99\> created\.$/
)
end
end
describe "#destroy" do
let(:cmd) { driver.destroy(state) }
let(:vagrant_root) do
File.join(%W{
#{@dir} .kitchen kitchen-vagrant suitey-fooos-99
})
end
before do
@dir = Dir.mktmpdir("kitchen_root")
config[:kitchen_root] = @dir
allow(driver).to receive(:run_command).and_return("")
with_modern_vagrant
FileUtils.mkdir_p(vagrant_root)
state[:hostname] = "hosta"
end
after do
FileUtils.remove_entry_secure(@dir)
end
it "logs a message on debug level for creating the Vagrantfile" do
cmd
expect(logged_output.string).to match(
/^D, .+ DEBUG -- : Creating Vagrantfile for \<suitey-fooos-99\> /
)
end
it "logs the Vagrantfile contents on debug level" do
cmd
expect(debug_lines).to match(Regexp.new(<<-REGEXP.gsub(/^ {8}/, "")))
------------
Vagrant.configure\("2"\) do \|c\|
.*
end
------------
REGEXP
end
it "does not run vagrant destroy if :hostname is not present in state" do
state.delete(:hostname)
expect(driver).to_not receive(:run_command)
.with("vagrant destroy -f", any_args)
cmd
end
it "closes the transport connection" do
connection = double(Kitchen::Transport::Base::Connection)
allow(transport).to receive(:connection).with(state) { connection }
expect(connection).to receive(:close)
cmd
end
it "runs vagrant destroy" do
expect(driver).to receive(:run_command)
.with("vagrant destroy -f", any_args)
cmd
end
it "deletes the vagrant root directory" do
expect(File.directory?(vagrant_root)).to eq(true)
cmd
expect(File.directory?(vagrant_root)).to eq(false)
end
it "logs a message on info level" do
cmd
expect(logged_output.string).to match(
/I, .+ INFO -- : Vagrant instance \<suitey-fooos-99\> destroyed\.$/
)
end
it "deletes :hostname from state" do
cmd
expect(state.keys).to_not include(:hostname)
end
end
describe "Vagrantfile" do
let(:cmd) { driver.create(state) }
let(:vagrant_root) do
File.join(%W{
#{@dir} .kitchen kitchen-vagrant suitey-fooos-99
})
end
before do
@dir = Dir.mktmpdir("kitchen_root")
config[:kitchen_root] = @dir
allow(driver).to receive(:run_command).and_return("")
with_modern_vagrant
end
after do
FileUtils.remove_entry_secure(@dir)
end
it "disables the vagrant-berkshelf plugin is present" do
cmd
expect(vagrantfile).to match(regexify(
"c.berkshelf.enabled = false " \
"if Vagrant.has_plugin?(\"vagrant-berkshelf\")"
))
end
it "sets no cache.scope if missing" do
config[:cachier] = nil
cmd
expect(vagrantfile).to_not match(regexify(%{c.cache.scope}, :partial))
end
it "sets cache.scope to :box if :cachier is set" do
config[:cachier] = true
cmd
expect(vagrantfile).to match(regexify(%{c.cache.scope = :box}))
end
it "sets cache.scope if :cachier is set to a custom value" do
config[:cachier] = ":machine"
cmd
expect(vagrantfile).to match(regexify(%{c.cache.scope = :machine}))
end
it "sets the vm.box" do
cmd
expect(vagrantfile).to match(regexify(%{c.vm.box = "fooos-99"}))
end
it "sets the vm.hostname" do
config[:vm_hostname] = "charlie"
cmd
expect(vagrantfile).to match(regexify(%{c.vm.hostname = "charlie"}))
end
it "disables the /vagrant synced folder by default" do
cmd
expect(vagrantfile).to match(regexify(
%{c.vm.synced_folder ".", "/vagrant", disabled: true}
))
end
it "creates an empty provider block by default" do
config[:provider] = "wowza"
cmd
expect(vagrantfile).to match(regexify(<<-RUBY.gsub(/^ {6}/, "").chomp))
c.vm.provider :wowza do |p|
end
RUBY
end
it "requires no Vagrantfiles by default" do
cmd
expect(vagrantfile).to_not match(regexify("require"))
end
it "requires each entry in :vagranfiles" do
config[:vagrantfiles] = %w{/a /b /c}
cmd
expect(vagrantfile).to match(regexify(<<-RUBY.gsub(/^ {8}/, "").chomp))
load "/a"
load "/b"
load "/c"
RUBY
end
it "sets no vm.box_url if missing" do
config[:box_url] = nil
cmd
expect(vagrantfile).to_not match(regexify(%{c.vm.box_url}, :partial))
end
it "sets vm.box_url if :box_url is set" do
config[:box_url] = "dat.url"
cmd
expect(vagrantfile).to match(regexify(%{c.vm.box_url = "dat.url"}))
end
it "sets no vm.box_version if missing" do
config[:box_version] = nil
cmd
expect(vagrantfile).to_not match(regexify(%{c.vm.box_version}, :partial))
end
it "sets vm.box_version if :box_version is set" do
config[:box_version] = "a.b.c"