Skip to content

Commit

Permalink
Virt-Viewer launcher: mockmodel changes
Browse files Browse the repository at this point in the history
This patch adds mockmodel.py and tests/test_rest.py changes
for the virtviewerfile API.

Due to how the control is implemented (internal_redirect),
the solution adopted was to create a temporary file to
represent the Virt Viewer script contents in mockmodel.py,
while in test_rest.py an assert is made to see if the
content was retrieved by the API.

This temporary file is erased in a cleanup method that is
subscribed in the cherrypy 'exit' engine, leaving no
trace in the host.

Signed-off-by: Daniel Henrique Barboza <danielhb@linux.vnet.ibm.com>
  • Loading branch information
danielhb committed Jul 27, 2016
1 parent ec34b6e commit 2fd0d58
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
23 changes: 23 additions & 0 deletions mockmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
# 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 cherrypy
import libvirt
import lxml.etree as ET
import os
import tempfile
import time

from collections import defaultdict
Expand Down Expand Up @@ -107,6 +109,7 @@ def __init__(self, objstore_loc=None):
# and BaseModel
# Because that a normal method override will not work here
# Instead of that we also need to do the override on runtime

for method in dir(self):
if method.startswith('_mock_'):
mock_method = getattr(self, method)
Expand Down Expand Up @@ -134,6 +137,21 @@ def __init__(self, objstore_loc=None):
VMHostDevsModel._get_pci_device_xml = self._get_pci_device_xml
VMHostDevsModel._validate_pci_passthrough_env = self._validate_pci

self._create_virt_viewer_tmp_file()
cherrypy.engine.subscribe('exit', self.virtviewertmpfile_cleanup)

def _create_virt_viewer_tmp_file(self):
self.virtviewerfile_tmp = tempfile.NamedTemporaryFile(
dir='../data/virtviewerfiles/',
delete=False
)
file_content = "[virt-viewer]\ntype=vnc\nhost=127.0.0.1\nport=5999\n"
self.virtviewerfile_tmp.write(file_content)
self.virtviewerfile_tmp.close()

def virtviewertmpfile_cleanup(self):
os.unlink(self.virtviewerfile_tmp.name)

def reset(self):
MockModel._mock_vms = defaultdict(list)
MockModel._mock_snapshots = {}
Expand Down Expand Up @@ -386,6 +404,11 @@ def _mock_vm_clone(self, name):
MockModel._mock_snapshots[new_name] = snapshots
return self._model_vm_clone(name)

def _mock_vmvirtviewerfile_lookup(self, vm_name):
file_name = 'plugins/kimchi/data/virtviewerfiles/%s' %\
os.path.basename(self.virtviewerfile_tmp.name)
return file_name

def _mock_vmsnapshots_create(self, vm_name, params):
name = params.get('name', unicode(int(time.time())))
params = {'vm_name': vm_name, 'name': name}
Expand Down
12 changes: 12 additions & 0 deletions tests/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,18 @@ def test_vm_lifecycle(self):
self.assertEquals(200, resp.status)
self.assertTrue(resp.getheader('Content-type').startswith('image'))

# Test Virt Viewer file
resp = self.request(
'/plugins/kimchi/vms/test-vm/virtviewerfile',
'{}',
'GET')
self.assertEquals(200, resp.status)
vvfilecontent = resp.read()
self.assertEqual(
vvfilecontent,
"[virt-viewer]\ntype=vnc\nhost=127.0.0.1\nport=5999\n"
)

# Clone a running VM
resp = self.request('/plugins/kimchi/vms/test-vm/clone', '{}', 'POST')
self.assertEquals(400, resp.status)
Expand Down

0 comments on commit 2fd0d58

Please sign in to comment.