diff options
author | Kenny Woodson <kwoodson@redhat.com> | 2017-03-24 13:07:24 -0400 |
---|---|---|
committer | Kenny Woodson <kwoodson@redhat.com> | 2017-03-28 15:28:34 -0400 |
commit | aa9e19c20b745f91a47292f8754a8cf52017c3e4 (patch) | |
tree | 4bb2dd64e3adc6483c5e76cad76d9f211ecffd50 /roles/lib_utils/src/test/unit/test_yedit.py | |
parent | a69a84339e1bf187aa970316a4148138cb7f82fb (diff) | |
download | openshift-aa9e19c20b745f91a47292f8754a8cf52017c3e4.tar.gz openshift-aa9e19c20b745f91a47292f8754a8cf52017c3e4.tar.bz2 openshift-aa9e19c20b745f91a47292f8754a8cf52017c3e4.tar.xz openshift-aa9e19c20b745f91a47292f8754a8cf52017c3e4.zip |
Adding a few more test cases. Fixed a bug when key was empty. Safeguard against yedit module being passed an empty key
Diffstat (limited to 'roles/lib_utils/src/test/unit/test_yedit.py')
-rwxr-xr-x | roles/lib_utils/src/test/unit/test_yedit.py | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/roles/lib_utils/src/test/unit/test_yedit.py b/roles/lib_utils/src/test/unit/test_yedit.py index 23a3f7353..f9f42843a 100755 --- a/roles/lib_utils/src/test/unit/test_yedit.py +++ b/roles/lib_utils/src/test/unit/test_yedit.py @@ -5,6 +5,7 @@ import os import sys import unittest +import mock # Removing invalid variable names for tests so that I can # keep them brief @@ -277,6 +278,91 @@ class YeditTest(unittest.TestCase): with self.assertRaises(YeditException): yed.put('new.stuff.here[0]', 'item') + def test_empty_key_with_int_value(self): + '''test editing top level with not list or dict''' + yed = Yedit(content={'a': {'b': 12}}) + result = yed.put('', 'b') + self.assertFalse(result[0]) + + def test_setting_separator(self): + '''test editing top level with not list or dict''' + yed = Yedit(content={'a': {'b': 12}}) + yed.separator = ':' + self.assertEqual(yed.separator, ':') + + def test_remove_all(self): + '''test removing all data''' + data = Yedit.remove_entry({'a': {'b': 12}}, '') + self.assertTrue(data) + + def test_remove_list_entry(self): + '''test removing list entry''' + data = {'a': {'b': [{'c': 3}]}} + results = Yedit.remove_entry(data, 'a.b[0]') + self.assertTrue(results) + self.assertTrue(data, {'a': {'b': []}}) + + def test_parse_value_string_true(self): + '''test parse_value''' + results = Yedit.parse_value('true', 'str') + self.assertEqual(results, 'true') + + def test_parse_value_bool_true(self): + '''test parse_value''' + results = Yedit.parse_value('true', 'bool') + self.assertTrue(results) + + def test_parse_value_bool_exception(self): + '''test parse_value''' + with self.assertRaises(YeditException): + Yedit.parse_value('TTT', 'bool') + + @mock.patch('yedit.Yedit.write') + def test_run_ansible_basic(self, mock_write): + '''test parse_value''' + params = { + 'src': None, + 'backup': False, + 'separator': '.', + 'state': 'present', + 'edits': [], + 'value': None, + 'key': None, + 'content': {'a': {'b': {'c': 1}}}, + 'content_type': '', + } + + results = Yedit.run_ansible(params) + + mock_write.side_effect = [ + (True, params['content']), + ] + + self.assertFalse(results['changed']) + + @mock.patch('yedit.Yedit.write') + def test_run_ansible_and_write(self, mock_write): + '''test parse_value''' + params = { + 'src': '/tmp/test', + 'backup': False, + 'separator': '.', + 'state': 'present', + 'edits': [], + 'value': None, + 'key': None, + 'content': {'a': {'b': {'c': 1}}}, + 'content_type': '', + } + + results = Yedit.run_ansible(params) + + mock_write.side_effect = [ + (True, params['content']), + ] + + self.assertTrue(results['changed']) + def tearDown(self): '''TearDown method''' os.unlink(YeditTest.filename) |