[virt-who] Don't use deprecated way for reading certificates
by Radek Novacek
commit c881b29fe03138421e5f52738a1f830ba236fb44
Author: Radek Novacek <rad.n(a)centrum.cz>
Date: Fri Aug 24 11:46:07 2012 +0200
Don't use deprecated way for reading certificates
subscriptionmanager.py | 6 ++----
1 files changed, 2 insertions(+), 4 deletions(-)
---
diff --git a/subscriptionmanager.py b/subscriptionmanager.py
index 2ffd8ce..4ca98dc 100644
--- a/subscriptionmanager.py
+++ b/subscriptionmanager.py
@@ -95,12 +95,10 @@ class SubscriptionManager:
""" Read consumer certificate and get consumer UUID from it. """
if not self.cert_uuid:
try:
- f = open(self.cert_file, "r")
+ certificate = rhsm_certificate.create_from_file(self.cert_file)
+ self.cert_uuid = certificate.subject["CN"]
except Exception, e:
raise SubscriptionManagerError("Unable to open certificate %s (%s):" % self.cert_file, str(e))
- certificate = rhsm_certificate.Certificate(f.read())
- f.close()
- self.cert_uuid = certificate.subject().get('CN')
return self.cert_uuid
def getFacts(self):
11 years, 8 months
[virt-who] Don't start event loop when not running in background mode
by Radek Novacek
commit e1a3c535050a41360e42406917570bc3c3c7f52d
Author: Radek Novacek <rad.n(a)centrum.cz>
Date: Fri Aug 24 10:55:15 2012 +0200
Don't start event loop when not running in background mode
virt-who.py | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
---
diff --git a/virt-who.py b/virt-who.py
index c9afa0f..7faaa17 100644
--- a/virt-who.py
+++ b/virt-who.py
@@ -406,7 +406,7 @@ def main():
logger.error("Unable to fork, continuing in foreground")
if not options.oneshot:
- if options.virtType == "libvirt":
+ if options.background and options.virtType == "libvirt":
logger.debug("Starting event loop")
virEventLoopPureStart()
else:
11 years, 8 months
[virt-who/unittests] Start implementing unittest
by Radek Novacek
commit 4644a0f1f8e3dcb9f262c51dae45d4258ccf72d1
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Thu Jun 14 10:55:23 2012 +0200
Start implementing unittest
unittests/test_subscriptionmanager.py | 72 +++++++++++++++++++++++++++++++++
1 files changed, 72 insertions(+), 0 deletions(-)
---
diff --git a/unittests/test_subscriptionmanager.py b/unittests/test_subscriptionmanager.py
new file mode 100644
index 0000000..7a56684
--- /dev/null
+++ b/unittests/test_subscriptionmanager.py
@@ -0,0 +1,72 @@
+
+import os
+import sys
+import unittest
+import tempfile
+
+sys.path.append("..")
+from subscriptionmanager import *
+
+class FakeLogger(object):
+ def error(self, *p):
+ pass
+ def debug(self, *p):
+ pass
+ def exception(self, *p):
+ pass
+ def warning(self, *p):
+ pass
+
+class SubscriptionManagerX(SubscriptionManager):
+ def __init__(self, logger):
+ self.cert_uuid = None
+ self.logger = logger
+ self.config = FakeConfig()
+
+class FakeConfig(object):
+ def __init__(self):
+ self.tmpdir = tempfile.mkdtemp()
+
+ def createCert(self):
+ self.cert_file = os.path.join(self.tmpdir, "cert.pem")
+ with open(self.cert_file, "w") as f:
+ f.write("CERT")
+
+ def createKey(self):
+ with open(os.path.join(self.tmpdir, "key.pem"), "w") as f:
+ f.write("KEY")
+
+ def __del__(self):
+ try:
+ os.rmdir(self.tmpdir)
+ except Exception:
+ pass
+
+ def get(self, group, name):
+ if group == 'rhsm' and name == 'consumerCertDir':
+ return self.tmpdir
+
+class FakeConnection(object):
+ def __init__(self, cert_file, key_file):
+ self.cert_file = cert_file
+ self.key_file = key_file
+
+ def ping(self):
+ return { 'result': 'OK' }
+
+class Test_SubscriptionManager(unittest.TestCase):
+ def setUp(self):
+ self.logger = FakeLogger()
+
+ def test_readConfig(self):
+ self.sm = SubscriptionManagerX(self.logger)
+ self.assertRaises(SubscriptionManagerCertError, self.sm.readConfig)
+
+ self.sm.config.createCert()
+ self.sm.readConfig()
+
+ def test_connect(self):
+ self.sm = SubscriptionManagerX(self.logger)
+ self.sm.config.createCert()
+ self.sm.readConfig()
+ self.sm.connect(FakeConnection)
\ No newline at end of file
11 years, 8 months
[virt-who] Make the interval option work again
by Radek Novacek
commit 894afa9681590376bf39567435652455618d1eba
Author: Radek Novacek <rad.n(a)centrum.cz>
Date: Thu Aug 23 11:27:57 2012 +0200
Make the interval option work again
virt-who.py | 4 +++-
1 files changed, 3 insertions(+), 1 deletions(-)
---
diff --git a/virt-who.py b/virt-who.py
index eff9015..c9afa0f 100644
--- a/virt-who.py
+++ b/virt-who.py
@@ -442,9 +442,11 @@ def main():
if virtWho.send():
# Check if connection is established each 'RetryInterval' seconds
slept = 0
- while slept <= options.interval:
+ while slept < options.interval:
# Sleep 'RetryInterval' or the rest of options.interval
t = min(RetryInterval, options.interval - slept)
+ # But sleep at least one second
+ t = max(t, 1)
time.sleep(t)
slept += t
11 years, 8 months
[virt-who] Don't register domain events in one-shot mode.
by Radek Novacek
commit 5c65b9deaee75a8b5fe4f3eebcc6ac5af603be9f
Author: Radek Novacek <rnovacek(a)redhat.com>
Date: Tue Aug 7 10:22:00 2012 +0200
Don't register domain events in one-shot mode.
virt-who.py | 2 +-
virt.py | 9 +++++----
2 files changed, 6 insertions(+), 5 deletions(-)
---
diff --git a/virt-who.py b/virt-who.py
index 417c59f..eff9015 100644
--- a/virt-who.py
+++ b/virt-who.py
@@ -92,7 +92,7 @@ class VirtWho(object):
if self.options.virtType == "vdsm":
self.virt = VDSM(self.logger)
elif self.options.virtType == "libvirt":
- self.virt = Virt(self.logger)
+ self.virt = Virt(self.logger, registerEvents=not self.options.oneshot)
# We can listen for libvirt events
self.tryRegisterEventCallback()
else:
diff --git a/virt.py b/virt.py
index 4cb4b5e..9a09a04 100644
--- a/virt.py
+++ b/virt.py
@@ -26,7 +26,7 @@ class VirtError(Exception):
class Virt:
""" Class for interacting with libvirt. """
- def __init__(self, logger):
+ def __init__(self, logger, registerEvents=True):
self.changedCallback = None
self.logger = logger
self.virt = None
@@ -34,9 +34,10 @@ class Virt:
libvirt.registerErrorHandler(lambda ctx, error: None, None) #self.logger.exception(error), None)
try:
self.virt = libvirt.openReadOnly("")
- # Register listener for domain changes
- self.virt.domainEventRegister(self.changed, None)
- event.virtType = self.virt.getType()
+ # Register listener for domain changes if we are not in oneshot mode
+ if registerEvents:
+ self.virt.domainEventRegister(self.changed, None)
+ event.virtType = self.virt.getType()
except libvirt.libvirtError, e:
raise VirtError(str(e))
11 years, 8 months