Virt Template deploy, and serial xml tests

- Added virt templates to setup
- moved NIC xml string template to file
- added tests for serial xml
This commit is contained in:
Ajith Antony 2013-10-03 13:18:23 -05:00
parent c7ae3ba592
commit b95a5d9fd0
5 changed files with 64 additions and 14 deletions

View file

@ -220,23 +220,23 @@ def _prepare_serial_port_xml(serial_type='pty', telnet_port='', console=True, **
console=console)
def _prepare_nics_xml(interfaces):
'''
Prepares the network interface section of the VM xml
template_str = '''
<interface type='{{ interface.type }}'>
<source {{ interface.type }}='{{ interface.source }}'/>
<mac address='{{ interface.mac }}'/>
<model type='{{ interface.model }}'/>
</interface>\n'''
interfaces: list of dicts as returned from _nic_profile
results = []
Returns string representing interfaces devices suitable for
insertion into the VM XML definition
'''
import jinja2
template = jinja2.Template(template_str)
template_name = 'interface.jinja'
for interface in interfaces:
results.append(template.render(interface=interface))
return ''.join(results)
try:
template = JINJA.get_template(template_name)
except jinja2.exceptions.TemplateNotFound:
log.error('Could not load template {0}'.format(template_name))
return ''
return template.render(interfaces=interfaces)
def _gen_xml(name,
cpu,

View file

@ -0,0 +1,7 @@
{% for interface in interfaces %}
<interface type='{{ interface.type }}'>
<source {{ interface.type }}='{{ interface.source }}'/>
<mac address='{{ interface.mac }}'/>
<model type='{{ interface.model }}'/>
</interface>
{% endfor %}

View file

@ -1,3 +1,4 @@
<serial type='pty'>
<target port='0'/>
</serial>

View file

@ -313,7 +313,11 @@ SETUP_KWARGS = {'name': NAME,
'salt.log.handlers',
'salt.templates',
],
'package_data': {'salt.templates': ['rh_ip/*.jinja']},
'package_data': {'salt.templates': [
'rh_ip/*.jinja',
'virt/*.jinja'
]
},
'data_files': [('share/man/man1',
['doc/man/salt-master.1',
'doc/man/salt-key.1',

View file

@ -32,6 +32,44 @@ virt.__salt__ = {
@skipIf(NO_MOCK, NO_MOCK_REASON)
class VirtTestCase(TestCase):
@skipIf(sys.version_info < (2, 7), 'ElementTree version 1.3 required'
' which comes with Python 2.7')
def test_gen_xml_for_serial(self):
diskp = virt._disk_profile('default', 'kvm')
nicp = virt._nic_profile('default', 'kvm')
xml_data = virt._gen_xml(
'hello',
1,
512,
diskp,
nicp,
'kvm',
serial_type="pty",
console=True
)
root = ElementTree.fromstring(xml_data)
self.assertEquals(root.find('devices/serial').attrib['type'], 'pty')
self.assertEquals(root.find('devices/console').attrib['type'], 'pty')
@skipIf(sys.version_info < (2, 7), 'ElementTree version 1.3 required'
' which comes with Python 2.7')
def test_gen_xml_for_serial_no_console(self):
diskp = virt._disk_profile('default', 'kvm')
nicp = virt._nic_profile('default', 'kvm')
xml_data = virt._gen_xml(
'hello',
1,
512,
diskp,
nicp,
'kvm',
serial_type="pty",
console=False
)
root = ElementTree.fromstring(xml_data)
self.assertEquals(root.find('devices/serial').attrib['type'], 'pty')
self.assertEquals(root.find('devices/console'), None)
def test_default_disk_profile_hypervisor_esxi(self):
mock = MagicMock(return_value={})
with patch.dict(virt.__salt__, {'config.get': mock}):