2017-03-13 04:33:12 +01:00
|
|
|
# -*- encoding: utf8 -*-
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2017 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program 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 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 Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
# with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
import qubesadmin.tests
|
|
|
|
import qubesadmin.tests.tools
|
|
|
|
import qubesadmin.tools.qvm_volume
|
2017-03-13 04:33:12 +01:00
|
|
|
|
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
class TC_00_qvm_volume(qubesadmin.tests.QubesTestCase):
|
2017-03-13 04:33:12 +01:00
|
|
|
|
|
|
|
def setup_expected_calls_for_list(self, vms=('vm1', 'sys-net')):
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('dom0', 'admin.vm.List', None, None)] = \
|
2017-03-13 04:33:12 +01:00
|
|
|
b'0\x00vm1 class=AppVM state=Running\n' \
|
|
|
|
b'sys-net class=AppVM state=Running\n'
|
|
|
|
for vm in vms:
|
|
|
|
for vol in ('root', 'private'):
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
(vm, 'admin.vm.volume.Info', vol, None)] = \
|
2017-03-13 04:33:12 +01:00
|
|
|
b'0\x00' + \
|
|
|
|
(b'pool=pool-file\n' if vol == 'root' else
|
|
|
|
b'pool=other-pool\n') + \
|
|
|
|
b'vid=' + vm.encode() + b'-' + vol.encode() + b'\n' \
|
|
|
|
b'internal=True\n' \
|
|
|
|
b'size=10737418240\n'
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
(vm, 'admin.vm.volume.ListSnapshots', vol, None)] = \
|
2017-03-13 04:33:12 +01:00
|
|
|
b'0\x00snap1\n' if vol == 'private' else b'0\x00'
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
(vm, 'admin.vm.volume.List', None, None)] = \
|
2017-03-13 04:33:12 +01:00
|
|
|
b'0\x00root\nprivate\n'
|
|
|
|
|
|
|
|
def test_000_list(self):
|
|
|
|
self.setup_expected_calls_for_list()
|
2017-05-11 23:21:04 +02:00
|
|
|
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
|
2017-03-13 04:33:12 +01:00
|
|
|
self.assertEqual(0,
|
2017-05-11 23:21:04 +02:00
|
|
|
qubesadmin.tools.qvm_volume.main(['ls', '-i'], app=self.app))
|
2017-03-13 04:33:12 +01:00
|
|
|
self.assertEqual(stdout.getvalue(),
|
|
|
|
'POOL:VOLUME VMNAME VOLUME_NAME '
|
|
|
|
'REVERT_POSSIBLE\n'
|
|
|
|
'other-pool:sys-net-private sys-net private Yes\n'
|
|
|
|
'other-pool:vm1-private vm1 private Yes\n'
|
|
|
|
'pool-file:sys-net-root sys-net root No\n'
|
|
|
|
'pool-file:vm1-root vm1 root No\n'
|
|
|
|
)
|
|
|
|
self.assertAllCalled()
|
|
|
|
|
|
|
|
def test_001_list_domain(self):
|
|
|
|
self.setup_expected_calls_for_list(vms=('vm1',))
|
2017-05-11 23:21:04 +02:00
|
|
|
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
|
2017-03-13 04:33:12 +01:00
|
|
|
self.assertEqual(0,
|
2017-05-11 23:21:04 +02:00
|
|
|
qubesadmin.tools.qvm_volume.main(['ls', '-i', 'vm1'],
|
2017-03-13 04:33:12 +01:00
|
|
|
app=self.app))
|
|
|
|
self.assertEqual(stdout.getvalue(),
|
|
|
|
'POOL:VOLUME VMNAME VOLUME_NAME REVERT_POSSIBLE\n'
|
|
|
|
'other-pool:vm1-private vm1 private Yes\n'
|
|
|
|
'pool-file:vm1-root vm1 root No\n'
|
|
|
|
)
|
|
|
|
self.assertAllCalled()
|
|
|
|
|
|
|
|
def test_002_list_domain_pool(self):
|
|
|
|
self.setup_expected_calls_for_list(vms=('vm1',))
|
2017-05-12 19:36:03 +02:00
|
|
|
self.app.expected_calls[('dom0', 'admin.pool.List', None, None)] = \
|
2017-03-13 04:33:12 +01:00
|
|
|
b'0\x00pool-file\nother-pool\n'
|
|
|
|
del self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('vm1', 'admin.vm.volume.ListSnapshots', 'private', None)]
|
2017-05-11 23:21:04 +02:00
|
|
|
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
|
2017-03-13 04:33:12 +01:00
|
|
|
self.assertEqual(0,
|
2017-05-11 23:21:04 +02:00
|
|
|
qubesadmin.tools.qvm_volume.main(
|
2017-03-13 04:33:12 +01:00
|
|
|
['ls', '-i', '-p', 'pool-file', 'vm1'],
|
|
|
|
app=self.app))
|
|
|
|
self.assertEqual(stdout.getvalue(),
|
|
|
|
'POOL:VOLUME VMNAME VOLUME_NAME REVERT_POSSIBLE\n'
|
|
|
|
'pool-file:vm1-root vm1 root No\n'
|
|
|
|
)
|
|
|
|
self.assertAllCalled()
|
|
|
|
|
|
|
|
def test_003_list_pool(self):
|
|
|
|
self.setup_expected_calls_for_list()
|
2017-05-12 19:36:03 +02:00
|
|
|
self.app.expected_calls[('dom0', 'admin.pool.List', None, None)] = \
|
2017-03-13 04:33:12 +01:00
|
|
|
b'0\x00pool-file\nother-pool\n'
|
|
|
|
del self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('vm1', 'admin.vm.volume.ListSnapshots', 'private', None)]
|
2017-03-13 04:33:12 +01:00
|
|
|
del self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('sys-net', 'admin.vm.volume.ListSnapshots', 'private', None)]
|
2017-03-13 04:33:12 +01:00
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
|
2017-03-13 04:33:12 +01:00
|
|
|
self.assertEqual(0,
|
2017-05-11 23:21:04 +02:00
|
|
|
qubesadmin.tools.qvm_volume.main(
|
2017-03-13 04:33:12 +01:00
|
|
|
['ls', '-i', '-p', 'pool-file'],
|
|
|
|
app=self.app))
|
|
|
|
self.assertEqual(stdout.getvalue(),
|
|
|
|
'POOL:VOLUME VMNAME VOLUME_NAME REVERT_POSSIBLE\n'
|
|
|
|
'pool-file:sys-net-root sys-net root No\n'
|
|
|
|
'pool-file:vm1-root vm1 root No\n'
|
|
|
|
)
|
|
|
|
self.assertAllCalled()
|
|
|
|
|
|
|
|
def test_004_list_multiple_domains(self):
|
|
|
|
self.setup_expected_calls_for_list(vms=('vm1', 'vm2'))
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('dom0', 'admin.vm.List', None, None)] = \
|
2017-03-13 04:33:12 +01:00
|
|
|
b'0\x00vm1 class=AppVM state=Running\n' \
|
|
|
|
b'vm2 class=AppVM state=Running\n' \
|
|
|
|
b'vm3 class=AppVM state=Running\n'
|
2017-05-11 23:21:04 +02:00
|
|
|
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
|
2017-03-13 04:33:12 +01:00
|
|
|
self.assertEqual(0,
|
2017-05-11 23:21:04 +02:00
|
|
|
qubesadmin.tools.qvm_volume.main(
|
2017-03-13 04:33:12 +01:00
|
|
|
['ls', '-i', 'vm1', 'vm2'], app=self.app))
|
|
|
|
self.assertEqual(stdout.getvalue(),
|
|
|
|
'POOL:VOLUME VMNAME VOLUME_NAME REVERT_POSSIBLE\n'
|
|
|
|
'other-pool:vm1-private vm1 private Yes\n'
|
|
|
|
'other-pool:vm2-private vm2 private Yes\n'
|
|
|
|
'pool-file:vm1-root vm1 root No\n'
|
|
|
|
'pool-file:vm2-root vm2 root No\n'
|
|
|
|
)
|
|
|
|
self.assertAllCalled()
|
|
|
|
|
|
|
|
def test_010_extend(self):
|
2017-05-12 19:36:03 +02:00
|
|
|
self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
|
2017-03-13 04:33:12 +01:00
|
|
|
b'0\x00testvm class=AppVM state=Running\n'
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('testvm', 'admin.vm.volume.List', None, None)] = \
|
2017-03-13 04:33:12 +01:00
|
|
|
b'0\x00root\nprivate\n'
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('testvm', 'admin.vm.volume.Resize', 'private', b'10737418240')] = \
|
2017-03-13 04:33:12 +01:00
|
|
|
b'0\x00'
|
|
|
|
self.assertEqual(0,
|
2017-05-11 23:21:04 +02:00
|
|
|
qubesadmin.tools.qvm_volume.main(
|
2017-03-13 04:33:12 +01:00
|
|
|
['extend', 'testvm:private', '10GiB'],
|
|
|
|
app=self.app))
|
|
|
|
self.assertAllCalled()
|
|
|
|
|
|
|
|
def test_011_extend_error(self):
|
2017-05-12 19:36:03 +02:00
|
|
|
self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
|
2017-03-13 04:33:12 +01:00
|
|
|
b'0\x00testvm class=AppVM state=Running\n'
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('testvm', 'admin.vm.volume.List', None, None)] = \
|
2017-03-13 04:33:12 +01:00
|
|
|
b'0\x00root\nprivate\n'
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('testvm', 'admin.vm.volume.Resize', 'private', b'1073741824')] = \
|
2017-03-13 04:33:12 +01:00
|
|
|
b'2\x00StoragePoolException\x00\x00Failed to resize volume: ' \
|
|
|
|
b'shrink not allowed\x00'
|
2017-05-11 23:21:04 +02:00
|
|
|
with qubesadmin.tests.tools.StderrBuffer() as stderr:
|
2017-03-13 04:33:12 +01:00
|
|
|
self.assertEqual(1,
|
2017-05-11 23:21:04 +02:00
|
|
|
qubesadmin.tools.qvm_volume.main(
|
2017-03-13 04:33:12 +01:00
|
|
|
['extend', 'testvm:private', '1GiB'],
|
|
|
|
app=self.app))
|
|
|
|
self.assertIn('shrink not allowed', stderr.getvalue())
|
|
|
|
self.assertAllCalled()
|