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
This commit is contained in:
Matthew Booth
2014-09-23 17:00:50 +01:00
parent e6f456a48d
commit 43e2069cc9
2 changed files with 12 additions and 0 deletions

View File

@@ -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')

View File

@@ -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."""