-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_dependencies.py
169 lines (140 loc) · 6.27 KB
/
test_dependencies.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
162
163
164
165
166
167
168
169
from collections import OrderedDict
import unittest
import copy
import graphlib
from buildrunner.config import loader
class TestDependencies(unittest.TestCase):
config = OrderedDict(
[
(
"steps",
OrderedDict(
[
(
"step1",
OrderedDict(
[
(
"run",
OrderedDict(
[
("image", "docker.io/ubuntu:latest"),
("cmd", 'echo "Hello from step 1"'),
]
),
)
]
),
),
(
"step2",
OrderedDict(
[
("depends", ["step3", "step4"]),
(
"run",
OrderedDict(
[
("image", "docker.io/ubuntu:latest"),
("cmd", 'echo "Hello from step 2"'),
]
),
),
]
),
),
(
"step3",
OrderedDict(
[
(
"run",
OrderedDict(
[
("image", "docker.io/ubuntu:latest"),
("cmd", 'echo "Hello from step 3"'),
]
),
)
]
),
),
(
"step4",
OrderedDict(
[
(
"run",
OrderedDict(
[
("image", "docker.io/ubuntu:latest"),
("cmd", 'echo "Hello from step 4"'),
]
),
)
]
),
),
]
),
)
]
)
KEYWORD_VERSION = "version"
KEYWORD_STEPS = "steps"
KEYWORD_DEPENDS = "depends"
def check_steps_equal(self, steps, expected_step_names):
for actual_step_name, actual_step in steps[self.KEYWORD_STEPS].items():
self.assertEqual(next(expected_step_names), actual_step_name)
expected_step = copy.deepcopy(
self.config[self.KEYWORD_STEPS][actual_step_name]
)
if self.KEYWORD_DEPENDS in expected_step:
del expected_step[self.KEYWORD_DEPENDS]
self.assertDictEqual(expected_step, actual_step)
def test_reorder_steps(self):
config = copy.deepcopy(self.config)
config[self.KEYWORD_VERSION] = 2.0
expected_step_names = iter(["step1", "step3", "step4", "step2"])
reordered_steps = loader._reorder_dependency_steps(config)
self.check_steps_equal(reordered_steps, expected_step_names)
def test_reorder_steps_higher_version(self):
config = copy.deepcopy(self.config)
config[self.KEYWORD_VERSION] = 2.1
expected_step_names = iter(["step1", "step3", "step4", "step2"])
reordered_steps = loader._reorder_dependency_steps(config)
self.check_steps_equal(reordered_steps, expected_step_names)
config = copy.deepcopy(self.config)
config[self.KEYWORD_VERSION] = 3.1
expected_step_names = iter(["step1", "step3", "step4", "step2"])
reordered_steps = loader._reorder_dependency_steps(config)
self.check_steps_equal(reordered_steps, expected_step_names)
def test_no_reorder(self):
config = copy.deepcopy(self.config)
config[self.KEYWORD_VERSION] = 2.0
# Remove any steps with 'depends' attribute
for name, step in config[self.KEYWORD_STEPS].items():
if self.KEYWORD_DEPENDS in step:
del step[self.KEYWORD_DEPENDS]
reordered_steps = loader._reorder_dependency_steps(config)
self.assertDictEqual(config, reordered_steps)
def test_not_supported_version(self):
config = copy.deepcopy(self.config)
config[self.KEYWORD_VERSION] = 1.9
reordered_steps = loader._reorder_dependency_steps(config)
del reordered_steps[self.KEYWORD_VERSION]
self.assertDictEqual(self.config, reordered_steps)
def test_missing_version(self):
config = copy.deepcopy(self.config)
reordered_steps = loader._reorder_dependency_steps(config)
self.assertDictEqual(self.config, reordered_steps)
def test_cycle_dependency(self):
config = copy.deepcopy(self.config)
config[self.KEYWORD_VERSION] = 2.0
config[self.KEYWORD_STEPS]["step4"][self.KEYWORD_DEPENDS] = ["step3", "step2"]
self.assertRaises(graphlib.CycleError, loader._reorder_dependency_steps, config)
def test_not_defined_dependency(self):
config = copy.deepcopy(self.config)
config[self.KEYWORD_VERSION] = 2.0
config[self.KEYWORD_STEPS]["step4"][self.KEYWORD_DEPENDS] = ["step1-typo"]
self.assertRaises(KeyError, loader._reorder_dependency_steps, config)