mirror of
https://github.com/puppetlabs/vmpooler.git
synced 2026-01-26 10:08:40 -05:00
(POOLER-73) Add spec tests for vsphere_helper
Previously there were no spec tests to document the behaviour of the vsphere_helper. This commit adds the behavioural spec tests in prepration of the refactoring work as part of POOLER-70, the connection pooling work in POOLER-52 and various bugs found in vsphere_helper while these spec tests were being created.
This commit is contained in:
parent
fd93c5bea8
commit
bcaad0a15b
3 changed files with 2827 additions and 0 deletions
722
spec/rbvmomi_helper.rb
Normal file
722
spec/rbvmomi_helper.rb
Normal file
|
|
@ -0,0 +1,722 @@
|
|||
# -----------------------------------------------------------------------------------------------------------------
|
||||
# Managed Objects (https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/index-mo_types.html)
|
||||
# -----------------------------------------------------------------------------------------------------------------
|
||||
|
||||
MockClusterComputeResource = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.ClusterComputeResource.html
|
||||
# From MockClusterComputeResource
|
||||
:actionHistory, :configuration, :drsFault, :drsRecommendation, :migrationHistory, :recommendation,
|
||||
# From ComputeResource
|
||||
:resourcePool,
|
||||
# From ManagedEntity
|
||||
:name
|
||||
)
|
||||
|
||||
MockComputeResource = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.ComputeResource.html
|
||||
# From ComputeResource
|
||||
:configurationEx, :datastore, :host, :network, :resourcePool, :summary,
|
||||
# From ManagedEntity
|
||||
:name
|
||||
)
|
||||
|
||||
MockContainerView = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.view.ContainerView.html
|
||||
# From ContainerView
|
||||
:container, :recursive, :type
|
||||
)
|
||||
|
||||
MockDatacenter = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.Datacenter.html
|
||||
# From Datacenter
|
||||
:datastore, :datastoreFolder, :hostFolder, :network, :networkFolder, :vmFolder,
|
||||
# From ManagedEntity
|
||||
:name
|
||||
) do
|
||||
# From RBVMOMI::VIM::Datacenter https://github.com/vmware/rbvmomi/blob/master/lib/rbvmomi/vim/Datacenter.rb
|
||||
|
||||
# Find the Datastore with the given +name+.
|
||||
def find_datastore name
|
||||
datastore.find { |x| x.name == name }
|
||||
end
|
||||
end
|
||||
|
||||
MockDatastore = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.Datastore.html
|
||||
# From Datastore
|
||||
:browser, :capability, :host, :info, :iormConfiguration, :summary, :vm,
|
||||
# From ManagedEntity
|
||||
:name
|
||||
)
|
||||
|
||||
MockFolder = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.Folder.html
|
||||
# From Folder
|
||||
:childEntity, :childType,
|
||||
# From ManagedEntity
|
||||
:name
|
||||
) do
|
||||
# From RBVMOMI::VIM::Folder https://github.com/vmware/rbvmomi/blob/master/lib/rbvmomi/vim/Folder.rb#L107-L110
|
||||
def children
|
||||
childEntity
|
||||
end
|
||||
end
|
||||
|
||||
MockHostSystem = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.HostSystem.html
|
||||
# From HostSystem
|
||||
:capability, :config, :configManager, :datastore, :datastoreBrowser, :hardware, :network, :runtime, :summary, :systemResources, :vm,
|
||||
# From ManagedEntity
|
||||
:overallStatus, :name, :parent
|
||||
)
|
||||
|
||||
MockPropertyCollector = Struct.new(
|
||||
# https://pubs.vmware.com/vsphere-55/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc%2Fvmodl.query.PropertyCollector.html
|
||||
# PropertyCollector
|
||||
:filter
|
||||
)
|
||||
|
||||
MockResourcePool = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.ResourcePool.html
|
||||
# From ResourcePool
|
||||
:childConfiguration, :config, :owner, :resourcePool, :runtime, :summary, :vm,
|
||||
# From ManagedEntity
|
||||
:name
|
||||
)
|
||||
|
||||
MockSearchIndex = Object
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.SearchIndex.html
|
||||
|
||||
MockServiceInstance = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.ServiceInstance.html
|
||||
# From ServiceInstance
|
||||
:capability, :content, :serverClock
|
||||
) do
|
||||
# From ServiceInstance
|
||||
# Mock the CurrentTime method so that it appears the ServiceInstance is valid.
|
||||
def CurrentTime
|
||||
Time.now
|
||||
end
|
||||
|
||||
# From RBVMOMI::VIM::ServiceInstance https://github.com/vmware/rbvmomi/blob/master/lib/rbvmomi/vim/ServiceInstance.rb
|
||||
def find_datacenter(path=nil)
|
||||
# In our mocked instance, DataCenters are always in the root Folder.
|
||||
# If path is nil the first DC is returned otherwise match by name
|
||||
content.rootFolder.childEntity.each do |child|
|
||||
if child.is_a?(MockDatacenter)
|
||||
return child if path.nil? || child.name == path
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
MockTask = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.Task.html
|
||||
# From Task
|
||||
:info,
|
||||
) do
|
||||
# From RBVMOMI https://github.com/vmware/rbvmomi/blob/master/lib/rbvmomi/vim/Task.rb
|
||||
# Mock the with 'Not Implemented'
|
||||
def wait_for_completion
|
||||
raise(RuntimeError,'Not Implemented')
|
||||
end
|
||||
end
|
||||
|
||||
MockViewManager = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.view.ViewManager.html
|
||||
# From ViewManager
|
||||
:viewList,
|
||||
) do
|
||||
# From ViewManager
|
||||
def CreateContainerView(options)
|
||||
mock_RbVmomi_VIM_ContainerView({
|
||||
:container => options[:container],
|
||||
:recursive => options[:recursive],
|
||||
:type => options[:type],
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
MockVirtualDiskManager = Object
|
||||
# https://pubs.vmware.com/vsphere-55/index.jsp#com.vmware.wssdk.apiref.doc/vim.VirtualDiskManager.html
|
||||
|
||||
MockVirtualMachine = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.VirtualMachine.html
|
||||
# From VirtualMachine
|
||||
:config, :snapshot, :summary,
|
||||
# From ManagedEntity
|
||||
:name
|
||||
)
|
||||
|
||||
MockVirtualMachineSnapshot = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.Snapshot.html
|
||||
# From VirtualMachineSnapshot
|
||||
:config
|
||||
)
|
||||
|
||||
# -------------------------------------------------------------------------------------------------------------
|
||||
# Data Objects (https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/index-do_types.html)
|
||||
# -------------------------------------------------------------------------------------------------------------
|
||||
|
||||
MockDescription = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.Description.html
|
||||
# From Description
|
||||
:label, :summary
|
||||
)
|
||||
|
||||
MockDynamicProperty = Struct.new(
|
||||
# https://pubs.vmware.com/vsphere-55/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc%2Fvmodl.DynamicProperty.html
|
||||
# From DynamicProperty
|
||||
:name, :val
|
||||
)
|
||||
|
||||
MockHostCpuPackage = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.host.CpuPackage.html
|
||||
# From HostCpuPackage
|
||||
:busHz, :cpuFeature, :description, :hz, :index, :threadId, :vendor
|
||||
)
|
||||
|
||||
MockHostHardwareSummary = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.host.Summary.HardwareSummary.html
|
||||
# From HostHardwareSummary
|
||||
:cpuMhz, :numCpuCores, :numCpuPkgs, :memorySize
|
||||
)
|
||||
|
||||
MockHostListSummary = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.host.Summary.html
|
||||
# From HostListSummary
|
||||
:quickStats, :hardware
|
||||
)
|
||||
|
||||
MockHostListSummaryQuickStats = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.host.Summary.QuickStats.html
|
||||
# From HostListSummaryQuickStats
|
||||
:overallCpuUsage, :overallMemoryUsage
|
||||
)
|
||||
|
||||
MockHostRuntimeInfo = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.host.RuntimeInfo.html
|
||||
# From HostRuntimeInfo
|
||||
:bootTime, :connectionState, :healthSystemRuntime, :inMaintenanceMode, :powerState, :tpmPcrValues
|
||||
)
|
||||
|
||||
MockHostSystemHostHardwareInfo = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.host.HardwareInfo.html
|
||||
# From HostHardwareInfo
|
||||
:biosInfo, :cpuFeature, :cpuInfo, :cpuPkg, :cpuPowerManagementInfo, :memorySize, :numaInfo, :pciDevice, :systemInfo
|
||||
)
|
||||
|
||||
MockObjectContent = Struct.new(
|
||||
# https://pubs.vmware.com/vsphere-55/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc%2Fvmodl.query.PropertyCollector.ObjectContent.html
|
||||
# From ObjectContent
|
||||
:missingSet, :obj, :propSet
|
||||
)
|
||||
|
||||
MockRetrieveResult = Struct.new(
|
||||
# https://pubs.vmware.com/vsphere-55/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc%2Fvmodl.query.PropertyCollector.RetrieveResult.html
|
||||
# From RetrieveResult
|
||||
:objects, :token
|
||||
)
|
||||
|
||||
MockServiceContent = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.ServiceInstanceContent.html#field_detail
|
||||
# From ServiceContent
|
||||
:propertyCollector, :rootFolder, :searchIndex, :viewManager, :virtualDiskManager
|
||||
)
|
||||
|
||||
MockVirtualDevice = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.device.VirtualDevice.html
|
||||
# From VirtualDevice
|
||||
:deviceInfo, :controllerKey, :key, :backing, :connectable, :unitNumber
|
||||
)
|
||||
|
||||
MockVirtualDisk = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.device.VirtualDisk.html
|
||||
# From VirtualDisk
|
||||
:capacityInKB, :shares,
|
||||
# From VirtualDevice
|
||||
:deviceInfo, :controllerKey, :key, :backing, :connectable, :unitNumber
|
||||
)
|
||||
|
||||
MockVirtualHardware = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.VirtualHardware.html
|
||||
# From VirtualHardware
|
||||
:device
|
||||
)
|
||||
|
||||
MockVirtualMachineConfigInfo = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.ConfigInfo.html
|
||||
# From VirtualMachineConfigInfo
|
||||
:hardware
|
||||
)
|
||||
|
||||
MockVirtualMachineFileLayoutExFileInfo = Struct.new(
|
||||
# https://pubs.vmware.com/vsphere-55/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc%2Fvim.vm.FileLayoutEx.FileInfo.html
|
||||
# From VirtualMachineFileLayoutExFileInfo
|
||||
:key, :name, :size, :type, :uniqueSize
|
||||
)
|
||||
|
||||
MockVirtualMachineRuntimeInfo = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.RuntimeInfo.html
|
||||
# From VirtualMachineRuntimeInfo
|
||||
:bootTime, :cleanPowerOff, :connectionState, :faultToleranceState, :host, :maxCpuUsage, :maxMemoryUsage, :memoryOverhead,
|
||||
:needSecondaryReason, :numMksConnections, :powerState, :question, :recordReplayState, :suspendInterval, :suspendTime, :toolsInstallerMounted
|
||||
)
|
||||
|
||||
MockVirtualMachineSnapshotInfo = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.SnapshotInfo.html
|
||||
# From MockVirtualMachineSnapshotInfo
|
||||
:currentSnapshot, :rootSnapshotList
|
||||
)
|
||||
|
||||
MockVirtualMachineSnapshotTree = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.SnapshotTree.html
|
||||
# From VirtualMachineSnapshotTree
|
||||
:backupManifest, :childSnapshotList, :createTime, :description, :id, :name, :quiesced, :replaySupported,
|
||||
:snapshot, :state, :vm
|
||||
)
|
||||
|
||||
MockVirtualMachineSummary = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.Summary.html
|
||||
# From VirtualMachineSummary
|
||||
:config, :customValue, :guest, :quickStats, :runtime, :storage, :vm
|
||||
)
|
||||
|
||||
MockVirtualSCSIController = Struct.new(
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.device.VirtualSCSIController.html
|
||||
# From VirtualSCSIController
|
||||
:hotAddRemove, :scsiCtlrUnitNumber, :sharedBus,
|
||||
# From VirtualDevice
|
||||
:deviceInfo, :controllerKey, :key, :backing, :connectable, :unitNumber
|
||||
)
|
||||
|
||||
# --------------------
|
||||
# RBVMOMI only Objects
|
||||
# --------------------
|
||||
MockRbVmomiVIMConnection = Struct.new(
|
||||
# https://github.com/vmware/rbvmomi/blob/master/lib/rbvmomi/vim.rb
|
||||
:serviceInstance, :serviceContent, :rootFolder, :root
|
||||
) do
|
||||
# From https://github.com/vmware/rbvmomi/blob/master/lib/rbvmomi/vim.rb
|
||||
# Alias to serviceContent.searchIndex
|
||||
def searchIndex
|
||||
serviceContent.searchIndex
|
||||
end
|
||||
# Alias to serviceContent.propertyCollector
|
||||
def propertyCollector
|
||||
serviceContent.propertyCollector
|
||||
end
|
||||
end
|
||||
|
||||
# -------------------------------------------------------------------------------------------------------------
|
||||
# Mocking Methods
|
||||
# -------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.ClusterComputeResource.html
|
||||
def mock_RbVmomi_VIM_ClusterComputeResource(options = {})
|
||||
options[:name] = 'Cluster' + rand(65536).to_s if options[:name].nil?
|
||||
|
||||
mock = MockClusterComputeResource.new()
|
||||
|
||||
mock.name = options[:name]
|
||||
# All cluster compute resources have a root Resource Pool
|
||||
mock.resourcePool = mock_RbVmomi_VIM_ResourcePool({:name => options[:name]})
|
||||
|
||||
allow(mock).to receive(:is_a?) do |expected_type|
|
||||
expected_type == RbVmomi::VIM::ClusterComputeResource
|
||||
end
|
||||
|
||||
mock
|
||||
end
|
||||
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.view.ContainerView.html
|
||||
def mock_RbVmomi_VIM_ContainerView(options = {})
|
||||
mock = MockContainerView.new()
|
||||
mock.container = options[:container]
|
||||
mock.recursive = options[:recursive]
|
||||
mock.type = options[:type]
|
||||
|
||||
mock
|
||||
end
|
||||
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.ComputeResource.html
|
||||
def mock_RbVmomi_VIM_ComputeResource(options = {})
|
||||
options[:name] = 'Compute' + rand(65536).to_s if options[:name].nil?
|
||||
options[:hosts] = [{}] if options[:hosts].nil?
|
||||
|
||||
mock = MockComputeResource.new()
|
||||
|
||||
mock.name = options[:name]
|
||||
mock.host = []
|
||||
|
||||
# A compute resource must have at least one host.
|
||||
options[:hosts].each do |host_options|
|
||||
mock_host = mock_RbVmomi_VIM_HostSystem(host_options)
|
||||
mock_host.parent = mock
|
||||
mock.host << mock_host
|
||||
end
|
||||
|
||||
mock
|
||||
end
|
||||
|
||||
# https://github.com/vmware/rbvmomi/blob/master/lib/rbvmomi/vim.rb
|
||||
def mock_RbVmomi_VIM_Connection(options = {})
|
||||
options[:serviceInstance] = {} if options[:serviceInstance].nil?
|
||||
options[:serviceContent] = {} if options[:serviceContent].nil?
|
||||
|
||||
mock = MockRbVmomiVIMConnection.new()
|
||||
mock.serviceContent = mock_RbVmomi_VIM_ServiceContent(options[:serviceContent])
|
||||
options[:serviceInstance][:servicecontent] = mock.serviceContent if options[:serviceInstance][:servicecontent].nil?
|
||||
mock.serviceInstance = mock_RbVmomi_VIM_ServiceInstance(options[:serviceInstance])
|
||||
|
||||
mock
|
||||
end
|
||||
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.Datastore.html
|
||||
def mock_RbVmomi_VIM_Datacenter(options = {})
|
||||
options[:hostfolder_tree] = {} if options[:hostfolder_tree].nil?
|
||||
options[:vmfolder_tree] = {} if options[:vmfolder_tree].nil?
|
||||
# Currently don't support mocking datastore tree
|
||||
options[:datastores] = [] if options[:datastores].nil?
|
||||
options[:name] = 'Datacenter' + rand(65536).to_s if options[:name].nil?
|
||||
|
||||
mock = MockDatacenter.new()
|
||||
|
||||
mock.name = options[:name]
|
||||
mock.hostFolder = mock_RbVmomi_VIM_Folder({ :name => 'hostFolderRoot'})
|
||||
mock.vmFolder = mock_RbVmomi_VIM_Folder({ :name => 'vmFolderRoot'})
|
||||
mock.datastore = []
|
||||
|
||||
# Create vmFolder hierarchy
|
||||
recurse_folder_tree(options[:vmfolder_tree],mock.vmFolder.childEntity)
|
||||
|
||||
# Create hostFolder hierarchy
|
||||
recurse_folder_tree(options[:hostfolder_tree],mock.hostFolder.childEntity)
|
||||
|
||||
# Create mock Datastores
|
||||
options[:datastores].each do |datastorename|
|
||||
mock_ds = mock_RbVmomi_VIM_Datastore({ :name => datastorename })
|
||||
mock.datastore << mock_ds
|
||||
end
|
||||
|
||||
mock
|
||||
end
|
||||
|
||||
def recurse_folder_tree(tree, root_object)
|
||||
tree.keys.each do |foldername|
|
||||
folder_options = tree[foldername].nil? ? {} : tree[foldername]
|
||||
folder_options[:name] = foldername if folder_options[:name].nil?
|
||||
|
||||
case folder_options[:object_type]
|
||||
when 'vm'
|
||||
child_object = mock_RbVmomi_VIM_VirtualMachine({ :name => folder_options[:name]})
|
||||
when 'compute_resource'
|
||||
child_object = mock_RbVmomi_VIM_ComputeResource({ :name => folder_options[:name]})
|
||||
when 'cluster_compute_resource'
|
||||
child_object = mock_RbVmomi_VIM_ClusterComputeResource({ :name => folder_options[:name]})
|
||||
when 'resource_pool'
|
||||
child_object = mock_RbVmomi_VIM_ResourcePool({ :name => folder_options[:name]})
|
||||
else
|
||||
child_object = mock_RbVmomi_VIM_Folder({:name => foldername})
|
||||
end
|
||||
|
||||
# Recursively create children - Default is the child_object is a Folder
|
||||
case folder_options[:object_type]
|
||||
when 'cluster_compute_resource'
|
||||
# Append children into the root Resource Pool for a cluster, instead of directly into the cluster itself.
|
||||
recurse_folder_tree(folder_options[:children],child_object.resourcePool.resourcePool) unless folder_options[:children].nil?
|
||||
when 'resource_pool'
|
||||
recurse_folder_tree(folder_options[:children],child_object.resourcePool) unless folder_options[:children].nil?
|
||||
else
|
||||
recurse_folder_tree(folder_options[:children],child_object.childEntity) unless folder_options[:children].nil?
|
||||
end
|
||||
|
||||
root_object << child_object
|
||||
end
|
||||
end
|
||||
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.Datastore.html
|
||||
def mock_RbVmomi_VIM_Datastore(options = {})
|
||||
options[:name] = 'Datastore' + rand(65536).to_s if options[:name].nil?
|
||||
|
||||
mock = MockDatastore.new()
|
||||
|
||||
mock.name = options[:name]
|
||||
|
||||
allow(mock).to receive(:is_a?) do |expected_type|
|
||||
expected_type == RbVmomi::VIM::Datastore
|
||||
end
|
||||
|
||||
mock
|
||||
end
|
||||
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.Datastore.html
|
||||
def mock_RbVmomi_VIM_Folder(options = {})
|
||||
options[:name] = 'Folder' + rand(65536).to_s if options[:name].nil?
|
||||
|
||||
mock = MockFolder.new()
|
||||
|
||||
mock.name = options[:name]
|
||||
mock.childEntity = []
|
||||
allow(mock).to receive(:is_a?) do |expected_type|
|
||||
expected_type == RbVmomi::VIM::Folder
|
||||
end
|
||||
|
||||
mock
|
||||
end
|
||||
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.HostSystem.html
|
||||
def mock_RbVmomi_VIM_HostSystem(options = {})
|
||||
options[:memory_size] = 4294967296 if options[:memory_size].nil? # 4GB RAM
|
||||
options[:num_cpu] = 1 if options[:num_cpu].nil?
|
||||
options[:num_cores_per_cpu] = 1 if options[:num_cores_per_cpu].nil?
|
||||
options[:cpu_speed] = 2048 if options[:cpu_speed].nil? # 2.0 GHz
|
||||
options[:cpu_model] = 'Intel(R) Xeon(R) CPU E5-2697 v4 @ 2.0GHz' if options[:cpu_model].nil?
|
||||
options[:maintenance_mode] = false if options[:maintenance_mode].nil?
|
||||
options[:overall_status] = 'green' if options[:overall_status].nil?
|
||||
options[:overall_cpu_usage] = 1 if options[:overall_cpu_usage].nil?
|
||||
options[:overall_memory_usage] = 1 if options[:overall_memory_usage].nil?
|
||||
options[:name] = 'HOST' + rand(65536).to_s if options[:name].nil?
|
||||
|
||||
mock = MockHostSystem.new()
|
||||
mock.name = options[:name]
|
||||
mock.summary = MockHostListSummary.new()
|
||||
mock.summary.quickStats = MockHostListSummaryQuickStats.new()
|
||||
mock.summary.hardware = MockHostHardwareSummary.new()
|
||||
mock.hardware = MockHostSystemHostHardwareInfo.new()
|
||||
mock.runtime = MockHostRuntimeInfo.new()
|
||||
|
||||
mock.hardware.cpuPkg = []
|
||||
(1..options[:num_cpu]).each do |cpuid|
|
||||
mockcpu = MockHostCpuPackage.new()
|
||||
mockcpu.hz = options[:cpu_speed] * 1024 * 1024
|
||||
mockcpu.description = options[:cpu_model]
|
||||
mockcpu.index = 0
|
||||
mock.hardware.cpuPkg << mockcpu
|
||||
end
|
||||
|
||||
mock.runtime.inMaintenanceMode = options[:maintenance_mode]
|
||||
mock.overallStatus = options[:overall_status]
|
||||
|
||||
mock.summary.hardware.memorySize = options[:memory_size]
|
||||
mock.hardware.memorySize = options[:memory_size]
|
||||
|
||||
mock.summary.hardware.cpuMhz = options[:cpu_speed]
|
||||
mock.summary.hardware.numCpuCores = options[:num_cpu] * options[:num_cores_per_cpu]
|
||||
mock.summary.hardware.numCpuPkgs = options[:num_cpu]
|
||||
mock.summary.quickStats.overallCpuUsage = options[:overall_cpu_usage]
|
||||
mock.summary.quickStats.overallMemoryUsage = options[:overall_memory_usage]
|
||||
|
||||
mock
|
||||
end
|
||||
|
||||
# https://pubs.vmware.com/vsphere-55/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc%2Fvmodl.query.PropertyCollector.RetrieveResult.html
|
||||
def mock_RbVmomi_VIM_RetrieveResult(options = {})
|
||||
options[:response] = [] if options[:response].nil?
|
||||
mock = MockRetrieveResult.new()
|
||||
|
||||
mock.objects = []
|
||||
|
||||
options[:response].each do |response|
|
||||
mock_objectdata = MockObjectContent.new()
|
||||
|
||||
mock_objectdata.propSet = []
|
||||
|
||||
mock_objectdata.obj = response[:object]
|
||||
|
||||
# Mock the object properties
|
||||
response.each do |key,value|
|
||||
unless key == :object
|
||||
mock_property = MockDynamicProperty.new()
|
||||
mock_property.name = key
|
||||
mock_property.val = value
|
||||
mock_objectdata.propSet << mock_property
|
||||
end
|
||||
end
|
||||
mock.objects << mock_objectdata
|
||||
end
|
||||
|
||||
mock
|
||||
end
|
||||
|
||||
# https://pubs.vmware.com/vsphere-55/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc%2Fvmodl.query.PropertyCollector.html
|
||||
def mock_RbVmomi_VIM_PropertyCollector(options = {})
|
||||
mock = MockPropertyCollector.new()
|
||||
|
||||
mock
|
||||
end
|
||||
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.ServiceInstanceContent.html#field_detail
|
||||
def mock_RbVmomi_VIM_ServiceContent(options = {})
|
||||
options[:propertyCollector] = {} if options[:propertyCollector].nil?
|
||||
options[:datacenters] = [] if options[:datacenters].nil?
|
||||
|
||||
mock = MockServiceContent.new()
|
||||
|
||||
mock.searchIndex = MockSearchIndex.new()
|
||||
mock.viewManager = MockViewManager.new()
|
||||
mock.virtualDiskManager = MockVirtualDiskManager.new()
|
||||
mock.rootFolder = mock_RbVmomi_VIM_Folder({ :name => 'RootFolder' })
|
||||
|
||||
mock.propertyCollector = mock_RbVmomi_VIM_PropertyCollector(options[:propertyCollector])
|
||||
|
||||
# Create the DCs in this ServiceContent
|
||||
options[:datacenters].each do |dc_options|
|
||||
mock_dc = mock_RbVmomi_VIM_Datacenter(dc_options)
|
||||
mock.rootFolder.childEntity << mock_dc
|
||||
end
|
||||
|
||||
mock
|
||||
end
|
||||
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.ServiceInstance.html
|
||||
def mock_RbVmomi_VIM_ServiceInstance(options = {})
|
||||
mock = MockServiceInstance.new()
|
||||
|
||||
mock.content = options[:servicecontent]
|
||||
|
||||
mock
|
||||
end
|
||||
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.Task.html
|
||||
def mock_RbVmomi_VIM_Task(options = {})
|
||||
mock = MockTask.new()
|
||||
|
||||
mock
|
||||
end
|
||||
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.ResourcePool.html
|
||||
def mock_RbVmomi_VIM_ResourcePool(options = {})
|
||||
options[:name] = 'ResourcePool' + rand(65536).to_s if options[:name].nil?
|
||||
|
||||
mock = MockResourcePool.new()
|
||||
|
||||
mock.name = options[:name]
|
||||
mock.resourcePool = []
|
||||
allow(mock).to receive(:is_a?) do |expected_type|
|
||||
expected_type == RbVmomi::VIM::ResourcePool
|
||||
end
|
||||
|
||||
mock
|
||||
end
|
||||
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.device.VirtualDisk.html
|
||||
def mock_RbVmomi_VIM_VirtualDisk(options = {})
|
||||
options[:controllerKey] = rand(65536) if options[:controllerKey].nil?
|
||||
options[:key] = rand(65536) if options[:key].nil?
|
||||
options[:label] = 'SCSI' + rand(65536).to_s if options[:label].nil?
|
||||
options[:unitNumber] = rand(65536) if options[:unitNumber].nil?
|
||||
|
||||
mock = MockVirtualDisk.new()
|
||||
mock.deviceInfo = MockDescription.new()
|
||||
|
||||
mock.deviceInfo.label = options[:label]
|
||||
mock.controllerKey = options[:controllerKey]
|
||||
mock.key = options[:key]
|
||||
mock.unitNumber = options[:unitNumber]
|
||||
|
||||
allow(mock).to receive(:is_a?) do |expected_type|
|
||||
expected_type == RbVmomi::VIM::VirtualDisk
|
||||
end
|
||||
|
||||
mock
|
||||
end
|
||||
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.VirtualMachine.html
|
||||
def mock_RbVmomi_VIM_VirtualMachine(options = {})
|
||||
options[:snapshot_tree] = nil if options[:snapshot_tree].nil?
|
||||
options[:name] = 'VM' + rand(65536).to_s if options[:name].nil?
|
||||
|
||||
mock = MockVirtualMachine.new()
|
||||
mock.config = MockVirtualMachineConfigInfo.new()
|
||||
mock.config.hardware = MockVirtualHardware.new([])
|
||||
mock.summary = MockVirtualMachineSummary.new()
|
||||
mock.summary.runtime = MockVirtualMachineRuntimeInfo.new()
|
||||
|
||||
mock.name = options[:name]
|
||||
unless options[:snapshot_tree].nil?
|
||||
mock.snapshot = MockVirtualMachineSnapshotInfo.new()
|
||||
mock.snapshot.rootSnapshotList = []
|
||||
index = 0
|
||||
|
||||
# Create a recursive snapshot tree
|
||||
recurse_snapshot_tree(options[:snapshot_tree],mock.snapshot.rootSnapshotList,index)
|
||||
end
|
||||
allow(mock).to receive(:is_a?) do |expected_type|
|
||||
expected_type == RbVmomi::VIM::VirtualMachine
|
||||
end
|
||||
|
||||
mock
|
||||
end
|
||||
|
||||
def recurse_snapshot_tree(tree, root_object, index)
|
||||
tree.keys.each do |snapshotname|
|
||||
snap_options = tree[snapshotname].nil? ? {} : tree[snapshotname]
|
||||
snap = MockVirtualMachineSnapshotTree.new()
|
||||
snap.id = index
|
||||
snap.name = snapshotname
|
||||
snap.childSnapshotList = []
|
||||
snap.description = "Snapshot #{snapshotname}"
|
||||
snap.snapshot = snap_options[:ref] unless snap_options[:ref].nil?
|
||||
|
||||
# Recursively create chilren
|
||||
recurse_snapshot_tree(snap_options[:children],snap.childSnapshotList,index) unless snap_options[:children].nil?
|
||||
|
||||
root_object << snap
|
||||
index += 1
|
||||
end
|
||||
end
|
||||
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.device.VirtualDevice.html
|
||||
def mock_RbVmomi_VIM_VirtualMachineDevice(options = {})
|
||||
mock = MockVirtualDevice.new()
|
||||
mock.deviceInfo = MockDescription.new()
|
||||
|
||||
mock.deviceInfo.label = options[:label]
|
||||
|
||||
mock
|
||||
end
|
||||
|
||||
# https://pubs.vmware.com/vsphere-55/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc%2Fvim.vm.FileLayoutEx.FileInfo.html
|
||||
def mock_RbVmomi_VIM_VirtualMachineFileLayoutExFileInfo(options = {})
|
||||
options[:key] = rand(65536).to_s if options[:key].nil?
|
||||
|
||||
mock = MockVirtualMachineFileLayoutExFileInfo.new()
|
||||
|
||||
mock.key = options[:key]
|
||||
mock.name = options[:name]
|
||||
mock.size = options[:size]
|
||||
mock.type = options[:type]
|
||||
mock.uniqueSize = options[:uniqueSize]
|
||||
|
||||
mock
|
||||
end
|
||||
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.Snapshot.html
|
||||
def mock_RbVmomi_VIM_VirtualMachineSnapshot(options = {})
|
||||
mock = MockVirtualMachineSnapshot.new()
|
||||
|
||||
mock
|
||||
end
|
||||
|
||||
# https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.device.VirtualSCSIController.html
|
||||
def mock_RbVmomi_VIM_VirtualSCSIController(options = {})
|
||||
options[:controllerKey] = rand(65536) if options[:controllerKey].nil?
|
||||
options[:key] = rand(65536) if options[:key].nil?
|
||||
options[:label] = 'SCSI' + rand(65536).to_s if options[:label].nil?
|
||||
options[:scsiCtlrUnitNumber] = 7 if options[:scsiCtlrUnitNumber].nil?
|
||||
|
||||
mock = MockVirtualSCSIController.new()
|
||||
mock.deviceInfo = MockDescription.new()
|
||||
|
||||
mock.deviceInfo.label = options[:label]
|
||||
mock.controllerKey = options[:controllerKey]
|
||||
mock.key = options[:key]
|
||||
mock.scsiCtlrUnitNumber = options[:scsiCtlrUnitNumber]
|
||||
|
||||
allow(mock).to receive(:is_a?) do |expected_type|
|
||||
expected_type == RbVmomi::VIM::VirtualSCSIController
|
||||
end
|
||||
|
||||
mock
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue