require 'test_helper'
class DeserializationTest < Test::Unit::TestCase
def setup
conn = VIM.new(:ns => 'urn:vim25', :rev => '4.0')
@deserializer = RbVmomi::Deserializer.new conn
end
def check str, expected, type
got = @deserializer.deserialize Nokogiri(str).root, type
assert_equal expected, got
end
def test_moref
check <<-EOS, VIM.Folder(nil, 'ha-folder-root'), 'Folder'
ha-folder-root
EOS
check <<-EOS, VIM.Datacenter(nil, 'ha-datacenter'), 'ManagedObjectReference'
ha-datacenter
EOS
end
def test_dataobject
obj = VIM.DatastoreSummary(
:capacity => 1000,
:accessible => true,
:datastore => VIM.Datastore(nil, "foo"),
:freeSpace => 31,
:multipleHostAccess => false,
:name => "baz",
:type => "VMFS",
:url => "http://foo/",
:dynamicProperty => []
)
check <<-EOS, obj, 'DatastoreSummary'
foo
baz
http://foo/
1000
31
1
false
VMFS
EOS
end
def test_enum
check <<-EOS, 'add', 'ConfigSpecOperation'
add
EOS
end
def test_array
obj = VIM.ObjectContent(
:obj => VIM.Folder(nil, 'ha-folder-root'),
:dynamicProperty => [],
:missingSet => [],
:propSet => [
VIM.DynamicProperty(
:name => 'childEntity',
:val => [
VIM.Datacenter(nil, 'ha-datacenter')
]
)
]
)
check <<-EOS, obj, 'ObjectContent'
ha-folder-root
childEntity
ha-datacenter
EOS
end
def test_array2
obj = VIM.DVPortStatus(
:dynamicProperty => [],
:linkUp => true,
:blocked => false,
:vlanIds => [
VIM::NumericRange(:dynamicProperty => [], :start => 5, :end => 7),
VIM::NumericRange(:dynamicProperty => [], :start => 10, :end => 20),
],
:vmDirectPathGen2InactiveReasonNetwork => [],
:vmDirectPathGen2InactiveReasonOther => []
)
check <<-EOS, obj, 'DVPortStatus'
1
false
5
7
10
20
EOS
end
def test_empty_array
obj = VIM.DVPortStatus(
:dynamicProperty => [],
:linkUp => true,
:blocked => false,
:vlanIds => [],
:vmDirectPathGen2InactiveReasonNetwork => [],
:vmDirectPathGen2InactiveReasonOther => []
)
check <<-EOS, obj, 'DVPortStatus'
1
false
EOS
end
def test_fault
obj = VIM.LocalizedMethodFault(
:localizedMessage => "The attempted operation cannot be performed in the current state (Powered off).",
:fault => VIM.InvalidPowerState(
:requestedState => 'poweredOn',
:existingState => 'poweredOff',
:faultMessage => []
)
)
check <<-EOS, obj, "LocalizedMethodFault"
poweredOn
poweredOff
The attempted operation cannot be performed in the current state (Powered off).
EOS
end
def test_wait_for_updates
obj = VIM.UpdateSet(
:version => '7',
:dynamicProperty => [],
:filterSet => [
VIM.PropertyFilterUpdate(
:dynamicProperty => [],
:filter => VIM.PropertyFilter(nil, "session[528BA5EB-335B-4AF6-B49C-6160CF5E8D5B]71E3AC7E-7927-4D9E-8BC3-522769F22DAF"),
:missingSet => [],
:objectSet => [
VIM.ObjectUpdate(
:dynamicProperty => [],
:kind => 'enter',
:obj => VIM.VirtualMachine(nil, 'vm-1106'),
:missingSet => [],
:changeSet => [
VIM.PropertyChange(
:dynamicProperty => [],
:name => 'runtime.powerState',
:op => 'assign',
:val => 'poweredOn'
)
]
)
]
)
]
)
check <<-EOS, obj, "UpdateSet"
7
session[528BA5EB-335B-4AF6-B49C-6160CF5E8D5B]71E3AC7E-7927-4D9E-8BC3-522769F22DAF
enter
vm-1106
runtime.powerState
assign
poweredOn
EOS
end
def test_binary
obj = "\x00foo\x01bar\x02baz"
check <<-EOS, obj, 'xsd:base64Binary'
AGZvbwFiYXICYmF6
EOS
end
def test_hba
obj = VIM::HostBlockHba(
:dynamicProperty => [],
:key => 'key-vim.host.BlockHba-vmhba0',
:device => 'vmhba0',
:bus => 0,
:status => 'unknown',
:model => 'Virtual Machine Chipset',
:driver => 'ata_piix',
:pci => '00:07.1')
check <<-EOS, obj, "HostBlockHba"
key-vim.host.BlockHba-vmhba0
vmhba0
0
unknown
Virtual Machine Chipset
ata_piix
00:07.1
EOS
end
=begin
def test_runtime_state
obj = VIM::VirtualMachineDeviceRuntimeInfoVirtualEthernetCardRuntimeState(
:dynamicProperty => [],
vmDirectPathGen2:Active => false,
vmDirectPathGen2:InactiveReasonOther => ["vmNptIncompatibleHost"],
vmDirectPathGen2:InactiveReasonVm => []
)
check <<-EOS, obj, 'VirtualMachineDeviceRuntimeInfoDeviceRuntimeState'
false
vmNptIncompatibleHost
EOS
end
=end
def test_runtime_info
obj = VIM::VirtualMachineRuntimeInfo(
:bootTime => Time.parse('2010-08-20 05:44:35 UTC'),
:connectionState => "connected",
:dynamicProperty => [],
:faultToleranceState => "notConfigured",
:featureMask => [],
:featureRequirement => [],
:host => VIM::HostSystem(nil, "host-32"),
:maxCpuUsage => 5612,
:maxMemoryUsage => 3072,
:memoryOverhead => 128671744,
:numMksConnections => 1,
:offlineFeatureRequirement => [],
:powerState => "poweredOn",
:recordReplayState => "inactive",
:suspendInterval => 0,
:toolsInstallerMounted => false,
:device => []
)
check <<-EOS, obj, 'VirtualMachineRuntimeInfo'
host-32
connected
poweredOn
notConfigured
false
2010-08-20T05:44:35.0Z
0
128671744
5612
3072
1
inactive
EOS
end
def test_keyvalue
obj = ['a', 'b']
check <<-EOS, obj, 'KeyValue'
a
b
EOS
end
def test_boolean
check "1", true, 'xsd:boolean'
check "true", true, 'xsd:boolean'
check "0", false, 'xsd:boolean'
check "false", false, 'xsd:boolean'
end
def test_int
check "5", 5, 'xsd:byte'
check "5", 5, 'xsd:short'
check "5", 5, 'xsd:int'
check "5", 5, 'xsd:long'
end
def test_float
obj = 1.2
check <<-EOS, obj, 'xsd:float'
1.2
EOS
end
def test_date
time_str = '2010-08-20T05:44:35.0Z'
obj = Time.parse(time_str)
check <<-EOS, obj, 'xsd:dateTime'
#{time_str}
EOS
end
def test_array_mangling
obj = ["foo"]
check <<-EOS, obj, 'ArrayOfString'
foo
EOS
time_str = '2010-08-20T05:44:35.0Z'
obj = [Time.parse(time_str)]
check <<-EOS, obj, 'ArrayOfDateTime'
#{time_str}
EOS
obj = [1]
check <<-EOS, obj, 'ArrayOfAnyType'
1
EOS
end
def test_propertypath
check "foo", "foo", 'PropertyPath'
end
def test_methodname
check "foo", "foo", 'MethodName'
end
def test_typename
check "foo", "foo", 'TypeName'
end
def test_new_fields
obj = VIM::HostBlockHba(
:dynamicProperty => [],
:key => 'key-vim.host.BlockHba-vmhba0',
:device => 'vmhba0',
:bus => 0,
:status => 'unknown',
:model => 'Virtual Machine Chipset',
:driver => 'ata_piix',
:pci => '00:07.1')
check <<-EOS, obj, "HostBlockHba"
key-vim.host.BlockHba-vmhba0
vmhba0
0
unknown
bar
Virtual Machine Chipset
ata_piix
00:07.1
EOS
end
end