-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_ansible_configutarion.py
103 lines (90 loc) · 4.17 KB
/
test_ansible_configutarion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from unittest import TestCase
from cloudshell.api.cloudshell_api import AttributeValueInfo
from mock import Mock
from cloudshell.cm.ansible.domain.ansible_configuration import AnsibleConfigurationParser
class TestAnsibleConfigurationParser(TestCase):
def setUp(self):
self.api = Mock()
self.parser = AnsibleConfigurationParser(self.api)
def test_cannot_parse_json_without_repository_details(self):
json = '{}'
with self.assertRaises(SyntaxError) as context:
self.parser.json_to_object(json)
self.assertIn('Missing "repositoryDetails" node.', str(context.exception))
def test_cannot_parse_json_without_repository_url(self):
json = '{"repositoryDetails":{}}'
with self.assertRaises(SyntaxError) as context:
self.parser.json_to_object(json)
self.assertIn('Missing "repositoryDetails.url" node.', str(context.exception))
def test_cannot_parse_json_with_an_empty_repository_url(self):
json = '{"repositoryDetails":{"url":""}}'
with self.assertRaises(SyntaxError) as context:
self.parser.json_to_object(json)
self.assertIn('"repositoryDetails.url" node cannot be empty.', str(context.exception))
def test_cannot_parse_json_without_hosts_detalis(self):
json = '{"repositoryDetails":{"url":"someurl"}}'
with self.assertRaises(SyntaxError) as context:
self.parser.json_to_object(json)
self.assertIn('Missing "hostsDetails" node.', str(context.exception))
def test_cannot_parse_json_with_an_empty_hosts_detalis(self):
json = '{"repositoryDetails":{"url":"someurl"},"hostsDetails":[]}'
with self.assertRaises(SyntaxError) as context:
self.parser.json_to_object(json)
self.assertIn('"hostsDetails" node cannot be empty.', str(context.exception))
def test_cannot_parse_json_with_host_with_an_empty_address(self):
json = '{"repositoryDetails":{"url":"someurl"},"hostsDetails":[{}]}'
with self.assertRaises(SyntaxError) as context:
self.parser.json_to_object(json)
self.assertIn('Missing "ip" node in 1 hosts', str(context.exception))
def test_cannot_parse_json_with_host_with_an_empty_connection_method(self):
json = '{"repositoryDetails":{"url":"someurl"},"hostsDetails":[{"ip":"x.x.x.x"}]}'
with self.assertRaises(SyntaxError) as context:
self.parser.json_to_object(json)
self.assertIn('Missing "connectionMethod" node in 1 hosts', str(context.exception))
def test_sanity(self):
def wrapIt(x):
m = Mock()
m.Value = 'decrypted-' + x
return m
self.api.DecryptPassword.side_effect = lambda x: wrapIt(x)
json = """
{
"additionalArgs": "A",
"repositoryDetails" : {
"url": "B",
"username": "C",
"password": "D"
},
"hostsDetails": [
{
"ip": "E",
"username": "F",
"password": "G",
"accessKey": "H",
"connectionMethod": "IiIiI",
"groups": ["J1","J2"],
"parameters": [{"name":"K11","value":"K12"}, {"name":"K21","value":"K22"}]
},
{
"ip": "E2",
"connectionMethod": "I2"
}]
}"""
conf = self.parser.json_to_object(json)
self.assertEqual("A", conf.additional_cmd_args)
self.assertEqual("B", conf.playbook_repo.url)
self.assertEqual("C", conf.playbook_repo.username)
self.assertEqual("D", conf.playbook_repo.password)
host1 = next((h for h in conf.hosts_conf if h.ip == 'E'), None)
self.assertEqual("F", host1.username)
self.assertEqual("decrypted-G", host1.password)
self.assertEqual("decrypted-H", host1.access_key)
self.assertEqual("iiiii", host1.connection_method)
self.assertEqual(False, host1.connection_secured)
self.assertEquals(['J1','J2'], host1.groups)
self.assertEquals('K12', host1.parameters['K11'])
self.assertEquals('K22', host1.parameters['K21'])
host1 = next((h for h in conf.hosts_conf if h.ip == 'E2'), None)
self.assertIsNotNone(host1)
self.api.DecryptPassword.assert_any_call('G')
self.api.DecryptPassword.assert_any_call('H')