Skip to content

Commit

Permalink
VMware: Make DatastorePath hashable
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mdbooth committed Sep 25, 2014
1 parent e6f456a commit 43e2069
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
9 changes: 9 additions & 0 deletions nova/tests/virt/vmwareapi/test_ds_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import mock
from oslo.vmware import exceptions as vexc
from testtools import matchers

from nova import exception
from nova.i18n import _
Expand Down Expand Up @@ -447,6 +448,14 @@ def test_ds_path_non_equivalence(self):
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')
Expand Down
3 changes: 3 additions & 0 deletions nova/virt/vmwareapi/ds_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ def __eq__(self, other):
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."""
Expand Down

0 comments on commit 43e2069

Please sign in to comment.