-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvcair_spec.rb
More file actions
876 lines (722 loc) · 25.6 KB
/
vcair_spec.rb
File metadata and controls
876 lines (722 loc) · 25.6 KB
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
# Encoding: UTF-8
#
# Authors:: Chef Partner Engineering (<partnereng@chef.io>)
# Copyright:: Copyright (c) 2015 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 'excon'
require 'kitchen/driver/vcair'
require 'kitchen/provisioner/dummy'
require 'kitchen/transport/dummy'
require 'kitchen/verifier/dummy'
describe Kitchen::Driver::Vcair do
let(:logged_output) { StringIO.new }
let(:logger) { Logger.new(logged_output) }
let(:platform) { Kitchen::Platform.new(name: 'fake_platform') }
let(:transport) { Kitchen::Transport::Dummy.new }
let(:driver) { Kitchen::Driver::Vcair.new(config) }
let(:config) do
{
vcair_username: 'myuser',
vcair_password: 'mypassword',
vcair_api_host: 'https://vcloud.air',
vcair_api_path: '/api/compute/api',
vcair_org: 'myorg',
cpus: 2,
memory: 2048,
vdc_id: 1,
catalog_id: 2,
image_id: 3,
network_id: 4
}
end
let(:instance) do
instance_double(Kitchen::Instance,
logger: logger,
transport: transport,
platform: platform,
to_str: 'instance_str')
end
before do
allow(driver).to receive(:instance).and_return(instance)
end
describe '#create' do
context 'when the server is already created' do
let(:state) { { vapp_id: 'vapp1' } }
it 'does not call create_server' do
expect(driver).not_to receive(:create_server)
end
end
context 'when the server is not yet created' do
let(:state) { {} }
let(:vm) { double('vm') }
it 'calls the expected methods' do
allow(driver).to receive(:vm).and_return(vm)
allow(driver.vm).to receive(:ip_address).and_return('1.2.3.4')
expect(driver).to receive(:validate!)
expect(driver).to receive(:create_server).with(state)
expect(driver.vm).to receive(:wait_for)
expect(driver).to receive(:wait_for_server).with(state)
driver.create(state)
end
end
end
describe '#destroy' do
context 'when the server does not exist' do
let(:state) { {} }
it 'does not fetch the vapp' do
expect(driver).not_to receive(:vapp)
driver.destroy(state)
end
end
context 'when the server exists' do
let(:state) { { vapp_id: 'vapp1' } }
let(:vapp) { double('vapp') }
before do
allow(driver).to receive(:validate!)
allow(driver).to receive(:vapp).and_return(vapp)
allow(vapp).to receive(:power_off)
allow(vapp).to receive(:undeploy)
allow(vapp).to receive(:destroy)
end
it 'calls validate!' do
expect(driver).to receive(:validate!)
driver.destroy(state)
end
it 'sets the vapp_id' do
expect(driver).to receive(:vapp_id=).with('vapp1')
driver.destroy(state)
end
context 'when the vapp does not exist' do
it 'does not call vapp again to power_off, etc.' do
expect(driver).to receive(:vapp).once.and_raise(Fog::Compute::VcloudDirector::Forbidden)
driver.destroy(state)
end
end
it 'fetches the vapp' do
expect(driver).to receive(:vapp)
driver.destroy(state)
end
it 'gets rid of the vapp' do
expect(driver.vapp).to receive(:power_off)
expect(driver.vapp).to receive(:undeploy)
expect(driver.vapp).to receive(:destroy)
driver.destroy(state)
end
end
end
describe '#vcloud_client' do
let(:fog_server_def) { double('fog_server_def') }
let(:client) { double('client') }
before do
allow(driver).to receive(:fog_server_def).and_return(fog_server_def)
end
it 'sets up a new Fog::Compute instance' do
expect(Fog::Compute).to receive(:new).with(fog_server_def).and_return(client)
expect(driver.vcloud_client).to eq(client)
end
it 'raises an error if unauthorized' do
allow(Fog::Compute).to receive(:new).and_raise(Excon::Errors::Unauthorized, 'auth failed')
expect { driver.vcloud_client }.to raise_error(
RuntimeError,
'Connection failure, please check your username and password. -- auth failed'
)
end
end
describe '#org' do
let(:client) { double('client') }
let(:organizations) { double('organizations') }
let(:org) { double('org') }
it 'fetches the organization by name' do
allow(driver).to receive(:vcloud_client).and_return(client)
allow(client).to receive(:organizations).and_return(organizations)
expect(organizations).to receive(:get_by_name).with('myorg').and_return(org)
expect(driver.org).to eq(org)
end
end
describe '#create_server' do
let(:state) { {} }
it 'calls the expected methods' do
expect(driver).to receive(:instantiate).and_return('vapp1')
expect(driver).to receive(:vapp_id=).with('vapp1')
expect(driver).to receive(:validate_vapp).and_return(true)
expect(driver).to receive(:update_customization)
expect(driver).to receive(:adjust_hardware)
expect(driver).to receive(:attach_network)
expect(driver).to receive(:tag_vm)
expect(driver).to receive(:power_on)
driver.create_server(state)
end
context 'when the vapp validation fails' do
before do
allow(driver).to receive(:instantiate)
allow(driver).to receive(:validate_vapp).and_return(false)
end
it 'destroys the server' do
expect(driver).to receive(:destroy).with(state)
driver.create_server(state)
end
it 'does not power on the vapp' do
expect(driver).not_to receive(:power_on)
driver.create_server(state)
end
end
end
describe '#adjust_hardware' do
let(:vm) { double('vm') }
before do
allow(driver).to receive(:vm).and_return(vm)
end
context 'when config parameters are supplied' do
it 'sets the cpus and memory on the VM' do
expect(vm).to receive(:cpu=).with(2)
expect(vm).to receive(:memory=).with(2048)
driver.adjust_hardware
end
end
context 'when config parameters are not supplied' do
before do
config[:cpus] = nil
config[:memory] = nil
end
it 'does not set the cpus and memory on the VM' do
expect(vm).not_to receive(:cpu=)
expect(vm).not_to receive(:memory=)
driver.adjust_hardware
end
end
end
describe '#attach_network' do
let(:client) { double('client') }
let(:payload) { {} }
let(:task) { double('task', body: 'body text') }
let(:vm) { double('vm', id: 'vm1') }
it 'submits the request and processes it' do
allow(driver).to receive(:vcloud_client).and_return(client)
allow(driver).to receive(:attach_network_payload).and_return(payload)
allow(driver).to receive(:vm).and_return(vm)
expect(client).to receive(:put_network_connection_system_section_vapp)
.with('vm1', payload).and_return(task)
expect(client).to receive(:process_task).with('body text')
driver.attach_network
end
end
describe '#tag_vm' do
let(:vm) { double('vm') }
let(:tags) { double('tags') }
it 'tags the VM' do
allow(driver).to receive(:vm).and_return(vm)
allow(vm).to receive(:tags).and_return(tags)
expect(tags).to receive(:create).with('created-by', 'test-kitchen')
driver.tag_vm
end
end
describe '#power_on' do
let(:vapp) { double('vapp') }
it 'powers on the vApp' do
allow(driver).to receive(:vapp).and_return(vapp)
expect(vapp).to receive(:power_on)
driver.power_on
end
end
describe '#wait_for_server' do
let(:connection) { instance.transport.connection(state) }
let(:state) { {} }
let(:vapp) { double('vapp', id: 'vapp1') }
let(:vm) { double('vm', name: 'vm1') }
before do
allow(transport).to receive(:connection).and_return(connection)
allow(driver).to receive(:vapp).and_return(vapp)
allow(driver).to receive(:vm).and_return(vm)
end
it 'calls wait_until_ready on the transport connection' do
expect(connection).to receive(:wait_until_ready)
driver.wait_for_server(state)
end
it 'destroys the vApp if the server failed to become ready' do
allow(connection).to receive(:wait_until_ready).and_raise(RuntimeError)
expect(driver).to receive(:destroy).with(state)
expect { driver.wait_for_server(state) }.to raise_error(RuntimeError)
end
end
describe '#vcloud_username' do
it 'returns a properly-formatted username' do
expect(driver.vcloud_username).to eq('myuser@myorg')
end
end
describe '#image' do
let(:catalog) { double('catalog') }
let(:catalog_items) { double('catalog_items') }
let(:image) { double('image') }
before do
allow(driver).to receive(:catalog).and_return(catalog)
allow(catalog).to receive(:catalog_items).and_return(catalog_items)
end
context 'when an ID is provided' do
before do
config[:image_id] = 1
config[:image_name] = nil
end
it 'fetches the catalog item by ID' do
expect(catalog_items).to receive(:get).with(1).and_return(image)
expect(driver.image).to eq(image)
end
end
context 'when a name is provided' do
before do
config[:image_id] = nil
config[:image_name] = 'image name'
end
it 'fetches the catalog item by name' do
expect(catalog_items).to receive(:get_by_name).with('image name').and_return(image)
expect(driver.image).to eq(image)
end
end
context 'when no image is returned' do
before do
config[:image_id] = 1
config[:image_name] = nil
end
it 'raises an exception' do
expect(catalog_items).to receive(:get).with(1).and_return(nil)
expect { driver.image }.to raise_error(RuntimeError)
end
end
end
describe '#catalog' do
let(:org) { double('org') }
let(:catalogs) { double('catalogs') }
let(:catalog) { double('catalog') }
before do
allow(driver).to receive(:org).and_return(org)
allow(org).to receive(:catalogs).and_return(catalogs)
end
context 'when an ID is provided' do
before do
config[:catalog_id] = 1
config[:catalog_name] = nil
end
it 'fetches the catalog by ID' do
expect(catalogs).to receive(:get).with(1).and_return(catalog)
expect(driver.catalog).to eq(catalog)
end
end
context 'when a name is provided' do
before do
config[:catalog_id] = nil
config[:catalog_name] = 'catalog name'
end
it 'fetches the catalog by name' do
expect(catalogs).to receive(:get_by_name).with('catalog name').and_return(catalog)
expect(driver.catalog).to eq(catalog)
end
end
context 'when no catalog is returned' do
before do
config[:catalog_id] = 1
config[:catalog_name] = nil
end
it 'raises an exception' do
expect(catalogs).to receive(:get).with(1).and_return(nil)
expect { driver.catalog }.to raise_error(RuntimeError)
end
end
end
describe '#vdc' do
let(:org) { double('org') }
let(:vdcs) { double('vdcs') }
let(:vdc) { double('vdc') }
before do
allow(driver).to receive(:org).and_return(org)
allow(org).to receive(:vdcs).and_return(vdcs)
end
context 'when an ID is provided' do
before do
config[:vdc_id] = 1
config[:vdc_name] = nil
end
it 'fetches the vdc by ID' do
expect(vdcs).to receive(:get).with(1).and_return(vdc)
expect(driver.vdc).to eq(vdc)
end
end
context 'when a name is provided' do
before do
config[:vdc_id] = nil
config[:vdc_name] = 'vdc name'
end
it 'fetches the vdc by name' do
expect(vdcs).to receive(:get_by_name).with('vdc name').and_return(vdc)
expect(driver.vdc).to eq(vdc)
end
end
context 'when no vdc is returned' do
before do
config[:vdc_id] = 1
config[:vdc_name] = nil
end
it 'raises an exception' do
expect(vdcs).to receive(:get).with(1).and_return(nil)
expect { driver.vdc }.to raise_error(RuntimeError)
end
end
end
describe '#network' do
let(:org) { double('org') }
let(:networks) { double('networks') }
let(:network) { double('network') }
before do
allow(driver).to receive(:org).and_return(org)
allow(org).to receive(:networks).and_return(networks)
end
context 'when an ID is provided' do
before do
config[:network_id] = 1
config[:network_name] = nil
end
it 'fetches the network by ID' do
expect(networks).to receive(:get).with(1).and_return(network)
expect(driver.network).to eq(network)
end
end
context 'when a name is provided' do
before do
config[:network_id] = nil
config[:network_name] = 'network name'
end
it 'fetches the network by name' do
expect(networks).to receive(:get_by_name).with('network name').and_return(network)
expect(driver.network).to eq(network)
end
end
context 'when no network is returned' do
before do
config[:network_id] = 1
config[:network_name] = nil
end
it 'raises an exception' do
expect(networks).to receive(:get).with(1).and_return(nil)
expect { driver.network }.to raise_error(RuntimeError)
end
end
end
describe '#node_description' do
context 'when a node description is provided' do
before do
config[:node_description] = 'sample description'
end
it 'returns the configured description' do
expect(driver.node_description).to eq('sample description')
end
end
context 'when a node description is not provided' do
it 'returns the default description' do
allow(driver).to receive(:node_name).and_return('node')
expect(driver.node_description).to eq('Test Kitchen: node')
end
end
end
describe '#node_name' do
context 'when a node name is provided' do
before do
config[:node_name] = 'testnode'
end
it 'returns the configured node name' do
expect(driver.node_name).to eq('testnode')
end
end
context 'when a node name is not provided' do
it 'returns a generated node name' do
expect(driver).to receive(:generate_node_name).and_return('a12345')
expect(driver.node_name).to eq('a12345')
end
end
end
describe '#generate_node_name' do
it 'generates a node name using SecureRandom' do
expect(SecureRandom).to receive(:hex).with(6).and_return('randomchars')
expect(driver.generate_node_name).to eq('tk-randomchars')
end
end
describe '#print_error_and_exit' do
it 'prints an error message and raises an exception' do
expect(driver).to receive(:error).with('error text')
expect { driver.print_error_and_exit('error text') }.to raise_error(RuntimeError)
end
end
describe '#validate!' do
it 'calls all the expected validate methods' do
expect(driver).to receive(:validate_parameter_pair!).with('vdc')
expect(driver).to receive(:validate_parameter_pair!).with('catalog')
expect(driver).to receive(:validate_parameter_pair!).with('image')
expect(driver).to receive(:validate_parameter_pair!).with('network')
expect(driver).to receive(:validate_method!).with(:org)
expect(driver).to receive(:validate_method!).with(:vdc)
expect(driver).to receive(:validate_method!).with(:catalog)
expect(driver).to receive(:validate_method!).with(:image)
expect(driver).to receive(:validate_method!).with(:network)
expect(driver).to receive(:validate_customization_script!)
expect(driver).to receive(:validate_computer_name!)
driver.validate!
end
end
describe '#validate_parameter_pair!' do
context 'when an ID exists but not a name' do
before do
config[:test_id] = 1
config[:test_name] = nil
end
it 'does not print an error' do
expect(driver).not_to receive(:print_error_and_exit)
driver.validate_parameter_pair!('test')
end
end
context 'when a name exists but not an ID' do
before do
config[:test_id] = nil
config[:test_name] = 'test'
end
it 'does not print an error' do
expect(driver).not_to receive(:print_error_and_exit)
driver.validate_parameter_pair!('test')
end
end
context 'when neither a name nor an ID exists' do
before do
config[:test_id] = nil
config[:test_name] = nil
end
it 'prints an error' do
expect(driver).to receive(:print_error_and_exit)
driver.validate_parameter_pair!('test')
end
end
end
describe '#validate_method!' do
context 'when the method is successful' do
it 'does not raise an exception' do
allow(driver).to receive(:test_method)
expect { driver.validate_method!(:test_method) }.not_to raise_error
end
end
context 'when the method is not successful' do
it 'raises an exception' do
allow(driver).to receive(:test_method).and_raise(RuntimeError)
expect { driver.validate_method!(:test_method) }.to raise_error(RuntimeError)
end
end
end
describe '#validate_computer_name' do
it 'allows an alphanumeric 15-char string' do
allow(driver).to receive(:node_name).and_return('a12345678901234')
expect(driver).not_to receive(:print_error_and_exit)
driver.validate_computer_name!
end
it 'does not allow a computer name that only has numbers' do
allow(driver).to receive(:node_name).and_return('12345')
expect(driver).to receive(:print_error_and_exit)
driver.validate_computer_name!
end
it 'does not allow a 16 character name' do
allow(driver).to receive(:node_name).and_return('a123456789012345')
expect(driver).to receive(:print_error_and_exit)
driver.validate_computer_name!
end
it 'does not allow a hyphen at the end' do
allow(driver).to receive(:node_name).and_return('a12345-')
expect(driver).to receive(:print_error_and_exit)
driver.validate_computer_name!
end
end
describe '#validate_customization_script!' do
context 'when no customization script has been configured' do
before do
config[:customization_script] = nil
end
it 'does not print an error' do
expect(driver).not_to receive(:print_error_and_exit)
driver.validate_customization_script!
end
end
context 'when a script is configured and is readable' do
before do
config[:customization_script] = '/path/to/script'
end
it 'does not print an error' do
expect(File).to receive(:readable?).with('/path/to/script').and_return(true)
expect(driver).not_to receive(:print_error_and_exit)
driver.validate_customization_script!
end
end
context 'when a script is configured but is not readable' do
before do
config[:customization_script] = '/path/to/script'
end
it 'does not print an error' do
expect(File).to receive(:readable?).with('/path/to/script').and_return(false)
expect(driver).to receive(:print_error_and_exit)
driver.validate_customization_script!
end
end
end
describe '#instantiate' do
let(:image) { double('image') }
it 'calls instantiate on the image' do
allow(driver).to receive(:image).and_return(image)
allow(driver).to receive(:vmapp_name).and_return('node')
allow(driver).to receive(:instantiate_config).and_return('config')
expect(image).to receive(:instantiate).with('node', 'config')
driver.instantiate
end
end
describe '#vapp' do
let(:vdc) { double('vdc') }
let(:vapps) { double('vapps') }
let(:vapp) { double('vapp') }
it 'gets the vApp by ID' do
allow(driver).to receive(:vapp_id).and_return('vapp1')
allow(driver).to receive(:vdc).and_return(vdc)
allow(vdc).to receive(:vapps).and_return(vapps)
expect(vapps).to receive(:get).with('vapp1').and_return(vapp)
expect(driver.vapp).to eq(vapp)
end
end
describe '#vm' do
let(:vapp) { double('vapp') }
let(:vms) { %w(vm1 vm2) }
it 'returns the first VM from the array' do
allow(driver).to receive(:vapp).and_return(vapp)
allow(vapp).to receive(:vms).and_return(vms)
expect(driver.vm).to eq('vm1')
end
end
describe '#validate_vapp' do
let(:vapp) { double('vapp') }
before do
allow(driver).to receive(:vapp).and_return(vapp)
end
it 'returns true when only 1 VM is present' do
allow(vapp).to receive(:vms).and_return([1])
expect(driver.validate_vapp).to eq(true)
end
it 'returns false when 0 VMs are present' do
allow(vapp).to receive(:vms).and_return([])
expect(driver.validate_vapp).to eq(false)
end
it 'returns false when >1 VMs are present' do
allow(vapp).to receive(:vms).and_return([1, 2])
expect(driver.validate_vapp).to eq(false)
end
end
describe '#customization' do
let(:vm) { double('vm') }
let(:customization) { double('customization') }
it 'fetches the customization from the VM' do
allow(driver).to receive(:vm).and_return(vm)
expect(vm).to receive(:customization).and_return(customization)
expect(driver.customization).to eq(customization)
end
end
describe '#update_customization' do
before do
allow(driver).to receive(:set_customization_script)
allow(driver).to receive(:set_customization_password)
allow(driver).to receive(:set_customization_computer_name)
allow(driver).to receive(:save_customization)
end
context 'when a customization script is provided' do
before do
config[:customization_script] = '/path/to/script'
end
it 'calls set_customization_script' do
expect(driver).to receive(:set_customization_script)
driver.update_customization
end
end
context 'when a customization script is not provided' do
before do
config[:customization_script] = nil
end
it 'does not call set_customization_script' do
expect(driver).not_to receive(:set_customization_script)
driver.update_customization
end
end
it 'calls the expected methods' do
expect(driver).to receive(:set_customization_password)
expect(driver).to receive(:set_customization_computer_name)
expect(driver).to receive(:save_customization)
driver.update_customization
end
end
describe '#set_customization_script' do
let(:customization) { double('customization') }
before do
config[:customization_script] = '/path/to/script'
allow(driver).to receive(:customization).and_return(customization)
allow(File).to receive(:read).with('/path/to/script').and_return('script body')
end
it 'sets the customization script to the file contents' do
expect(customization).to receive(:script=).with('script body')
driver.set_customization_script
end
end
describe '#set_customization_password' do
let(:customization) { double('customization') }
before do
allow(driver).to receive(:customization).and_return(customization)
end
context 'when a VM password is provided' do
before do
config[:vm_password] = 'password123'
end
it 'sets the password and disables auto-generation and reset' do
expect(customization).to receive(:admin_password=).with('password123')
expect(customization).to receive(:admin_password_auto=).with(false)
expect(customization).to receive(:reset_password_required=).with(false)
driver.set_customization_password
end
end
context 'when a VM password is not provided' do
before do
config[:vm_password] = nil
end
it 'sets nulls-out the password, enables auto-generation, disables reset reset' do
expect(customization).to receive(:admin_password=).with(nil)
expect(customization).to receive(:admin_password_auto=).with(true)
expect(customization).to receive(:reset_password_required=).with(false)
driver.set_customization_password
end
end
end
describe '#set_customization_computer_name' do
let(:customization) { double('customization') }
it 'sets the computer name' do
allow(driver).to receive(:customization).and_return(customization)
allow(driver).to receive(:node_name).and_return('test node')
expect(customization).to receive(:computer_name=).with('test node')
driver.set_customization_computer_name
end
end
describe '#save_customization' do
let(:customization) { double('customization') }
it 'enables and saves the customization' do
allow(driver).to receive(:customization).and_return(customization)
expect(customization).to receive(:enabled=).with(true)
expect(customization).to receive(:save)
driver.save_customization
end
end
end