core-admin/tests/basic.py

245 lines
10 KiB
Python
Raw Normal View History

2014-10-02 10:56:04 +02:00
#!/usr/bin/python
2015-02-05 15:46:40 +01:00
# vim: fileencoding=utf-8
2014-10-02 10:56:04 +02:00
#
2015-02-05 15:46:40 +01:00
# The Qubes OS Project, https://www.qubes-os.org/
2014-10-02 10:56:04 +02:00
#
2015-02-05 15:46:40 +01:00
# Copyright (C) 2014-2015
# Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
# Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
2014-10-02 10:56:04 +02:00
#
2015-02-05 15:46:40 +01:00
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
2014-10-02 10:56:04 +02:00
# This program 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 General Public License for more details.
2015-02-05 15:46:40 +01:00
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2014-10-02 10:56:04 +02:00
#
2015-02-05 15:46:40 +01:00
import multiprocessing
2014-10-02 10:56:04 +02:00
import os
import shutil
import unittest
import time
2014-10-02 10:56:04 +02:00
from qubes.qubes import QubesVmCollection, QubesException, system_path
2015-02-05 15:46:40 +01:00
import qubes.qubes
import qubes.tests
2015-07-08 04:26:11 +02:00
from qubes.qubes import QubesVmLabels
2014-10-02 10:56:04 +02:00
class TC_00_Basic(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase):
2015-02-05 15:46:40 +01:00
def test_000_create(self):
vmname = self.make_vm_name('appvm')
vm = self.qc.add_new_vm('QubesAppVm',
name=vmname, template=self.qc.get_default_template())
2014-10-02 10:56:04 +02:00
self.assertIsNotNone(vm)
self.assertEqual(vm.name, vmname)
self.assertEqual(vm.template, self.qc.get_default_template())
vm.create_on_disk(verbose=False)
2015-02-05 15:46:40 +01:00
with self.assertNotRaises(qubes.qubes.QubesException):
2014-10-02 10:56:04 +02:00
vm.verify_files()
class TC_01_Properties(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase):
2014-10-02 10:56:04 +02:00
def setUp(self):
2015-02-05 15:46:40 +01:00
super(TC_01_Properties, self).setUp()
self.vmname = self.make_vm_name('appvm')
self.vm = self.qc.add_new_vm('QubesAppVm',
name=self.vmname, template=self.qc.get_default_template())
2014-10-02 10:56:04 +02:00
self.vm.create_on_disk(verbose=False)
def save_and_reload_db(self):
super(TC_01_Properties, self).save_and_reload_db()
if hasattr(self, 'vm'):
self.vm = self.qc.get(self.vm.qid, None)
if hasattr(self, 'netvm'):
self.netvm = self.qc.get(self.netvm.qid, None)
2015-02-05 15:46:40 +01:00
def test_000_rename(self):
newname = self.make_vm_name('newname')
2014-10-02 10:56:04 +02:00
self.assertEqual(self.vm.name, self.vmname)
2015-02-05 15:46:40 +01:00
2014-10-02 10:56:04 +02:00
#TODO: change to setting property when implemented
self.vm.set_name(newname)
self.assertEqual(self.vm.name, newname)
self.assertEqual(self.vm.dir_path,
2015-02-05 15:46:40 +01:00
os.path.join(system_path['qubes_appvms_dir'], newname))
2014-10-02 10:56:04 +02:00
self.assertEqual(self.vm.conf_file,
2015-02-05 15:46:40 +01:00
os.path.join(self.vm.dir_path, newname + '.conf'))
2014-10-02 10:56:04 +02:00
self.assertTrue(os.path.exists(
os.path.join(self.vm.dir_path, "apps", newname + "-vm.directory")))
# FIXME: set whitelisted-appmenus.list first
self.assertTrue(os.path.exists(
os.path.join(self.vm.dir_path, "apps", newname + "-firefox.desktop")))
self.assertTrue(os.path.exists(
os.path.join(os.getenv("HOME"), ".local/share/desktop-directories",
2015-02-05 15:46:40 +01:00
newname + "-vm.directory")))
2014-10-02 10:56:04 +02:00
self.assertTrue(os.path.exists(
os.path.join(os.getenv("HOME"), ".local/share/applications",
2015-02-05 15:46:40 +01:00
newname + "-firefox.desktop")))
2014-10-02 10:56:04 +02:00
self.assertFalse(os.path.exists(
os.path.join(os.getenv("HOME"), ".local/share/desktop-directories",
2015-02-05 15:46:40 +01:00
self.vmname + "-vm.directory")))
2014-10-02 10:56:04 +02:00
self.assertFalse(os.path.exists(
os.path.join(os.getenv("HOME"), ".local/share/applications",
2015-02-05 15:46:40 +01:00
self.vmname + "-firefox.desktop")))
def test_010_netvm(self):
if self.qc.get_default_netvm() is None:
self.skip("Set default NetVM before running this test")
self.netvm = self.qc.add_new_vm("QubesNetVm",
name=self.make_vm_name('netvm'),
template=self.qc.get_default_template())
self.netvm.create_on_disk(verbose=False)
# TODO: remove this line after switching to core3
self.save_and_reload_db()
self.assertEquals(self.vm.netvm, self.qc.get_default_netvm())
self.vm.uses_default_netvm = False
self.vm.netvm = None
self.assertIsNone(self.vm.netvm)
self.save_and_reload_db()
self.assertIsNone(self.vm.netvm)
self.vm.netvm = self.qc[self.netvm.qid]
self.assertEquals(self.vm.netvm.qid, self.netvm.qid)
self.save_and_reload_db()
self.assertEquals(self.vm.netvm.qid, self.netvm.qid)
self.vm.uses_default_netvm = True
# TODO: uncomment when properly implemented
# self.assertEquals(self.vm.netvm.qid, self.qc.get_default_netvm().qid)
self.save_and_reload_db()
self.assertEquals(self.vm.netvm.qid, self.qc.get_default_netvm().qid)
with self.assertRaises(ValueError):
self.vm.netvm = self.vm
def test_020_dispvm_netvm(self):
if self.qc.get_default_netvm() is None:
self.skip("Set default NetVM before running this test")
self.netvm = self.qc.add_new_vm("QubesNetVm",
name=self.make_vm_name('netvm'),
template=self.qc.get_default_template())
self.netvm.create_on_disk(verbose=False)
self.assertEquals(self.vm.netvm, self.vm.dispvm_netvm)
self.vm.uses_default_dispvm_netvm = False
self.vm.dispvm_netvm = None
self.assertIsNone(self.vm.dispvm_netvm)
self.save_and_reload_db()
self.assertIsNone(self.vm.dispvm_netvm)
self.vm.dispvm_netvm = self.netvm
self.assertEquals(self.vm.dispvm_netvm, self.netvm)
self.save_and_reload_db()
self.assertEquals(self.vm.dispvm_netvm, self.netvm)
self.vm.uses_default_dispvm_netvm = True
self.assertEquals(self.vm.dispvm_netvm, self.vm.netvm)
self.save_and_reload_db()
self.assertEquals(self.vm.dispvm_netvm, self.vm.netvm)
with self.assertRaises(ValueError):
self.vm.dispvm_netvm = self.vm
2015-07-08 04:26:11 +02:00
def test_030_clone(self):
testvm1 = self.qc.add_new_vm(
"QubesAppVm",
name=self.make_vm_name("vm"),
template=self.qc.get_default_template())
testvm1.create_on_disk(verbose=False)
testvm2 = self.qc.add_new_vm(testvm1.__class__.__name__,
name=self.make_vm_name("clone"),
template=testvm1.template,
)
testvm2.clone_attrs(src_vm=testvm1)
testvm2.clone_disk_files(src_vm=testvm1, verbose=False)
# qubes.xml reload
self.save_and_reload_db()
testvm1 = self.qc[testvm1.qid]
testvm2 = self.qc[testvm2.qid]
self.assertEquals(testvm1.label, testvm2.label)
self.assertEquals(testvm1.netvm, testvm2.netvm)
self.assertEquals(testvm1.uses_default_netvm,
testvm2.uses_default_netvm)
self.assertEquals(testvm1.kernel, testvm2.kernel)
self.assertEquals(testvm1.kernelopts, testvm2.kernelopts)
self.assertEquals(testvm1.uses_default_kernel,
testvm2.uses_default_kernel)
self.assertEquals(testvm1.uses_default_kernelopts,
testvm2.uses_default_kernelopts)
self.assertEquals(testvm1.memory, testvm2.memory)
self.assertEquals(testvm1.maxmem, testvm2.maxmem)
self.assertEquals(testvm1.pcidevs, testvm2.pcidevs)
self.assertEquals(testvm1.include_in_backups,
testvm2.include_in_backups)
self.assertEquals(testvm1.default_user, testvm2.default_user)
self.assertEquals(testvm1.services, testvm2.services)
self.assertEquals(testvm1.get_firewall_conf(),
testvm2.get_firewall_conf())
# now some non-default values
testvm1.netvm = None
testvm1.uses_default_netvm = False
testvm1.label = QubesVmLabels['orange']
testvm1.memory = 512
firewall = testvm1.get_firewall_conf()
firewall['allowDns'] = False
firewall['allowYumProxy'] = False
firewall['rules'] = [{'address': '1.2.3.4',
'netmask': 24,
'proto': 'tcp',
'portBegin': 22,
'portEnd': 22,
}]
testvm1.write_firewall_conf(firewall)
testvm3 = self.qc.add_new_vm(testvm1.__class__.__name__,
name=self.make_vm_name("clone2"),
template=testvm1.template,
)
testvm3.clone_attrs(src_vm=testvm1)
testvm3.clone_disk_files(src_vm=testvm1, verbose=False)
# qubes.xml reload
self.save_and_reload_db()
testvm1 = self.qc[testvm1.qid]
testvm3 = self.qc[testvm3.qid]
self.assertEquals(testvm1.label, testvm3.label)
self.assertEquals(testvm1.netvm, testvm3.netvm)
self.assertEquals(testvm1.uses_default_netvm,
testvm3.uses_default_netvm)
self.assertEquals(testvm1.kernel, testvm3.kernel)
self.assertEquals(testvm1.kernelopts, testvm3.kernelopts)
self.assertEquals(testvm1.uses_default_kernel,
testvm3.uses_default_kernel)
self.assertEquals(testvm1.uses_default_kernelopts,
testvm3.uses_default_kernelopts)
self.assertEquals(testvm1.memory, testvm3.memory)
self.assertEquals(testvm1.maxmem, testvm3.maxmem)
self.assertEquals(testvm1.pcidevs, testvm3.pcidevs)
self.assertEquals(testvm1.include_in_backups,
testvm3.include_in_backups)
self.assertEquals(testvm1.default_user, testvm3.default_user)
self.assertEquals(testvm1.services, testvm3.services)
self.assertEquals(testvm1.get_firewall_conf(),
testvm3.get_firewall_conf())
2015-02-05 15:46:40 +01:00
# vim: ts=4 sw=4 et