jx-test
is a small command line tool working with Kubernetes based (system tests, integration tests, BDD tests etc) with Terraform and the Terraform Operator
Download the jx-test binary for your operating system and add it to your $PATH
.
Or you can use jx test
directly in the Jenkins X 3.x CLI
First you need to create a Terraform
resource which can use Go / Helm style templating of values.
e.g.
apiVersion: tf.isaaguilar.com/v1alpha1
kind: Terraform
spec:
env:
- name: TF_VAR_jx_git_url
value: https://github.com/jenkins-x-bdd/cluster-{{ .Env.TF_VAR_cluster_name }}-dev.git
- name: TF_VAR_jx_bot_username
value: jenkins-x-bot-bdd
- name: TF_VAR_jx_bot_token
valueFrom:
secretKeyRef:
name: bdd-git
key: password
{{- range $pkey, $pval := .Env }}
- name: {{ $pkey }}
value: {{ quote $pval }}
{{- end }}
scmAuthMethods:
- host: github.com
git:
https:
requireProxy: false
tokenSecretRef:
name: bdd-git
namespace: jx
key: password
terraformRunner: ghcr.io/jenkins-x/terraform-operator-gcp
terraformVersion: 0.0.6
terraformModule:
address: https://github.com/jenkins-x-bdd/infra-{{ .Env.TF_VAR_cluster_name }}-dev
customBackend: |-
terraform {
backend "kubernetes" {
secret_suffix = "{{ .Name }}-state"
namespace = "{{ .Namespace }}"
in_cluster_config = true
}
}
serviceAccount: tekton-bot
applyOnCreate: true
applyOnUpdate: true
applyOnDelete: true
ignoreDelete: false
postrunScript: |-
#!/usr/bin/env bash
echo "Terraform is done!"
echo "lets connect to the remote cluster"
$(terraform output -raw connect)
# TODO now do more tests...
You then run a Terraform based test by creating an instance of the resource passing in the template values:
export TF_VAR_gcp_project=myproject
export TF_VAR_cluster_name=mycluster
jx test create --file tf.yaml
This command will:
-
Create the
Terraform
resource for thetf.yaml
file after rendering the template and substituting in any variables for the current pipeline, build number, PR and so forth. -
The Terraform Operator detects the
Terraform
and will create aJob
to perform theterraform apply
and then run anypostrunScript:
scripts -
The terminal will tail the output of this Job and pass/fail based on the Job
kubectl get terraform
or the more brief:
kubectl get tf
Run the following command periodically:
jx test gc
If a test fails and you need time to investigate you can label the Terraform resource to ensure it doesn't get garbage collected as follows
Run the following command periodically:
kubectl label terraform mytest keep=yes
When you are ready to remove the test case resources do:
kubectl delete terraform mytest
See the jx-test command reference