Adjust property value decoding

Now property.Get contains also explicit value type.
This commit is contained in:
Marek Marczykowski-Górecki 2017-03-11 19:03:09 +01:00
parent ade0030efe
commit c1ae5f8bf5
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
4 changed files with 63 additions and 16 deletions

View File

@ -148,6 +148,7 @@ class PropertyHolder(object):
return is_default return is_default
def __getattr__(self, item): def __getattr__(self, item):
# pylint: disable=too-many-return-statements
if item.startswith('_'): if item.startswith('_'):
raise AttributeError(item) raise AttributeError(item)
try: try:
@ -158,12 +159,35 @@ class PropertyHolder(object):
None) None)
except qubesmgmt.exc.QubesDaemonNoResponseError: except qubesmgmt.exc.QubesDaemonNoResponseError:
raise qubesmgmt.exc.QubesPropertyAccessError(item) raise qubesmgmt.exc.QubesPropertyAccessError(item)
(_default, value) = property_str.split(b' ', 1) (_default, prop_type, value) = property_str.split(b' ', 2)
prop_type = prop_type.decode('ascii')
if not prop_type.startswith('type='):
raise qubesmgmt.exc.QubesDaemonCommunicationError(
'Invalid type prefix received: {}'.format(prop_type))
(_, prop_type) = prop_type.split('=', 1)
value = value.decode() value = value.decode()
if value[0] == '\'': if prop_type == 'str':
return ast.literal_eval('u' + value) return str(value)
else: elif prop_type == 'bool':
if value == '':
raise AttributeError
return ast.literal_eval(value) return ast.literal_eval(value)
elif prop_type == 'int':
if value == '':
raise AttributeError
return ast.literal_eval(value)
elif prop_type == 'vm':
if value == '':
return None
return self.app.domains[value]
elif prop_type == 'label':
if value == '':
return None
# TODO
return self.app.labels[value]
else:
raise qubesmgmt.exc.QubesDaemonCommunicationError(
'Received invalid value type: {}'.format(prop_type))
def __setattr__(self, key, value): def __setattr__(self, key, value):
if key.startswith('_') or key in dir(self): if key.startswith('_') or key in dir(self):

View File

@ -30,10 +30,10 @@ class TC_00_qubes_prefs(qubesmgmt.tests.QubesTestCase):
b'0\x00prop1\nprop2\n' b'0\x00prop1\nprop2\n'
self.app.expected_calls[ self.app.expected_calls[
('dom0', 'mgmt.property.Get', 'prop1', None)] = \ ('dom0', 'mgmt.property.Get', 'prop1', None)] = \
b'0\x00default=True \'value1\'' b'0\x00default=True type=str value1'
self.app.expected_calls[ self.app.expected_calls[
('dom0', 'mgmt.property.Get', 'prop2', None)] = \ ('dom0', 'mgmt.property.Get', 'prop2', None)] = \
b'0\x00default=False \'value2\'' b'0\x00default=False type=str value2'
with qubesmgmt.tests.tools.StdoutBuffer() as stdout: with qubesmgmt.tests.tools.StdoutBuffer() as stdout:
self.assertEqual(0, qubesmgmt.tools.qubes_prefs.main([], app=self.app)) self.assertEqual(0, qubesmgmt.tools.qubes_prefs.main([], app=self.app))
self.assertEqual(stdout.getvalue(), self.assertEqual(stdout.getvalue(),

View File

@ -33,10 +33,10 @@ class TC_00_qvm_prefs(qubesmgmt.tests.QubesTestCase):
b'0\x00prop1\nprop2\n' b'0\x00prop1\nprop2\n'
self.app.expected_calls[ self.app.expected_calls[
('dom0', 'mgmt.vm.property.Get', 'prop1', None)] = \ ('dom0', 'mgmt.vm.property.Get', 'prop1', None)] = \
b'0\x00default=True \'value1\'' b'0\x00default=True type=str value1'
self.app.expected_calls[ self.app.expected_calls[
('dom0', 'mgmt.vm.property.Get', 'prop2', None)] = \ ('dom0', 'mgmt.vm.property.Get', 'prop2', None)] = \
b'0\x00default=False \'value2\'' b'0\x00default=False type=str value2'
with qubesmgmt.tests.tools.StdoutBuffer() as stdout: with qubesmgmt.tests.tools.StdoutBuffer() as stdout:
self.assertEqual(0, qubesmgmt.tools.qvm_prefs.main([ self.assertEqual(0, qubesmgmt.tools.qvm_prefs.main([
'dom0'], app=self.app)) 'dom0'], app=self.app))

View File

@ -35,21 +35,21 @@ class TC_00_Properties(qubesmgmt.tests.vm.VMTestCase):
def test_001_get_str(self): def test_001_get_str(self):
self.app.expected_calls[ self.app.expected_calls[
('test-vm', 'mgmt.vm.property.Get', 'prop1', None)] = \ ('test-vm', 'mgmt.vm.property.Get', 'prop1', None)] = \
b'0\x00default=False \'value\'' b'0\x00default=False type=str value'
self.assertEqual(self.vm.prop1, 'value') self.assertEqual(self.vm.prop1, 'value')
self.assertAllCalled() self.assertAllCalled()
def test_002_get_int(self): def test_002_get_int(self):
self.app.expected_calls[ self.app.expected_calls[
('test-vm', 'mgmt.vm.property.Get', 'prop1', None)] = \ ('test-vm', 'mgmt.vm.property.Get', 'prop1', None)] = \
b'0\x00default=False 123' b'0\x00default=False type=int 123'
self.assertEqual(self.vm.prop1, 123) self.assertEqual(self.vm.prop1, 123)
self.assertAllCalled() self.assertAllCalled()
def test_003_get_bool(self): def test_003_get_bool(self):
self.app.expected_calls[ self.app.expected_calls[
('test-vm', 'mgmt.vm.property.Get', 'prop1', None)] = \ ('test-vm', 'mgmt.vm.property.Get', 'prop1', None)] = \
b'0\x00default=False True' b'0\x00default=False type=bool True'
self.assertEqual(self.vm.prop1, True) self.assertEqual(self.vm.prop1, True)
self.assertAllCalled() self.assertAllCalled()
@ -57,28 +57,51 @@ class TC_00_Properties(qubesmgmt.tests.vm.VMTestCase):
self.skipTest('not specified') self.skipTest('not specified')
self.app.expected_calls[ self.app.expected_calls[
('test-vm', 'mgmt.vm.property.Get', 'prop1', None)] = \ ('test-vm', 'mgmt.vm.property.Get', 'prop1', None)] = \
b'0\x00default=False \'test-vm\'' b'0\x00default=False type=vm test-vm'
self.assertEqual(self.vm.prop1, True) self.assertEqual(self.vm.prop1, True)
self.assertAllCalled() self.assertAllCalled()
def test_005_get_none(self): def test_005_get_none_vm(self):
self.app.expected_calls[ self.app.expected_calls[
('test-vm', 'mgmt.vm.property.Get', 'prop1', None)] = \ ('test-vm', 'mgmt.vm.property.Get', 'prop1', None)] = \
b'0\x00default=False None' b'0\x00default=False type=vm '
self.assertEqual(self.vm.prop1, None) self.assertEqual(self.vm.prop1, None)
self.assertAllCalled() self.assertAllCalled()
def test_006_get_none_bool(self):
self.app.expected_calls[
('test-vm', 'mgmt.vm.property.Get', 'prop1', None)] = \
b'0\x00default=False type=bool '
with self.assertRaises(AttributeError):
self.vm.prop1
self.assertAllCalled()
def test_007_get_none_int(self):
self.app.expected_calls[
('test-vm', 'mgmt.vm.property.Get', 'prop1', None)] = \
b'0\x00default=False type=int '
with self.assertRaises(AttributeError):
self.vm.prop1
self.assertAllCalled()
def test_008_get_none_str(self):
self.app.expected_calls[
('test-vm', 'mgmt.vm.property.Get', 'prop1', None)] = \
b'0\x00default=False type=str '
self.assertEqual(self.vm.prop1, '')
self.assertAllCalled()
def test_010_get_default(self): def test_010_get_default(self):
self.app.expected_calls[ self.app.expected_calls[
('test-vm', 'mgmt.vm.property.Get', 'prop1', None)] = \ ('test-vm', 'mgmt.vm.property.Get', 'prop1', None)] = \
b'0\x00default=False \'value\'' b'0\x00default=False type=str value'
self.assertEqual(self.vm.property_is_default('prop1'), False) self.assertEqual(self.vm.property_is_default('prop1'), False)
self.assertAllCalled() self.assertAllCalled()
def test_011_get_default(self): def test_011_get_default(self):
self.app.expected_calls[ self.app.expected_calls[
('test-vm', 'mgmt.vm.property.Get', 'prop1', None)] = \ ('test-vm', 'mgmt.vm.property.Get', 'prop1', None)] = \
b'0\x00default=True \'value\'' b'0\x00default=True type=str value'
self.assertEqual(self.vm.property_is_default('prop1'), True) self.assertEqual(self.vm.property_is_default('prop1'), True)
self.assertAllCalled() self.assertAllCalled()