From 43e2069cc9dea194da03d63d34d68e7a79686375 Mon Sep 17 00:00:00 2001 From: Matthew Booth Date: Tue, 23 Sep 2014 17:00:50 +0100 Subject: [PATCH] VMware: Make DatastorePath hashable Make DatastorePath work properly when used as a key value in a dict or set. This change specifically enables a subsequent change in the fake driver, which stores DatastorePath objects in a set. However, it's probably a good idea in general as I spent quite some time trying to work out why it didn't work when I hit it. Change-Id: Ia5519b84bc47ea1e0a5a70f9368806f762d12db5 --- nova/tests/virt/vmwareapi/test_ds_util.py | 9 +++++++++ nova/virt/vmwareapi/ds_util.py | 3 +++ 2 files changed, 12 insertions(+) diff --git a/nova/tests/virt/vmwareapi/test_ds_util.py b/nova/tests/virt/vmwareapi/test_ds_util.py index 2715f423e390..3ae1a60139d8 100644 --- a/nova/tests/virt/vmwareapi/test_ds_util.py +++ b/nova/tests/virt/vmwareapi/test_ds_util.py @@ -17,6 +17,7 @@ import re import mock from oslo.vmware import exceptions as vexc +from testtools import matchers from nova import exception from nova.i18n import _ @@ -447,6 +448,14 @@ class DatastorePathTestCase(test.NoDBTestCase): p = ds_util.DatastorePath(t[0], *t[1]) self.assertNotEqual(str(canonical_p), str(p)) + def test_ds_path_hashable(self): + ds1 = ds_util.DatastorePath('dsname', 'path') + ds2 = ds_util.DatastorePath('dsname', 'path') + + # If the above objects have the same hash, they will only be added to + # the set once + self.assertThat(set([ds1, ds2]), matchers.HasLength(1)) + def test_equal(self): a = ds_util.DatastorePath('ds_name', 'a') b = ds_util.DatastorePath('ds_name', 'a') diff --git a/nova/virt/vmwareapi/ds_util.py b/nova/virt/vmwareapi/ds_util.py index 764272e548e3..b5881480ee96 100644 --- a/nova/virt/vmwareapi/ds_util.py +++ b/nova/virt/vmwareapi/ds_util.py @@ -161,6 +161,9 @@ class DatastorePath(object): self._datastore_name == other._datastore_name and self._rel_path == other._rel_path) + def __hash__(self): + return str(self).__hash__() + @classmethod def parse(cls, datastore_path): """Constructs a DatastorePath object given a datastore path string."""