Browse code

Adding more unit tests for AnsibleModule things in basic.py

James Cammarata authored on 2016/03/17 15:01:47
Showing 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,58 @@
0
+# -*- coding: utf-8 -*-
1
+# (c) 2016, James Cammarata <jimi@sngx.net>
2
+#
3
+# This file is part of Ansible
4
+#
5
+# Ansible is free software: you can redistribute it and/or modify
6
+# it under the terms of the GNU General Public License as published by
7
+# the Free Software Foundation, either version 3 of the License, or
8
+# (at your option) any later version.
9
+#
10
+# Ansible is distributed in the hope that it will be useful,
11
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+# GNU General Public License for more details.
14
+#
15
+# You should have received a copy of the GNU General Public License
16
+# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
17
+
18
+# Make coding more python3-ish
19
+from __future__ import (absolute_import, division)
20
+__metaclass__ = type
21
+
22
+import json
23
+
24
+from ansible.compat.tests import unittest
25
+from ansible.compat.tests.mock import MagicMock
26
+
27
+class TestModuleUtilsBasic(unittest.TestCase):
28
+
29
+    def test_module_utils_basic__log_invocation(self):
30
+        from ansible.module_utils import basic
31
+
32
+        # test basic log invocation
33
+        basic.MODULE_COMPLEX_ARGS = json.dumps(dict(foo=False, bar=[1,2,3], bam="bam", baz=u'baz'))
34
+        am = basic.AnsibleModule(
35
+            argument_spec=dict(
36
+                foo = dict(default=True, type='bool'),
37
+                bar = dict(default=[], type='list'),
38
+                bam = dict(default="bam"),
39
+                baz = dict(default=u"baz"),
40
+                password = dict(default=True),
41
+                no_log = dict(default="you shouldn't see me", no_log=True),
42
+            ),
43
+        )
44
+
45
+        am.log = MagicMock()
46
+        am._log_invocation()
47
+        am.log.assert_called_with(
48
+            'Invoked with bam=bam bar=[1, 2, 3] foo=False baz=baz no_log=NOT_LOGGING_PARAMETER password=NOT_LOGGING_PASSWORD ',
49
+            log_args={
50
+                'foo': 'False',
51
+                'bar': '[1, 2, 3]',
52
+                'bam': 'bam',
53
+                'baz': 'baz',
54
+                'password': 'NOT_LOGGING_PASSWORD',
55
+                'no_log': 'NOT_LOGGING_PARAMETER',
56
+            },
57
+        )
0 58
new file mode 100644
... ...
@@ -0,0 +1,64 @@
0
+# -*- coding: utf-8 -*-
1
+# (c) 2015, Toshio Kuratomi <tkuratomi@ansible.com>
2
+#
3
+# This file is part of Ansible
4
+#
5
+# Ansible is free software: you can redistribute it and/or modify
6
+# it under the terms of the GNU General Public License as published by
7
+# the Free Software Foundation, either version 3 of the License, or
8
+# (at your option) any later version.
9
+#
10
+# Ansible is distributed in the hope that it will be useful,
11
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+# GNU General Public License for more details.
14
+#
15
+# You should have received a copy of the GNU General Public License
16
+# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
17
+
18
+# Make coding more python3-ish
19
+from __future__ import (absolute_import, division)
20
+__metaclass__ = type
21
+
22
+from ansible.compat.tests import unittest
23
+
24
+
25
+class TestAnsibleModuleExitJson(unittest.TestCase):
26
+
27
+    def test_module_utils_basic_safe_eval(self):
28
+        from ansible.module_utils import basic
29
+
30
+        basic.MODULE_COMPLEX_ARGS = '{}'
31
+        am = basic.AnsibleModule(
32
+            argument_spec=dict(),
33
+        )
34
+
35
+        # test some basic usage
36
+        # string (and with exceptions included), integer, bool
37
+        self.assertEqual(am.safe_eval("'a'"), 'a')
38
+        self.assertEqual(am.safe_eval("'a'", include_exceptions=True), ('a', None))
39
+        self.assertEqual(am.safe_eval("1"), 1)
40
+        self.assertEqual(am.safe_eval("True"), True)
41
+        self.assertEqual(am.safe_eval("False"), False)
42
+        self.assertEqual(am.safe_eval("{}"), {})
43
+        # not passing in a string to convert
44
+        self.assertEqual(am.safe_eval({'a':1}), {'a':1})
45
+        self.assertEqual(am.safe_eval({'a':1}, include_exceptions=True), ({'a':1}, None))
46
+        # invalid literal eval
47
+        self.assertEqual(am.safe_eval("a=1"), "a=1")
48
+        res = am.safe_eval("a=1", include_exceptions=True)
49
+        self.assertEqual(res[0], "a=1")
50
+        self.assertEqual(type(res[1]), SyntaxError)
51
+        self.assertEqual(am.safe_eval("a.foo()"), "a.foo()")
52
+        res = am.safe_eval("a.foo()", include_exceptions=True)
53
+        self.assertEqual(res[0], "a.foo()")
54
+        self.assertEqual(res[1], None)
55
+        self.assertEqual(am.safe_eval("import foo"), "import foo")
56
+        res = am.safe_eval("import foo", include_exceptions=True)
57
+        self.assertEqual(res[0], "import foo")
58
+        self.assertEqual(res[1], None)
59
+        self.assertEqual(am.safe_eval("__import__('foo')"), "__import__('foo')")
60
+        res = am.safe_eval("__import__('foo')", include_exceptions=True)
61
+        self.assertEqual(res[0], "__import__('foo')")
62
+        self.assertEqual(type(res[1]), ValueError)
63
+
... ...
@@ -99,6 +99,12 @@ class TestModuleUtilsBasic(unittest.TestCase):
99 99
         mock_import.side_effect = _mock_import
100 100
         mod = builtins.__import__('ansible.module_utils.basic')
101 101
 
102
+    # FIXME: doesn't work yet
103
+    #@patch.object(builtins, 'bytes')
104
+    #def test_module_utils_basic_bytes(self, mock_bytes):
105
+    #    mock_bytes.side_effect = NameError()
106
+    #    from ansible.module_utils import basic
107
+
102 108
     @patch.object(builtins, '__import__')
103 109
     @unittest.skipIf(sys.version_info[0] >= 3, "Python 3 is not supported on targets (yet)")
104 110
     def test_module_utils_basic_import_literal_eval(self, mock_import):