浏览代码

Fixed a bug in modify_yaml module.

When adding a property to a section that does not yet exist, the
property was being inserted into the section above where it should have
due to the pointer not being adjusted for the new section we created.

Adds a test sub-directory, would like to eventually move all the
ooinstall tests here as well so nosetests just works from top level
directory.
Devan Goodwin 8 年之前
父节点
当前提交
f28826b474
共有 2 个文件被更改,包括 56 次插入11 次删除
  1. 19 11
      library/modify_yaml.py
  2. 37 0
      test/modify_yaml_tests.py

+ 19 - 11
library/modify_yaml.py

@@ -20,6 +20,24 @@ EXAMPLES = '''
     yaml_value: 2
 '''
 
+
+# pylint: disable=missing-docstring
+def set_key(yaml_data, yaml_key, yaml_value):
+    changes = []
+    ptr = yaml_data
+    for key in yaml_key.split('.'):
+        if key not in ptr and key != yaml_key.split('.')[-1]:
+            ptr[key] = {}
+            ptr = ptr[key]
+        elif key == yaml_key.split('.')[-1]:
+            if (key in ptr and module.safe_eval(ptr[key]) != yaml_value) or (key not in ptr):
+                ptr[key] = yaml_value
+                changes.append((yaml_key, yaml_value))
+        else:
+            ptr = ptr[key]
+    return changes
+
+
 def main():
     ''' Modify key (supplied in jinja2 dot notation) in yaml file, setting
         the key to the desired value.
@@ -53,22 +71,12 @@ def main():
     yaml.add_representer(type(None), none_representer)
 
     try:
-        changes = []
 
         yaml_file = open(dest)
         yaml_data = yaml.safe_load(yaml_file.read())
         yaml_file.close()
 
-        ptr = yaml_data
-        for key in yaml_key.split('.'):
-            if key not in ptr and key != yaml_key.split('.')[-1]:
-                ptr[key] = {}
-            elif key == yaml_key.split('.')[-1]:
-                if (key in ptr and module.safe_eval(ptr[key]) != yaml_value) or (key not in ptr):
-                    ptr[key] = yaml_value
-                    changes.append((yaml_key, yaml_value))
-            else:
-                ptr = ptr[key]
+        changes = set_key(yaml_data, yaml_key, yaml_value)
 
         if len(changes) > 0:
             if backup:

+ 37 - 0
test/modify_yaml_tests.py

@@ -0,0 +1,37 @@
+""" Tests for the modify_yaml Ansible module. """
+# pylint: disable=missing-docstring,invalid-name
+
+import os
+import sys
+import unittest
+
+sys.path = [os.path.abspath(os.path.dirname(__file__) + "/../library/")] + sys.path
+
+# pylint: disable=import-error
+from modify_yaml import set_key
+
+class ModifyYamlTests(unittest.TestCase):
+
+    def test_simple_nested_value(self):
+        cfg = {"section": {"a": 1, "b": 2}}
+        changes = set_key(cfg, 'section.c', 3)
+        self.assertEquals(1, len(changes))
+        self.assertEquals(3, cfg['section']['c'])
+
+    # Tests a previous bug where property would land in section above where it should,
+    # if the destination section did not yet exist:
+    def test_nested_property_in_new_section(self):
+        cfg = {
+            "masterClients": {
+                "externalKubernetesKubeConfig": "",
+                "openshiftLoopbackKubeConfig": "openshift-master.kubeconfig",
+            },
+        }
+
+        yaml_key = 'masterClients.externalKubernetesClientConnectionOverrides.acceptContentTypes'
+        yaml_value = 'application/vnd.kubernetes.protobuf,application/json'
+        set_key(cfg, yaml_key, yaml_value)
+        self.assertEquals(yaml_value, cfg['masterClients']
+                          ['externalKubernetesClientConnectionOverrides']
+                          ['acceptContentTypes'])
+