add cmdline tests for -k

This commit is contained in:
Jingfang Liu 2019-02-25 16:31:03 -08:00
parent b38cf738d6
commit beacc87ebf
8 changed files with 125 additions and 0 deletions

View File

@ -0,0 +1,7 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: the-map
data:
altGreeting: "Good Morning!"
enableRisky: "false"

35
hack/testdata/kustomize/deployment.yaml vendored Normal file
View File

@ -0,0 +1,35 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: the-deployment
labels:
deployment: hello
spec:
replicas: 3
selector:
matchLabels:
deployment: hello
template:
metadata:
labels:
deployment: hello
spec:
containers:
- name: the-container
image: monopole/hello:1
command: ["/hello",
"--port=8080",
"--enableRiskyFeature=$(ENABLE_RISKY)"]
ports:
- containerPort: 8080
env:
- name: ALT_GREETING
valueFrom:
configMapKeyRef:
name: the-map
key: altGreeting
- name: ENABLE_RISKY
valueFrom:
configMapKeyRef:
name: the-map
key: enableRisky

View File

@ -0,0 +1,5 @@
nameprefix: test-
resources:
- deployment.yaml
- service.yaml
- configmap.yaml

12
hack/testdata/kustomize/service.yaml vendored Normal file
View File

@ -0,0 +1,12 @@
kind: Service
apiVersion: v1
metadata:
name: the-service
spec:
selector:
deployment: hello
type: LoadBalancer
ports:
- protocol: TCP
port: 8666
targetPort: 8080

View File

@ -223,6 +223,21 @@ __EOF__
# cleanup
kubectl delete -f hack/testdata/service-revision2.yaml "${kube_flags[@]}"
## kubectl apply -k somedir
kubectl apply -k hack/testdata/kustomize
kube::test::get_object_assert 'configmap test-the-map' "{{${id_field}}}" 'test-the-map'
kube::test::get_object_assert 'deployment test-the-deployment' "{{${id_field}}}" 'test-the-deployment'
kube::test::get_object_assert 'service test-the-service' "{{${id_field}}}" 'test-the-service'
# cleanup
kubectl delete configmap/test-the-map deployment/test-the-deployment service/test-the-service
## kubectl apply --kustomize somedir
kubectl apply --kustomize hack/testdata/kustomize
kube::test::get_object_assert 'configmap test-the-map' "{{${id_field}}}" 'test-the-map'
kube::test::get_object_assert 'deployment test-the-deployment' "{{${id_field}}}" 'test-the-deployment'
kube::test::get_object_assert 'service test-the-service' "{{${id_field}}}" 'test-the-service'
# cleanup
kubectl delete -k hack/testdata/kustomize
set +o nounset
set +o errexit

View File

@ -108,3 +108,28 @@ run_create_job_tests() {
set +o nounset
set +o errexit
}
run_kubectl_create_kustomization_directory_tests() {
set -o nounset
set -o errexit
## kubectl create -k <dir> for kustomization directory
# Pre-condition: no ConfigMap, Deployment, Service exist
kube::test::get_object_assert configmaps "{{range.items}}{{$id_field}}:{{end}}" ''
kube::test::get_object_assert deployment "{{range.items}}{{$id_field}}:{{end}}" ''
kube::test::get_object_assert services "{{range.items}}{{$id_field}}:{{end}}" ''
# Command
kubectl create -k hack/testdata/kustomize
# Post-condition: test-the-map, test-the-deployment, test-the-service exist
# Check that all items in the list are printed
kube::test::get_object_assert 'configmap test-the-map' "{{${id_field}}}" 'test-the-map'
kube::test::get_object_assert 'deployment test-the-deployment' "{{${id_field}}}" 'test-the-deployment'
kube::test::get_object_assert 'service test-the-service' "{{${id_field}}}" 'test-the-service'
# cleanup
kubectl delete -k hack/testdata/kustomize
set +o nounset
set +o errexit
}

View File

@ -202,6 +202,29 @@ run_kubectl_get_tests() {
# cleanup
kubectl delete pods redis-master valid-pod "${kube_flags[@]}"
### Test 'kubectl get -k <dir>' prints all the items built from a kustomization directory
# Pre-condition: no ConfigMap, Deployment, Service exist
kube::test::get_object_assert configmaps "{{range.items}}{{$id_field}}:{{end}}" ''
kube::test::get_object_assert deployment "{{range.items}}{{$id_field}}:{{end}}" ''
kube::test::get_object_assert services "{{range.items}}{{$id_field}}:{{end}}" ''
# Command
kubectl apply -k hack/testdata/kustomize
# Post-condition: test-the-map, test-the-deployment, test-the-service exist
# Check that all items in the list are printed
output_message=$(kubectl get -k hack/testdata/kustomize -o jsonpath="{..metadata.name}" "${kube_flags[@]}")
kube::test::if_has_string "${output_message}" "test-the-map"
kube::test::if_has_string "${output_message}" "test-the-deployment"
kube::test::if_has_string "${output_message}" "test-the-service"
# cleanup
kubectl delete -k hack/testdata/kustomize
# Check that all items in the list are deleted
kube::test::get_object_assert configmaps "{{range.items}}{{$id_field}}:{{end}}" ''
kube::test::get_object_assert deployment "{{range.items}}{{$id_field}}:{{end}}" ''
kube::test::get_object_assert services "{{range.items}}{{$id_field}}:{{end}}" ''
set +o nounset
set +o errexit
}

View File

@ -492,6 +492,9 @@ runTests() {
if kube::test::if_supports_resource "${secrets}" ; then
record_command run_create_secret_tests
fi
if kube::test::if_supports_resource "${deployments}"; then
record_command run_kubectl_create_kustomization_directory_tests
fi
######################
# Delete #