diff options
Diffstat (limited to 'roles/lib_utils')
| -rw-r--r-- | roles/lib_utils/library/yedit.py | 11 | ||||
| -rw-r--r-- | roles/lib_utils/meta/main.yml | 3 | ||||
| -rw-r--r-- | roles/lib_utils/src/class/yedit.py | 11 | ||||
| -rwxr-xr-x | roles/lib_utils/src/test/unit/test_repoquery.py | 20 | ||||
| -rwxr-xr-x | roles/lib_utils/src/test/unit/test_yedit.py | 33 | ||||
| -rw-r--r-- | roles/lib_utils/tasks/main.yml | 10 | 
6 files changed, 38 insertions, 50 deletions
| diff --git a/roles/lib_utils/library/yedit.py b/roles/lib_utils/library/yedit.py index b1d9d6869..a2ae6b4f6 100644 --- a/roles/lib_utils/library/yedit.py +++ b/roles/lib_utils/library/yedit.py @@ -311,7 +311,8 @@ class Yedit(object):                      continue                  elif data and not isinstance(data, dict): -                    return None +                    raise YeditException("Unexpected item type found while going through key " + +                                         "path: {} (at key: {})".format(key, dict_key))                  data[dict_key] = {}                  data = data[dict_key] @@ -320,7 +321,7 @@ class Yedit(object):                    int(arr_ind) <= len(data) - 1):                  data = data[int(arr_ind)]              else: -                return None +                raise YeditException("Unexpected item type found while going through key path: {}".format(key))          if key == '':              data = item @@ -334,6 +335,12 @@ class Yedit(object):          elif key_indexes[-1][1] and isinstance(data, dict):              data[key_indexes[-1][1]] = item +        # didn't add/update to an existing list, nor add/update key to a dict +        # so we must have been provided some syntax like a.b.c[<int>] = "data" for a +        # non-existent array +        else: +            raise YeditException("Error adding to object at path: {}".format(key)) +          return data      @staticmethod diff --git a/roles/lib_utils/meta/main.yml b/roles/lib_utils/meta/main.yml index e06b9a0f1..cc18c453c 100644 --- a/roles/lib_utils/meta/main.yml +++ b/roles/lib_utils/meta/main.yml @@ -11,5 +11,4 @@ galaxy_info:      - 7    categories:    - cloud -dependencies: -- { role: openshift_repos } +dependencies: [] diff --git a/roles/lib_utils/src/class/yedit.py b/roles/lib_utils/src/class/yedit.py index 74ee52fe3..533665db2 100644 --- a/roles/lib_utils/src/class/yedit.py +++ b/roles/lib_utils/src/class/yedit.py @@ -125,7 +125,8 @@ class Yedit(object):                      continue                  elif data and not isinstance(data, dict): -                    return None +                    raise YeditException("Unexpected item type found while going through key " + +                                         "path: {} (at key: {})".format(key, dict_key))                  data[dict_key] = {}                  data = data[dict_key] @@ -134,7 +135,7 @@ class Yedit(object):                    int(arr_ind) <= len(data) - 1):                  data = data[int(arr_ind)]              else: -                return None +                raise YeditException("Unexpected item type found while going through key path: {}".format(key))          if key == '':              data = item @@ -148,6 +149,12 @@ class Yedit(object):          elif key_indexes[-1][1] and isinstance(data, dict):              data[key_indexes[-1][1]] = item +        # didn't add/update to an existing list, nor add/update key to a dict +        # so we must have been provided some syntax like a.b.c[<int>] = "data" for a +        # non-existent array +        else: +            raise YeditException("Error adding to object at path: {}".format(key)) +          return data      @staticmethod diff --git a/roles/lib_utils/src/test/unit/test_repoquery.py b/roles/lib_utils/src/test/unit/test_repoquery.py index c487ab254..e39d9d83f 100755 --- a/roles/lib_utils/src/test/unit/test_repoquery.py +++ b/roles/lib_utils/src/test/unit/test_repoquery.py @@ -1,14 +1,6 @@ -#!/usr/bin/env python2  '''   Unit tests for repoquery  ''' -# To run: -# ./repoquery.py -# -# . -# Ran 1 test in 0.002s -# -# OK  import os  import sys @@ -31,10 +23,6 @@ class RepoQueryTest(unittest.TestCase):       Test class for RepoQuery      ''' -    def setUp(self): -        ''' setup method for other tests ''' -        pass -      @mock.patch('repoquery._run')      def test_querying_a_package(self, mock_cmd):          ''' Testing querying a package ''' @@ -77,11 +65,3 @@ class RepoQueryTest(unittest.TestCase):          mock_cmd.assert_has_calls([              mock.call(['/usr/bin/repoquery', '--plugins', '--quiet', '--pkgnarrow=repos', '--queryformat=%{version}|%{release}|%{arch}|%{repo}|%{version}-%{release}', 'bash']),          ]) - -    def tearDown(self): -        '''TearDown method''' -        pass - - -if __name__ == "__main__": -    unittest.main() diff --git a/roles/lib_utils/src/test/unit/test_yedit.py b/roles/lib_utils/src/test/unit/test_yedit.py index ed07ac96e..23a3f7353 100755 --- a/roles/lib_utils/src/test/unit/test_yedit.py +++ b/roles/lib_utils/src/test/unit/test_yedit.py @@ -1,14 +1,6 @@ -#!/usr/bin/env python2  '''   Unit tests for yedit  ''' -# To run -# python -m unittest yedit_test -# -# ............................. -# ---------------------------------------------------------------------- -# Ran 29 tests in 0.133s -# OK  import os  import sys @@ -23,7 +15,7 @@ import unittest  yedit_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library')  # noqa: E501  sys.path.insert(0, yedit_path) -from yedit import Yedit  # noqa: E402 +from yedit import Yedit, YeditException  # noqa: E402  # pylint: disable=too-many-public-methods  # Silly pylint, moar tests! @@ -200,7 +192,6 @@ class YeditTest(unittest.TestCase):          yed.append('x:y:z', [5, 6])          yed.append('x:y:z', [5, 6])          self.assertTrue(yed.get('x:y:z') == [1, 2, 3, [5, 6], [5, 6]]) -        self.assertTrue(2 == yed.get('x:y:z').count([5, 6]))          self.assertFalse(yed.exists('x:y:z', 4))      def test_add_item_to_dict(self): @@ -268,10 +259,24 @@ class YeditTest(unittest.TestCase):          yed.pop('a#b', 'c')          self.assertTrue({'a': {'b': {'d': 2}}} == yed.yaml_dict) +    def test_accessing_path_with_unexpected_objects(self): +        '''test providing source path objects that differ from current object state''' +        yed = Yedit(content={'a': {'b': {'c': ['d', 'e']}}}) +        with self.assertRaises(YeditException): +            yed.put('a.b.c.d', 'x') + +    def test_creating_new_objects_with_embedded_list(self): +        '''test creating new objects with an embedded list in the creation path''' +        yed = Yedit(content={'a': {'b': 12}}) +        with self.assertRaises(YeditException): +            yed.put('new.stuff[0].here', 'value') + +    def test_creating_new_objects_with_trailing_list(self): +        '''test creating new object(s) where the final piece is a list''' +        yed = Yedit(content={'a': {'b': 12}}) +        with self.assertRaises(YeditException): +            yed.put('new.stuff.here[0]', 'item') +      def tearDown(self):          '''TearDown method'''          os.unlink(YeditTest.filename) - - -if __name__ == "__main__": -    unittest.main() diff --git a/roles/lib_utils/tasks/main.yml b/roles/lib_utils/tasks/main.yml index 32ab9e0c6..ed97d539c 100644 --- a/roles/lib_utils/tasks/main.yml +++ b/roles/lib_utils/tasks/main.yml @@ -1,11 +1 @@  --- -- name: lib_utils detect ostree -  stat: -    path: /run/ostree-booted -  register: ostree_booted - -- name: lib_utils ensure python-ruamel-yaml package is on target -  package: -    name: python-ruamel-yaml -    state: present -  when: not ostree_booted.stat.exists | 
