This repository has been archived by the owner on Jul 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_tests.py
161 lines (108 loc) · 4.43 KB
/
_tests.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'''Test moira_trigger'''
import unittest
import warnings
from _mocking import ansible_pkg, moira_api
from moira_trigger import MoiraAnsible, HAS_MOIRA_CLIENT
test_trigger = {
'name': 'test',
'targets': ['target1', 'target2'],
'desc': 'test desc',
'tags': ['tag1', 'tag2']}
test_trigger_name = test_trigger['name']
moira_ansible = MoiraAnsible(moira_api)
class TestMoiraAnsible(unittest.TestCase):
'''Test MoiraAnsible from moira_trigger'''
@staticmethod
def _clear_data():
'''Clear data for next iterations'''
moira_api.trigger_body.id = None
moira_api.methods_calls.trace = ''
def test_moira_client(self):
'''Test if moira_client has all required methods'''
result = set()
trigger_save_attributes = 'save', 'update'
trigger_edit_attributes = 'fetch_all', 'fetch_by_id', \
'create', 'delete'
if HAS_MOIRA_CLIENT:
import moira_client
moira = moira_client.Moira('http://test/')
for save_attribute in trigger_save_attributes:
result.add(
hasattr(moira_client.models.trigger.Trigger,
save_attribute))
for edit_attribute in trigger_edit_attributes:
result.add(
hasattr(moira.trigger,
edit_attribute))
del moira_client
else:
warnings.warn('module not found: moira_client')
self.assertNotIn(bool(0), result)
def test_trigger_create(self):
'''Create new trigger'''
moira_ansible.trigger_customize(test_trigger, 'present')
self.assertIn(
'new trigger created',
moira_ansible.success[test_trigger_name])
self.assertFalse(moira_ansible.failed)
if not moira_ansible.dry_run:
self.assertEqual(moira_api.methods_calls.trace,
'.fetch_all.create.save.update')
else:
self.assertEqual(moira_api.methods_calls.trace,
'.fetch_all.create')
self._clear_data()
def test_trigger_update(self):
'''Update existing trigger'''
moira_ansible.trigger_customize(test_trigger, 'present')
del moira_ansible.success[test_trigger_name]
test_trigger.update({'other_param': 'other_value'})
moira_ansible.trigger_customize(test_trigger, 'present')
self.assertIn(
'other_param',
moira_api.trigger_body.__dict__)
self.assertFalse(moira_ansible.failed)
if not moira_ansible.dry_run:
self.assertIn(
'trigger changed',
moira_ansible.success[test_trigger_name])
self.assertEqual(moira_api.methods_calls.trace,
'.fetch_all.create.save.update.fetch_all.fetch_by_id.update')
else:
self.assertEqual(moira_api.methods_calls.trace,
'.fetch_all.create.fetch_all.create')
self._clear_data()
def test_trigger_delete(self):
'''Remove existing trigger'''
moira_ansible.trigger_customize(test_trigger, 'present')
moira_ansible.trigger_customize(test_trigger, 'absent')
self.assertFalse(moira_ansible.failed)
if not moira_ansible.dry_run:
self.assertIn(
'trigger removed',
moira_ansible.success[test_trigger_name])
self.assertEqual(moira_api.methods_calls.trace,
'.fetch_all.create.save.update.fetch_all.delete')
else:
self.assertEqual(moira_api.methods_calls.trace,
'.fetch_all.create.fetch_all')
self._clear_data()
def test_no_id_found(self):
'''No trigger found to remove'''
moira_ansible.trigger_customize(test_trigger, 'absent')
self.assertIn(
'no id found for trigger',
moira_ansible.success[test_trigger_name])
self.assertFalse(moira_ansible.failed)
self.assertEqual(moira_api.methods_calls.trace,
'.fetch_all')
self._clear_data()
def test_dry_run(self):
'''Check mode'''
moira_ansible.dry_run = True
self.test_no_id_found()
self.test_trigger_delete()
self.test_trigger_update()
self.test_trigger_create()
if __name__ == '__main__':
unittest.main()