-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathtest_vmtemplate.py
207 lines (189 loc) · 7.68 KB
/
test_vmtemplate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#
# Project Kimchi
#
# Copyright IBM Corp, 2013-2016
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import os
import unittest
import uuid
import iso_gen
import psutil
from wok.plugins.kimchi.osinfo import get_template_default
from wok.plugins.kimchi.osinfo import MEM_DEV_SLOTS
from wok.plugins.kimchi.vmtemplate import VMTemplate
from wok.xmlutils.utils import xpath_get_text
DISKS = [
{
'size': 10,
'format': 'raw',
'index': 0,
'pool': {'name': '/plugins/kimchi/storagepools/default-pool'},
},
{
'size': 5,
'format': 'qcow2',
'index': 1,
'pool': {'name': '/plugins/kimchi/storagepools/default-pool'},
},
]
class VMTemplateTests(unittest.TestCase):
def setUp(self):
self.iso = '/tmp/mock.iso'
iso_gen.construct_fake_iso(self.iso, True, '12.04', 'ubuntu')
def tearDown(self):
os.unlink(self.iso)
def test_minimal_construct(self):
disk_bus = get_template_default('old', 'disk_bus')
memory = get_template_default('old', 'memory')
nic_model = get_template_default('old', 'nic_model')
fields = (
('name', 'test'),
('cdrom', self.iso),
('os_distro', 'unknown'),
('os_version', 'unknown'),
('cpu_info', {'vcpus': 1, 'maxvcpus': 1}),
('memory', memory),
('networks', ['default']),
('disk_bus', disk_bus),
('nic_model', nic_model),
('graphics', {'type': 'vnc', 'listen': '127.0.0.1'}),
)
args = {'name': 'test', 'cdrom': self.iso}
t = VMTemplate(args)
for name, val in fields:
if os.uname()[4] == 's390x' and name == 'networks':
continue
self.assertEqual(val, t.info.get(name))
def test_construct_overrides(self):
graphics = {'type': 'spice', 'listen': '127.0.0.1'}
args = {'name': 'test', 'disks': DISKS,
'graphics': graphics, 'cdrom': self.iso}
t = VMTemplate(args)
self.assertEqual(2, len(t.info['disks']))
self.assertEqual(graphics, t.info['graphics'])
def test_specified_graphics(self):
# Test specified listen
graphics = {'type': 'vnc', 'listen': '127.0.0.1'}
args = {'name': 'test', 'disks': DISKS,
'graphics': graphics, 'cdrom': self.iso}
t = VMTemplate(args)
self.assertEqual(graphics, t.info['graphics'])
# Test specified type
graphics = {'type': 'spice', 'listen': '127.0.0.1'}
args['graphics'] = graphics
t = VMTemplate(args)
self.assertEqual(graphics, t.info['graphics'])
# If no listen specified, test the default listen
graphics = {'type': 'vnc'}
args['graphics'] = graphics
t = VMTemplate(args)
self.assertEqual(graphics['type'], t.info['graphics']['type'])
self.assertEqual('127.0.0.1', t.info['graphics']['listen'])
def test_mem_dev_slots(self):
vm_uuid = str(uuid.uuid4()).replace('-', '')
t = VMTemplate(
{
'name': 'test-template',
'cdrom': self.iso,
'memory': {'current': 2048, 'maxmemory': 3072},
}
)
xml = t.to_vm_xml('test-vm', vm_uuid)
expr = '/domain/maxMemory/@slots'
slots = str(MEM_DEV_SLOTS[os.uname()[4]])
self.assertEqual(slots, xpath_get_text(xml, expr)[0])
def test_to_xml(self):
if not os.uname()[4] == 's390x':
graphics = {'type': 'spice', 'listen': '127.0.0.1'}
else:
graphics = {'type': 'vnc', 'listen': '127.0.0.1'}
vm_uuid = str(uuid.uuid4()).replace('-', '')
t = VMTemplate({'name': 'test-template', 'cdrom': self.iso})
xml = t.to_vm_xml('test-vm', vm_uuid, graphics=graphics)
self.assertEqual(vm_uuid, xpath_get_text(xml, '/domain/uuid')[0])
self.assertEqual('test-vm', xpath_get_text(xml, '/domain/name')[0])
if not os.uname()[4] == 's390x':
expr = '/domain/devices/graphics/@type'
self.assertEqual(graphics['type'], xpath_get_text(xml, expr)[0])
expr = '/domain/devices/graphics/@listen'
self.assertEqual(graphics['listen'], xpath_get_text(xml, expr)[0])
expr = '/domain/maxMemory/@slots'
# The default is memory and maxmemory have the same value, so
# max memory tag is not set
self.assertEqual(0, len(xpath_get_text(xml, expr)))
expr = '/domain/memory'
self.assertEqual(str(2048), xpath_get_text(xml, expr)[0])
if hasattr(psutil, 'virtual_memory'):
host_memory = psutil.virtual_memory().total >> 10
else:
host_memory = psutil.TOTAL_PHYMEM >> 10
t = VMTemplate(
{
'name': 'test-template',
'cdrom': self.iso,
'memory': {'current': (host_memory >> 10) - 512},
}
)
try:
xml = t.to_vm_xml('test-vm', vm_uuid, graphics=graphics)
except Exception as e:
# Test current memory greater than maxmemory (1024/default)
self.assertTrue('KCHVM0041E' in str(e))
def test_arg_merging(self):
"""
Make sure that default parameters from osinfo do not override user-
provided parameters.
"""
graphics = {'type': 'vnc', 'listen': '127.0.0.1'}
args = {
'name': 'test',
'os_distro': 'opensuse',
'os_version': '12.3',
'cpu_info': {'vcpus': 2, 'maxvcpus': 4},
'memory': {'current': 2048, 'maxmemory': 3072},
'networks': ['foo'],
'cdrom': self.iso,
'graphics': graphics,
}
t = VMTemplate(args)
self.assertEqual(2, t.info.get('cpu_info', {}).get('vcpus'))
self.assertEqual(4, t.info.get('cpu_info', {}).get('maxvcpus'))
self.assertEqual(2048, t.info.get('memory').get('current'))
self.assertEqual(3072, t.info.get('memory').get('maxmemory'))
self.assertEqual(['foo'], t.info.get('networks'))
self.assertEqual(self.iso, t.info.get('cdrom'))
self.assertEqual(graphics, t.info.get('graphics'))
def test_netboot_vmtemplate(self):
disk_bus = get_template_default('old', 'disk_bus')
memory = get_template_default('old', 'memory')
nic_model = get_template_default('old', 'nic_model')
fields = (
('name', 'test'),
('os_distro', 'unknown'),
('os_version', 'unknown'),
('cpu_info', {'vcpus': 1, 'maxvcpus': 1}),
('memory', memory),
('networks', ['default']),
('disk_bus', disk_bus),
('nic_model', nic_model),
('graphics', {'type': 'vnc', 'listen': '127.0.0.1'}),
)
t = VMTemplate({'name': 'test'}, netboot=True)
for name, val in fields:
if os.uname()[4] == 's390x' and name == 'networks':
continue
self.assertEqual(val, t.info.get(name))
self.assertNotIn('cdrom', t.info.keys())