summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDaniel P. Berrangé <berrange@redhat.com>2020-10-26 11:02:49 +0000
committerDaniel Berrange <berrange@redhat.com>2020-11-06 10:35:05 +0000
commit4a6f381bd9d1eb23220e1497ee6be646dec8e9ea (patch)
tree406b92dbcd4fbc8171ed423802b9410fb3ecb970 /tests
parent2822e231569b1546988f48583c73e846047f3e3a (diff)
downloadlibvirt-python-4a6f381bd9d1eb23220e1497ee6be646dec8e9ea.tar.gz
Add unit tests for openAuth method
Validate that the type hinting is working as expected Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/test_conn.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/tests/test_conn.py b/tests/test_conn.py
index a5eb797..557384a 100644
--- a/tests/test_conn.py
+++ b/tests/test_conn.py
@@ -1,5 +1,8 @@
import unittest
import libvirt
+import tempfile
+import contextlib
+import os
class TestLibvirtConn(unittest.TestCase):
@@ -14,3 +17,76 @@ class TestLibvirtConn(unittest.TestCase):
self.assertEquals(len(doms), 1)
self.assertEquals(type(doms[0]), libvirt.virDomain)
self.assertEquals(doms[0].name(), "test")
+
+class TestLibvirtConnAuth(unittest.TestCase):
+ connXML = """
+<node>
+ <auth>
+ <user password="2147483647">marin</user>
+ <user password="87539319">srinivasa</user>
+ </auth>
+</node>"""
+ def setUp(self):
+ def noop(msg, opaque):
+ pass
+ libvirt.registerErrorHandler(noop, None)
+
+ @contextlib.contextmanager
+ def tempxmlfile(content):
+ try:
+ fp = tempfile.NamedTemporaryFile(delete=False,
+ prefix="libvirt-python-test",
+ suffix=".xml")
+ fname = fp.name
+ fp.write(content.encode("utf8"))
+ fp.close()
+ yield fname
+ finally:
+ os.unlink(fname)
+
+ def authHelper(self, username, password):
+ with TestLibvirtConnAuth.tempxmlfile(self.connXML) as fname:
+ magic = 142857
+ def authCB(creds, opaque):
+ if opaque != magic:
+ return -1
+
+ for cred in creds:
+ if (cred[0] == libvirt.VIR_CRED_AUTHNAME and
+ username is not None):
+ cred[4] = username
+ return 0
+ elif (cred[0] == libvirt.VIR_CRED_PASSPHRASE and
+ password is not None):
+ cred[4] = password
+ return 0
+ return -1
+ return 0
+
+ auth = [[libvirt.VIR_CRED_AUTHNAME,
+ libvirt.VIR_CRED_ECHOPROMPT,
+ libvirt.VIR_CRED_REALM,
+ libvirt.VIR_CRED_PASSPHRASE,
+ libvirt.VIR_CRED_NOECHOPROMPT,
+ libvirt.VIR_CRED_EXTERNAL],
+ authCB, magic]
+
+ return libvirt.openAuth("test://" + fname,
+ auth, 0)
+
+ def testOpenAuthGood(self):
+ conn = self.authHelper("srinivasa", "87539319")
+
+ def testOpenAuthBad(self):
+ try:
+ conn = self.authHelper("srinivasa", "2147483647")
+ raise Exception("Unexpected open success")
+ except libvirt.libvirtError as ex:
+ pass
+
+ def testOpenAuthNone(self):
+ try:
+ conn = self.authHelper(None, None)
+ raise Exception("Unexpected open success")
+ except libvirt.libvirtError as ex:
+ pass