Skip to content

Commit f79f9eb

Browse files
committed
Add support for running acceptance tests in a kind cluster
* make the injector-leader-elector a bit more reliable when run locally
1 parent e5ff06a commit f79f9eb

File tree

4 files changed

+62
-6
lines changed

4 files changed

+62
-6
lines changed

Makefile

+30-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ ACCEPTANCE_TESTS?=acceptance
77
# filter bats unit tests to run.
88
UNIT_TESTS_FILTER?='.*'
99

10+
# set to 'true' to run acceptance tests locally in a kind cluster
11+
LOCAL_ACCEPTANCE_TESTS?=false
12+
13+
# kind cluster name
14+
KIND_CLUSTER_NAME?=vault-helm
15+
16+
# kind k8s version
17+
KIND_K8S_VERSION?=v1.20.2
18+
1019
# Generate json schema for chart values. See test/README.md for more details.
1120
values-schema:
1221
helm schema-gen values.yaml > values.schema.json
@@ -24,14 +33,18 @@ test: test-image test-bats
2433
# run acceptance tests on GKE
2534
# set google project/credential vars above
2635
test-acceptance:
36+
ifeq ($(LOCAL_ACCEPTANCE_TESTS),true)
37+
make setup-kind acceptance
38+
else
2739
@docker run -it -v ${PWD}:/helm-test \
2840
-e GOOGLE_CREDENTIALS=${GOOGLE_CREDENTIALS} \
2941
-e CLOUDSDK_CORE_PROJECT=${CLOUDSDK_CORE_PROJECT} \
3042
-e KUBECONFIG=/helm-test/.kube/config \
3143
-w /helm-test \
3244
$(TEST_IMAGE) \
3345
make acceptance
34-
46+
endif
47+
3548
# destroy GKE cluster using terraform
3649
test-destroy:
3750
@docker run -it -v ${PWD}:/helm-test \
@@ -54,7 +67,9 @@ test-provision:
5467
# this target is for running the acceptance tests
5568
# it is run in the docker container above when the test-acceptance target is invoked
5669
acceptance:
70+
ifneq ($(LOCAL_ACCEPTANCE_TESTS),true)
5771
gcloud auth activate-service-account --key-file=${GOOGLE_CREDENTIALS}
72+
endif
5873
bats test/${ACCEPTANCE_TESTS}
5974

6075
# this target is for provisioning the GKE cluster
@@ -69,4 +84,17 @@ provision-cluster:
6984
destroy-cluster:
7085
terraform destroy -auto-approve
7186

72-
.PHONY: values-schema test-image test-unit test-bats test test-acceptance test-destroy test-provision acceptance provision-cluster destroy-cluster
87+
# create a kind cluster for running the acceptance tests locally
88+
setup-kind:
89+
kind get clusters | grep -q "^${KIND_CLUSTER_NAME}$$" || \
90+
kind create cluster \
91+
--image kindest/node:${KIND_K8S_VERSION} \
92+
--name ${KIND_CLUSTER_NAME} \
93+
--config $(CURDIR)/test/kind/config.yaml
94+
kubectl config use-context kind-${KIND_CLUSTER_NAME}
95+
96+
# delete the kind cluster
97+
delete-kind:
98+
kind delete cluster --name ${KIND_CLUSTER_NAME} || :
99+
100+
.PHONY: values-schema test-image test-unit test-bats test test-acceptance test-destroy test-provision acceptance provision-cluster destroy-cluster

test/README.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@
22

33
## Running Vault Helm Acceptance tests
44

5-
The Makefile at the top level of this repo contains a few target that should help with running acceptance tests in your own GKE instance.
5+
The Makefile at the top level of this repo contains a few target that should help with running acceptance tests in your own GKE instance or in kind cluster.
66

7-
* Set the GOOGLE_CREDENTIALS and CLOUDSDK_CORE_PROJECT variables at the top of the file. GOOGLE_CREDENTIALS should contain the local path to your Google Cloud Platform account credentials in JSON format. CLOUDSDK_CORE_PROJECT should be set to the ID of your GCP project.
7+
### Running in a GKE cluster
8+
* Set the `GOOGLE_CREDENTIALS` and `CLOUDSDK_CORE_PROJECT` variables at the top of the file. `GOOGLE_CREDENTIALS` should contain the local path to your Google Cloud Platform account credentials in JSON format. `CLOUDSDK_CORE_PROJECT` should be set to the ID of your GCP project.
89
* Run `make test-image` to create the docker image (with dependencies installed) that will be re-used in the below steps.
910
* Run `make test-provision` to provision the GKE cluster using terraform.
1011
* Run `make test-acceptance` to run the acceptance tests in this already provisioned cluster.
1112
* You can choose to only run certain tests by setting the ACCEPTANCE_TESTS variable and re-running the above target.
1213
* Run `make test-destroy` when you have finished testing and want to tear-down and remove the cluster.
1314

15+
### Running in a kind cluster
16+
* Run `make test-acceptance LOCAL_ACCEPTANCE_TESTS=true`
17+
* You can choose to only run certain tests by setting the `ACCEPTANCE_TESTS` variable and re-running the above target.
18+
* Run `make delete-kind` when you have finished testing and want to tear-down and remove the cluster.
19+
* You can set an alternate kind cluster name by specifying the `KIND_CLUSTER_NAME` variable for any of the above targets.
20+
* You can set an alternate K8S version by specifying the `KIND_K8S_VERSION` variable for any of the above targets.
21+
22+
See [kind-quick-start](https://kind.sigs.k8s.io/docs/user/quick-start/) if you don't have kind installed on your system.
23+
1424
## Running chart verification tests
1525

1626
If [chart-verifier](https://github.com/redhat-certification/chart-verifier) is built and available in your PATH, run:

test/acceptance/injector-leader-elector.bats

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,30 @@ load _helpers
44

55
@test "injector: testing leader elector" {
66
cd `chart_dir`
7-
7+
88
kubectl delete namespace acceptance --ignore-not-found=true
99
kubectl create namespace acceptance
1010
kubectl config set-context --current --namespace=acceptance
1111

1212
helm install "$(name_prefix)" \
13+
--wait \
14+
--timeout=5m \
1315
--set="injector.replicas=3" .
1416
kubectl wait --for condition=Ready pod -l app.kubernetes.io/name=vault-agent-injector --timeout=5m
1517

1618
pods=($(kubectl get pods -l app.kubernetes.io/name=vault-agent-injector -o json | jq -r '.items[] | .metadata.name'))
1719
[ "${#pods[@]}" == 3 ]
1820

19-
leader="$(echo "$(kubectl exec ${pods[0]} -c sidecar-injector -- wget --quiet --output-document - localhost:4040)" | jq -r .name)"
21+
leader=''
22+
tries=0
23+
until [ $tries -ge 60 ]
24+
do
25+
leader="$(echo "$(kubectl exec ${pods[0]} -c sidecar-injector -- wget --quiet --output-document - localhost:4040)" | jq -r .name)"
26+
[ -n "${leader}" ] && break
27+
((tries++))
28+
sleep .5
29+
done
30+
2031
# Check the leader name is valid - i.e. one of the 3 pods
2132
[[ " ${pods[@]} " =~ " ${leader} " ]]
2233

test/kind/config.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
kind: Cluster
2+
apiVersion: kind.x-k8s.io/v1alpha4
3+
nodes:
4+
- role: control-plane
5+
- role: worker
6+
- role: worker
7+
- role: worker

0 commit comments

Comments
 (0)