2017-01-18 22:16:46 +01:00
|
|
|
#
|
2015-11-13 22:27:54 +01:00
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2015 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
|
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# 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.
|
2015-11-13 22:27:54 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2015-11-13 22:27:54 +01:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2017-10-12 00:11:50 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2015-11-13 22:27:54 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
|
2017-01-18 22:16:46 +01:00
|
|
|
#
|
2015-11-13 22:27:54 +01:00
|
|
|
|
2016-07-12 17:53:31 +02:00
|
|
|
''' Tests for the file storage backend '''
|
|
|
|
|
2015-11-13 22:27:54 +01:00
|
|
|
import os
|
|
|
|
import shutil
|
2016-04-15 20:11:02 +02:00
|
|
|
|
2017-04-25 23:01:17 +02:00
|
|
|
import asyncio
|
2017-07-25 05:44:10 +02:00
|
|
|
import unittest.mock
|
2017-04-25 23:01:17 +02:00
|
|
|
|
2015-11-13 22:27:54 +01:00
|
|
|
import qubes.storage
|
2016-02-11 05:41:42 +01:00
|
|
|
import qubes.tests.storage
|
2016-04-15 20:11:02 +02:00
|
|
|
from qubes.config import defaults
|
|
|
|
|
|
|
|
# :pylint: disable=invalid-name
|
2015-11-13 22:27:54 +01:00
|
|
|
|
2016-07-12 17:53:31 +02:00
|
|
|
|
2016-06-16 21:09:48 +02:00
|
|
|
class TestApp(qubes.Qubes):
|
2016-07-12 17:53:31 +02:00
|
|
|
''' A Mock App object '''
|
|
|
|
def __init__(self, *args, **kwargs): # pylint: disable=unused-argument
|
|
|
|
super(TestApp, self).__init__('/tmp/qubes-test.xml', load=False,
|
|
|
|
offline_mode=True, **kwargs)
|
2016-06-16 21:09:48 +02:00
|
|
|
self.load_initial_values()
|
|
|
|
self.pools['linux-kernel'].dir_path = '/tmp/qubes-test-kernel'
|
2016-07-12 17:53:31 +02:00
|
|
|
dummy_kernel = os.path.join(self.pools['linux-kernel'].dir_path,
|
|
|
|
'dummy')
|
2017-09-27 03:24:57 +02:00
|
|
|
os.makedirs(dummy_kernel, exist_ok=True)
|
2016-06-16 21:09:48 +02:00
|
|
|
open(os.path.join(dummy_kernel, 'vmlinuz'), 'w').close()
|
|
|
|
open(os.path.join(dummy_kernel, 'modules.img'), 'w').close()
|
|
|
|
open(os.path.join(dummy_kernel, 'initramfs'), 'w').close()
|
|
|
|
self.default_kernel = 'dummy'
|
2016-04-15 20:11:02 +02:00
|
|
|
|
2016-06-16 21:09:48 +02:00
|
|
|
def cleanup(self):
|
2016-07-12 17:53:31 +02:00
|
|
|
''' Remove temporary directories '''
|
2016-06-16 21:09:48 +02:00
|
|
|
shutil.rmtree(self.pools['linux-kernel'].dir_path)
|
2017-07-25 05:44:10 +02:00
|
|
|
if os.path.exists(self.store):
|
|
|
|
os.unlink(self.store)
|
2016-06-16 21:09:48 +02:00
|
|
|
|
|
|
|
def create_dummy_template(self):
|
2016-07-12 17:53:31 +02:00
|
|
|
''' Initalizes a dummy TemplateVM as the `default_template` '''
|
|
|
|
template = self.add_new_vm(qubes.vm.templatevm.TemplateVM,
|
|
|
|
name='test-template', label='red',
|
|
|
|
memory=1024, maxmem=1024)
|
|
|
|
self.default_template = template
|
|
|
|
|
2016-06-16 21:09:48 +02:00
|
|
|
|
2016-07-12 17:53:31 +02:00
|
|
|
class TC_00_FilePool(qubes.tests.QubesTestCase):
|
|
|
|
""" This class tests some properties of the 'default' pool.
|
|
|
|
|
|
|
|
This test might become obsolete if we change the driver for the default
|
|
|
|
pool to something else as 'file'.
|
|
|
|
"""
|
2015-11-13 22:27:54 +01:00
|
|
|
|
2016-06-16 21:09:48 +02:00
|
|
|
def setUp(self):
|
|
|
|
super(TC_00_FilePool, self).setUp()
|
|
|
|
self.app = TestApp()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.app.cleanup()
|
2017-09-19 17:01:29 +02:00
|
|
|
self.app.close()
|
|
|
|
del self.app
|
2016-06-16 21:09:48 +02:00
|
|
|
super(TC_00_FilePool, self).tearDown()
|
|
|
|
|
2015-11-13 22:27:54 +01:00
|
|
|
def test000_default_pool_dir(self):
|
|
|
|
""" The predefined dir for the default pool should be ``/var/lib/qubes``
|
|
|
|
|
|
|
|
.. sealso::
|
|
|
|
Data :data:``qubes.qubes.defaults['pool_config']``.
|
|
|
|
"""
|
2017-07-25 05:23:17 +02:00
|
|
|
result = self.app.get_pool("varlibqubes").dir_path
|
2016-02-11 05:41:42 +01:00
|
|
|
expected = '/var/lib/qubes'
|
2017-05-29 16:42:40 +02:00
|
|
|
self.assertEqual(result, expected)
|
2015-11-13 22:27:54 +01:00
|
|
|
|
|
|
|
def test001_default_storage_class(self):
|
2016-07-12 17:53:31 +02:00
|
|
|
""" Check when using default pool the Storage is
|
|
|
|
``qubes.storage.Storage``. """
|
2015-11-13 22:27:54 +01:00
|
|
|
result = self._init_app_vm().storage
|
2016-07-12 17:53:31 +02:00
|
|
|
self.assertIsInstance(result, qubes.storage.Storage)
|
2015-11-13 22:27:54 +01:00
|
|
|
|
|
|
|
def _init_app_vm(self):
|
|
|
|
""" Return initalised, but not created, AppVm. """
|
|
|
|
vmname = self.make_vm_name('appvm')
|
2016-06-16 21:09:48 +02:00
|
|
|
self.app.create_dummy_template()
|
2016-07-12 17:53:31 +02:00
|
|
|
return self.app.add_new_vm(qubes.vm.appvm.AppVM, name=vmname,
|
2016-04-15 20:11:02 +02:00
|
|
|
template=self.app.default_template,
|
|
|
|
label='red')
|
|
|
|
|
|
|
|
|
2016-07-12 17:53:31 +02:00
|
|
|
class TC_01_FileVolumes(qubes.tests.QubesTestCase):
|
|
|
|
''' Test correct handling of different types of volumes '''
|
|
|
|
|
2016-06-16 21:09:48 +02:00
|
|
|
POOL_DIR = '/tmp/test-pool'
|
2016-04-15 20:11:02 +02:00
|
|
|
POOL_NAME = 'test-pool'
|
2016-04-30 20:42:46 +02:00
|
|
|
POOL_CONF = {'driver': 'file', 'dir_path': POOL_DIR, 'name': POOL_NAME}
|
2016-04-15 20:11:02 +02:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
""" Add a test file based storage pool """
|
2016-04-30 20:42:46 +02:00
|
|
|
super(TC_01_FileVolumes, self).setUp()
|
2016-06-16 21:09:48 +02:00
|
|
|
self.app = TestApp()
|
2016-04-15 20:11:02 +02:00
|
|
|
self.app.add_pool(**self.POOL_CONF)
|
2017-09-27 03:24:57 +02:00
|
|
|
self.app.default_pool = self.app.get_pool(self.POOL_NAME)
|
|
|
|
self.app.create_dummy_template()
|
2016-04-15 20:11:02 +02:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
""" Remove the file based storage pool after testing """
|
|
|
|
self.app.remove_pool("test-pool")
|
2016-06-16 21:09:48 +02:00
|
|
|
self.app.cleanup()
|
2017-09-19 17:01:29 +02:00
|
|
|
self.app.close()
|
|
|
|
del self.app
|
2016-04-30 20:42:46 +02:00
|
|
|
super(TC_01_FileVolumes, self).tearDown()
|
2016-04-15 20:11:02 +02:00
|
|
|
shutil.rmtree(self.POOL_DIR, ignore_errors=True)
|
|
|
|
|
|
|
|
def test_000_origin_volume(self):
|
|
|
|
config = {
|
|
|
|
'name': 'root',
|
|
|
|
'pool': self.POOL_NAME,
|
2016-07-12 17:53:31 +02:00
|
|
|
'save_on_stop': True,
|
|
|
|
'rw': True,
|
2016-04-15 20:11:02 +02:00
|
|
|
'size': defaults['root_img_size'],
|
|
|
|
}
|
2016-07-12 17:53:31 +02:00
|
|
|
vm = qubes.tests.storage.TestVM(self)
|
|
|
|
volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
|
|
|
|
self.assertEqual(volume.name, 'root')
|
|
|
|
self.assertEqual(volume.pool, self.POOL_NAME)
|
|
|
|
self.assertEqual(volume.size, defaults['root_img_size'])
|
|
|
|
self.assertFalse(volume.snap_on_start)
|
|
|
|
self.assertTrue(volume.save_on_stop)
|
|
|
|
self.assertTrue(volume.rw)
|
2017-07-01 22:45:22 +02:00
|
|
|
block = volume.block_device()
|
|
|
|
self.assertEqual(block.path,
|
|
|
|
'{base}.img:{base}-cow.img'.format(
|
|
|
|
base=self.POOL_DIR + '/appvms/' + vm.name + '/root'))
|
|
|
|
self.assertEqual(block.script, 'block-origin')
|
|
|
|
self.assertEqual(block.rw, True)
|
|
|
|
self.assertEqual(block.name, 'root')
|
|
|
|
self.assertEqual(block.devtype, 'disk')
|
|
|
|
self.assertIsNone(block.domain)
|
2016-04-15 20:11:02 +02:00
|
|
|
|
|
|
|
def test_001_snapshot_volume(self):
|
2017-06-09 04:46:46 +02:00
|
|
|
template_vm = self.app.default_template
|
|
|
|
vm = qubes.tests.storage.TestVM(self, template=template_vm)
|
|
|
|
|
2016-04-15 20:11:02 +02:00
|
|
|
original_size = qubes.config.defaults['root_img_size']
|
2017-06-09 04:46:46 +02:00
|
|
|
source_config = {
|
|
|
|
'name': 'root',
|
|
|
|
'pool': self.POOL_NAME,
|
|
|
|
'save_on_stop': True,
|
|
|
|
'rw': False,
|
|
|
|
'size': original_size,
|
|
|
|
}
|
|
|
|
source = self.app.get_pool(self.POOL_NAME).init_volume(template_vm,
|
|
|
|
source_config)
|
2016-04-15 20:11:02 +02:00
|
|
|
config = {
|
|
|
|
'name': 'root',
|
2017-06-09 04:46:46 +02:00
|
|
|
'pool': self.POOL_NAME,
|
2016-07-12 17:53:31 +02:00
|
|
|
'snap_on_start': True,
|
|
|
|
'rw': False,
|
|
|
|
'source': source,
|
|
|
|
'size': original_size,
|
2016-04-15 20:11:02 +02:00
|
|
|
}
|
2016-07-12 17:53:31 +02:00
|
|
|
|
|
|
|
volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
|
|
|
|
self.assertEqual(volume.name, 'root')
|
2017-06-09 04:46:46 +02:00
|
|
|
self.assertEqual(volume.pool, self.POOL_NAME)
|
2016-07-12 17:53:31 +02:00
|
|
|
self.assertEqual(volume.size, original_size)
|
|
|
|
self.assertTrue(volume.snap_on_start)
|
|
|
|
self.assertTrue(volume.snap_on_start)
|
|
|
|
self.assertFalse(volume.save_on_stop)
|
|
|
|
self.assertFalse(volume.rw)
|
|
|
|
self.assertEqual(volume.usage, 0)
|
2017-07-01 22:45:22 +02:00
|
|
|
block = volume.block_device()
|
|
|
|
assert isinstance(block, qubes.storage.BlockDevice)
|
|
|
|
self.assertEqual(block.path,
|
|
|
|
'{base}/{src}.img:{base}/{src}-cow.img:'
|
|
|
|
'{base}/{dst}-cow.img'.format(
|
|
|
|
base=self.POOL_DIR,
|
|
|
|
src='vm-templates/' + template_vm.name + '/root',
|
|
|
|
dst='appvms/' + vm.name + '/root',
|
|
|
|
))
|
|
|
|
self.assertEqual(block.name, 'root')
|
|
|
|
self.assertEqual(block.script, 'block-snapshot')
|
|
|
|
self.assertEqual(block.rw, False)
|
|
|
|
self.assertEqual(block.devtype, 'disk')
|
2016-04-15 20:11:02 +02:00
|
|
|
|
|
|
|
def test_002_read_write_volume(self):
|
|
|
|
config = {
|
|
|
|
'name': 'root',
|
|
|
|
'pool': self.POOL_NAME,
|
2016-07-12 17:53:31 +02:00
|
|
|
'rw': True,
|
|
|
|
'save_on_stop': True,
|
2016-04-15 20:11:02 +02:00
|
|
|
'size': defaults['root_img_size'],
|
|
|
|
}
|
2016-07-12 17:53:31 +02:00
|
|
|
vm = qubes.tests.storage.TestVM(self)
|
|
|
|
volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
|
|
|
|
self.assertEqual(volume.name, 'root')
|
|
|
|
self.assertEqual(volume.pool, self.POOL_NAME)
|
|
|
|
self.assertEqual(volume.size, defaults['root_img_size'])
|
|
|
|
self.assertFalse(volume.snap_on_start)
|
|
|
|
self.assertTrue(volume.save_on_stop)
|
|
|
|
self.assertTrue(volume.rw)
|
2017-07-01 22:45:22 +02:00
|
|
|
block = volume.block_device()
|
|
|
|
self.assertEqual(block.name, 'root')
|
|
|
|
self.assertEqual(block.path, '{base}.img:{base}-cow.img'.format(
|
|
|
|
base=self.POOL_DIR + '/appvms/' + vm.name + '/root'))
|
|
|
|
self.assertEqual(block.rw, True)
|
|
|
|
self.assertEqual(block.script, 'block-origin')
|
2016-07-12 17:53:31 +02:00
|
|
|
|
|
|
|
def test_003_read_only_volume(self):
|
2016-04-15 20:11:02 +02:00
|
|
|
template = self.app.default_template
|
2016-07-12 17:53:31 +02:00
|
|
|
vid = template.volumes['root'].vid
|
2017-06-09 04:46:46 +02:00
|
|
|
config = {
|
|
|
|
'name': 'root',
|
|
|
|
'pool': self.POOL_NAME,
|
|
|
|
'rw': False,
|
|
|
|
'vid': vid,
|
|
|
|
}
|
2016-07-12 17:53:31 +02:00
|
|
|
vm = qubes.tests.storage.TestVM(self, template=template)
|
2016-04-15 20:11:02 +02:00
|
|
|
|
2016-07-12 17:53:31 +02:00
|
|
|
volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
|
|
|
|
self.assertEqual(volume.name, 'root')
|
2017-06-09 04:46:46 +02:00
|
|
|
self.assertEqual(volume.pool, self.POOL_NAME)
|
2016-07-12 17:53:31 +02:00
|
|
|
|
|
|
|
# original_size = qubes.config.defaults['root_img_size']
|
|
|
|
# FIXME: self.assertEqual(volume.size, original_size)
|
|
|
|
self.assertFalse(volume.snap_on_start)
|
|
|
|
self.assertFalse(volume.save_on_stop)
|
|
|
|
self.assertFalse(volume.rw)
|
2017-07-01 22:45:22 +02:00
|
|
|
block = volume.block_device()
|
|
|
|
self.assertEqual(block.rw, False)
|
2016-04-15 20:11:02 +02:00
|
|
|
|
|
|
|
def test_004_volatile_volume(self):
|
|
|
|
config = {
|
|
|
|
'name': 'root',
|
|
|
|
'pool': self.POOL_NAME,
|
|
|
|
'size': defaults['root_img_size'],
|
2016-07-12 17:53:31 +02:00
|
|
|
'rw': True,
|
2016-04-15 20:11:02 +02:00
|
|
|
}
|
2016-07-12 17:53:31 +02:00
|
|
|
vm = qubes.tests.storage.TestVM(self)
|
|
|
|
volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
|
|
|
|
self.assertEqual(volume.name, 'root')
|
|
|
|
self.assertEqual(volume.pool, self.POOL_NAME)
|
|
|
|
self.assertEqual(volume.size, defaults['root_img_size'])
|
|
|
|
self.assertFalse(volume.snap_on_start)
|
|
|
|
self.assertFalse(volume.save_on_stop)
|
|
|
|
self.assertTrue(volume.rw)
|
2016-04-15 20:11:02 +02:00
|
|
|
|
|
|
|
def test_005_appvm_volumes(self):
|
|
|
|
''' Check if AppVM volumes are propertly initialized '''
|
|
|
|
vmname = self.make_vm_name('appvm')
|
2016-07-12 17:53:31 +02:00
|
|
|
vm = self.app.add_new_vm(qubes.vm.appvm.AppVM, name=vmname,
|
2016-04-15 20:11:02 +02:00
|
|
|
template=self.app.default_template,
|
|
|
|
label='red')
|
|
|
|
|
2017-09-27 03:24:57 +02:00
|
|
|
template_dir = os.path.join(self.POOL_DIR, 'vm-templates',
|
|
|
|
vm.template.name)
|
|
|
|
vm_dir = os.path.join(self.POOL_DIR, 'appvms', vmname)
|
|
|
|
expected = template_dir + '/root.img:' + \
|
|
|
|
template_dir + '/root-cow.img:' + \
|
|
|
|
vm_dir + '/root-cow.img'
|
2016-04-15 20:11:02 +02:00
|
|
|
self.assertVolumePath(vm, 'root', expected, rw=False)
|
2017-09-27 03:24:57 +02:00
|
|
|
expected = vm_dir + '/private.img:' + \
|
|
|
|
vm_dir + '/private-cow.img'
|
2016-04-15 20:11:02 +02:00
|
|
|
self.assertVolumePath(vm, 'private', expected, rw=True)
|
2017-09-27 03:24:57 +02:00
|
|
|
expected = vm_dir + '/volatile.img'
|
2016-04-15 20:11:02 +02:00
|
|
|
self.assertVolumePath(vm, 'volatile', expected, rw=True)
|
|
|
|
|
|
|
|
def test_006_template_volumes(self):
|
|
|
|
''' Check if TemplateVM volumes are propertly initialized '''
|
|
|
|
vmname = self.make_vm_name('appvm')
|
2016-07-12 17:53:31 +02:00
|
|
|
vm = self.app.add_new_vm(qubes.vm.templatevm.TemplateVM, name=vmname,
|
2016-04-15 20:11:02 +02:00
|
|
|
label='red')
|
|
|
|
|
2017-09-27 03:24:57 +02:00
|
|
|
vm_dir = os.path.join(self.POOL_DIR, 'vm-templates', vmname)
|
|
|
|
expected = vm_dir + '/root.img:' + vm_dir + '/root-cow.img'
|
2016-04-15 20:11:02 +02:00
|
|
|
self.assertVolumePath(vm, 'root', expected, rw=True)
|
2017-09-27 03:24:57 +02:00
|
|
|
expected = vm_dir + '/private.img:' + \
|
|
|
|
vm_dir + '/private-cow.img'
|
2016-04-15 20:11:02 +02:00
|
|
|
self.assertVolumePath(vm, 'private', expected, rw=True)
|
2017-09-27 03:24:57 +02:00
|
|
|
expected = vm_dir + '/volatile.img'
|
2016-04-15 20:11:02 +02:00
|
|
|
self.assertVolumePath(vm, 'volatile', expected, rw=True)
|
|
|
|
|
|
|
|
def assertVolumePath(self, vm, dev_name, expected, rw=True):
|
|
|
|
# :pylint: disable=invalid-name
|
|
|
|
volumes = vm.volumes
|
|
|
|
b_dev = volumes[dev_name].block_device()
|
|
|
|
self.assertEqual(b_dev.rw, rw)
|
2017-05-29 16:42:40 +02:00
|
|
|
self.assertEqual(b_dev.path, expected)
|
2016-04-15 20:11:02 +02:00
|
|
|
|
2015-11-13 22:27:54 +01:00
|
|
|
|
2016-07-12 17:53:31 +02:00
|
|
|
class TC_03_FilePool(qubes.tests.QubesTestCase):
|
2016-04-30 20:42:46 +02:00
|
|
|
""" Test the paths for the default file based pool (``FilePool``).
|
2015-11-13 22:27:54 +01:00
|
|
|
"""
|
|
|
|
|
2016-06-16 21:09:48 +02:00
|
|
|
POOL_DIR = '/tmp/test-pool'
|
|
|
|
APPVMS_DIR = '/tmp/test-pool/appvms'
|
|
|
|
TEMPLATES_DIR = '/tmp/test-pool/vm-templates'
|
|
|
|
SERVICE_DIR = '/tmp/test-pool/servicevms'
|
2016-04-15 20:11:02 +02:00
|
|
|
POOL_NAME = 'test-pool'
|
2016-04-30 20:42:46 +02:00
|
|
|
POOL_CONFIG = {'driver': 'file', 'dir_path': POOL_DIR, 'name': POOL_NAME}
|
2015-11-13 22:27:54 +01:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
""" Add a test file based storage pool """
|
2016-04-30 20:42:46 +02:00
|
|
|
super(TC_03_FilePool, self).setUp()
|
2017-07-25 05:44:10 +02:00
|
|
|
self.test_base_dir = '/tmp/qubes-test-dir'
|
|
|
|
self.base_dir_patch = unittest.mock.patch.dict(qubes.config.system_path,
|
|
|
|
{'qubes_base_dir': self.test_base_dir})
|
|
|
|
self.base_dir_patch2 = unittest.mock.patch(
|
|
|
|
'qubes.config.qubes_base_dir', self.test_base_dir)
|
|
|
|
self.base_dir_patch3 = unittest.mock.patch.dict(
|
|
|
|
qubes.config.defaults['pool_configs']['varlibqubes'],
|
|
|
|
{'dir_path': self.test_base_dir})
|
|
|
|
self.base_dir_patch.start()
|
|
|
|
self.base_dir_patch2.start()
|
|
|
|
self.base_dir_patch3.start()
|
2016-06-16 21:09:48 +02:00
|
|
|
self.app = TestApp()
|
2016-04-15 20:11:02 +02:00
|
|
|
self.app.add_pool(**self.POOL_CONFIG)
|
2017-07-25 05:44:10 +02:00
|
|
|
self.app.create_dummy_template()
|
2015-11-13 22:27:54 +01:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
""" Remove the file based storage pool after testing """
|
2016-04-15 20:11:02 +02:00
|
|
|
self.app.remove_pool("test-pool")
|
2016-06-16 21:09:48 +02:00
|
|
|
self.app.cleanup()
|
2017-09-19 17:01:29 +02:00
|
|
|
self.app.close()
|
|
|
|
del self.app
|
2017-07-25 05:44:10 +02:00
|
|
|
self.base_dir_patch3.stop()
|
|
|
|
self.base_dir_patch2.stop()
|
|
|
|
self.base_dir_patch.stop()
|
2016-04-30 20:42:46 +02:00
|
|
|
super(TC_03_FilePool, self).tearDown()
|
2015-11-13 22:27:54 +01:00
|
|
|
shutil.rmtree(self.POOL_DIR, ignore_errors=True)
|
2017-07-25 05:44:10 +02:00
|
|
|
if os.path.exists('/tmp/qubes-test-dir'):
|
|
|
|
shutil.rmtree('/tmp/qubes-test-dir')
|
2016-06-16 21:09:48 +02:00
|
|
|
|
2015-11-13 22:27:54 +01:00
|
|
|
def test_001_pool_exists(self):
|
|
|
|
""" Check if the storage pool was added to the storage pool config """
|
2016-04-15 20:11:02 +02:00
|
|
|
self.assertIn('test-pool', self.app.pools.keys())
|
2015-11-13 22:27:54 +01:00
|
|
|
|
|
|
|
def test_002_pool_dir_create(self):
|
|
|
|
""" Check if the storage pool dir and subdirs were created """
|
|
|
|
# The dir should not exists before
|
2016-04-15 20:11:02 +02:00
|
|
|
pool_name = 'foo'
|
|
|
|
pool_dir = '/tmp/foo'
|
|
|
|
appvms_dir = '/tmp/foo/appvms'
|
|
|
|
templates_dir = '/tmp/foo/vm-templates'
|
2015-11-13 22:27:54 +01:00
|
|
|
|
2016-04-15 20:11:02 +02:00
|
|
|
self.assertFalse(os.path.exists(pool_dir))
|
2015-11-13 22:27:54 +01:00
|
|
|
|
2016-04-30 20:42:46 +02:00
|
|
|
self.app.add_pool(name=pool_name, dir_path=pool_dir, driver='file')
|
2015-11-13 22:27:54 +01:00
|
|
|
|
2016-04-15 20:11:02 +02:00
|
|
|
self.assertTrue(os.path.exists(pool_dir))
|
|
|
|
self.assertTrue(os.path.exists(appvms_dir))
|
|
|
|
self.assertTrue(os.path.exists(templates_dir))
|
2015-11-13 22:27:54 +01:00
|
|
|
|
2016-04-15 20:11:02 +02:00
|
|
|
shutil.rmtree(pool_dir, ignore_errors=True)
|
2015-11-13 22:27:54 +01:00
|
|
|
|
2017-10-29 02:23:00 +02:00
|
|
|
def test_003_size(self):
|
|
|
|
pool = self.app.get_pool(self.POOL_NAME)
|
|
|
|
with self.assertNotRaises(NotImplementedError):
|
|
|
|
size = pool.size
|
|
|
|
statvfs = os.statvfs(self.POOL_DIR)
|
|
|
|
self.assertEqual(size, statvfs.f_blocks * statvfs.f_frsize)
|
|
|
|
|
|
|
|
def test_004_usage(self):
|
|
|
|
pool = self.app.get_pool(self.POOL_NAME)
|
|
|
|
with self.assertNotRaises(NotImplementedError):
|
|
|
|
usage = pool.usage
|
|
|
|
statvfs = os.statvfs(self.POOL_DIR)
|
|
|
|
self.assertEqual(usage,
|
|
|
|
statvfs.f_frsize * (statvfs.f_blocks - statvfs.f_bfree))
|
|
|
|
|
2015-11-13 22:27:54 +01:00
|
|
|
def test_011_appvm_file_images(self):
|
|
|
|
""" Check if all the needed image files are created for an AppVm"""
|
|
|
|
|
|
|
|
vmname = self.make_vm_name('appvm')
|
2016-07-12 17:53:31 +02:00
|
|
|
vm = self.app.add_new_vm(qubes.vm.appvm.AppVM, name=vmname,
|
2016-04-15 20:11:02 +02:00
|
|
|
template=self.app.default_template,
|
|
|
|
volume_config={
|
|
|
|
'private': {
|
|
|
|
'pool': 'test-pool'
|
|
|
|
},
|
|
|
|
'volatile': {
|
|
|
|
'pool': 'test-pool'
|
|
|
|
}
|
2016-07-12 17:53:31 +02:00
|
|
|
}, label='red')
|
2017-04-25 23:01:17 +02:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
loop.run_until_complete(vm.create_on_disk())
|
2015-11-13 22:27:54 +01:00
|
|
|
|
|
|
|
expected_vmdir = os.path.join(self.APPVMS_DIR, vm.name)
|
|
|
|
|
2016-07-12 17:53:31 +02:00
|
|
|
expected_private_path = os.path.join(expected_vmdir, 'private.img')
|
2017-05-29 16:42:40 +02:00
|
|
|
self.assertEqual(vm.volumes['private'].path, expected_private_path)
|
2015-11-13 22:27:54 +01:00
|
|
|
|
|
|
|
expected_volatile_path = os.path.join(expected_vmdir, 'volatile.img')
|
2017-06-09 04:46:46 +02:00
|
|
|
vm.volumes['volatile'].reset()
|
2017-05-29 16:42:40 +02:00
|
|
|
self.assertEqualAndExists(vm.volumes['volatile'].path,
|
2017-06-09 04:46:46 +02:00
|
|
|
expected_volatile_path)
|
2015-11-13 22:27:54 +01:00
|
|
|
|
2016-03-21 17:25:27 +01:00
|
|
|
def test_013_template_file_images(self):
|
|
|
|
""" Check if root.img, private.img, volatile.img and root-cow.img are
|
|
|
|
created propertly by the storage system
|
|
|
|
"""
|
|
|
|
vmname = self.make_vm_name('tmvm')
|
2016-07-12 17:53:31 +02:00
|
|
|
vm = self.app.add_new_vm(qubes.vm.templatevm.TemplateVM, name=vmname,
|
2016-04-15 20:11:02 +02:00
|
|
|
volume_config={
|
|
|
|
'root': {
|
|
|
|
'pool': 'test-pool'
|
|
|
|
},
|
|
|
|
'private': {
|
|
|
|
'pool': 'test-pool'
|
|
|
|
},
|
|
|
|
'volatile': {
|
|
|
|
'pool': 'test-pool'
|
|
|
|
}
|
2016-07-12 17:53:31 +02:00
|
|
|
}, label='red')
|
2017-04-25 23:01:17 +02:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
loop.run_until_complete(vm.create_on_disk())
|
2016-03-21 17:25:27 +01:00
|
|
|
|
|
|
|
expected_vmdir = os.path.join(self.TEMPLATES_DIR, vm.name)
|
|
|
|
|
2016-04-15 20:11:02 +02:00
|
|
|
expected_root_origin_path = os.path.join(expected_vmdir, 'root.img')
|
|
|
|
expected_root_cow_path = os.path.join(expected_vmdir, 'root-cow.img')
|
|
|
|
expected_root_path = '%s:%s' % (expected_root_origin_path,
|
|
|
|
expected_root_cow_path)
|
2017-05-29 16:42:40 +02:00
|
|
|
self.assertEqual(vm.volumes['root'].block_device().path,
|
2016-07-12 17:53:31 +02:00
|
|
|
expected_root_path)
|
|
|
|
self.assertExist(vm.volumes['root'].path)
|
2016-03-21 17:25:27 +01:00
|
|
|
|
|
|
|
expected_private_path = os.path.join(expected_vmdir, 'private.img')
|
2017-05-29 16:42:40 +02:00
|
|
|
self.assertEqualAndExists(vm.volumes['private'].path,
|
2016-03-21 17:25:27 +01:00
|
|
|
expected_private_path)
|
|
|
|
|
|
|
|
expected_rootcow_path = os.path.join(expected_vmdir, 'root-cow.img')
|
2017-07-25 05:32:45 +02:00
|
|
|
self.assertEqual(vm.volumes['root'].path_cow, expected_rootcow_path)
|
2016-03-21 17:25:27 +01:00
|
|
|
|
2017-05-29 16:42:40 +02:00
|
|
|
def assertEqualAndExists(self, result_path, expected_path):
|
2015-11-13 22:27:54 +01:00
|
|
|
""" Check if the ``result_path``, matches ``expected_path`` and exists.
|
|
|
|
|
|
|
|
See also: :meth:``assertExist``
|
|
|
|
"""
|
2016-04-15 20:11:02 +02:00
|
|
|
# :pylint: disable=invalid-name
|
2017-05-29 16:42:40 +02:00
|
|
|
self.assertEqual(result_path, expected_path)
|
2015-11-13 22:27:54 +01:00
|
|
|
self.assertExist(result_path)
|
|
|
|
|
|
|
|
def assertExist(self, path):
|
|
|
|
""" Assert that the given path exists. """
|
2016-04-15 20:11:02 +02:00
|
|
|
# :pylint: disable=invalid-name
|
2016-07-12 17:53:31 +02:00
|
|
|
self.assertTrue(
|
|
|
|
os.path.exists(path), "Path {!s} does not exist".format(path))
|