Change in vdsm[master]: ceph: Add libvirt secret testing infrastructure

nsoffer at redhat.com nsoffer at redhat.com
Mon May 11 20:39:30 UTC 2015


Nir Soffer has uploaded a new change for review.

Change subject: ceph: Add libvirt secret testing infrastructure
......................................................................

ceph: Add libvirt secret testing infrastructure

New vmfakelib.Error function create a libvirt.libvirtError with specific
error code, for testing code that must handle certain libvirt errors.

vmfakelib.Connection supports now secret related methods:

- secretDefineXML
- secretLookupByUUIDString

New vmfakelibTests module added to verify that the fakes are behaving
like the real libvirt objects.

Change-Id: Iac2d4ebaf8cadeff2818b2eac593b8d28b5d6706
Signed-off-by: Nir Soffer <nsoffer at redhat.com>
---
M tests/Makefile.am
M tests/vmfakelib.py
A tests/vmfakelibTests.py
3 files changed, 113 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/89/40789/1

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 02f2672..64010c4 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -103,6 +103,7 @@
 	vdsmDumpChainsTests.py \
 	verify.py \
 	vmApiTests.py \
+	vmfakelibTests.py \
 	vmMigrationTests.py \
 	vmStorageTests.py \
 	vmTests.py \
diff --git a/tests/vmfakelib.py b/tests/vmfakelib.py
index 995508c..ac78970 100644
--- a/tests/vmfakelib.py
+++ b/tests/vmfakelib.py
@@ -38,9 +38,28 @@
 from monkeypatch import MonkeyPatchScope
 
 
-class Connection:
+def Error(code, msg="fake error"):
+    e = libvirt.libvirtError(msg)
+    e.err = [code, None, msg]
+    return e
+
+
+class Connection(object):
+
     def __init__(self, *args):
-        pass
+        self.secrets = {}
+
+    def secretDefineXML(self, xml):
+        root = etree.fromstring(xml)
+        sec_uuid = root.find("./uuid").text
+        sec = Secret(self, sec_uuid, xml)
+        self.secrets[sec_uuid] = sec
+        return sec
+
+    def secretLookupByUUIDString(self, sec_uuid):
+        if sec_uuid not in self.secrets:
+            raise Error(libvirt.VIR_ERR_NO_SECRET)
+        return self.secrets[sec_uuid]
 
     def domainEventRegisterAny(self, *arg):
         pass
@@ -49,6 +68,21 @@
         return []
 
 
+class Secret(object):
+
+    def __init__(self, con, uuid, xml):
+        self.con = con
+        self.uuid = uuid
+        self.xml = xml
+        self.value = None
+
+    def undefine(self):
+        del self.con.secrets[self.uuid]
+
+    def setValue(self, value):
+        self.value = value
+
+
 class ClientIF(clientIF.clientIF):
     def __init__(self):
         # the bare minimum initialization for our test needs.
diff --git a/tests/vmfakelibTests.py b/tests/vmfakelibTests.py
new file mode 100644
index 0000000..68186f3
--- /dev/null
+++ b/tests/vmfakelibTests.py
@@ -0,0 +1,76 @@
+#
+# Copyright 2015 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+#
+# Refer to the README and COPYING files for full details of the license
+#
+
+import libvirt
+from testlib import VdsmTestCase
+import vmfakelib
+
+
+class SecretsTests(VdsmTestCase):
+
+    def test_define_new(self):
+        con = vmfakelib.Connection()
+        xml = """
+        <secret>
+            <uuid>uuid</uuid>
+        </secret>
+        """
+        con.secretDefineXML(xml)
+        sec = con.secrets['uuid']
+        self.assertEqual(sec.xml, xml)
+
+    def test_define_replace(self):
+        con = vmfakelib.Connection()
+        xml1 = """
+        <secret>
+            <description>old secert</description>
+            <uuid>uuid</uuid>
+        </secret>
+        """
+        xml2 = """
+        <secret>
+            <description>new secrect</description>
+            <uuid>uuid</uuid>
+        </secret>
+        """
+        con.secretDefineXML(xml1)
+        con.secretDefineXML(xml2)
+        sec = con.secrets['uuid']
+        self.assertEqual(sec.xml, xml2)
+
+    def test_lookup(self):
+        con = vmfakelib.Connection()
+        xml = """
+        <secret>
+            <uuid>uuid</uuid>
+        </secret>
+        """
+        con.secretDefineXML(xml)
+        sec = con.secretLookupByUUIDString('uuid')
+        self.assertEqual(sec.xml, xml)
+
+    def test_lookup_error(self):
+        con = vmfakelib.Connection()
+        try:
+            con.secretLookupByUUIDString('no-such-uuid')
+        except libvirt.libvirtError as e:
+            self.assertEqual(e.get_error_code(), libvirt.VIR_ERR_NO_SECRET)
+        else:
+            self.fail("libvirtError was not raised")


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iac2d4ebaf8cadeff2818b2eac593b8d28b5d6706
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Nir Soffer <nsoffer at redhat.com>


More information about the vdsm-patches mailing list