beaker-vcloud 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,43 +2,41 @@ require 'spec_helper'
2
2
 
3
3
  module Beaker
4
4
  describe Vcloud do
5
-
6
- before :each do
7
- MockVsphereHelper.set_config( fog_file_contents )
8
- MockVsphereHelper.set_vms( make_hosts() )
9
- stub_const( "VsphereHelper", MockVsphereHelper )
10
- stub_const( "Net", MockNet )
11
- json = double( 'json' )
12
- allow( json ).to receive( :parse ) do |arg|
5
+ before do
6
+ MockVsphereHelper.set_config(fog_file_contents)
7
+ MockVsphereHelper.set_vms(make_hosts)
8
+ stub_const('VsphereHelper', MockVsphereHelper)
9
+ stub_const('Net', MockNet)
10
+ json = double('json')
11
+ allow(json).to receive(:parse) do |arg|
13
12
  arg
14
13
  end
15
- stub_const( "JSON", json )
16
- allow( Socket ).to receive( :getaddrinfo ).and_return( true )
17
- allow_any_instance_of( Beaker::Shared ).to receive( :get_fog_credentials ).and_return( fog_file_contents )
18
- allow_any_instance_of( VsphereHelper ).to receive( :new ).and_return (MockVsphereHelper)
14
+ stub_const('JSON', json)
15
+ allow(Socket).to receive(:getaddrinfo).and_return(true)
16
+ allow_any_instance_of(Beaker::Shared).to receive(:get_fog_credentials).and_return(fog_file_contents)
17
+ allow_any_instance_of(VsphereHelper).to receive(:new).and_return(MockVsphereHelper)
19
18
  end
20
19
 
21
- describe "#provision" do
22
-
20
+ describe '#provision' do
23
21
  it 'warns about deprecated behavior if pooling_api is provided' do
24
22
  opts = make_opts
25
23
  opts[:pooling_api] = 'testpool'
26
- expect( opts[:logger] ).to receive( :warn ).once
27
- expect{ Beaker::Vcloud.new( make_hosts, opts ) }.to raise_error( /datacenter/ )
24
+ expect(opts[:logger]).to receive(:warn).once
25
+ expect { Beaker::Vcloud.new(make_hosts, opts) }.to raise_error(/datacenter/)
28
26
  end
29
27
 
30
28
  it 'does not instantiate vmpooler if pooling_api is provided' do
31
29
  opts = make_opts
32
30
  opts[:pooling_api] = 'testpool'
33
- expect{ Beaker::Vcloud.new( make_hosts, opts ) }.to raise_error( /datacenter/ )
31
+ expect { Beaker::Vcloud.new(make_hosts, opts) }.to raise_error(/datacenter/)
34
32
  end
35
33
 
36
34
  it 'ignores pooling_api and instantiates self' do
37
35
  opts = make_opts
38
36
  opts[:pooling_api] = 'testpool'
39
37
  opts[:datacenter] = 'testdatacenter'
40
- hypervisor = Beaker::Vcloud.new( make_hosts, opts)
41
- expect( hypervisor.class ).to be Beaker::Vcloud
38
+ hypervisor = Beaker::Vcloud.new(make_hosts, opts)
39
+ expect(hypervisor.class).to be Beaker::Vcloud
42
40
  end
43
41
 
44
42
  it 'provisions hosts and add them to the pool' do
@@ -48,18 +46,17 @@ module Beaker
48
46
  opts[:pooling_api] = nil
49
47
  opts[:datacenter] = 'testdc'
50
48
 
51
- vcloud = Beaker::Vcloud.new( make_hosts, opts )
52
- allow( vcloud ).to receive( :require ).and_return( true )
53
- allow( vcloud ).to receive( :sleep ).and_return( true )
49
+ vcloud = Beaker::Vcloud.new(make_hosts, opts)
50
+ allow(vcloud).to receive(:require).and_return(true)
51
+ allow(vcloud).to receive(:sleep).and_return(true)
54
52
  vcloud.provision
55
53
 
56
- hosts = vcloud.instance_variable_get( :@hosts )
57
- hosts.each do | host |
54
+ hosts = vcloud.instance_variable_get(:@hosts)
55
+ hosts.each do |host|
58
56
  name = host['vmhostname']
59
- vm = MockVsphereHelper.find_vm( name )
60
- expect( vm.toolsRunningStatus ).to be === "guestToolsRunning"
57
+ vm = MockVsphereHelper.find_vm(name)
58
+ expect(vm.toolsRunningStatus).to be === 'guestToolsRunning'
61
59
  end
62
-
63
60
  end
64
61
 
65
62
  it 'does not run enable_root on cygwin hosts' do
@@ -71,44 +68,37 @@ module Beaker
71
68
 
72
69
  hosts = make_hosts
73
70
  hosts.each do |host|
74
- allow( host ).to receive( :is_cygwin? ).and_return( true )
71
+ allow(host).to receive(:is_cygwin?).and_return(true)
75
72
  end
76
- vcloud = Beaker::Vcloud.new( hosts, opts )
77
- allow( vcloud ).to receive( :require ).and_return( true )
78
- allow( vcloud ).to receive( :sleep ).and_return( true )
79
- expect( vcloud ).to receive( :enable_root ).never
73
+ vcloud = Beaker::Vcloud.new(hosts, opts)
74
+ allow(vcloud).to receive(:require).and_return(true)
75
+ allow(vcloud).to receive(:sleep).and_return(true)
76
+ expect(vcloud).not_to receive(:enable_root)
80
77
  vcloud.provision
81
-
82
78
  end
83
-
84
79
  end
85
80
 
86
- describe "#cleanup" do
87
-
88
- it "cleans up hosts not in the pool" do
81
+ describe '#cleanup' do
82
+ it 'cleans up hosts not in the pool' do
89
83
  MockVsphereHelper.powerOn
90
84
 
91
85
  opts = make_opts
92
86
  opts[:pooling_api] = nil
93
87
  opts[:datacenter] = 'testdc'
94
88
 
95
- vcloud = Beaker::Vcloud.new( make_hosts, opts )
96
- allow( vcloud ).to receive( :require ).and_return( true )
97
- allow( vcloud ).to receive( :sleep ).and_return( true )
89
+ vcloud = Beaker::Vcloud.new(make_hosts, opts)
90
+ allow(vcloud).to receive(:require).and_return(true)
91
+ allow(vcloud).to receive(:sleep).and_return(true)
98
92
  vcloud.provision
99
93
  vcloud.cleanup
100
94
 
101
- hosts = vcloud.instance_variable_get( :@hosts )
102
- vm_names = hosts.map {|h| h['vmhostname'] }.compact
103
- vm_names.each do | name |
104
- vm = MockVsphereHelper.find_vm( name )
105
- expect( vm.runtime.powerState ).to be === "poweredOff"
95
+ hosts = vcloud.instance_variable_get(:@hosts)
96
+ vm_names = hosts.map { |h| h['vmhostname'] }.compact
97
+ vm_names.each do |name|
98
+ vm = MockVsphereHelper.find_vm(name)
99
+ expect(vm.runtime.powerState).to be === 'poweredOff'
106
100
  end
107
-
108
101
  end
109
-
110
102
  end
111
-
112
103
  end
113
-
114
104
  end
@@ -0,0 +1,284 @@
1
+ class MockRbVmomiSnapshot
2
+ attr_accessor :name, :rootSnapshotList, :childSnapshotList
3
+
4
+ def initialize
5
+ @name = nil
6
+ @rootSnapshotList = []
7
+ @childSnapshotList = []
8
+ end
9
+
10
+ def print_nested_array(arg)
11
+ str = '[ '
12
+ arg.each do |arry|
13
+ if arry.is_a?(Array)
14
+ str += print_nested_array(arry)
15
+ elsif arry.is_a?(MockRbVmomiSnapshot)
16
+ str += arry.to_s + ', '
17
+ end
18
+ end
19
+ str + ' ]'
20
+ end
21
+
22
+ def to_s
23
+ "Snapshot(name: #{@name}, rootSnapshotlist: #{print_nested_array(@rootSnapshotList)}, childSnapshotList: #{print_nested_array(@childSnapshotList)})"
24
+ end
25
+
26
+ def snapshot
27
+ self
28
+ end
29
+ end
30
+
31
+ class MockRbVmomiVM
32
+ attr_accessor :snapshot, :name, :state
33
+
34
+ def info
35
+ self
36
+ end
37
+
38
+ def process_snaphash(snaphash)
39
+ shotlist = []
40
+ snaphash.each do |name, subsnaps|
41
+ new_snap = MockRbVmomiSnapshot.new
42
+ new_snap.name = name
43
+ new_snap.childSnapshotList = process_snaphash(subsnaps) if subsnaps.is_a?(Hash)
44
+ shotlist << new_snap
45
+ end
46
+ shotlist
47
+ end
48
+
49
+ def get_snapshot(name, snaplist = @snapshot.rootSnapshotList)
50
+ snapshot = nil
51
+ snaplist.each do |snap|
52
+ if snap.is_a?(Array)
53
+ snapshot = get_snapshot(snap, name)
54
+ elsif snap.name == name
55
+ snapshot = snap.snapshot
56
+ end
57
+ end
58
+ snapshot
59
+ end
60
+
61
+ def initialize(name, snaphash)
62
+ @name = name
63
+ @snapshot = MockRbVmomiSnapshot.new
64
+ @snapshot.name = name
65
+ @snapshot.rootSnapshotList = process_snaphash(snaphash)
66
+ end
67
+ end
68
+
69
+ class MockRbVmomiConnection
70
+ class CustomizationSpecManager
71
+ class CustomizationSpec
72
+ def spec
73
+ true
74
+ end
75
+ end
76
+
77
+ def initialize
78
+ @customizationspec = CustomizationSpec.new
79
+ end
80
+
81
+ def GetCustomizationSpec(_arg)
82
+ @customizationspec
83
+ end
84
+ end
85
+
86
+ class PropertyCollector
87
+ class Result
88
+ def initialize(name, object)
89
+ @name = name
90
+ @object = object
91
+ end
92
+
93
+ def val
94
+ @name
95
+ end
96
+
97
+ def obj
98
+ @object
99
+ end
100
+
101
+ def propSet
102
+ [self]
103
+ end
104
+ end
105
+
106
+ class ResultContainer
107
+ attr_accessor :token
108
+
109
+ def initialize
110
+ @results = []
111
+ end
112
+
113
+ def objects
114
+ @results
115
+ end
116
+
117
+ def add_object(obj)
118
+ @results << obj
119
+ end
120
+ end
121
+
122
+ def initialize
123
+ @results = ResultContainer.new
124
+ end
125
+
126
+ def RetrievePropertiesEx(_hash)
127
+ @results.token = true
128
+ @results
129
+ end
130
+
131
+ def ContinueRetrievePropertiesEx(_token)
132
+ @results.token = false
133
+ @results
134
+ end
135
+
136
+ def add_result(name, object)
137
+ @results.add_object(Result.new(name, object))
138
+ end
139
+
140
+ def WaitForUpdates(_arg)
141
+ result = OpenStruct.new
142
+ result.version = 'version'
143
+ result
144
+ end
145
+
146
+ def CreateFilter(_arg)
147
+ filter = OpenStruct.new
148
+ filter.DestroyPropertyFilter = true
149
+ filter
150
+ end
151
+ end
152
+
153
+ class ServiceInstance
154
+ class Datacenter
155
+ attr_accessor :vmFolder, :hostFolder
156
+
157
+ def initialize
158
+ @vmFolder = MockRbVmomi::VIM::Folder.new
159
+ @vmFolder.name = '/root'
160
+ end
161
+
162
+ def find_datastore(_arg)
163
+ true
164
+ end
165
+ end
166
+
167
+ def initialize
168
+ @datacenter = Datacenter.new
169
+ end
170
+
171
+ def find_datacenter(_dc)
172
+ @datacenter
173
+ end
174
+ end
175
+
176
+ class ServiceManager
177
+ class ViewManager
178
+ def CreateContainerView(hash)
179
+ @view = hash
180
+ end
181
+ end
182
+
183
+ def initialize
184
+ @customizationspecmanager = CustomizationSpecManager.new
185
+ @viewmanager = ViewManager.new
186
+ end
187
+
188
+ def customizationSpecManager
189
+ @customizationspecmanager
190
+ end
191
+
192
+ def viewManager
193
+ @viewmanager
194
+ end
195
+
196
+ def rootFolder
197
+ '/root'
198
+ end
199
+ end
200
+
201
+ def initialize(opts)
202
+ @host = opts[:host]
203
+ @user = opts[:user]
204
+ @password = opts[:password]
205
+ @insecure = opts[:insecure]
206
+ @serviceinstance = ServiceInstance.new
207
+ @servicemanager = ServiceManager.new
208
+ @propertycollector = PropertyCollector.new
209
+ end
210
+
211
+ def serviceInstance
212
+ @serviceinstance
213
+ end
214
+
215
+ def serviceContent
216
+ @servicemanager
217
+ end
218
+
219
+ def propertyCollector
220
+ @propertycollector
221
+ end
222
+
223
+ def set_info(vms)
224
+ vms.each do |vm|
225
+ @propertycollector.add_result(vm.name, vm)
226
+ end
227
+ end
228
+ end
229
+
230
+ class MockRbVmomi
231
+ class VIM
232
+ class Folder
233
+ attr_accessor :name
234
+
235
+ def find
236
+ self
237
+ end
238
+
239
+ def childEntity
240
+ self
241
+ end
242
+
243
+ def resourcePool
244
+ self
245
+ end
246
+
247
+ def traverse(_path, _type = Object, _create = false)
248
+ self
249
+ end
250
+ end
251
+
252
+ class ResourcePool
253
+ attr_accessor :name
254
+
255
+ def find
256
+ self
257
+ end
258
+
259
+ def resourcePool
260
+ self
261
+ end
262
+ end
263
+
264
+ class ClusterComputeResource
265
+ attr_accessor :name
266
+
267
+ def find
268
+ self
269
+ end
270
+
271
+ def resourcePool
272
+ self
273
+ end
274
+ end
275
+
276
+ class TraversalSpec
277
+ def initialize(hash); end
278
+ end
279
+
280
+ def self.connect(opts)
281
+ MockRbVmomiConnection.new(opts)
282
+ end
283
+ end
284
+ end
@@ -0,0 +1,167 @@
1
+ class MockVsphereSnapshot
2
+ attr_accessor :name
3
+
4
+ def RevertToSnapshot_Task
5
+ self
6
+ end
7
+
8
+ def wait_for_completion
9
+ true
10
+ end
11
+ end
12
+
13
+ class MockVsphereVM
14
+ attr_accessor :name, :powerState, :snapshot, :template, :toolsRunningStatus
15
+
16
+ def initialize
17
+ @powerState = 'poweredOff'
18
+ @toolsRunningStatus = 'guestToolsStopped'
19
+ @first = true
20
+ end
21
+
22
+ def runtime
23
+ self
24
+ end
25
+
26
+ attr_reader :powerState
27
+
28
+ def toolsRunningStatus
29
+ prev = @toolsRunningStatus
30
+ @toolsRunningStatus = 'guestToolsRunning'
31
+ prev
32
+ end
33
+
34
+ def PowerOnVM_Task
35
+ @powerState = 'poweredOn'
36
+ self
37
+ end
38
+
39
+ def PowerOffVM_Task
40
+ @powerState = 'poweredOff'
41
+ self
42
+ end
43
+
44
+ def summary
45
+ self
46
+ end
47
+
48
+ def guest
49
+ self
50
+ end
51
+
52
+ def ipAddress
53
+ @toolsRunningStatus = 'guestToolsRunning'
54
+ "#{@name}.ip.address"
55
+ end
56
+
57
+ def CloneVM_Task(opts)
58
+ clone = MockVsphereVM.new
59
+ clone.name = opts[:name]
60
+ clone.snapshot = snapshot
61
+ clone.template = template
62
+ clone.PowerOnVM_Task
63
+ MockVsphereHelper.add_vm(opts[:name], clone)
64
+ clone
65
+ end
66
+
67
+ def wait_for_completion
68
+ true
69
+ end
70
+
71
+ def Destroy_Task
72
+ true
73
+ end
74
+ end
75
+
76
+ class MockVsphereHelper
77
+ @@fog_file = {}
78
+ @@vms = {}
79
+
80
+ def initialize(arg); end
81
+
82
+ def self.add_vm(name, vm)
83
+ @@vms[name] = vm
84
+ end
85
+
86
+ def self.set_config(conf)
87
+ @@fog_file = conf
88
+ end
89
+
90
+ def self.powerOn
91
+ @@vms.each do |_name, vm|
92
+ vm.powerState = 'poweredOn'
93
+ end
94
+ end
95
+
96
+ def self.powerOff
97
+ @@vms.each do |_name, vm|
98
+ vm.powerState = 'poweredOff'
99
+ end
100
+ end
101
+
102
+ def self.set_vms(hosts)
103
+ @@vms = {}
104
+ hosts.each do |host|
105
+ vm = MockVsphereVM.new
106
+ vm.name = host.name
107
+ vm.snapshot = MockVsphereSnapshot.new
108
+ vm.snapshot.name = host[:snapshot]
109
+ @@vms[host.name] = vm
110
+ template = MockVsphereVM.new
111
+ template.name = host[:template]
112
+ template.snapshot = MockVsphereSnapshot.new
113
+ template.snapshot.name = host[:snapshot]
114
+ @@vms[host[:template]] = template
115
+ end
116
+ end
117
+
118
+ def self.load_config(_file)
119
+ @@fog_file
120
+ end
121
+
122
+ def find_vms(keys)
123
+ found = {}
124
+ keys = ([] << keys)
125
+ keys.flatten!
126
+ keys.each do |key|
127
+ found[key] = @@vms[key] if @@vms.has_key?(key)
128
+ end
129
+ found
130
+ end
131
+
132
+ def self.find_vm(key)
133
+ return unless @@vms.has_key?(key)
134
+
135
+ @@vms[key]
136
+ end
137
+
138
+ def find_snapshot(vm, snap)
139
+ return unless @@vms[vm.name].snapshot.name == snap
140
+
141
+ @@vms[vm.name].snapshot
142
+ end
143
+
144
+ def find_customization(_template)
145
+ nil
146
+ end
147
+
148
+ def find_datastore(_dc, datastore)
149
+ datastore
150
+ end
151
+
152
+ def find_pool(_dc, pool)
153
+ pool
154
+ end
155
+
156
+ def find_folder(_dc, folder)
157
+ folder
158
+ end
159
+
160
+ def wait_for_tasks(_tasks, _try, _attempts)
161
+ true
162
+ end
163
+
164
+ def close
165
+ true
166
+ end
167
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,32 @@
1
- require 'simplecov'
2
- require 'rspec/its'
3
1
  require 'beaker'
4
- require 'beaker/hypervisor/vcloud'
2
+ require 'mock_vsphere'
3
+ require 'mock_vsphere_helper'
4
+
5
+ begin
6
+ require 'simplecov'
7
+ require 'simplecov-console'
8
+ require 'codecov'
9
+ rescue LoadError
10
+ # Do nothing if no required gem installed
11
+ else
12
+ SimpleCov.start do
13
+ track_files 'lib/**/*.rb'
14
+
15
+ add_filter '/spec'
16
+ # do not track vendored files
17
+ add_filter '/vendor'
18
+ add_filter '/.vendor'
19
+
20
+ enable_coverage :branch
21
+ end
22
+
23
+ SimpleCov.formatters = [
24
+ SimpleCov::Formatter::Console,
25
+ SimpleCov::Formatter::Codecov,
26
+ ]
27
+ end
28
+
29
+ Dir['./lib/beaker/hypervisor/*.rb'].sort.each { |file| require file }
5
30
 
6
31
  # setup & require beaker's spec_helper.rb
7
32
  beaker_gem_spec = Gem::Specification.find_by_name('beaker')