forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
delete.sh
executable file
·126 lines (102 loc) · 5.65 KB
/
delete.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
#!/usr/bin/env bash
# Copyright 2018 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o errexit
set -o nounset
set -o pipefail
# Runs tests related to kubectl delete --all-namespaces.
run_kubectl_delete_allnamespaces_tests() {
set -o nounset
set -o errexit
ns_one="namespace-$(date +%s)-${RANDOM}"
ns_two="namespace-$(date +%s)-${RANDOM}"
kubectl create namespace "${ns_one}"
kubectl create namespace "${ns_two}"
kubectl create configmap "one" --namespace="${ns_one}"
kubectl create configmap "two" --namespace="${ns_two}"
kubectl label configmap "one" --namespace="${ns_one}" deletetest=true
kubectl label configmap "two" --namespace="${ns_two}" deletetest=true
# dry-run
kubectl delete configmap --dry-run=client -l deletetest=true --all-namespaces
kubectl delete configmap --dry-run=server -l deletetest=true --all-namespaces
kubectl config set-context "${CONTEXT}" --namespace="${ns_one}"
kube::test::get_object_assert 'configmap -l deletetest' "{{range.items}}{{${id_field:?}}}:{{end}}" 'one:'
kubectl config set-context "${CONTEXT}" --namespace="${ns_two}"
kube::test::get_object_assert 'configmap -l deletetest' "{{range.items}}{{${id_field:?}}}:{{end}}" 'two:'
kubectl delete configmap -l deletetest=true --all-namespaces
# no configmaps should be in either of those namespaces with label deletetest
kubectl config set-context "${CONTEXT}" --namespace="${ns_one}"
kube::test::get_object_assert 'configmap -l deletetest' "{{range.items}}{{${id_field:?}}}:{{end}}" ''
kubectl config set-context "${CONTEXT}" --namespace="${ns_two}"
kube::test::get_object_assert 'configmap -l deletetest' "{{range.items}}{{${id_field:?}}}:{{end}}" ''
set +o nounset
set +o errexit
}
# Runs tests related to kubectl delete --confirm
run_kubectl_delete_interactive_tests() {
set -o nounset
set -o errexit
# enable interactivity flag feature environment variable
export KUBECTL_INTERACTIVE_DELETE=true
ns_one="namespace-$(date +%s)-${RANDOM}"
ns_two="namespace-$(date +%s)-${RANDOM}"
kubectl create namespace "${ns_one}"
kubectl create namespace "${ns_two}"
# create configmaps
kubectl create configmap "one" --namespace="${ns_one}"
kubectl create configmap "two" --namespace="${ns_two}"
kubectl label configmap "one" --namespace="${ns_one}" deletetest=true
kubectl label configmap "two" --namespace="${ns_two}" deletetest=true
# not confirm dry-run=server deletions
output_message=$(kubectl delete configmap --dry-run=server --interactive -l deletetest=true --all-namespaces <<< $'n\n')
kube::test::if_has_string "${output_message}" 'configmap/two' 'configmap/one'
# confirm dry-run=server deletions
kubectl delete configmap --dry-run=server --interactive -l deletetest=true --all-namespaces <<< $'y\n'
# not confirm resource deletions
output_message=$(kubectl delete configmap --interactive -l deletetest=true --all-namespaces <<< $'n\n')
kube::test::if_has_string "${output_message}" 'configmap/two' 'configmap/one'
kubectl config set-context "${CONTEXT}" --namespace="${ns_one}"
kube::test::get_object_assert 'configmap -l deletetest' "{{range.items}}{{${id_field:?}}}:{{end}}" 'one:'
kubectl config set-context "${CONTEXT}" --namespace="${ns_two}"
kube::test::get_object_assert 'configmap -l deletetest' "{{range.items}}{{${id_field:?}}}:{{end}}" 'two:'
# clean configmaps with label deletetest=true
kubectl delete configmap -l deletetest=true --all-namespaces
# create new configmaps
kubectl create configmap "third" --namespace="${ns_one}"
kubectl create configmap "fourth" --namespace="${ns_two}"
kubectl label configmap "third" --namespace="${ns_one}" deletetest2=true
kubectl label configmap "fourth" --namespace="${ns_two}" deletetest2=true
# confirm all resource deletions with waiting
kubectl delete configmaps --interactive -l deletetest2=true --all-namespaces --wait <<< $'y\n'
# no configmaps should be in either of those namespaces with label deletetest2
kubectl config set-context "${CONTEXT}" --namespace="${ns_one}"
kube::test::get_object_assert 'configmap -l deletetest2' "{{range.items}}{{${id_field:?}}}:{{end}}" ''
kubectl config set-context "${CONTEXT}" --namespace="${ns_two}"
kube::test::get_object_assert 'configmap -l deletetest2' "{{range.items}}{{${id_field:?}}}:{{end}}" ''
# clean configmaps with label deletetest2=true
kubectl delete configmap -l deletetest2=true --all-namespaces
# create new configmaps in one namespace
kubectl create configmap "fifth" --namespace="${ns_one}"
kubectl create configmap "sixth" --namespace="${ns_one}"
kubectl label configmap "fifth" --namespace="${ns_one}" deletetest3=true
kubectl label configmap "sixth" --namespace="${ns_one}" deletetest3=true
# confirm all resource deletions with forcing and waiting
kubectl delete configmaps -l deletetest3=true --force --interactive --namespace="${ns_one}" --wait <<< $'y\n'
# no configmaps should be in either of those namespaces with label deletetest3
kubectl config set-context "${CONTEXT}" --namespace="${ns_one}"
kube::test::get_object_assert 'configmap -l deletetest3' "{{range.items}}{{${id_field:?}}}:{{end}}" ''
unset KUBECTL_INTERACTIVE_DELETE
set +o nounset
set +o errexit
}