-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_tests.py
executable file
·134 lines (98 loc) · 4.59 KB
/
run_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
#!/usr/bin/env python
import logging
import unittest
import os
import ipa
def get_path_to_resource_file(tc_name, file_name):
return os.path.abspath(
os.path.join(os.path.dirname(__file__), 'tests', tc_name, file_name))
class _BaseTestCase(unittest.TestCase):
"""Extend unittest.TestCase with more functionality"""
def __init__(self, *args, **kwargs):
super(_BaseTestCase, self).__init__(*args, **kwargs)
self.addTypeEqualityFunc(str, self.assertEqualWithDiff)
def assertEqualWithDiff(self, left, right, msg=None):
import difflib
try:
self._baseAssertEqual(left, right)
except self.failureException:
diff = difflib.unified_diff(
left.splitlines(True),
right.splitlines(True),
n=0
)
diff = ''.join(diff)
raise self.failureException("{0}\n{1}".format(msg or '', diff))
class IpaTest(_BaseTestCase):
def setUp(self):
# show all differences
self.maxDiff = None
# disable logging temporarily to reduce spam
logging.getLogger().setLevel(logging.WARNING)
def test_first_run_text_output(self):
self.run_test('first_run', 'human', True)
def test_first_run_json_output(self):
self.run_test('first_run', 'json', True)
def test_first_run_yaml_anchors_output(self):
self.run_test('first_run', 'yaml-anchors', True)
def test_prev_run_no_change_text_output(self):
self.run_test('with_previous_no_change', 'human', False)
def test_prev_run_no_change_json_output(self):
self.run_test('with_previous_no_change', 'json', False)
def test_prev_run_no_change_yaml_anchors_output(self):
self.run_test('with_previous_no_change', 'yaml-anchors', False)
def test_prev_run_basic_change_text_output(self):
self.run_test('with_previous_basic_change', 'human', False)
def test_prev_run_basic_change_json_output(self):
self.run_test('with_previous_basic_change', 'json', False)
def test_prev_run_basic_change_yaml_anchors_output(self):
self.run_test('with_previous_basic_change', 'yaml-anchors', False)
def test_new_prev_run_no_change_text_output(self):
self.run_test('with_new_previous_no_change', 'human', False)
def test_new_prev_run_no_change_json_output(self):
self.run_test('with_new_previous_no_change', 'json', False)
def test_new_prev_run_no_change_yaml_anchors_output(self):
self.run_test('with_new_previous_no_change', 'yaml-anchors', False)
def test_first_run_with_ip_range_local_text_output(self):
self.run_test('first_run_with_ip_range_local', 'human', True)
def test_first_run_with_ip_range_local_json_output(self):
self.run_test('first_run_with_ip_range_local', 'json', True)
def test_first_run_with_ip_range_local_yaml_anchors_output(self):
self.run_test('first_run_with_ip_range_local', 'yaml-anchors', True)
def test_first_run_with_ip_range_local_reverse_text_output(self):
self.run_test('first_run_with_ip_range_local_reverse', 'human', True)
def test_first_run_with_ip_range_local_reverse_json_output(self):
self.run_test('first_run_with_ip_range_local_reverse', 'json', True)
def test_first_run_with_ip_range_local_reverse_yaml_anchors_output(self):
self.run_test('first_run_with_ip_range_local_reverse', 'yaml-anchors', True)
def run_test(self, tc_name, output_format, is_first_run):
if output_format == 'human':
ofile_name = 'output.txt'
elif output_format == 'json':
ofile_name = 'output.json'
elif output_format == 'yaml-anchors':
ofile_name = 'output.yaml'
else:
raise NotImplementedError
input_file = get_path_to_resource_file(tc_name, 'input.yaml')
output_file = get_path_to_resource_file(tc_name, ofile_name)
# create the list of arguments
args = [
input_file,
'-o', output_format
]
if is_first_run:
args.append('--first-run')
else:
prev_res = get_path_to_resource_file(tc_name, 'previous.json')
args.extend(['-p', prev_res])
res = ipa.main(args)
# compare the content of the output file
# to the content of the expected file
with open(output_file) as f:
exp = f.read()
self.assertEqualWithDiff(exp.strip(), res.strip())
if __name__ == "__main__":
suite = unittest.TestSuite()
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(IpaTest))
unittest.TextTestRunner().run(suite)