-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.sh
executable file
·259 lines (213 loc) · 5.93 KB
/
tests.sh
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env bash
# Purpose : Test the tested scripts
# Author : Ky-Anh Huynh
# License : Public domain
_gk8s() {
"$(pwd -P)"/gk8s "${@}"
}
_fail_issue_6_with_empty_cluster_name() {
_gk8s ''
}
_fail_without_any_argument() {
_gk8s
}
_fail_with_wrong_cluster_prefix() {
_gk8s local
}
_ok_with_local_and_without_any_argument() {
_gk8s :local
}
_fail_when_command_not_found() {
_gk8s :any_name -- foo/bar xyz
}
# NOTE: helm command is required.
_fail_with_helm_repo_list() {
(
export HELM_REPOSITORY_CONFIG=/dev/null
touch ~/.config/gk8s/helm_test
_gk8s :helm_test helm repo list
)
}
# NOTE: kubectl command is required.
_fail_with_cluster_config_not_found() {
(
# shellcheck disable=SC2030
PATH="$(pwd -P)":$PATH
export PATH
_gk8s :localzzzzz get pods
)
}
_fail_with_local_and_cluster_config_not_found() {
_gk8s :local get pods
}
_fail_with_fake_kubectl_get_pods_with_dashes_way() {
(
touch ~/.config/gk8s/foobar
# shellcheck disable=SC2030
# shellcheck disable=SC2031
PATH="$(pwd -P)":$PATH
export PATH
_gk8s :foobar -- get pods
)
}
_ok_with_fake_kubectl_get_pods_with_dashes_way() {
(
touch ~/.config/gk8s/foobar
# shellcheck disable=SC2030
# shellcheck disable=SC2031
PATH="$(pwd -P)":$PATH
export PATH
_gk8s :foobar -- echo get pods
)
}
_ok_with_fake_kubectl_get_pods() {
(
touch ~/.config/gk8s/foobar
# shellcheck disable=SC2030
# shellcheck disable=SC2031
PATH="$(pwd -P)":$PATH
export PATH
_gk8s :foobar get pods
)
}
_ok_with_fake_kubectl_get_pods_with_kubectl_explicitly() {
(
touch ~/.config/gk8s/foobar
# shellcheck disable=SC2030
# shellcheck disable=SC2031
PATH="$(pwd -P)":$PATH
export PATH
_gk8s :foobar kubectl get pods
)
}
_fail_to_delete() {
(
touch ~/.config/gk8s/foobar
# shellcheck disable=SC2030
# shellcheck disable=SC2031
PATH="$(pwd -P)":$PATH
export PATH
_gk8s :foobar delete pods
)
}
_fail_to_del() {
(
touch ~/.config/gk8s/foobar
# shellcheck disable=SC2030
# shellcheck disable=SC2031
PATH="$(pwd -P)":$PATH
export PATH
_gk8s :foobar del pods
)
}
_ok_to_delete() {
(
touch ~/.config/gk8s/foobar
# shellcheck disable=SC2030
# shellcheck disable=SC2031
PATH="$(pwd -P)":$PATH
export PATH
touch .delete
_gk8s :foobar delete pods
)
}
_ok_to_delete_with_env_flag() {
(
touch ~/.config/gk8s/foobar
# shellcheck disable=SC2030
# shellcheck disable=SC2031
PATH="$(pwd -P)":$PATH
export PATH
DELETE=true _gk8s :foobar delete pods
)
}
# $1: function to test
# $2: regular expression
# $*: Test description
_test() {
fun="$1"; shift
reg="$1"; shift
>&2 echo "::"
>&2 echo ":: ${fun}: $*"
>&2 echo ":: ${fun}: expecting '$reg'"
_f_output="${fun}.tmp"
"${fun}" > "$_f_output" 2>&1
ret="$?"
if grep -sqEe "$reg" -- "$_f_output"; then
>&2 echo ":: PASSED: ${fun}"
else
>&2 echo ":: FAILED: ${fun}"
< "$_f_output" awk '{printf(":: (output) %s\n", $0)}'
(( errors++ ))
fi
if [[ "$fun" =~ _fail.* ]]; then
[[ "$ret" -ge 1 ]] \
|| { >&2 echo ":: FAILED: ${fun}: Return code must be >= 1"; (( errors++ )); }
else
[[ "$ret" -eq 0 ]] \
|| { >&2 echo ":: FAILED: ${fun}: Return code must be 0"; (( errors++ )); }
fi
}
default() {
ln -sfv /bin/true kubectl
mkdir -pv ~/.config/gk8s
_test _fail_when_command_not_found \
"Command not found" \
"Exit immediately when requested kubectl command is not found."
_test _fail_issue_6_with_empty_cluster_name \
"Error: Cluster name must be something like :foo" \
"#6: Cluster name must not be empty."
_test _fail_with_wrong_cluster_prefix \
"must be prefixed with" \
"Return error when cluster name is not started with :"
_test _fail_without_any_argument \
"Cluster name.*is required." \
"Return error when nothing is specified at command line."
_test _ok_with_local_and_without_any_argument \
"noop command does nothing" \
"The noop command just does nothing"
_test _fail_with_cluster_config_not_found \
"KUBECONFIG file not found:" \
"Return immediately when kubecfg file not found."
_test _fail_with_local_and_cluster_config_not_found \
"(Command not found)|(KUBECONFIG file not found:)|(The connection to the server localhost:8080 was refused)" \
"Return when cluster configuration doesn't work"
_test _ok_with_fake_kubectl_get_pods \
"Executing.*kubectl get pods.*KUBECONFIG:.*foobar" \
"Ok when there is a simple get pods command"
_test _ok_with_fake_kubectl_get_pods_with_kubectl_explicitly \
"Executing.*kubectl get pods.*KUBECONFIG:.*foobar" \
"Ok when kubectl command is aslo provided"
_test _fail_with_fake_kubectl_get_pods_with_dashes_way \
"Command not found get" \
"Fail if raw command is not found."
_test _ok_with_fake_kubectl_get_pods_with_dashes_way \
"echo get pods" \
"Fail if raw command is not found."
_test _fail_to_delete \
"File .delete does.* exist" \
"Fail to delete because .delete file not found."
_test _fail_to_del \
"File .delete does.* exist" \
"Fail to del(ete) because .delete file not found."
_test _ok_to_delete \
"File .delete was removed." \
"Flag file will be removed first."
_test _ok_to_delete \
"Executing.*kubectl delete pods" \
"Now executing actual delete command."
_test _ok_to_delete_with_env_flag \
"Executing.*kubectl delete pods" \
"Now executing actual delete command."
_test _fail_with_helm_repo_list \
"Error: no repositories to show" \
"helm would not need any separator (--). To get this test passed,
helm binary should be found on your local system."
}
### main routines ######################################################
set -u
errors=0
"${@:-_panic}"
>&2 echo "::"
>&2 echo ":: ${errors} test(s) failed."
[[ "${errors}" -eq 0 ]]