import sys import unittest from ansible_module.generic import modspec from ansible_module.module import SystemdUnitModule, docify class ModuleMock(SystemdUnitModule): name = "mock" module_spec = modspec({}) class TestSystemdUnitModule(unittest.TestCase): def setUp(self): sys.argv = [sys.argv[0], '{"ANSIBLE_MODULE_ARGS":{}}'] self.mod = ModuleMock() def test_header(self): self.assertEqual("[Unit]\n", self.mod.header()) def test_doc(self): ModuleMock.doc() def test_params(self): self.mod.module.params = dict(ok="ok", none=None, true=True, false=False, list=("item1", "item2")) self.assertEqual("ok", self.mod.get("ok")) self.assertEqual("default", self.mod.get("none", "default")) self.assertRaises(KeyError, self.mod.get, "none", None) self.assertListEqual( ['TRUE=yes\n', "FALSE=no\n", "OK=ok\n", "LIST=item1\n", "LIST=item2\n"], self.mod.map_param(true="TRUE", false="FALSE", ok="OK", list="LIST"), ) self.assertRaises(TypeError, self.mod.diff, diff=dict(), before=True, after=False) self.mod.diff(before="a", after="b", before_header="a", after_header="b") self.mod.diff(dict(before="c", after="d")) def test_result(self): self.assertFalse(self.mod.changed) self.mod.changed = 5 self.assertTrue(self.mod.changed) self.mod.set("key", "value") self.assertDictEqual( dict( changed=True, key="value", ), self.mod.result, ) class TestFunctions(unittest.TestCase): def test_docify(self): input = dict( item1=dict(type="str", description=""), item2=dict(required=True, type="int", default=5), item3=dict(type="list", required=False, elements="int"), item4=dict(type="list", required=True, elements="int"), item5=dict(type="str", choices=["a", "b", "c"]), item6=dict(type="dict", options=dict(a=dict(type="str"))), ) expected = dict( item1=dict(required=False, type="str", description=[""]), item2=dict(required=True, type="int", default=5), item3=dict(type="list", required=False, elements="int", default=[]), item4=dict(type="list", required=True, elements="int"), item5=dict(type="str", required=False, choices=("a", "b", "c")), item6=dict(type="dict", required=False, options=dict(a=dict(type="str", required=False))), ) output = docify(input) for key, value in input.items(): self.assertDictEqual(expected[key], output[key])