Nir Soffer has uploaded a new change for review.

Change subject: storageServer: Add tests for equality and hash
......................................................................

storageServer: Add tests for equality and hash

Current __eq__ and __hash__ are wrong and have no tets. Add tests
showing what works and what not.

To make the tests results more clear, implement __str__. This can also
be useful when logging the objects.

Change-Id: Ie1dc9510a9f0f6c44534fd568cbcda161f36497c
Signed-off-by: Nir Soffer <[email protected]>
---
M tests/storageServerTests.py
M vdsm/storage/storageServer.py
2 files changed, 69 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/88/43988/1

diff --git a/tests/storageServerTests.py b/tests/storageServerTests.py
index 9b2939f..f835419 100644
--- a/tests/storageServerTests.py
+++ b/tests/storageServerTests.py
@@ -21,6 +21,7 @@
 from monkeypatch import MonkeyPatch
 from testlib import permutations, expandPermutations
 from testlib import VdsmTestCase
+from testValidation import brokentest
 from storage.storageServer import GlusterFSConnection
 from storage.storageServer import IscsiConnection
 from storage.storageServer import MountConnection
@@ -66,6 +67,67 @@
 
 
 @expandPermutations
+class TestMountConnectionEquality(VdsmTestCase):
+
+    def test_eq(self):
+        c1 = MountConnection("spec", "vfstype", "options")
+        c2 = MountConnection("spec", "vfstype", "options")
+        self.assertTrue(c1 == c2, "%s should equal %s" % (c1, c2))
+
+    @brokentest("__eq__ check isinstance instead fo class")
+    def test_eq_subclass(self):
+        class Subclass(MountConnection):
+            pass
+        c1 = MountConnection("spec", "vfstype", "options")
+        c2 = Subclass("spec", "vfstype", "options")
+        self.assertFalse(c1 == c2, "%s should not equal %s" % (c1, c2))
+
+    @brokentest("__eq__ not using all object state")
+    @permutations([
+        ("s1", "s2", "t", "t", "o", "o"),
+        ("s", "s", "t1", "t2", "o", "o"),
+        ("s", "s", "t", "t", "o1", "o2"),
+    ])
+    def test_eq_different(self, s1, s2, t1, t2, o1, o2):
+        c1 = MountConnection(s1, t1, o1)
+        c2 = MountConnection(s2, t2, o2)
+        self.assertFalse(c1 == c2, "%s should not equal %s" % (c1, c2))
+
+    @brokentest("__ne__ not implemented")
+    def test_ne(self):
+        c1 = MountConnection("spec", "vfstype", "options")
+        c2 = MountConnection("spec", "vfstype", "options")
+        self.assertFalse(c1 != c2, "%s should equal %s" % (c1, c2))
+
+
+@expandPermutations
+class TestMountConnectionHash(VdsmTestCase):
+
+    def test_eq(self):
+        c1 = MountConnection("spec", "vfstype", "options")
+        c2 = MountConnection("spec", "vfstype", "options")
+        self.assertEqual(hash(c1), hash(c2))
+
+    def test_eq_subclass(self):
+        class Subclass(MountConnection):
+            pass
+        c1 = MountConnection("spec", "vfstype", "options")
+        c2 = Subclass("spec", "vfstype", "options")
+        self.assertNotEqual(hash(c1), hash(c2))
+
+    @brokentest("__hash__ not using all object state")
+    @permutations([
+        ("s1", "s2", "t", "t", "o", "o"),
+        ("s", "s", "t1", "t2", "o", "o"),
+        ("s", "s", "t", "t", "o1", "o2"),
+    ])
+    def test_ne(self, s1, s2, t1, t2, o1, o2):
+        c1 = MountConnection(s1, t1, o1)
+        c2 = MountConnection(s2, t2, o2)
+        self.assertNotEqual(hash(c1), hash(c2))
+
+
+@expandPermutations
 class GlusterFSConnectionTests(VdsmTestCase):
 
     def test_mountpoint(self):
diff --git a/vdsm/storage/storageServer.py b/vdsm/storage/storageServer.py
index e71b88c..95e1099 100644
--- a/vdsm/storage/storageServer.py
+++ b/vdsm/storage/storageServer.py
@@ -264,6 +264,13 @@
     def __hash__(self):
         return hash(type(self)) ^ hash(self._mount)
 
+    def __str__(self):
+        return "<{0} spec={1!r} vfstype={2!r} options={3!r}>".format(
+            self.__class__.__name__,
+            self._remotePath,
+            self._vfsType,
+            self._options)
+
 
 class GlusterFSConnection(MountConnection):
 


-- 
To view, visit https://gerrit.ovirt.org/43988
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie1dc9510a9f0f6c44534fd568cbcda161f36497c
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Nir Soffer <[email protected]>
_______________________________________________
vdsm-patches mailing list
[email protected]
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to