
From: Daniel Henrique Barboza <danielhb@linux.vnet.ibm.com> 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> --- mockmodel.py | 23 +++++++++++++++++++++++ tests/test_rest.py | 12 ++++++++++++ 2 files changed, 35 insertions(+) diff --git a/mockmodel.py b/mockmodel.py index 9f9de52..2f55af7 100644 --- a/mockmodel.py +++ b/mockmodel.py @@ -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 @@ -107,6 +109,7 @@ class MockModel(Model): # 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) @@ -134,6 +137,21 @@ class MockModel(Model): 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 = {} @@ -386,6 +404,11 @@ class MockModel(Model): 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} diff --git a/tests/test_rest.py b/tests/test_rest.py index 1a43411..26e3930 100644 --- a/tests/test_rest.py +++ b/tests/test_rest.py @@ -348,6 +348,18 @@ class RestTests(unittest.TestCase): 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) -- 2.5.5