forked from Yelp/paasta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_apply_external_resources.py
159 lines (145 loc) · 4.42 KB
/
test_apply_external_resources.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
import os
from subprocess import CalledProcessError
import mock
import pytest
from paasta_tools.apply_external_resources import main
@pytest.fixture
def mock_run():
with mock.patch(
"paasta_tools.apply_external_resources.run", autospec=True
) as mock_runner:
yield mock_runner
@pytest.fixture(autouse=True)
def setup_external_files(fs):
fs.create_file(
"/external_resources/00-common/10-foo/10-deployment.yaml",
contents="foo: bar",
)
fs.create_file(
"/external_resources/00-common/10-foo/20-service.yaml",
contents="fizz: buzz",
)
fs.create_file(
"/external_resources/20-common/10-foo/20-deployment.yaml",
contents="baz: biz",
)
fs.create_file(
"/external_resources/.applied/00-common/10-foo/10-deployment.yaml",
contents="foo: bar",
)
fs.create_file(
"/external_resources/.applied/00-common/10-foo/20-service.yaml",
contents="fizz: buzz",
)
fs.create_file(
"/external_resources/.applied/20-common/10-foo/20-deployment.yaml",
contents="baz: biz",
)
def test_no_changes(mock_run):
assert main("/external_resources") == 0
assert mock_run.call_count == 0
def test_resources_added_in_order(mock_run, fs):
fs.create_file(
"/external_resources/00-common/10-foo/30-hpa.yaml",
contents="blah: blah",
)
fs.create_file(
"/external_resources/00-common/10-foo/40-service.yaml",
contents="blah: blah",
)
fs.create_file(
"/external_resources/00-common/30-foo/10-deployment.yaml",
contents="blah: blah",
)
assert main("/external_resources") == 0
assert mock_run.call_args_list == [
mock.call(
[
"kubectl",
"apply",
"-f",
"/external_resources/00-common/10-foo/30-hpa.yaml",
],
check=True,
),
mock.call(
[
"kubectl",
"apply",
"-f",
"/external_resources/00-common/10-foo/40-service.yaml",
],
check=True,
),
mock.call(
[
"kubectl",
"apply",
"-f",
"/external_resources/00-common/30-foo/10-deployment.yaml",
],
check=True,
),
]
assert os.path.exists("/external_resources/.applied/00-common/10-foo/30-hpa.yaml")
assert os.path.exists(
"/external_resources/.applied/00-common/10-foo/40-service.yaml"
)
assert os.path.exists(
"/external_resources/.applied/00-common/30-foo/10-deployment.yaml"
)
def test_resources_deleted_in_reverse_order(mock_run, fs):
fs.create_file(
"/external_resources/.applied/00-common/10-foo/30-hpa.yaml",
contents="blah: blah",
)
fs.create_file(
"/external_resources/.applied/00-common/10-foo/40-service.yaml",
contents="blah: blah",
)
assert main("/external_resources") == 0
assert mock_run.call_args_list == [
mock.call(
[
"kubectl",
"delete",
"--ignore-not-found=true",
"-f",
"/external_resources/.applied/00-common/10-foo/40-service.yaml",
],
check=True,
),
mock.call(
[
"kubectl",
"delete",
"--ignore-not-found=true",
"-f",
"/external_resources/.applied/00-common/10-foo/30-hpa.yaml",
],
check=True,
),
]
assert not os.path.exists(
"/external_resources/.applied/00-common/10-foo/30-hpa.yaml"
)
assert not os.path.exists(
"/external_resources/.applied/00-common/10-foo/40-service.yaml"
)
def test_kubectl_fails(mock_run, fs):
mock_run.side_effect = [CalledProcessError(cmd="kubectl", returncode=1), None]
fs.create_file(
"/external_resources/00-common/10-foo/30-hpa.yaml",
contents="blah: blah",
)
fs.create_file(
"/external_resources/00-common/10-foo/40-service.yaml",
contents="blah: blah",
)
assert main("/external_resources") == 1
assert not os.path.exists(
"/external_resources/.applied/00-common/10-foo/30-hpa.yaml"
)
assert os.path.exists(
"/external_resources/.applied/00-common/10-foo/40-service.yaml"
)