rhn_register: fix broken imports, add unit tests (#26878)

Fix 'module' object is not callable

* rhn_register: fix Python 3 compatibility
* rhn_register: update requirements
* rhn_register: add unit tests
* Add missing method name
* use a dedicated line for XML related requirements
* rhn_register: drop support for Python 2.4
* rhn_register unit tests: fix Python 3 compatibility
* refactor in order to check order of the requests
This commit is contained in:
Pilou 2017-08-04 16:12:27 +02:00 committed by Adrian Likins
commit b57af4428d
3 changed files with 333 additions and 12 deletions

View file

@ -27,6 +27,7 @@ notes:
- In order to register a system, rhnreg_ks requires either a username and password, or an activationkey.
requirements:
- rhnreg_ks
- either libxml2 or lxml
options:
state:
description:
@ -132,8 +133,6 @@ RETURN = '''
import os
import sys
import urlparse
import xmlrpclib
# Attempt to import rhn client tools
sys.path.insert(0, '/usr/share/rhn')
@ -146,7 +145,8 @@ except ImportError:
# INSERT REDHAT SNIPPETS
from ansible.module_utils import redhat
from ansible.module_utils.basic import AnsibleModule, get_exception
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six.moves import urllib, xmlrpc_client
class Rhn(redhat.RegistrationBase):
@ -184,7 +184,7 @@ class Rhn(redhat.RegistrationBase):
Returns: str
'''
url = urlparse.urlparse(self.server_url)
url = urllib.parse.urlparse(self.server_url)
return url[1].replace('xmlrpc.', '')
@property
@ -283,7 +283,7 @@ class Rhn(redhat.RegistrationBase):
url = "https://%s/rpc/api" % self.hostname
else:
url = "https://xmlrpc.%s/rpc/api" % self.hostname
self.server = xmlrpclib.Server(url, verbose=0)
self.server = xmlrpc_client.ServerProxy(url)
self.session = self.server.auth.login(self.username, self.password)
func = getattr(self.server, method)
@ -396,9 +396,8 @@ def main():
rhn.enable()
rhn.register(enable_eus, activationkey, profilename, sslcacert, systemorgid)
rhn.subscribe(channels)
except Exception:
e = get_exception()
module.fail_json(msg="Failed to register with '%s': %s" % (rhn.hostname, e))
except Exception as exc:
module.fail_json(msg="Failed to register with '%s': %s" % (rhn.hostname, exc))
finally:
rhn.logout()
@ -411,9 +410,8 @@ def main():
try:
rhn.unregister()
except Exception:
e = get_exception()
module.fail_json(msg="Failed to unregister: %s" % e)
except Exception as exc:
module.fail_json(msg="Failed to unregister: %s" % exc)
finally:
rhn.logout()