def test_render(self, mock_open): mock_open.side_effect = [ unittest.mock.mock_open(read_data='{"people": "stuff"}').return_value ] cnc = service.CnC() self.assertEqual(cnc.render( { "{{ people }}": [ "{{ stuff }}", { "secret": {"name": "{{ things }}", "path": "people"} }, True, "{? 1 == 0 ?}", "{? 1 == 1 ?}" ] }, { "people": "stuff", "stuff": "things", "things": "data.yaml" } ), { "stuff": [ "things", "stuff", True, False, True ] })
def test_satisfied(self): cnc = service.CnC() # unsatisfied field = { "name": "happy", "condition": "False" } self.assertFalse(cnc.satisfied(field)) field = { "name": "happy", "condition": False, } self.assertFalse(cnc.satisfied(field)) # satisfied field = { "name": "happy", "condition": "True" } self.assertTrue(cnc.satisfied(field)) field = { "name": "happy", "condition": True, } self.assertTrue(cnc.satisfied(field))
def test_ready(self): cnc = service.CnC() fields = opengui.Fields() # missing field = { "name": "moar", "requires": "some" } self.assertFalse(cnc.ready(fields, field)) # invalid fields.append({ "name": "some", "required": True }) self.assertFalse(cnc.ready(fields, field)) # requirement met fields["some"].value = "fun" self.assertTrue(cnc.ready(fields, field))
def test_secret(self, mock_open): mock_open.side_effect = [ unittest.mock.mock_open(read_data='{"people": "stuff"}').return_value ] cnc = service.CnC() self.assertEqual(cnc.secret({"name": "{{ things }}", "path": "people"}, {"things": "data.yaml"}), "stuff") mock_open.assert_called_once_with("/opt/service/secret/data.yaml", "r")
def test_values(self): cnc = service.CnC() fields = opengui.Fields(fields=[ {"name": "none"}, {"name": "some", "default": "fun"} ]) self.assertEqual(cnc.values(fields), { "none": None, "some": "fun" })
def test_fields(self, mock_open, mock_exists): mock_exists.return_value = True mock_open.side_effect = [ unittest.mock.mock_open(read_data='fields:\n- name: extra').return_value, unittest.mock.mock_open(read_data='{}').return_value, unittest.mock.mock_open(read_data='{}').return_value ] cnc = service.CnC() forge = { "id": "here", "description": "Here" } fields = cnc.fields(forge, {}) self.assertFields(fields, [ { "name": "forge", "description": "what to craft from", "readonly": True, "value": "here" }, { "name": "craft", "description": "name of what to craft, used for repos, branches, change requests", "validation": '^[a-z][a-z0-9\-]{1,46}$', "required": True, "trigger": True }, { "name": "extra" } ],ready=True) mock_exists.assert_called_once_with("/opt/service/forge/fields.yaml") forge = { "id": "here", "description": "Here", "input": { "fields": [ { "name": "some" } ] } } fields = cnc.fields(forge, {"craft": "fun", "some": "thing"}) self.assertFields(fields, [ { "name": "forge", "description": "what to craft from", "readonly": True, "value": "here" }, { "name": "craft", "description": "name of what to craft, used for repos, branches, change requests", "validation": '^[a-z][a-z0-9\-]{1,46}$', "required": True, "trigger": True, "value": "fun" }, { "name":"some", "value":"thing" } ]) forge = { "id": "here", "description": "Here", "input": { "fields": [ { "name": "code" } ] } } self.assertRaisesRegex(Exception, "field name 'code' is reserved", cnc.fields, forge, {})
def test_field(self, mock_session): cnc = service.CnC() fields = opengui.Fields(values={"moar": "lees"}) # clear field = { "name": "moar", "requires": "some" } cnc.field(fields, field) self.assertEqual(len(fields), 0) field = { "name": "moar", "condition": False } cnc.field(fields, field) self.assertEqual(len(fields), 0) # default fields.append({ "name": "some", "required": True, "value": "fun" }) field = { "name": "happy", "default": "{{ port(some) }} bone", "requires": "some" } cnc.field(fields, field) self.assertEqual(len(fields), 2) self.assertEqual(fields["happy"].default, "7085 bone") # update field = { "name": "happy", "options": [1, 2, 3], "requires": "some" } cnc.field(fields, field) self.assertEqual(len(fields), 2) self.assertEqual(fields["happy"].default, "7085 bone") self.assertEqual(fields["happy"].options, [1, 2, 3]) # api mock_session.return_value.get.return_value.json.return_value = { "numbers": [ { "id": 1, "name": "one" }, { "id": 2, "name": "two" }, { "id": 3, "name": "three" } ] } field = { "name": "friend", "api": { "uri": "yep", "options": "numbers", "option": "id", "title": "name" } } cnc.field(fields, field) self.assertEqual(len(fields), 3) self.assertEqual(fields["friend"].options, [1, 2, 3]) self.assertEqual(fields["friend"].content['titles'], { 1: "one", 2: "two", 3: "three" })
def test_api(self, mock_open, mock_session): mock_session.return_value.headers = {} def secrets(path, mode): if path.endswith("things.yaml"): read_data = '{"people": "stuff"}' elif path.endswith("auth.yaml"): read_data = '{"username": "******", "password": "******"}' elif path.endswith("token.yaml"): read_data = '{"token": "toll"}' elif path.endswith("headers.yaml"): read_data = '{"first": "yep", "second": "sure"}' return unittest.mock.mock_open(read_data=read_data).return_value mock_open.side_effect = secrets values = { "a": 1, "b": 2 } api = { "uri": { "secret": { "name": "things.yaml", "path": "people" } }, "verify": False, "params": {"yin": "{{ a }}"}, "body": {"yang": "{{ b }}"}, "auth": {"secret": "auth.yaml"}, "token": { "secret": { "name": "token.yaml", "path": "token" } }, "headers": { "secret": "headers.yaml" } } mock_session.return_value.get.return_value.json.return_value = [1, 2, 3] cnc = service.CnC() extra = {} cnc.api(api, values, extra) self.assertEqual(extra, { "options": [1, 2, 3] }) self.assertEqual(mock_session.return_value.auth, ("me", "ssh")) self.assertEqual(mock_session.return_value.headers, { "Authorization": "Bearer toll", "first": "yep", "second": "sure" }) mock_session.return_value.get.assert_called_once_with( "stuff", verify=False, params={"yin": '1'}, json={"yang": '2'} ) mock_session.return_value.get.return_value.json.return_value = { "numbers": [ { "id": 1, "name": "one" }, { "id": 2, "name": "two" }, { "id": 3, "name": "three" } ] } api = { "uri": "yep", "options": "numbers", "option": "id", "title": "name" } extra = {} cnc.api(api, values, extra) self.assertEqual(extra, { "options": [1, 2, 3], "titles": { 1: "one", 2: "two", 3: "three" } })
def test___init__(self): cnc = service.CnC() self.assertIn("port", cnc.env.globals)