3.3 对tf文件进行测试 · tf文件
该代码仅简单输出一个"Hello, World!"字符串。
terraform {
# This module is now only being tested with Terraform 0.13.x. However, to make upgrading easier, we are setting
# 0.12.26 as the minimum version, as that version added support for required_providers with source URLs, making it
# forwards compatible with 0.13.x code.
required_version = ">= 0.12.26"
}
# The simplest possible Terraform module: it just outputs "Hello, World!"
output "hello_world" {
value = "Hello, World!"
}
传统的利用terraform进行测试需要init/plan/apply,之后进行destroy清理。
利用terratest可省去以上繁琐命令,集成为代码中进行统一测试 · terraform_hello_world_example_test.go
package test
import (
"testing"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/stretchr/testify/assert"
)
func TestTerraformHelloWorldExample(t *testing.T) {
// Construct the terraform options with default retryable errors to handle the most common
// retryable errors in terraform testing.
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
// Set the path to the Terraform code that will be tested.
TerraformDir: "../examples/terraform-hello-world-example",
})
// Clean up resources with "terraform destroy" at the end of the test.
defer terraform.Destroy(t, terraformOptions)
// Run "terraform init" and "terraform apply". Fail the test if there are any errors.
terraform.InitAndApply(t, terraformOptions)
// Run `terraform output` to get the values of output variables and check they have the expected values.
output := terraform.Output(t, terraformOptions, "hello_world")
assert.Equal(t, "Hello, World!", output)
}
以上代码完成手工执行所有步骤,包括运行terraform init,terraform apply,使用terraform output读取输出变量,检查它的值是我们所期望的,以及运行terraform destroy(使用defer在测试结束时运行它,无论测试成功还是失败)。如果此代码放入名为terraform_hello_world_example_test.go的文件中,您可以通过执行go test来运行它,您将看到如下所示的输出(为了可读性而截断):
$ go test -v -timeout 30m
=== RUN TestTerraformHelloWorldExample
Running command terraform with args [init]
Initializing provider plugins...
[...]
Terraform has been successfully initialized!
[...]
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
hello_world = "Hello, World!"
[...]
Running command terraform with args [destroy -force -input=false]
[...]
Destroy complete! Resources: 2 destroyed.
--- PASS: TestTerraformHelloWorldExample (149.36s)
3.4 测试Dockerfile · Dockerfile
# Build a simple Docker image that contains a text file with the contents "Hello, World!"
FROM ubuntu:18.04
RUN echo 'Hello, World!' > /test.txt
传统测试Dockerfile使用docker build,之后docker run查看是否正常生成了内容为'Hello, World!'的test.txt文件,利用terratest可写成代码。 · docker_hello_world_example_test.go
package test
import (
"testing"
"github.com/gruntwork-io/terratest/modules/docker"
"github.com/stretchr/testify/assert"
)
func TestDockerHelloWorldExample(t *testing.T) {
// Configure the tag to use on the Docker image.
tag := "gruntwork/docker-hello-world-example"
buildOptions := &docker.BuildOptions{
Tags: []string{tag},
}
// Build the Docker image.
docker.Build(t, "../examples/docker-hello-world-example", buildOptions)
// Run the Docker image, read the text file from it, and make sure it contains the expected output.
opts := &docker.RunOptions{Command: []string{"cat", "/test.txt"}}
output := docker.Run(t, tag, opts)
assert.Equal(t, "Hello, World!", output)
}
3.5 测试Kubernetes资源清单 · hello-world-deployment.yml
---
# Deploy the training/webapp Docker Container: https://hub.docker.com/r/training/webapp/
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world-deployment
spec:
selector:
matchLabels:
app: hello-world
replicas: 1
template:
metadata:
labels:
app: hello-world
spec:
containers:
# The container runs a Python webapp on port 5000 that responds with "Hello, World!"
- name: hello-world
image: training/webapp:latest
ports:
- containerPort: 5000
---
# Expose the Python webapp on port 5000 via a Kubernetes LoadBalancer.
kind: Service
apiVersion: v1
metadata:
name: hello-world-service
spec:
selector:
app: hello-world
ports:
- protocol: TCP
targetPort: 5000
port: 5000
type: LoadBalancer
· kubernetes_hello_world_example_test.go
//go:build kubeall || kubernetes
// +build kubeall kubernetes
// NOTE: See the notes in the other Kubernetes example tests for why this build tag is included.
package test
import (
"fmt"
"testing"
"time"
http_helper "github.com/gruntwork-io/terratest/modules/http-helper"
"github.com/gruntwork-io/terratest/modules/k8s"
)
func TestKubernetesHelloWorldExample(t *testing.T) {
t.Parallel()
// Path to the Kubernetes resource config we will test.
kubeResourcePath := "../examples/kubernetes-hello-world-example/hello-world-deployment.yml"
// Setup the kubectl config and context.
options := k8s.NewKubectlOptions("", "", "default")
// At the end of the test, run "kubectl delete" to clean up any resources that were created.
defer k8s.KubectlDelete(t, options, kubeResourcePath)
// Run `kubectl apply` to deploy. Fail the test if there are any errors.
k8s.KubectlApply(t, options, kubeResourcePath)
// Verify the service is available and get the URL for it.
k8s.WaitUntilServiceAvailable(t, options, "hello-world-service", 10, 1*time.Second)
service := k8s.GetService(t, options, "hello-world-service")
url := fmt.Sprintf("http://%s", k8s.GetServiceEndpoint(t, options, service, 5000))
// Make an HTTP request to the URL and make sure it returns a 200 OK with the body "Hello, World".
http_helper.HttpGetWithRetry(t, url, nil, 200, "Hello world!", 30, 3*time.Second)
}