← Back to team overview

cloud-init-dev team mailing list archive

[Merge] ~dylan.perry/cloud-init:fix-yum-config-array-quoting into cloud-init:master

 

Dylan Perry has proposed merging ~dylan.perry/cloud-init:fix-yum-config-array-quoting into cloud-init:master.

Requested reviews:
  cloud init development team (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~dylan.perry/cloud-init/+git/cloud-init/+merge/322268

This is an attempt to fix https://bugs.launchpad.net/cloud-init/+bug/1592150

-- 
Your team cloud init development team is requested to review the proposed merge of ~dylan.perry/cloud-init:fix-yum-config-array-quoting into cloud-init:master.
diff --git a/cloudinit/config/cc_yum_add_repo.py b/cloudinit/config/cc_yum_add_repo.py
index ef8535e..3f1e706 100644
--- a/cloudinit/config/cc_yum_add_repo.py
+++ b/cloudinit/config/cc_yum_add_repo.py
@@ -32,7 +32,10 @@ entry, the config entry will be skipped.
 
 import os
 
-import configobj
+try:
+    from configparser import ConfigParser
+except ImportError:
+    from ConfigParser import ConfigParser
 import six
 
 from cloudinit import util
@@ -52,8 +55,7 @@ def _format_repo_value(val):
         return str(int(val))
     if isinstance(val, (list, tuple)):
         # Can handle 'lists' in certain cases
-        # See: http://bit.ly/Qqrf1t
-        return "\n    ".join([_format_repo_value(v) for v in val])
+        return "\n".join([_format_repo_value(v) for v in val])
     if not isinstance(val, six.string_types):
         return str(val)
     return val
@@ -62,16 +64,19 @@ def _format_repo_value(val):
 # TODO(harlowja): move to distro?
 # See man yum.conf
 def _format_repository_config(repo_id, repo_config):
-    to_be = configobj.ConfigObj()
-    to_be[repo_id] = {}
+    to_be = ConfigParser()
+    to_be.add_section(repo_id)
     # Do basic translation of the items -> values
     for (k, v) in repo_config.items():
         # For now assume that people using this know
         # the format of yum and don't verify keys/values further
-        to_be[repo_id][k] = _format_repo_value(v)
-    lines = to_be.write()
-    lines.insert(0, "# Created by cloud-init on %s" % (util.time_rfc2822()))
-    return "\n".join(lines)
+        to_be.set(repo_id, k, _format_repo_value(v))
+    to_be_stream = six.StringIO()
+    to_be.write(to_be_stream)
+    to_be_stream.seek(0)
+    lines = to_be_stream.readlines()
+    lines.insert(0, "# Created by cloud-init on %s\n" % (util.time_rfc2822()))
+    return "".join(lines)
 
 
 def handle(name, cfg, _cloud, log, _args):
diff --git a/tests/unittests/test_handler/test_handler_yum_add_repo.py b/tests/unittests/test_handler/test_handler_yum_add_repo.py
index 3feba86..4815bdb 100644
--- a/tests/unittests/test_handler/test_handler_yum_add_repo.py
+++ b/tests/unittests/test_handler/test_handler_yum_add_repo.py
@@ -5,10 +5,13 @@ from cloudinit import util
 
 from .. import helpers
 
-import configobj
+try:
+    from configparser import ConfigParser
+except ImportError:
+    from ConfigParser import ConfigParser
 import logging
 import shutil
-from six import BytesIO
+from six import StringIO
 import tempfile
 
 LOG = logging.getLogger(__name__)
@@ -54,9 +57,9 @@ class TestConfig(helpers.FilesystemMockingTestCase):
         }
         self.patchUtils(self.tmp)
         cc_yum_add_repo.handle('yum_add_repo', cfg, None, LOG, [])
-        contents = util.load_file("/etc/yum.repos.d/epel_testing.repo",
-                                  decode=False)
-        contents = configobj.ConfigObj(BytesIO(contents))
+        contents = util.load_file("/etc/yum.repos.d/epel_testing.repo")
+        parser = ConfigParser()
+        parser.readfp(StringIO(contents))
         expected = {
             'epel_testing': {
                 'name': 'Extra Packages for Enterprise Linux 5 - Testing',
@@ -67,6 +70,47 @@ class TestConfig(helpers.FilesystemMockingTestCase):
                 'gpgcheck': '1',
             }
         }
-        self.assertEqual(expected, dict(contents))
+        for section in expected:
+            self.assertTrue(parser.has_section(section),
+                            "Contains section {}".format(section))
+            for k, v in expected[section].items():
+                self.assertEqual(parser.get(section, k), v)
+
+    def test_write_config_array(self):
+        cfg = {
+            'yum_repos': {
+                'puppetlabs-products': {
+                    'name': 'Puppet Labs Products El 6 - $basearch',
+                    'baseurl':
+                        'http://yum.puppetlabs.com/el/6/products/$basearch',
+                    'gpgkey': [
+                        'file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs',
+                        'file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppet',
+                    ],
+                    'enabled': True,
+                    'gpgcheck': True,
+                }
+            }
+        }
+        self.patchUtils(self.tmp)
+        cc_yum_add_repo.handle('yum_add_repo', cfg, None, LOG, [])
+        contents = util.load_file("/etc/yum.repos.d/puppetlabs_products.repo")
+        parser = ConfigParser()
+        parser.readfp(StringIO(contents))
+        expected = {
+            'puppetlabs_products': {
+                'name': 'Puppet Labs Products El 6 - $basearch',
+                'baseurl': 'http://yum.puppetlabs.com/el/6/products/$basearch',
+                'gpgkey': 'file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppetlabs\n'
+                          'file:///etc/pki/rpm-gpg/RPM-GPG-KEY-puppet',
+                'enabled': '1',
+                'gpgcheck': '1',
+            }
+        }
+        for section in expected:
+            self.assertTrue(parser.has_section(section),
+                            "Contains section {}".format(section))
+            for k, v in expected[section].items():
+                self.assertEqual(parser.get(section, k), v)
 
 # vi: ts=4 expandtab

Follow ups