sailesh1993 / rpms / cloud-init

Forked from rpms/cloud-init a year ago
Clone
3956c3
From fc4ca923d02886669f2f0e0916732133e1969bb6 Mon Sep 17 00:00:00 2001
3956c3
From: Dave Mulford <dmulford@redhat.com>
3956c3
Date: Mon, 9 Oct 2017 15:28:15 -0500
3956c3
Subject: [PATCH] rh_subscription: Perform null checks for enabled and disabled
3956c3
 repos.
3956c3
3956c3
The rh_subscription module doesn't perform null checks when attempting to
3956c3
iterate on the enabled and disable repos arrays. When only one is
3956c3
specified, cloud-init fails to run.
3956c3
3956c3
(cherry picked from commit 9bc4ce0596544ffa56d9d67245b00e07006a8662)
3956c3
3956c3
Resolves: rhbz#1498974
3956c3
Signed-off-by: Ryan McCabe <rmccabe@redhat.com>
3956c3
---
3956c3
 cloudinit/config/cc_rh_subscription.py  | 46 ++++++++++++++++++++-------------
3956c3
 tests/unittests/test_rh_subscription.py | 15 +++++++++++
3956c3
 2 files changed, 43 insertions(+), 18 deletions(-)
3956c3
3956c3
diff --git a/cloudinit/config/cc_rh_subscription.py b/cloudinit/config/cc_rh_subscription.py
3956c3
index 7f36cf8f..a9d21e78 100644
3956c3
--- a/cloudinit/config/cc_rh_subscription.py
3956c3
+++ b/cloudinit/config/cc_rh_subscription.py
3956c3
@@ -38,14 +38,16 @@ Subscription`` example config.
3956c3
         server-hostname: <hostname>
3956c3
 """
3956c3
 
3956c3
+from cloudinit import log as logging
3956c3
 from cloudinit import util
3956c3
 
3956c3
+LOG = logging.getLogger(__name__)
3956c3
+
3956c3
 distros = ['fedora', 'rhel']
3956c3
 
3956c3
 
3956c3
 def handle(name, cfg, _cloud, log, _args):
3956c3
-    sm = SubscriptionManager(cfg)
3956c3
-    sm.log = log
3956c3
+    sm = SubscriptionManager(cfg, log=log)
3956c3
     if not sm.is_configured():
3956c3
         log.debug("%s: module not configured.", name)
3956c3
         return None
3956c3
@@ -86,10 +88,9 @@ def handle(name, cfg, _cloud, log, _args):
3956c3
                 if not return_stat:
3956c3
                     raise SubscriptionError("Unable to attach pools {0}"
3956c3
                                             .format(sm.pools))
3956c3
-            if (sm.enable_repo is not None) or (sm.disable_repo is not None):
3956c3
-                return_stat = sm.update_repos(sm.enable_repo, sm.disable_repo)
3956c3
-                if not return_stat:
3956c3
-                    raise SubscriptionError("Unable to add or remove repos")
3956c3
+            return_stat = sm.update_repos()
3956c3
+            if not return_stat:
3956c3
+                raise SubscriptionError("Unable to add or remove repos")
3956c3
             sm.log_success("rh_subscription plugin completed successfully")
3956c3
         except SubscriptionError as e:
3956c3
             sm.log_warn(str(e))
3956c3
@@ -108,7 +109,10 @@ class SubscriptionManager(object):
3956c3
                      'rhsm-baseurl', 'server-hostname',
3956c3
                      'auto-attach', 'service-level']
3956c3
 
3956c3
-    def __init__(self, cfg):
3956c3
+    def __init__(self, cfg, log=None):
3956c3
+        if log is None:
3956c3
+            log = LOG
3956c3
+        self.log = log
3956c3
         self.cfg = cfg
3956c3
         self.rhel_cfg = self.cfg.get('rh_subscription', {})
3956c3
         self.rhsm_baseurl = self.rhel_cfg.get('rhsm-baseurl')
3956c3
@@ -130,7 +134,7 @@ class SubscriptionManager(object):
3956c3
 
3956c3
     def log_warn(self, msg):
3956c3
         '''Simple wrapper for logging warning messages. Useful for unittests'''
3956c3
-        self.log.warn(msg)
3956c3
+        self.log.warning(msg)
3956c3
 
3956c3
     def _verify_keys(self):
3956c3
         '''
3956c3
@@ -245,7 +249,7 @@ class SubscriptionManager(object):
3956c3
             return False
3956c3
 
3956c3
         reg_id = return_out.split("ID: ")[1].rstrip()
3956c3
-        self.log.debug("Registered successfully with ID {0}".format(reg_id))
3956c3
+        self.log.debug("Registered successfully with ID %s", reg_id)
3956c3
         return True
3956c3
 
3956c3
     def _set_service_level(self):
3956c3
@@ -347,7 +351,7 @@ class SubscriptionManager(object):
3956c3
             try:
3956c3
                 self._sub_man_cli(cmd)
3956c3
                 self.log.debug("Attached the following pools to your "
3956c3
-                               "system: %s" % (", ".join(pool_list))
3956c3
+                               "system: %s", (", ".join(pool_list))
3956c3
                                .replace('--pool=', ''))
3956c3
                 return True
3956c3
             except util.ProcessExecutionError as e:
3956c3
@@ -355,18 +359,24 @@ class SubscriptionManager(object):
3956c3
                               "due to {1}".format(pool, e))
3956c3
                 return False
3956c3
 
3956c3
-    def update_repos(self, erepos, drepos):
3956c3
+    def update_repos(self):
3956c3
         '''
3956c3
         Takes a list of yum repo ids that need to be disabled or enabled; then
3956c3
         it verifies if they are already enabled or disabled and finally
3956c3
         executes the action to disable or enable
3956c3
         '''
3956c3
 
3956c3
-        if (erepos is not None) and (not isinstance(erepos, list)):
3956c3
+        erepos = self.enable_repo
3956c3
+        drepos = self.disable_repo
3956c3
+        if erepos is None:
3956c3
+            erepos = []
3956c3
+        if drepos is None:
3956c3
+            drepos = []
3956c3
+        if not isinstance(erepos, list):
3956c3
             self.log_warn("Repo IDs must in the format of a list.")
3956c3
             return False
3956c3
 
3956c3
-        if (drepos is not None) and (not isinstance(drepos, list)):
3956c3
+        if not isinstance(drepos, list):
3956c3
             self.log_warn("Repo IDs must in the format of a list.")
3956c3
             return False
3956c3
 
3956c3
@@ -399,14 +409,14 @@ class SubscriptionManager(object):
3956c3
             for fail in enable_list_fail:
3956c3
                 # Check if the repo exists or not
3956c3
                 if fail in active_repos:
3956c3
-                    self.log.debug("Repo {0} is already enabled".format(fail))
3956c3
+                    self.log.debug("Repo %s is already enabled", fail)
3956c3
                 else:
3956c3
                     self.log_warn("Repo {0} does not appear to "
3956c3
                                   "exist".format(fail))
3956c3
         if len(disable_list_fail) > 0:
3956c3
             for fail in disable_list_fail:
3956c3
-                self.log.debug("Repo {0} not disabled "
3956c3
-                               "because it is not enabled".format(fail))
3956c3
+                self.log.debug("Repo %s not disabled "
3956c3
+                               "because it is not enabled", fail)
3956c3
 
3956c3
         cmd = ['repos']
3956c3
         if len(disable_list) > 0:
3956c3
@@ -422,10 +432,10 @@ class SubscriptionManager(object):
3956c3
             return False
3956c3
 
3956c3
         if len(enable_list) > 0:
3956c3
-            self.log.debug("Enabled the following repos: %s" %
3956c3
+            self.log.debug("Enabled the following repos: %s",
3956c3
                            (", ".join(enable_list)).replace('--enable=', ''))
3956c3
         if len(disable_list) > 0:
3956c3
-            self.log.debug("Disabled the following repos: %s" %
3956c3
+            self.log.debug("Disabled the following repos: %s",
3956c3
                            (", ".join(disable_list)).replace('--disable=', ''))
3956c3
         return True
3956c3
 
3956c3
diff --git a/tests/unittests/test_rh_subscription.py b/tests/unittests/test_rh_subscription.py
3956c3
index ca14cd46..7b35b9d0 100644
3956c3
--- a/tests/unittests/test_rh_subscription.py
3956c3
+++ b/tests/unittests/test_rh_subscription.py
3956c3
@@ -2,6 +2,7 @@
3956c3
 
3956c3
 """Tests for registering RHEL subscription via rh_subscription."""
3956c3
 
3956c3
+import copy
3956c3
 import logging
3956c3
 
3956c3
 from cloudinit.config import cc_rh_subscription
3956c3
@@ -68,6 +69,20 @@ class GoodTests(TestCase):
3956c3
         self.assertEqual(self.SM.log_success.call_count, 1)
3956c3
         self.assertEqual(self.SM._sub_man_cli.call_count, 2)
3956c3
 
3956c3
+    @mock.patch.object(cc_rh_subscription.SubscriptionManager, "_getRepos")
3956c3
+    @mock.patch.object(cc_rh_subscription.SubscriptionManager, "_sub_man_cli")
3956c3
+    def test_update_repos_disable_with_none(self, m_sub_man_cli, m_get_repos):
3956c3
+        cfg = copy.deepcopy(self.config)
3956c3
+        m_get_repos.return_value = ([], ['repo1'])
3956c3
+        m_sub_man_cli.return_value = (b'', b'')
3956c3
+        cfg['rh_subscription'].update(
3956c3
+            {'enable-repo': ['repo1'], 'disable-repo': None})
3956c3
+        mysm = cc_rh_subscription.SubscriptionManager(cfg)
3956c3
+        self.assertEqual(True, mysm.update_repos())
3956c3
+        m_get_repos.assert_called_with()
3956c3
+        self.assertEqual(m_sub_man_cli.call_args_list,
3956c3
+                         [mock.call(['repos', '--enable=repo1'])])
3956c3
+
3956c3
     def test_full_registration(self):
3956c3
         '''
3956c3
         Registration with auto-attach, service-level, adding pools,
3956c3
-- 
3956c3
2.13.6
3956c3