2017-03-12 00:47:45 +01:00
|
|
|
# pylint: disable=protected-access,pointless-statement
|
|
|
|
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubesmgmt-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2015 Wojtek Porczyk <woju@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/>.
|
|
|
|
import unittest
|
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
import qubesadmin
|
|
|
|
import qubesadmin.vm
|
|
|
|
import qubesadmin.tools.qvm_ls
|
2017-03-12 00:47:45 +01:00
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
import qubesadmin.tests
|
|
|
|
import qubesadmin.tests.tools
|
2017-03-12 00:47:45 +01:00
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
from qubesadmin.tests import TestVM, TestVMCollection
|
2017-03-12 00:47:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestApp(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.domains = TestVMCollection(
|
|
|
|
[
|
|
|
|
('dom0', TestVM('dom0')),
|
|
|
|
('test-vm', TestVM('test-vm')),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
class TC_00_Column(qubesadmin.tests.QubesTestCase):
|
2017-03-12 00:47:45 +01:00
|
|
|
def test_100_init(self):
|
|
|
|
try:
|
2017-05-11 23:21:04 +02:00
|
|
|
testcolumn = qubesadmin.tools.qvm_ls.Column('TESTCOLUMN')
|
2017-03-12 00:47:45 +01:00
|
|
|
self.assertEqual(testcolumn.ls_head, 'TESTCOLUMN')
|
|
|
|
finally:
|
|
|
|
try:
|
2017-05-11 23:21:04 +02:00
|
|
|
qubesadmin.tools.qvm_ls.Column.columns['TESTCOLUMN']
|
2017-03-12 00:47:45 +01:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
class TC_10_globals(qubesadmin.tests.QubesTestCase):
|
2017-03-12 00:47:45 +01:00
|
|
|
def test_100_simple_flag(self):
|
2017-05-11 23:21:04 +02:00
|
|
|
flag = qubesadmin.tools.qvm_ls.simple_flag(1, 'T', 'internal')
|
2017-03-12 00:47:45 +01:00
|
|
|
|
|
|
|
# TODO after serious testing of QubesVM and Qubes app, this should be
|
|
|
|
# using normal components
|
|
|
|
vm = TestVM('test-vm', internal=False)
|
|
|
|
|
|
|
|
self.assertFalse(flag(None, vm))
|
|
|
|
vm.internal = True
|
|
|
|
self.assertTrue(flag(None, vm))
|
|
|
|
|
|
|
|
@unittest.skip('column list generated dynamically')
|
|
|
|
def test_900_formats_columns(self):
|
2017-05-11 23:21:04 +02:00
|
|
|
for fmt in qubesadmin.tools.qvm_ls.formats:
|
|
|
|
for col in qubesadmin.tools.qvm_ls.formats[fmt]:
|
|
|
|
self.assertIn(col.upper(), qubesadmin.tools.qvm_ls.Column.columns)
|
2017-03-12 00:47:45 +01:00
|
|
|
|
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
class TC_50_List(qubesadmin.tests.QubesTestCase):
|
2017-03-12 00:47:45 +01:00
|
|
|
def test_100_list_with_status(self):
|
|
|
|
app = TestApp()
|
|
|
|
app.domains['test-vm'].internal = False
|
|
|
|
app.domains['test-vm'].updateable = False
|
|
|
|
app.domains['test-vm'].template = TestVM('template')
|
|
|
|
app.domains['test-vm'].netvm = TestVM('sys-net')
|
|
|
|
app.domains['test-vm'].label = 'green'
|
|
|
|
app.domains['dom0'].label = 'black'
|
2017-05-11 23:21:04 +02:00
|
|
|
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
|
|
|
|
qubesadmin.tools.qvm_ls.main([], app=app)
|
2017-03-12 00:47:45 +01:00
|
|
|
self.assertEqual(stdout.getvalue(),
|
2017-06-08 13:10:44 +02:00
|
|
|
'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
|
|
|
|
'dom0 Running TestVM black - -\n'
|
|
|
|
'test-vm Running TestVM green template sys-net\n')
|
2017-03-12 00:47:45 +01:00
|
|
|
|
2017-12-29 00:56:16 +01:00
|
|
|
def test_101_list_with_underscore(self):
|
|
|
|
app = TestApp()
|
|
|
|
app.domains['test-vm'].virt_mode = 'pv'
|
|
|
|
app.domains['test-vm'].label = 'green'
|
|
|
|
app.domains['dom0'].label = 'black'
|
|
|
|
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
|
|
|
|
qubesadmin.tools.qvm_ls.main(['-O', 'name,virt_mode,class'], app=app)
|
|
|
|
self.assertEqual(stdout.getvalue(),
|
|
|
|
'NAME VIRT-MODE CLASS\n'
|
|
|
|
'dom0 - TestVM\n'
|
|
|
|
'test-vm pv TestVM\n')
|
2017-03-12 00:47:45 +01:00
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
class TC_90_List_with_qubesd_calls(qubesadmin.tests.QubesTestCase):
|
2017-03-12 00:47:45 +01:00
|
|
|
def test_100_list_with_status(self):
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('dom0', 'admin.vm.List', None, None)] = \
|
2017-03-12 00:47:45 +01:00
|
|
|
b'0\x00vm1 class=AppVM state=Running\n' \
|
|
|
|
b'template1 class=TemplateVM state=Halted\n' \
|
|
|
|
b'sys-net class=AppVM state=Running\n'
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('vm1', 'admin.vm.List', None, None)] = \
|
2017-03-12 00:47:45 +01:00
|
|
|
b'0\x00vm1 class=AppVM state=Running\n'
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('sys-net', 'admin.vm.List', None, None)] = \
|
2017-03-12 00:47:45 +01:00
|
|
|
b'0\x00sys-net class=AppVM state=Running\n'
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('template1', 'admin.vm.List', None, None)] = \
|
2017-03-12 00:47:45 +01:00
|
|
|
b'0\x00template1 class=TemplateVM state=Halted\n'
|
|
|
|
props = {
|
|
|
|
'label': b'type=label green',
|
|
|
|
'template': b'type=vm template1',
|
|
|
|
'netvm': b'type=vm sys-net',
|
2017-07-18 04:22:34 +02:00
|
|
|
# 'virt_mode': b'type=str pv',
|
2017-03-12 00:47:45 +01:00
|
|
|
}
|
|
|
|
for key, value in props.items():
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('vm1', 'admin.vm.property.Get', key, None)] = \
|
2017-03-12 00:47:45 +01:00
|
|
|
b'0\x00default=True ' + value
|
|
|
|
|
|
|
|
# setup template1
|
|
|
|
props['label'] = b'type=label black'
|
|
|
|
for key, value in props.items():
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('template1', 'admin.vm.property.Get', key, None)] = \
|
2017-03-12 00:47:45 +01:00
|
|
|
b'0\x00default=True ' + value
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('template1', 'admin.vm.property.Get', 'template', None)] = \
|
2017-03-12 00:47:45 +01:00
|
|
|
b'' # request refused - no such property
|
|
|
|
|
|
|
|
# setup sys-net
|
|
|
|
props['label'] = b'type=label red'
|
|
|
|
for key, value in props.items():
|
|
|
|
self.app.expected_calls[
|
2017-05-12 19:36:03 +02:00
|
|
|
('sys-net', 'admin.vm.property.Get', key, None)] = \
|
2017-03-12 00:47:45 +01:00
|
|
|
b'0\x00default=True ' + value
|
|
|
|
|
2017-05-11 23:21:04 +02:00
|
|
|
with qubesadmin.tests.tools.StdoutBuffer() as stdout:
|
|
|
|
qubesadmin.tools.qvm_ls.main([], app=self.app)
|
2017-03-12 00:47:45 +01:00
|
|
|
self.assertEqual(stdout.getvalue(),
|
2017-06-08 13:10:44 +02:00
|
|
|
'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
|
|
|
|
'sys-net Running AppVM red template1 sys-net\n'
|
|
|
|
'template1 Halted TemplateVM black - sys-net\n'
|
|
|
|
'vm1 Running AppVM green template1 sys-net\n')
|
2017-03-12 00:47:45 +01:00
|
|
|
self.assertAllCalled()
|