class NodeEntryTests(unittest2.TestCase): def setUp(self): self.entry = Entry('node', 'L{Node}', 'node which is required') def test_validate(self): valid_json = '{"node_id": "33333", "unknown_arg": 123}' self.entry._get_json_and_validate(valid_json) malformed_json = '{"id"}' self.assertRaises(MalformedJSONError, self.entry._get_json_and_validate, malformed_json) invalid_json = '{"node_id": 123}' self.assertRaises(ValidationError, self.entry._get_json_and_validate, invalid_json) def test_get_arguments(self): argument = self.entry.get_arguments()[0] self.assertEqual(argument['name'], 'node_id') self.assertEqual(argument['type'], 'str') def test_to_json(self): node = Node('111', 'test', NodeState.RUNNING, ['123.123.123.123'], None, Node) json_data = json.loads(self.entry.to_json(node)) self.assertEqual(node.name, json_data['name']) self.assertEqual(node.id, json_data['id']) self.assertEqual(node.state, json_data['state']) self.assertEqual(node.public_ips, json_data['public_ips']) self.assertRaises(ValueError, self.entry.to_json, ['pass']) def test_from_json(self): json_data = '{"node_id": "2600"}' node = self.entry.from_json(json_data, None) self.assertEqual(node.id, '2600')
class StringEntryTests(unittest2.TestCase): def setUp(self): self.entry = Entry('zone_id', 'C{str}', 'ID of the zone which is required') def test_validate(self): valid_json = '{"zone_id": "123"}' self.entry._get_json_and_validate(valid_json) malformed_json = "{ '1'}" self.assertRaises(MalformedJSONError, self.entry._get_json_and_validate, malformed_json) invalid_json = '{"zone_id": 123}' self.assertRaises(ValidationError, self.entry._get_json_and_validate, invalid_json) def test_get_arguments(self): argument = self.entry.get_arguments()[0] self.assertEqual(argument['name'], 'zone_id') self.assertEqual(argument['description'], 'ID of the zone which is required') self.assertEqual(argument['type'], 'str') def test_to_json(self): valid = '123' self.entry.to_json(valid) invalid = 123 self.assertRaises(ValueError, self.entry.to_json, invalid) def test_from_json(self): valid = '{"zone_id": "123"}' self.entry.from_json(valid) invalid = '{"zone_id": 333}' self.assertRaises(ValidationError, self.entry.from_json, invalid) def test_default(self): entry = Entry('zone_id', 'C{str}', 'ID of the zone which is required', default='12345') valid_json = '{"zone_id": "33333", "unknown_arg": 123}' self.assertEqual('33333', entry.from_json(valid_json)) self.assertTrue(entry._get_json_and_validate(valid_json)) node_json = '{"node_id": "33333", "unknown_arg": 123}' self.assertEqual('12345', entry.from_json(node_json)) self.assertRaises(MissingArguments, entry._get_json_and_validate, node_json) valid = '123' entry.to_json(valid) invalid = 123 self.assertRaises(ValueError, entry.to_json, invalid)
class DefaultOneOfEntryTests(unittest2.TestCase): def setUp(self): self.entry = Entry('attr', 'C{str} or C{dict}', 'Test description', default='default_value') def test_get_arguments(self): args = self.entry.get_arguments() self.assertEqual(2, len(args)) test_args = [{ 'type': 'str', 'name': 'attr', 'description': 'Test description', 'required': True }, { 'type': 'dict', 'name': 'attr', 'description': 'Test description', 'required': True }] self.assertEqual(test_args, args) def test_from_json(self): str_json = '{"attr": "123", "unknown_arg": 123}' self.assertEqual("123", self.entry.from_json(str_json, None)) dict_json = '{"attr": "{}", "unknown_arg": 123}' self.assertEqual("{}", self.entry.from_json(dict_json, None)) invalid_json = '{"attr": 555}' self.assertRaises(ValidationError, self.entry.from_json, invalid_json, None) without_attr_json = '{"unknown_arg": 123}' self.assertEqual('default_value', self.entry.from_json(without_attr_json, None)) malformed_json = '{' self.assertRaises(MalformedJSONError, self.entry.from_json, malformed_json, None) def test_to_json(self): str_data = 'abc' self.assertEqual('{"attr": "abc"}', self.entry.to_json(str_data)) dict_data = {'1': 2} self.assertEqual('{"attr": {"1": 2}}', self.entry.to_json(dict_data)) int_data = 5 self.assertRaises(ValueError, self.entry.to_json, int_data)
class OneOfEntryTests(unittest2.TestCase): def setUp(self): self.entry = Entry('node', 'L{NodeAuthSSHKey} or L{NodeAuthPassword}', 'Initial authentication information for the node') def test_get_arguments(self): args = self.entry.get_arguments() self.assertEqual(2, len(args)) get_arg = lambda name: [arg for arg in args if arg['name'] == name][0] node_pubkey_arg = get_arg('node_pubkey') self.assertEqual(node_pubkey_arg['type'], 'str') node_password_arg = get_arg('node_password') self.assertEqual(node_password_arg['type'], 'str') def test_from_json(self): key_json = '{"node_pubkey": "123", "unknown_arg": 123}' node_auth_ssh_key = self.entry.from_json(key_json, None) self.assertEqual("123", node_auth_ssh_key.pubkey) password_json = '{"node_password": "******", "unknown_arg": 123}' node_auth_password = self.entry.from_json(password_json, None) self.assertEqual("321", node_auth_password.password) key_password_json = '{"node_pubkey": "123",' \ ' "node_password": "******", "unknown_args": 123}' self.assertRaises(TooManyArgumentsError, self.entry.from_json, key_password_json, None) empty_json = "{}" self.assertRaises(MissingArguments, self.entry.from_json, empty_json, None) invalid_json = '{"node_pubkey": 123}' self.assertRaises(ValidationError, self.entry.from_json, invalid_json, None) def test_to_json(self): node_ssh_key = NodeAuthSSHKey('pk') self.assertEqual('{"pubkey": "pk"}', self.entry.to_json(node_ssh_key)) node_password = NodeAuthPassword('pwd') self.assertEqual('{"password": "******"}', self.entry.to_json(node_password))
class DefaultOneOfEntryTests(unittest2.TestCase): def setUp(self): self.entry = Entry('attr', 'C{str} or C{dict}', 'Test description', default='default_value') def test_get_arguments(self): args = self.entry.get_arguments() self.assertEqual(2, len(args)) test_args = [{'type': 'str', 'name': 'attr', 'description': 'Test description', 'required': True}, {'type': 'dict', 'name': 'attr', 'description': 'Test description', 'required': True}] self.assertEqual(test_args, args) def test_from_json(self): str_json = '{"attr": "123", "unknown_arg": 123}' self.assertEqual("123", self.entry.from_json(str_json, None)) dict_json = '{"attr": "{}", "unknown_arg": 123}' self.assertEqual("{}", self.entry.from_json(dict_json, None)) invalid_json = '{"attr": 555}' self.assertRaises(ValidationError, self.entry.from_json, invalid_json, None) without_attr_json = '{"unknown_arg": 123}' self.assertEqual('default_value', self.entry.from_json(without_attr_json, None)) malformed_json = '{' self.assertRaises(MalformedJSONError, self.entry.from_json, malformed_json, None) def test_to_json(self): str_data = 'abc' self.assertEqual('{"attr": "abc"}', self.entry.to_json(str_data)) dict_data = {'1': 2} self.assertEqual('{"attr": {"1": 2}}', self.entry.to_json(dict_data)) int_data = 5 self.assertRaises(ValueError, self.entry.to_json, int_data)