Nir Soffer has uploaded a new change for review.
Change subject: guestagent: Simpler and safer disk mapping hash
......................................................................
guestagent: Simpler and safer disk mapping hash
Previously diskMapingHash was initialized to the magic value 0, or to
hash of a sorted json dump of the diskMapping dict. Now diskMappingHash
is always a real hash, and is read only.
The hash is computed automatically when the value of guestDiskMapping is
changed, and there is no way to forget to update the hash.
Change-Id: Ia914cf9860cef019b3040a3f034e479e6a93ed97
Signed-off-by: Nir Soffer <nsoffer(a)redhat.com>
---
M tests/guestagentTests.py
M vdsm/virt/guestagent.py
2 files changed, 28 insertions(+), 3 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/25/34625/1
diff --git a/tests/guestagentTests.py b/tests/guestagentTests.py
index f4f1a35..d9684ae 100644
--- a/tests/guestagentTests.py
+++ b/tests/guestagentTests.py
@@ -193,3 +193,18 @@
# If the message size was within the allowed range
# the message should have been put into the guestInfo dict
self.assertEqual(self.fakeGuestAgent.guestInfo[k], v)
+
+
+class DiskMappingTests(TestCaseBase):
+
+ def setUp(self):
+ self.agent = guestagent.GuestAgent(None, None, None)
+
+ def test_init(self):
+ self.assertEqual(self.agent.guestDiskMapping, {})
+ self.assertTrue(isinstance(self.agent.diskMappingHash, int))
+
+ def test_change_disk_mapping(self):
+ old_hash = self.agent.diskMappingHash
+ self.agent.guestDiskMapping = {'/dev/vda': 'xxx'}
+ self.assertNotEqual(self.agent.diskMappingHash, old_hash)
diff --git a/vdsm/virt/guestagent.py b/vdsm/virt/guestagent.py
index a403b0d..dbada69 100644
--- a/vdsm/virt/guestagent.py
+++ b/vdsm/virt/guestagent.py
@@ -121,7 +121,6 @@
self._stopped = True
self.guestStatus = None
self.guestDiskMapping = {}
- self.diskMappingHash = 0
self.guestInfo = {
'username': user,
'memUsage': 0,
@@ -136,6 +135,19 @@
self._agentTimestamp = 0
self._channelListener = channelListener
self._messageState = MessageState.NORMAL
+
+ @property
+ def guestDiskMapping(self):
+ return self._guestDiskMapping
+
+ @guestDiskMapping.setter
+ def guestDiskMapping(self, value):
+ self._guestDiskMapping = value
+ self._diskMappingHash = hash(json.dumps(value, sort_keys=True))
+
+ @property
+ def diskMappingHash(self):
+ return self._diskMappingHash
def connect(self):
self._prepare_socket()
@@ -303,8 +315,6 @@
disks.append(disk)
self.guestInfo['disksUsage'] = disks
self.guestDiskMapping = args.get('mapping', {})
- self.diskMappingHash = hash(json.dumps(self.guestDiskMapping,
- sort_keys=True))
elif message == 'number-of-cpus':
self.guestInfo['guestCPUCount'] = int(args['count'])
else:
--
To view, visit
http://gerrit.ovirt.org/34625
To unsubscribe, visit
http://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia914cf9860cef019b3040a3f034e479e6a93ed97
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Nir Soffer <nsoffer(a)redhat.com>