-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelm_hooks_test.py
224 lines (206 loc) · 5.45 KB
/
helm_hooks_test.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from tiltfile_runner import run_tiltfile_func
from unittest.mock import Mock
import yaml
def test_excludes_test_and_rollback_hook_workloads():
k8s_yaml = Mock()
run_tiltfile_func("helm_hooks/Tiltfile", "helm_install_handle_hooks", mocks={'k8s_yaml': k8s_yaml}, yaml="""
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave
namespace: test
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave2
namespace: test
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave3
namespace: test
annotations:
helm.sh/hook: test-success
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave4
namespace: test
annotations:
helm.sh/hook: test-failure
""")
saved_objs = yaml.safe_load_all(k8s_yaml.call_args.args[0])
assert [obj["metadata"]["name"] for obj in saved_objs] == ["dave", "dave2"]
def test_errors_on_delete_hooks():
fail = Mock()
run_tiltfile_func("helm_hooks/Tiltfile", "helm_install_handle_hooks", mocks={'fail': fail}, yaml="""
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave3
namespace: test
annotations:
helm.sh/hook: pre-delete
""")
assert fail.called
def test_makes_non_hook_workloads_dependent_on_pre_install_and_upgrade_hooks():
k8s_resource = Mock()
run_tiltfile_func("helm_hooks/Tiltfile", "helm_install_handle_hooks", mocks={'k8s_resource': k8s_resource}, yaml="""
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave
namespace: test
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave2
namespace: test
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave3
namespace: test
annotations:
helm.sh/hook: pre-install
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave4
namespace: test
annotations:
helm.sh/hook: pre-upgrade
""")
assert k8s_resource.call_count == 2
k8s_resource.assert_any_call(workload="dave:deployment:test", resource_deps=["dave3:deployment:test", "dave4:deployment:test"])
k8s_resource.assert_any_call(workload="dave2:deployment:test", resource_deps=["dave3:deployment:test", "dave4:deployment:test"])
def test_makes_post_install_or_upgrade_hook_workloads_dependent_on_other_workloads():
k8s_resource = Mock()
run_tiltfile_func("helm_hooks/Tiltfile", "helm_install_handle_hooks", mocks={'k8s_resource': k8s_resource}, yaml="""
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave
namespace: test
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave3
namespace: test
annotations:
helm.sh/hook: pre-install
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave4
namespace: test
annotations:
helm.sh/hook: post-install
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave5
namespace: test
annotations:
helm.sh/hook: post-upgrade
""")
assert k8s_resource.call_count == 3
k8s_resource.assert_any_call(workload="dave4:deployment:test", resource_deps=["dave:deployment:test", "dave3:deployment:test"])
k8s_resource.assert_any_call(workload="dave5:deployment:test", resource_deps=["dave:deployment:test", "dave3:deployment:test"])
def test_doesnt_assign_deps_for_excluded_resources():
k8s_resource = Mock()
run_tiltfile_func("helm_hooks/Tiltfile", "helm_install_handle_hooks", mocks={'k8s_resource': k8s_resource}, yaml="""
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave
namespace: test
annotations:
helm.sh/hook: test-failure
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave3
namespace: test
annotations:
helm.sh/hook: pre-install
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave4
namespace: test
annotations:
helm.sh/hook: post-install
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave5
namespace: test
annotations:
helm.sh/hook: post-upgrade
""")
assert k8s_resource.call_count == 2
k8s_resource.assert_any_call(workload="dave4:deployment:test", resource_deps=["dave3:deployment:test"])
k8s_resource.assert_any_call(workload="dave5:deployment:test", resource_deps=["dave3:deployment:test"])
def test_doesnt_assign_pre_install_deps_when_no_dependencies():
k8s_resource = Mock()
run_tiltfile_func("helm_hooks/Tiltfile", "helm_install_handle_hooks", mocks={'k8s_resource': k8s_resource}, yaml="""
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave
namespace: test
""")
assert k8s_resource.call_count == 0
def test_doesnt_assign_post_install_deps_when_no_dependencies():
k8s_resource = Mock()
run_tiltfile_func("helm_hooks/Tiltfile", "helm_install_handle_hooks", mocks={'k8s_resource': k8s_resource}, yaml="""
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave
namespace: test
annotations:
helm.sh/hook: post-install
""")
assert k8s_resource.call_count == 0
def test_works_for_compound_hooks():
k8s_resource = Mock()
run_tiltfile_func("helm_hooks/Tiltfile", "helm_install_handle_hooks", mocks={'k8s_resource': k8s_resource}, yaml="""
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave
namespace: test
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave3
namespace: test
annotations:
helm.sh/hook: pre-install,pre-upgrade
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave4
namespace: test
annotations:
helm.sh/hook: post-install,post-upgrade
""")
k8s_resource.assert_any_call(workload="dave:deployment:test", resource_deps=["dave3:deployment:test"])
k8s_resource.assert_any_call(workload="dave4:deployment:test", resource_deps=["dave:deployment:test", "dave3:deployment:test"])
assert k8s_resource.call_count == 2