-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathtest_mockmodel.py
209 lines (185 loc) · 7.06 KB
/
test_mockmodel.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
208
209
#
# Project Kimchi
#
# Copyright IBM Corp, 2013-2017
#
# 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 json
import os
import time
import unittest
import cherrypy
import iso_gen
from wok.exception import InvalidOperation
from wok.plugins.kimchi.osinfo import get_template_default
from tests.utils import patch_auth
from tests.utils import request
from tests.utils import run_server
from tests.utils import wait_task
test_server = None
model = None
fake_iso = None
def setUpModule():
global model, test_server, fake_iso
cherrypy.request.headers = {'Accept': 'application/json'}
patch_auth()
test_server = run_server(test_mode=True)
model = cherrypy.tree.apps['/plugins/kimchi'].root.model
fake_iso = '/tmp/fake.iso'
iso_gen.construct_fake_iso(fake_iso, True, '12.04', 'ubuntu')
def tearDownModule():
test_server.stop()
os.unlink(fake_iso)
class MockModelTests(unittest.TestCase):
def setUp(self):
model.reset()
def test_screenshot_refresh(self):
# Create a VM
req = json.dumps(
{'name': 'test', 'source_media': {'type': 'disk', 'path': fake_iso}}
)
request('/plugins/kimchi/templates', req, 'POST')
req = json.dumps(
{'name': 'test-vm', 'template': '/plugins/kimchi/templates/test'}
)
resp = request('/plugins/kimchi/vms', req, 'POST')
task = json.loads(resp.read().decode('utf-8'))
wait_task(model.task_lookup, task['id'])
# Test screenshot refresh for running vm
request('/plugins/kimchi/vms/test-vm/start', '{}', 'POST')
resp = request('/plugins/kimchi/vms/test-vm/screenshot')
self.assertEqual(200, resp.status)
self.assertEqual('image/png', resp.getheader('content-type'))
resp1 = request('/plugins/kimchi/vms/test-vm')
rspBody = resp1.read().decode('utf-8')
testvm_Data = json.loads(rspBody)
screenshotURL = '/' + testvm_Data['screenshot']
time.sleep(5)
resp2 = request(screenshotURL)
self.assertEqual(200, resp2.status)
self.assertEqual(
resp2.getheader('content-type'), resp.getheader('content-type')
)
self.assertEqual(
resp2.getheader('content-length'), resp.getheader('content-length')
)
self.assertEqual(
resp2.getheader('last-modified'), resp.getheader('last-modified')
)
def test_vm_list_sorted(self):
req = json.dumps(
{'name': 'test', 'source_media': {'type': 'disk', 'path': fake_iso}}
)
request('/plugins/kimchi/templates', req, 'POST')
def add_vm(name):
# Create a VM
req = json.dumps(
{'name': name, 'template': '/plugins/kimchi/templates/test'}
)
task = json.loads(
request('/plugins/kimchi/vms', req,
'POST').read().decode('utf-8')
)
wait_task(model.task_lookup, task['id'])
vms = [u'abc', u'bca', u'cab', u'xba']
for vm in vms:
add_vm(vm)
vms.append(u'test')
self.assertEqual(model.vms_get_list(), sorted(vms))
def test_memory_window_changes(self):
model.templates_create(
{'name': u'test', 'source_media': {'type': 'disk', 'path': fake_iso}}
)
task = model.vms_create(
{'name': u'test-vm', 'template': '/plugins/kimchi/templates/test'}
)
wait_task(model.task_lookup, task['id'])
info = model.device_lookup('pci_0000_1a_00_0')
model.vmhostdevs_update_mmio_guest(u'test-vm', True)
model._attach_device(
u'test-vm', model._get_pci_device_xml(info, 0, False))
def test_hotplug_3D_card(self):
model.templates_create(
{'name': u'test', 'source_media': {'type': 'disk', 'path': fake_iso}}
)
task = model.vms_create(
{'name': u'test-vm', 'template': '/plugins/kimchi/templates/test'}
)
wait_task(model.task_lookup, task['id'])
model.vm_start(u'test-vm')
# attach the 3D cards found to a running guest
all_devices = model.devices_get_list()
for device in all_devices:
device_info = model.device_lookup(device)
if model.device_is_device_3D_controller(device_info):
try:
model.vmhostdevs_create(u'test-vm', {'name': device})
# expect the error: KCHVMHDEV0006E
except InvalidOperation as e:
self.assertEqual(e.message[:14], u'KCHVMHDEV0006E')
def test_vm_info(self):
model.templates_create(
{'name': u'test', 'source_media': {'type': 'disk', 'path': fake_iso}}
)
task = model.vms_create(
{'name': u'test-vm', 'template': '/plugins/kimchi/templates/test'}
)
wait_task(model.task_lookup, task['id'])
vms = model.vms_get_list()
self.assertEqual(2, len(vms))
self.assertIn(u'test-vm', vms)
keys = set(
(
'name',
'state',
'stats',
'uuid',
'memory',
'cpu_info',
'screenshot',
'icon',
'graphics',
'users',
'groups',
'access',
'persistent',
'bootorder',
'bootmenu',
'title',
'description',
'autostart',
)
)
stats_keys = set(
(
'cpu_utilization',
'mem_utilization',
'net_throughput',
'net_throughput_peak',
'io_throughput',
'io_throughput_peak',
)
)
info = model.vm_lookup(u'test-vm')
self.assertEqual(keys, set(info.keys()))
self.assertEqual('shutoff', info['state'])
self.assertEqual('test-vm', info['name'])
self.assertEqual(get_template_default('old', 'memory'), info['memory'])
self.assertEqual(1, info['cpu_info']['vcpus'])
self.assertEqual(1, info['cpu_info']['maxvcpus'])
self.assertEqual('plugins/kimchi/images/icon-vm.png', info['icon'])
self.assertEqual(stats_keys, set(info['stats'].keys()))
self.assertEqual('vnc', info['graphics']['type'])
self.assertEqual('127.0.0.1', info['graphics']['listen'])