def test_featuresElement(self): """Testing OvfLibvirt.featuresElement""" self.assertEqual(Ovf.xmlString(OvfLibvirt.featuresElement(True, True, True, True)), '<features><pae/><nonpae/><acpi/><apic/></features>') self.assertEqual(Ovf.xmlString(OvfLibvirt.featuresElement(pae=True)), '<features><pae/></features>') self.assertEqual(Ovf.xmlString(OvfLibvirt.featuresElement(nonpae=True)), '<features><nonpae/></features>') self.assertEqual(Ovf.xmlString(OvfLibvirt.featuresElement(acpi=True)), '<features><acpi/></features>') self.assertEqual(Ovf.xmlString(OvfLibvirt.featuresElement(apic=True)), '<features><apic/></features>')
def test_getPlatformDict(self): """ Test OvfPlatform.getPlatformDict """ ovfFileName = self.path+"/"+self.ovf ovfFile = OvfFile(ovfFileName) # Get the virtual system node in the ovf vsNode = Ovf.getContentEntities(ovfFile.envelope, None, True, False)[0] # gather some values to compare (langCode, encoding) = getlocale() if langCode == None: (langCode, encoding) = getdefaultlocale() # Case with no platform type platformDict = OvfPlatform.getPlatformDict(vsNode) assert platformDict['Kind'] == 'vmx-4', "failed type test" assert platformDict['Locale'] == langCode, "failed locale test" assert platformDict['Timezone'] == timezone, "failed timezone test" # Case with platform type platformDict = OvfPlatform.getPlatformDict(vsNode, "qemu") assert platformDict['Kind'] == 'qemu', "with type: failed type test" assert platformDict['Locale'] == langCode, \ "with type: failed locale test" assert platformDict['Timezone'] == timezone, \ "with type: failed timezone test"
def test_getPropertyDefaultValue(self): """ Test OvfProperty.getPropertyDefaultValue """ ovfFileName = self.path+"/"+self.ovf ovfFile = OvfFile(ovfFileName) # Get all property nodes in the ovf propertyNodes = Ovf.getNodes(ovfFile.envelope, (Ovf.hasTagName, 'Property')) # The first (hostname) property is the one we want to test with testNode = propertyNodes[0] # Test no default specified value = OvfProperty.getPropertyDefaultValue(testNode, None) assert value == None, "failed no default test" # The seventh (httpPort) property is the one we want to test with testNode = propertyNodes[6] # Test default specified as attribute value = OvfProperty.getPropertyDefaultValue(testNode, None) assert value == '80', "failed attribute test" # The ninth (startThreads) property is the one we want to test with testNode = propertyNodes[8] # Test default specified as default configuration value = OvfProperty.getPropertyDefaultValue(testNode, None) assert value == '50', "failed attribute test" # Using the same property node # Test default specified for given configuration value = OvfProperty.getPropertyDefaultValue(testNode, 'Minimal') assert value == '10', "failed config test"
def get_ovf_os_type(ovf_file): oss = ovf_file.document.getElementsByTagName("OperatingSystemSection") if len(oss) == 0: return 'redhat' # default supported linux os_section = oss[0] for e in Ovf.getDict(os_section)['children']: if e['name'] == u'Description': return e['text'] return 'unknown'
def test_libvirtDocument(self): """Testing OvfLibvirt.libvirtDocument""" domain = self.testDocument.createElement('domain') domain.setAttribute('type', 'xen') elem = self.testDocument.createElement('name') elem.appendChild(self.testDocument.createTextNode('test')) domain.appendChild(elem) # no sections testDoc = OvfLibvirt.libvirtDocument(domain) testStr = self.docTag + '<domain type="xen"><name>test</name></domain>' self.assertEqual(Ovf.xmlString(testDoc), testStr) # new section clockSection = self.testDocument.createElement('clock') clockSection.setAttribute('sync','utc') testDoc = OvfLibvirt.libvirtDocument(domain, clockSection) testStr = self.docTag + '<domain type="xen"><name>test</name>' + \ '<clock sync="utc"/></domain>' self.assertEqual(Ovf.xmlString(testDoc), testStr) # replace section domain.appendChild(clockSection) newClockSection = self.testDocument.createElement('clock') newClockSection.setAttribute('sync','localtime') testDoc = OvfLibvirt.libvirtDocument(domain, newClockSection) testStr = self.docTag + '<domain type="xen"><name>test</name>' + \ '<clock sync="localtime"/></domain>' self.assertEqual(Ovf.xmlString(testDoc), testStr) # multiple sections (new and old) newNameSection = self.testDocument.createElement('name') nameNode = self.testDocument.createTextNode('test_completed') newNameSection.appendChild(nameNode) testDoc = OvfLibvirt.libvirtDocument(domain, newNameSection, newClockSection) testStr = self.docTag + '<domain type="xen">' + \ '<name>test_completed</name>' + \ '<clock sync="localtime"/></domain>' self.assertEqual(Ovf.xmlString(testDoc), testStr)
def __init__(self, nodeList, baseMessage=None): """ Create the objects for the class. """ Exception.__init__(self) self.baseMessage = baseMessage self.nodeList = nodeList if baseMessage == None: baseMessage = "More than 1 Node was found please use" + " --node-number flag to specify which node." self.message = baseMessage + Ovf.createTextDescriptionOfNodeList(nodeList)
def _get_ovf_vcpu(ovf_file, bound): """ Retrieves the number of virtual CPUs to be allocated for the virtual machine from the Ovf file. """ vcpu = '' virtual_hardware_node = ovf_file.document.getElementsByTagName("VirtualHardwareSection")[0] rasd = Ovf.getDict(virtual_hardware_node)['children'] for resource in rasd: if ('rasd:ResourceType' in resource and resource['rasd:ResourceType'] == '3'): _bound = resource.get('ovf:bound', 'normal') if _bound == bound: vcpu = resource['rasd:VirtualQuantity'] break return vcpu
def _get_ovf_vcpu(ovf_file, bound): """ Retrieves the number of virtual CPUs to be allocated for the virtual machine from the Ovf file. """ vcpu = '' virtual_hardware_node = ovf_file.document.getElementsByTagName( "VirtualHardwareSection")[0] rasd = Ovf.getDict(virtual_hardware_node)['children'] for resource in rasd: if ('rasd:ResourceType' in resource and resource['rasd:ResourceType'] == '3'): _bound = resource.get('ovf:bound', 'normal') if _bound == bound: vcpu = resource['rasd:VirtualQuantity'] break return vcpu
def _get_ovf_memory_gb(ovf_file, bound): """ Retrieves the amount of memory (GB) to be allocated for the virtual machine from the Ovf file. @note: Implementation adopted from module ovf.Ovf @note: DSP0004 v2.5.0 outlines the Programmatic Unit forms for OVF. This pertains specifically to rasd:AllocationUnits, which accepts both the current and deprecated forms. New implementations should not use Unit Qualifiers as this form is deprecated. - PUnit form, as in "byte * 2^20" - PUnit form w/ Units Qualifier(deprecated), as in "MegaBytes" @param ovf_file: Ovf template configuration file @type ovf_file: OvfFile @param bound: memory resource bound: min, max, normal @type bound: String @return: memory in GB or empty string if no information for the given bound is provided. @rtype: String """ memory = '' virtual_hardware_node = ovf_file.document.getElementsByTagName("VirtualHardwareSection")[0] rasd = Ovf.getDict(virtual_hardware_node)['children'] for resource in rasd: if(resource.has_key('rasd:ResourceType') and resource['rasd:ResourceType'] == '4'): memoryQuantity = resource['rasd:VirtualQuantity'] memoryUnits = resource['rasd:AllocationUnits'] _bound = resource.get('ovf:bound', 'normal') if _bound == bound: if (memoryUnits.startswith('byte') or memoryUnits.startswith('bit')): # Calculate PUnit numerical factor memoryUnits = memoryUnits.replace('^', '**') # Determine PUnit Quantifier DMTF DSP0004, {byte, bit} # Convert to kilobytes memoryUnits = memoryUnits.split(' ', 1) quantifier = memoryUnits[0] if quantifier not in ['bit', 'byte']: raise ValueError("Incompatible PUnit quantifier for memory.") else: memoryUnits[0] = '2**-10' if quantifier is 'byte' else '2**-13' memoryUnits = ' '.join(memoryUnits) memoryFactor = int(eval(memoryUnits, {}, {})) else: if memoryUnits.startswith('Kilo'): memoryFactor = 1024 ** 0 elif memoryUnits.startswith('Mega'): memoryFactor = 1024 ** 1 elif memoryUnits.startswith('Giga'): memoryFactor = 1024 ** 2 else: raise ValueError("Incompatible PUnit quantifier for memory.") if memoryUnits.endswith('Bytes'): memoryFactor *= 1 elif memoryUnits.endswith('Bits'): memoryFactor /= 8.0 else: raise ValueError("Incompatible PUnit quantifier for memory.") memory = str(float(memoryQuantity) * memoryFactor / 1024 ** 2) break return memory
def test_getPropertiesForNode(self): """ Test OvfProperty.getPropertiesForNode """ propertiesNoConfig = [('org.linuxdistx.hostname', None), ('org.linuxdistx.ip', None), ('org.linuxdistx.subnet', None), ('org.linuxdistx.gateway', None), ('org.linuxdistx.dns', None), ('org.linuxdistx.netCoreRmemMaxMB', None), ('org.apache.httpd.httpPort', '80'), ('org.apache.httpd.httpsPort', '443'), ('org.apache.httpd.startThreads', '50'), ('org.apache.httpd.minSpareThreads', '15'), ('org.apache.httpd.maxSpareThreads', '30'), ('org.apache.httpd.maxClients', '256'), ('org.mysql.db.queryCacheSizeMB', '32'), ('org.mysql.db.maxConnections', '500'), ('org.mysql.db.waitTimeout', '100'), ('org.mysql.db.waitTimeout', '100'), ('net.php.sessionTimeout', '5'), ('net.php.concurrentSessions', '500'), ('net.php.memoryLimit', '32')] propertiesMinConfig = [('org.linuxdistx.hostname', None), ('org.linuxdistx.ip', None), ('org.linuxdistx.subnet', None), ('org.linuxdistx.gateway', None), ('org.linuxdistx.dns', None), ('org.linuxdistx.netCoreRmemMaxMB', None), ('org.apache.httpd.httpPort', '80'), ('org.apache.httpd.httpsPort', '443'), ('org.apache.httpd.startThreads', '10'), ('org.apache.httpd.minSpareThreads', '5'), ('org.apache.httpd.maxSpareThreads', '15'), ('org.apache.httpd.maxClients', '128'), ('org.mysql.db.queryCacheSizeMB', '32'), ('org.mysql.db.maxConnections', '500'), ('org.mysql.db.waitTimeout', '100'), ('org.mysql.db.waitTimeout', '100'), ('net.php.sessionTimeout', '5'), ('net.php.concurrentSessions', '500'), ('net.php.memoryLimit', '32')] propertiesMaxConfig = [('org.linuxdistx.hostname', None), ('org.linuxdistx.ip', None), ('org.linuxdistx.subnet', None), ('org.linuxdistx.gateway', None), ('org.linuxdistx.dns', None), ('org.linuxdistx.netCoreRmemMaxMB', None), ('org.apache.httpd.httpPort', '80'), ('org.apache.httpd.httpsPort', '443'), ('org.apache.httpd.startThreads', '100'), ('org.apache.httpd.minSpareThreads', '25'), ('org.apache.httpd.maxSpareThreads', '45'), ('org.apache.httpd.maxClients', '512'), ('org.mysql.db.queryCacheSizeMB', '32'), ('org.mysql.db.maxConnections', '500'), ('org.mysql.db.waitTimeout', '100'), ('org.mysql.db.waitTimeout', '100'), ('net.php.sessionTimeout', '5'), ('net.php.concurrentSessions', '500'), ('net.php.memoryLimit', '32')] ovfFileName = self.path + "/" + self.ovf ovfFile = OvfFile(ovfFileName) # Get the virtual system from the ovf vsNodes = Ovf.getNodes(ovfFile.envelope, (Ovf.hasTagName, 'VirtualSystem'), (Ovf.hasAttribute, 'ovf:id', 'MyLampService')) vsNode = vsNodes[0] # Get the environment for the node properties = OvfProperty.getPropertiesForNode(vsNode, None) # validate the returned data propOffset = 0 for (key, node, value) in properties: assert key == propertiesNoConfig[propOffset][0], "key mismatch" assert node != None, "failed with invalid property node" assert value == propertiesNoConfig[propOffset][1], "value mismatch" propOffset = propOffset + 1 # Get the environment for the node with configuration properties = OvfProperty.getPropertiesForNode(vsNode, 'Minimal') # validate the returned data propOffset = 0 for (key, node, value) in properties: assert key == propertiesMinConfig[propOffset][0], "key mismatch" assert value == propertiesMinConfig[propOffset][1], "value mismatch" propOffset = propOffset + 1 # Get the environment for the node with configuration properties = OvfProperty.getPropertiesForNode(vsNode, 'Maximum') # validate the returned data propOffset = 0 for (key, node, value) in properties: assert key == propertiesMaxConfig[propOffset][0], "key mismatch" assert value == propertiesMaxConfig[propOffset][1], "value mismatch" propOffset = propOffset + 1
def test_uuidElement(self): """Testing OvfLibvirt.uuidElement""" testStr = '4dea22b31d52d8f32516782e98ab3fa0' self.assertEqual(Ovf.xmlString(OvfLibvirt.uuidElement(testStr)), '<uuid>' + testStr + '</uuid>')
def test_domainElement(self): """Testing OvfLibvirt.domainElement""" testElem = OvfLibvirt.domainElement('kqemu') self.assertEqual(Ovf.xmlString(testElem), '<domain type="kqemu"/>')
def test_nameElement(self): """Testing OvfLibvirt.nameElement""" self.assertEqual(Ovf.xmlString(OvfLibvirt.nameElement('test')), '<name>test</name>')
def test_graphicsElement(self): """Testing OvfLibvirt.graphicsElement""" self.assertEqual(Ovf.xmlString(OvfLibvirt.graphicsElement('vnc', '192.168.1.1', '6522')), '<graphics listen="192.168.1.1" port="6522" type="vnc"/>') self.assertEqual(Ovf.xmlString(OvfLibvirt.graphicsElement('sdl')), '<graphics type="sdl"/>')
def test_memoryElement(self): """Testing OvfLibvirt.memoryElement""" self.assertEqual(Ovf.xmlString(OvfLibvirt.memoryElement('262144')), '<memory>262144</memory>')
def test_emulatorElement(self): """Testing OvfLibvirt.emulatorElement""" testElem = OvfLibvirt.emulatorElement('/usr/bin/qemu') self.assertEqual(Ovf.xmlString(testElem), '<emulator>/usr/bin/qemu</emulator>')
def test_inputElement(self): """Testing OvfLibvirt.inputElement""" self.assertEqual(Ovf.xmlString(OvfLibvirt.inputElement('mouse', 'usb')), '<input bus="usb" type="mouse"/>') self.assertEqual(Ovf.xmlString(OvfLibvirt.inputElement('tablet')), '<input type="tablet"/>')
def test_currentMemoryElement(self): """Testing OvfLibvirt.currentMemoryElement""" self.assertEqual(Ovf.xmlString(OvfLibvirt.currentMemoryElement('262144')), '<currentMemory>262144</currentMemory>')
def test_vcpuElement(self): """Testing OvfLibvirt.vcpuElement""" self.assertEqual(Ovf.xmlString(OvfLibvirt.vcpuElement('1')), '<vcpu>1</vcpu>')
def test_onCrashElement(self): """Testing OvfLibvirt.onCrashElement""" self.assertEqual(Ovf.xmlString(OvfLibvirt.onCrashElement('restart')), '<on_crash>restart</on_crash>')
def test_onRebootElement(self): """Testing OvfLibvirt.onRebootElement""" self.assertEqual(Ovf.xmlString(OvfLibvirt.onRebootElement('restart')), '<on_reboot>restart</on_reboot>')
def test_onPowerOffElement(self): """Testing OvfLibvirt.onPowerOffElement""" self.assertEqual(Ovf.xmlString(OvfLibvirt.onPowerOffElement('destroy')), '<on_poweroff>destroy</on_poweroff>')
def test_clockElement(self): """Testing OvfLibvirt.clockElement""" self.assertEqual(Ovf.xmlString(OvfLibvirt.clockElement('localtime')), '<clock sync="localtime"/>')
def _get_ovf_memory_gb(ovf_file, bound): """ Retrieves the amount of memory (GB) to be allocated for the virtual machine from the Ovf file. @note: Implementation adopted from module ovf.Ovf @note: DSP0004 v2.5.0 outlines the Programmatic Unit forms for OVF. This pertains specifically to rasd:AllocationUnits, which accepts both the current and deprecated forms. New implementations should not use Unit Qualifiers as this form is deprecated. - PUnit form, as in "byte * 2^20" - PUnit form w/ Units Qualifier(deprecated), as in "MegaBytes" @param ovf_file: Ovf template configuration file @type ovf_file: OvfFile @param bound: memory resource bound: min, max, normal @type bound: String @return: memory in GB or empty string if no information for the given bound is provided. @rtype: String """ memory = '' virtual_hardware_node = ovf_file.document.getElementsByTagName( "VirtualHardwareSection")[0] rasd = Ovf.getDict(virtual_hardware_node)['children'] for resource in rasd: if (resource.has_key('rasd:ResourceType') and resource['rasd:ResourceType'] == '4'): memoryQuantity = resource['rasd:VirtualQuantity'] memoryUnits = resource['rasd:AllocationUnits'] _bound = resource.get('ovf:bound', 'normal') if _bound == bound: if (memoryUnits.startswith('byte') or memoryUnits.startswith('bit')): # Calculate PUnit numerical factor memoryUnits = memoryUnits.replace('^', '**') # Determine PUnit Quantifier DMTF DSP0004, {byte, bit} # Convert to kilobytes memoryUnits = memoryUnits.split(' ', 1) quantifier = memoryUnits[0] if quantifier not in ['bit', 'byte']: raise ValueError( "Incompatible PUnit quantifier for memory.") else: memoryUnits[ 0] = '2**-10' if quantifier is 'byte' else '2**-13' memoryUnits = ' '.join(memoryUnits) memoryFactor = int(eval(memoryUnits, {}, {})) else: if memoryUnits.startswith('Kilo'): memoryFactor = 1024**0 elif memoryUnits.startswith('Mega'): memoryFactor = 1024**1 elif memoryUnits.startswith('Giga'): memoryFactor = 1024**2 else: raise ValueError( "Incompatible PUnit quantifier for memory.") if memoryUnits.endswith('Bytes'): memoryFactor *= 1 elif memoryUnits.endswith('Bits'): memoryFactor /= 8.0 else: raise ValueError( "Incompatible PUnit quantifier for memory.") memory = str(float(memoryQuantity) * memoryFactor / 1024**2) break return memory