beaker-vmware 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,162 +2,158 @@ require 'spec_helper'
2
2
 
3
3
  module Beaker
4
4
  describe VsphereHelper do
5
- let( :logger ) { double('logger').as_null_object }
6
- let( :vInfo ) { { :server => "vsphere.labs.net", :user => "vsphere@labs.com", :pass => "supersekritpassword" } }
7
- let( :vsphere_helper ) { VsphereHelper.new ( vInfo.merge( { :logger => logger } ) ) }
8
- let( :snaplist ) { { 'snap1' => { 'snap1sub1' => nil ,
9
- 'snap1sub2' => nil },
10
- 'snap2' => nil,
11
- 'snap3' => { 'snap3sub1' => nil ,
12
- 'snap3sub2' => nil ,
13
- 'snap3sub3' => nil } } }
14
- let( :vms ) { [ MockRbVmomiVM.new( 'mockvm1', snaplist ),
15
- MockRbVmomiVM.new( 'mockvm2', snaplist ),
16
- MockRbVmomiVM.new( 'mockvm3', snaplist ) ] }
17
-
18
- before :each do
19
- stub_const( "RbVmomi", MockRbVmomi )
5
+ let(:logger) { double('logger').as_null_object }
6
+ let(:vInfo) do
7
+ { server: 'vsphere.labs.net', user: 'vsphere@labs.com', pass: 'supersekritpassword' }
8
+ end
9
+ let(:vsphere_helper) { VsphereHelper.new(vInfo.merge({ logger: logger })) }
10
+ let(:snaplist) do
11
+ { 'snap1' => { 'snap1sub1' => nil,
12
+ 'snap1sub2' => nil, },
13
+ 'snap2' => nil,
14
+ 'snap3' => { 'snap3sub1' => nil,
15
+ 'snap3sub2' => nil,
16
+ 'snap3sub3' => nil, }, }
17
+ end
18
+ let(:vms) do
19
+ [MockRbVmomiVM.new('mockvm1', snaplist),
20
+ MockRbVmomiVM.new('mockvm2', snaplist),
21
+ MockRbVmomiVM.new('mockvm3', snaplist),]
20
22
  end
21
23
 
22
- describe "#load_config" do
24
+ before do
25
+ stub_const('RbVmomi', MockRbVmomi)
26
+ end
23
27
 
28
+ describe '#load_config' do
24
29
  it 'can load a .fog file' do
25
- allow( File ).to receive( :exist? ).and_return( true )
26
- allow( YAML ).to receive( :load_file ).and_return( fog_file_contents )
27
-
28
- expect( VsphereHelper.load_config ).to be === vInfo
30
+ allow(File).to receive(:exist?).and_return(true)
31
+ allow(YAML).to receive(:load_file).and_return(fog_file_contents)
29
32
 
33
+ expect(VsphereHelper.load_config).to be === vInfo
30
34
  end
31
35
 
32
36
  it 'raises an error when the .fog file is missing' do
33
- allow( File ).to receive( :exist? ).and_return( false )
34
-
35
- expect{ VsphereHelper.load_config }.to raise_error( ArgumentError )
37
+ allow(File).to receive(:exist?).and_return(false)
36
38
 
39
+ expect { VsphereHelper.load_config }.to raise_error(ArgumentError)
37
40
  end
38
-
39
41
  end
40
42
 
41
- describe "#find_snapshot" do
42
- it 'can find a given snapshot name' do
43
- mockvm = MockRbVmomiVM.new( 'mockvm', snaplist )
44
-
45
- expect( vsphere_helper.find_snapshot( mockvm, 'snap2' ) ).to be === mockvm.get_snapshot( 'snap2' )
46
-
47
- end
48
-
49
- end
43
+ describe '#find_snapshot' do
44
+ it 'can find a given snapshot name' do
45
+ mockvm = MockRbVmomiVM.new('mockvm', snaplist)
50
46
 
51
- describe "#find_customization" do
52
- it 'returns the customization spec' do
53
-
54
- expect( vsphere_helper.find_customization( 'name' ) ).to be === true
55
-
56
- end
57
-
58
- end
59
-
60
- describe "#find_vms" do
61
- it 'finds the list of vms' do
62
- connection = vsphere_helper.instance_variable_get( :@connection )
63
- connection.set_info( vms )
64
-
65
- expect( vsphere_helper.find_vms( 'mockvm1' ) ).to be === {vms[0].name => vms[0]}
66
- end
47
+ expect(vsphere_helper.find_snapshot(mockvm, 'snap2')).to be === mockvm.get_snapshot('snap2')
48
+ end
49
+ end
67
50
 
68
- it 'returns {} when no vm is found' do
69
- connection = vsphere_helper.instance_variable_get( :@connection )
70
- connection.set_info( vms )
51
+ describe '#find_customization' do
52
+ it 'returns the customization spec' do
53
+ expect(vsphere_helper.find_customization('name')).to be === true
54
+ end
55
+ end
71
56
 
72
- expect( vsphere_helper.find_vms( 'novm' ) ).to be === {}
73
- end
57
+ describe '#find_vms' do
58
+ it 'finds the list of vms' do
59
+ connection = vsphere_helper.instance_variable_get(:@connection)
60
+ connection.set_info(vms)
74
61
 
75
- end
62
+ expect(vsphere_helper.find_vms('mockvm1')).to be === { vms[0].name => vms[0] }
63
+ end
76
64
 
77
- describe "#find_datastore" do
78
- it 'finds the datastore from the connection object' do
79
- connection = vsphere_helper.instance_variable_get( :@connection )
80
- dc = connection.serviceInstance.find_datacenter('testdc')
81
- expect(vsphere_helper.find_datastore( dc,'datastorename' ) ).to be === true
82
- end
65
+ it 'returns {} when no vm is found' do
66
+ connection = vsphere_helper.instance_variable_get(:@connection)
67
+ connection.set_info(vms)
83
68
 
84
- end
69
+ expect(vsphere_helper.find_vms('novm')).to be === {}
70
+ end
71
+ end
85
72
 
86
- describe "#find_folder" do
87
- it 'can find a folder in the datacenter' do
88
- connection = vsphere_helper.instance_variable_get( :@connection )
89
- expect(vsphere_helper.find_folder( 'testdc','root' ) ).to be === connection.serviceInstance.find_datacenter('testdc').vmFolder
90
- end
73
+ describe '#find_datastore' do
74
+ it 'finds the datastore from the connection object' do
75
+ connection = vsphere_helper.instance_variable_get(:@connection)
76
+ dc = connection.serviceInstance.find_datacenter('testdc')
77
+ expect(vsphere_helper.find_datastore(dc, 'datastorename')).to be === true
78
+ end
79
+ end
91
80
 
92
- end
81
+ describe '#find_folder' do
82
+ it 'can find a folder in the datacenter' do
83
+ connection = vsphere_helper.instance_variable_get(:@connection)
84
+ expect(vsphere_helper.find_folder('testdc',
85
+ 'root')).to be === connection.serviceInstance.find_datacenter('testdc').vmFolder
86
+ end
87
+ end
93
88
 
94
- describe "#find_pool" do
95
- it 'can find a pool in a folder in the datacenter' do
96
- connection = vsphere_helper.instance_variable_get( :@connection )
97
- dc = connection.serviceInstance.find_datacenter('testdc')
98
- dc.hostFolder = MockRbVmomi::VIM::Folder.new
99
- dc.hostFolder.name = "/root"
89
+ describe '#find_pool' do
90
+ it 'can find a pool in a folder in the datacenter' do
91
+ connection = vsphere_helper.instance_variable_get(:@connection)
92
+ dc = connection.serviceInstance.find_datacenter('testdc')
93
+ dc.hostFolder = MockRbVmomi::VIM::Folder.new
94
+ dc.hostFolder.name = '/root'
100
95
 
101
- expect(vsphere_helper.find_pool( 'testdc','root' ) ).to be === connection.serviceInstance.find_datacenter('testdc').hostFolder
96
+ expect(vsphere_helper.find_pool('testdc',
97
+ 'root')).to be === connection.serviceInstance.find_datacenter('testdc').hostFolder
98
+ end
102
99
 
103
- end
104
- it 'can find a pool in a clustercomputeresource in the datacenter' do
105
- connection = vsphere_helper.instance_variable_get( :@connection )
106
- dc = connection.serviceInstance.find_datacenter('testdc')
107
- dc.hostFolder = MockRbVmomi::VIM::ClusterComputeResource.new
108
- dc.hostFolder.name = "/root"
100
+ it 'can find a pool in a clustercomputeresource in the datacenter' do
101
+ connection = vsphere_helper.instance_variable_get(:@connection)
102
+ dc = connection.serviceInstance.find_datacenter('testdc')
103
+ dc.hostFolder = MockRbVmomi::VIM::ClusterComputeResource.new
104
+ dc.hostFolder.name = '/root'
109
105
 
110
- expect(vsphere_helper.find_pool( 'testdc','root' ) ).to be === connection.serviceInstance.find_datacenter('testdc').hostFolder
111
- end
112
- it 'can find a pool in a resourcepool in the datacenter' do
113
- connection = vsphere_helper.instance_variable_get( :@connection )
114
- dc = connection.serviceInstance.find_datacenter('testdc')
115
- dc.hostFolder = MockRbVmomi::VIM::ResourcePool.new
116
- dc.hostFolder.name = "/root"
106
+ expect(vsphere_helper.find_pool('testdc',
107
+ 'root')).to be === connection.serviceInstance.find_datacenter('testdc').hostFolder
108
+ end
117
109
 
118
- expect(vsphere_helper.find_pool( 'testdc','root' ) ).to be === connection.serviceInstance.find_datacenter('testdc').hostFolder
119
- end
110
+ it 'can find a pool in a resourcepool in the datacenter' do
111
+ connection = vsphere_helper.instance_variable_get(:@connection)
112
+ dc = connection.serviceInstance.find_datacenter('testdc')
113
+ dc.hostFolder = MockRbVmomi::VIM::ResourcePool.new
114
+ dc.hostFolder.name = '/root'
120
115
 
121
- end
116
+ expect(vsphere_helper.find_pool('testdc',
117
+ 'root')).to be === connection.serviceInstance.find_datacenter('testdc').hostFolder
118
+ end
119
+ end
122
120
 
123
- describe "#wait_for_tasks" do
124
- it "can wait for tasks to error" do
125
- allow( vsphere_helper ).to receive( :sleep ).and_return( true )
126
- vms.each do |vm|
127
- vm.info.state = 'error'
128
- end
121
+ describe '#wait_for_tasks' do
122
+ it 'can wait for tasks to error' do
123
+ allow(vsphere_helper).to receive(:sleep).and_return(true)
124
+ vms.each do |vm|
125
+ vm.info.state = 'error'
126
+ end
129
127
 
130
- expect(vsphere_helper.wait_for_tasks( vms, 0, 5 ) ).to be === vms
131
- end
128
+ expect(vsphere_helper.wait_for_tasks(vms, 0, 5)).to be === vms
129
+ end
132
130
 
133
- it "can wait for tasks to succeed" do
134
- allow( vsphere_helper ).to receive( :sleep ).and_return( true )
135
- vms.each do |vm|
136
- vm.info.state = 'success'
137
- end
131
+ it 'can wait for tasks to succeed' do
132
+ allow(vsphere_helper).to receive(:sleep).and_return(true)
133
+ vms.each do |vm|
134
+ vm.info.state = 'success'
135
+ end
138
136
 
139
- expect(vsphere_helper.wait_for_tasks( vms, 0, 5 ) ).to be === vms
140
- end
137
+ expect(vsphere_helper.wait_for_tasks(vms, 0, 5)).to be === vms
138
+ end
141
139
 
142
- it "errors when tasks fail to error/success before timing out" do
143
- allow( vsphere_helper ).to receive( :sleep ).and_return( true )
144
- vms.each do |vm|
145
- vm.info.state = 'nope'
146
- end
140
+ it 'errors when tasks fail to error/success before timing out' do
141
+ allow(vsphere_helper).to receive(:sleep).and_return(true)
142
+ vms.each do |vm|
143
+ vm.info.state = 'nope'
144
+ end
147
145
 
148
- expect{ vsphere_helper.wait_for_tasks( vms, 0, 5 ) }.to raise_error
149
- end
146
+ expect { vsphere_helper.wait_for_tasks(vms, 0, 5) }.to raise_error
147
+ end
148
+ end
150
149
 
151
- end
150
+ describe '#close' do
151
+ it 'closes the connection' do
152
+ connection = vsphere_helper.instance_variable_get(:@connection)
153
+ expect(connection).to receive(:close).once
152
154
 
153
- describe "#close" do
154
- it 'closes the connection' do
155
- connection = vsphere_helper.instance_variable_get( :@connection )
156
- expect( connection ).to receive( :close ).once
157
-
158
- vsphere_helper.close
159
- end
160
- end
161
-
162
- end
155
+ vsphere_helper.close
156
+ end
157
+ end
158
+ end
163
159
  end
@@ -2,89 +2,77 @@ require 'spec_helper'
2
2
 
3
3
  module Beaker
4
4
  describe Vsphere do
5
-
6
- before :each do
7
- MockVsphereHelper.set_config( fog_file_contents )
8
- MockVsphereHelper.set_vms( make_hosts() )
9
- stub_const( "VsphereHelper", MockVsphereHelper )
5
+ before do
6
+ MockVsphereHelper.set_config(fog_file_contents)
7
+ MockVsphereHelper.set_vms(make_hosts)
8
+ stub_const('VsphereHelper', MockVsphereHelper)
10
9
  end
11
10
 
12
- describe "#provision" do
13
-
14
- it 'provisions hosts' do
11
+ describe '#provision' do
12
+ it 'provisions hosts' do
15
13
  MockVsphereHelper.powerOff
16
- vsphere = Beaker::Vsphere.new( make_hosts(), make_opts )
14
+ vsphere = Beaker::Vsphere.new(make_hosts, make_opts)
17
15
 
18
16
  vsphere.provision
19
17
 
20
- hosts = vsphere.instance_variable_get( :@hosts )
18
+ hosts = vsphere.instance_variable_get(:@hosts)
21
19
  hosts.each do |host|
22
- expect( MockVsphereHelper.find_vm( host.name ).powerState ) == "poweredOn"
20
+ expect(MockVsphereHelper.find_vm(host.name).powerState) == 'poweredOn'
23
21
  end
24
-
25
22
  end
26
23
 
27
24
  it 'raises an error if a vm is missing in Vsphere' do
28
25
  MockVsphereHelper.powerOff
29
- hosts = make_hosts()
26
+ hosts = make_hosts
30
27
  hosts[0][:vmname] = 'unknown'
31
- vsphere = Beaker::Vsphere.new( hosts, make_opts )
32
-
33
- expect{ vsphere.provision }.to raise_error
28
+ vsphere = Beaker::Vsphere.new(hosts, make_opts)
34
29
 
30
+ expect { vsphere.provision }.to raise_error
35
31
  end
36
32
 
37
33
  it 'raises an error if a vm does not have a given snapshot name' do
38
34
  MockVsphereHelper.powerOff
39
- hosts = make_hosts()
40
- hosts[0]["snapshot"] = 'unknown'
41
- vsphere = Beaker::Vsphere.new( hosts, make_opts )
42
-
43
- expect{ vsphere.provision }.to raise_error
35
+ hosts = make_hosts
36
+ hosts[0]['snapshot'] = 'unknown'
37
+ vsphere = Beaker::Vsphere.new(hosts, make_opts)
44
38
 
39
+ expect { vsphere.provision }.to raise_error
45
40
  end
46
41
 
47
42
  it 'provisions hosts if no snapshot is provided' do
48
43
  MockVsphereHelper.powerOff
49
- hosts = make_hosts()
50
- hosts[0]["snapshot"] = nil
51
- vsphere = Beaker::Vsphere.new( hosts, make_opts )
44
+ hosts = make_hosts
45
+ hosts[0]['snapshot'] = nil
46
+ vsphere = Beaker::Vsphere.new(hosts, make_opts)
52
47
 
53
48
  vsphere.provision
54
49
 
55
50
  hosts.each do |host|
56
- expect( MockVsphereHelper.find_vm( host.name ).powerState ) == "poweredOn"
51
+ expect(MockVsphereHelper.find_vm(host.name).powerState) == 'poweredOn'
57
52
  end
58
-
59
53
  end
60
-
61
54
  end
62
55
 
63
- describe "#cleanup" do
64
-
65
- it "cleans up" do
56
+ describe '#cleanup' do
57
+ it 'cleans up' do
66
58
  MockVsphereHelper.powerOn
67
- vsphere = Beaker::Vsphere.new( make_hosts(), make_opts )
59
+ vsphere = Beaker::Vsphere.new(make_hosts, make_opts)
68
60
  vsphere.cleanup
69
61
 
70
- hosts = vsphere.instance_variable_get( :@hosts )
62
+ hosts = vsphere.instance_variable_get(:@hosts)
71
63
  hosts.each do |host|
72
- expect( MockVsphereHelper.find_vm( host.name ).powerState ) == "poweredOff"
64
+ expect(MockVsphereHelper.find_vm(host.name).powerState) == 'poweredOff'
73
65
  end
74
66
  end
75
67
 
76
68
  it 'raises an error if a vm is missing in Vsphere' do
77
69
  MockVsphereHelper.powerOn
78
- hosts = make_hosts()
70
+ hosts = make_hosts
79
71
  hosts[0][:vmname] = 'unknown'
80
- vsphere = Beaker::Vsphere.new( hosts, make_opts )
81
-
82
- expect{ vsphere.cleanup }.to raise_error
72
+ vsphere = Beaker::Vsphere.new(hosts, make_opts)
83
73
 
74
+ expect { vsphere.cleanup }.to raise_error
84
75
  end
85
-
86
76
  end
87
-
88
77
  end
89
-
90
78
  end
@@ -0,0 +1,63 @@
1
+ class Response
2
+ attr_accessor :code, :message, :data
3
+
4
+ def initialize(code = 0, message = '', data = nil)
5
+ @code = code
6
+ @message = message
7
+ @data = data
8
+ end
9
+ end
10
+
11
+ class MockFissionVM
12
+ attr_accessor :name
13
+
14
+ @@snaps = []
15
+ def initialize(name)
16
+ @name = name
17
+ @running = true
18
+ end
19
+
20
+ def self.set_snapshots(snaps)
21
+ @@snaps = snaps
22
+ end
23
+
24
+ def snapshots
25
+ Response.new(0, '', @@snaps)
26
+ end
27
+
28
+ def revert_to_snapshot(_name)
29
+ @running = false
30
+ end
31
+
32
+ def running?
33
+ Response.new(0, '', @running)
34
+ end
35
+
36
+ def start(_opt)
37
+ @running = true
38
+ end
39
+
40
+ def exists?
41
+ true
42
+ end
43
+ end
44
+
45
+ class MockFission
46
+ @@vms = []
47
+ def self.presets(hosts)
48
+ snaps = []
49
+ hosts.each do |host|
50
+ @@vms << MockFissionVM.new(host.name)
51
+ snaps << host[:snapshot]
52
+ end
53
+ MockFissionVM.set_snapshots(snaps)
54
+ end
55
+
56
+ def self.all
57
+ Response.new(0, '', @@vms)
58
+ end
59
+
60
+ def self.new(name)
61
+ MockFissionVM.new(name)
62
+ end
63
+ end