Slide 1

Slide 1 text

Testing Infrastructure with Terratest Ladislav Prskavec Ladislav Prskavec - Teststack conference, 4. 6. 2020 1

Slide 2

Slide 2 text

Who Am I 4 Oracle Cloud Infrastructure - API Services team 4 Twitter: @abtris 4 Website: https://www.prskavec.net/ 4 Golang Prague organizer (@GoMeetupPrague) Ladislav Prskavec - Teststack conference, 4. 6. 2020 2

Slide 3

Slide 3 text

Infrastructure as code Ladislav Prskavec - Teststack conference, 4. 6. 2020 3

Slide 4

Slide 4 text

Ladislav Prskavec - Teststack conference, 4. 6. 2020 4

Slide 5

Slide 5 text

How to Test Infrastructure Code Ladislav Prskavec - Teststack conference, 4. 6. 2020 5

Slide 6

Slide 6 text

Ladislav Prskavec - Teststack conference, 4. 6. 2020 6

Slide 7

Slide 7 text

Yevgeniy Brikman 4 co-founder of Gruntwork with Josh Padnick 4 Gruntwork are authors of tools: Terragrunt and Terratest 4 Books Author - Terraform: Up & Running and Hello, Startup 4 Yevgeniy Brikman talk Automated Testing for Terraform, Docker, Packer, Kubernetes, and More, 11 Nov 2019 Ladislav Prskavec - Teststack conference, 4. 6. 2020 7

Slide 8

Slide 8 text

Test Pyramid 4 https://martinfowler.com/articles/practical-test-pyramid.html Ladislav Prskavec - Teststack conference, 4. 6. 2020 8

Slide 9

Slide 9 text

How we can test infrastructure code Ladislav Prskavec - Teststack conference, 4. 6. 2020 9

Slide 10

Slide 10 text

Static Analysis Ladislav Prskavec - Teststack conference, 4. 6. 2020 10

Slide 11

Slide 11 text

Syntax validation 4 terraform validate 4 kubernetes apply -f --dry-run -- validate=true Ladislav Prskavec - Teststack conference, 4. 6. 2020 11

Slide 12

Slide 12 text

Dry-run 4 terraform plan 4 kubectl apply -f --server-dry-run Ladislav Prskavec - Teststack conference, 4. 6. 2020 12

Slide 13

Slide 13 text

Linting & policies checks 4 hadolint 4 kube-score 4 conftest - Open Policy Agent Rego query language Ladislav Prskavec - Teststack conference, 4. 6. 2020 13

Slide 14

Slide 14 text

Unit Tests Ladislav Prskavec - Teststack conference, 4. 6. 2020 14

Slide 15

Slide 15 text

No pure unit testing for infrastructure Ladislav Prskavec - Teststack conference, 4. 6. 2020 15

Slide 16

Slide 16 text

Test strategy (per unit) 1. Deploy real infrastructure 2. Validate it works 3. Undeploy the infrastructure Ladislav Prskavec - Teststack conference, 4. 6. 2020 16

Slide 17

Slide 17 text

Example Hello World with docker Ladislav Prskavec - Teststack conference, 4. 6. 2020 17

Slide 18

Slide 18 text

Dockerfile FROM alpine RUN echo 'Hello, World!' > /test.txt Ladislav Prskavec - Teststack conference, 4. 6. 2020 18

Slide 19

Slide 19 text

Test package test import ( "testing" "github.com/gruntwork-io/terratest/modules/docker" "github.com/magiconair/properties/assert" ) func TestDockerHelloWorldExample(t *testing.T) { tag := "teststack/demo-terratest-docker" buildOptions := &docker.BuildOptions{ Tags: []string{tag}, } docker.Build(t, "../hello-world", buildOptions) opts := &docker.RunOptions{Command: []string{"cat", "/test.txt"}} output := docker.Run(t, tag, opts) assert.Equal(t, "Hello, World!", output) } Ladislav Prskavec - Teststack conference, 4. 6. 2020 19

Slide 20

Slide 20 text

Local vs CI tests Ladislav Prskavec - Teststack conference, 4. 6. 2020 20

Slide 21

Slide 21 text

Running tests in CI Ladislav Prskavec - Teststack conference, 4. 6. 2020 21

Slide 22

Slide 22 text

Integration Tests Ladislav Prskavec - Teststack conference, 4. 6. 2020 22

Slide 23

Slide 23 text

Test strategy - integration test 1. Deploy all the infrastruture 2. Validate it works 3. Undeploy all the infrastruture Ladislav Prskavec - Teststack conference, 4. 6. 2020 23

Slide 24

Slide 24 text

Basic example OCI - APIGW and Fn Ladislav Prskavec - Teststack conference, 4. 6. 2020 24

Slide 25

Slide 25 text

const fdk = require('@fnproject/fdk'); fdk.handle(function(input) { let name = 'World'; if (input.name) { name = input.name; } return {message: 'Hello ' + name}; }); Ladislav Prskavec - Teststack conference, 4. 6. 2020 25

Slide 26

Slide 26 text

schema_version: 20180708 name: hello-world version: 0.0.2 runtime: node entrypoint: node func.js format: http-stream triggers: - name: hello-world-trigger type: http source: /hello-world-trigger Ladislav Prskavec - Teststack conference, 4. 6. 2020 26

Slide 27

Slide 27 text

package test import (...) func TestHelloWorldAppUnit(t *testing.T) { uniqueID := random.UniqueId() terraformOptions := &terraform.Options{ TerraformDir: "../", Vars: map[string]interface{}{ "name": fmt.Sprintf("hello-world-%s", uniqueID), "suffix": fmt.Sprintf("%s", uniqueID), "tenancy_ocid": os.Getenv("TF_VAR_tenancy_ocid"), "user_ocid": os.Getenv("TF_VAR_user_ocid"), "private_key_path": os.Getenv("TF_VAR_private_key_path"), "fingerprint": os.Getenv("TF_VAR_fingerprint"), "private_key_password": os.Getenv("TF_VAR_private_key_password"), "region": os.Getenv("TF_VAR_region"), }, } defer terraform.Destroy(t, terraformOptions) terraform.InitAndApply(t, terraformOptions) validateHelloWorldApp(t, terraformOptions) } Ladislav Prskavec - Teststack conference, 4. 6. 2020 27

Slide 28

Slide 28 text

func validateHelloWorldApp(t *testing.T, terraformOptions *terraform.Options) { url := terraform.Output(t, terraformOptions, "gateway_url") expectedStatus := 200 expectedBody := `{"message":"Hello World"}` maxRetries := 10 timeBetweenRetries := 10 * time.Second http_helper.HttpGetWithRetry(t, url, nil, expectedStatus, expectedBody, maxRetries, timeBetweenRetries ) } Ladislav Prskavec - Teststack conference, 4. 6. 2020 28

Slide 29

Slide 29 text

Ladislav Prskavec - Teststack conference, 4. 6. 2020 29

Slide 30

Slide 30 text

Cleanup after tests Ladislav Prskavec - Teststack conference, 4. 6. 2020 30

Slide 31

Slide 31 text

4 aws-nuke 4 cloud-nuke Ladislav Prskavec - Teststack conference, 4. 6. 2020 31

Slide 32

Slide 32 text

Validate Ladislav Prskavec - Teststack conference, 4. 6. 2020 32

Slide 33

Slide 33 text

Validate what make sense for you 4 test terraform output 4 http calls api/application 4 tcp/udp calls 4 ssh commands Ladislav Prskavec - Teststack conference, 4. 6. 2020 33

Slide 34

Slide 34 text

Test parallelism Ladislav Prskavec - Teststack conference, 4. 6. 2020 34

Slide 35

Slide 35 text

Parallelism 4 locking resources issue, 4 separations (compartments, accounts, regions) Ladislav Prskavec - Teststack conference, 4. 6. 2020 35

Slide 36

Slide 36 text

Running tests in parallel func TestKubernetesBasicExample(t *testing.T) { t.Parallel() ... Ladislav Prskavec - Teststack conference, 4. 6. 2020 36

Slide 37

Slide 37 text

Resource conflicts 4 randomize 4 namespace (k8s) / compartments (OCI) / accounts (AWS) import "github.com/gruntwork-io/terratest/modules/random" randomSuffix := random.UniqueId() // unique namespace namespaceName := fmt.Sprintf("test-%s", strings.ToLower(random.UniqueId())) Ladislav Prskavec - Teststack conference, 4. 6. 2020 37

Slide 38

Slide 38 text

Test stages Ladislav Prskavec - Teststack conference, 4. 6. 2020 38

Slide 39

Slide 39 text

import test_structure "github.com/gruntwork-io/terratest/modules/test-structure" func TestDockerComposeWithStagesLocal(t *testing.T) { workingDir := "../hello-world-docker-compose" test_structure.RunTestStage(t, "build_docker_image", func() { buildImage(t, "go-webapp", "../demowebapp") buildImage(t, "local/nginx", "../nginx") }) test_structure.RunTestStage(t, "run_docker_compose", func() { runCompose(t, workingDir) }) } Ladislav Prskavec - Teststack conference, 4. 6. 2020 39

Slide 40

Slide 40 text

Why skip stages 4 skip cleanup on first run 4 skip deploy/build/cleanup on iterations Ladislav Prskavec - Teststack conference, 4. 6. 2020 40

Slide 41

Slide 41 text

How skip stages 4 use environment variable SKIP_stage_name=1 4 run with that: SKIP_build_docker_image=1 go test Ladislav Prskavec - Teststack conference, 4. 6. 2020 41

Slide 42

Slide 42 text

Why and how make retries 4 Exponential Backoff And Jitter 4 Simulator for AWS architecture blog (http://www.awsarchitectureblog.com/ ) about jitter and backoff. Ladislav Prskavec - Teststack conference, 4. 6. 2020 42

Slide 43

Slide 43 text

How make retries import http_helper "github.com/gruntwork-io/terratest/modules/http-helper" retries := 30 sleepBetweenRetries := 5*time.Second http_helper.HttpGetWithRetry(t, url, nil, 200, "Hello world!", retries, sleepBetweenRetries) Ladislav Prskavec - Teststack conference, 4. 6. 2020 43

Slide 44

Slide 44 text

How make retries with curl opts := &docker.RunOptions{ Command: []string{"--retry", "5", "--retry-connrefused", "-s", "http://production_nginx:80/hello"}, OtherOptions: []string{"--network", "testdockercomposewithstageslocal_teststack-network"}, } tag := "appropriate/curl" output := docker.Run(t, tag, opts) Ladislav Prskavec - Teststack conference, 4. 6. 2020 44

Slide 45

Slide 45 text

Example Hello World with K8S Ladislav Prskavec - Teststack conference, 4. 6. 2020 45

Slide 46

Slide 46 text

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: - name: hello-world image: training/webapp:latest ports: - containerPort: 5000 Ladislav Prskavec - Teststack conference, 4. 6. 2020 46

Slide 47

Slide 47 text

kind: Service apiVersion: v1 metadata: name: hello-world-service spec: selector: app: hello-world ports: - protocol: TCP targetPort: 5000 port: 5000 type: LoadBalancer Ladislav Prskavec - Teststack conference, 4. 6. 2020 47

Slide 48

Slide 48 text

package test import (...) func TestKubernetesHelloWorldExample(t *testing.T) { kubeResourcePath := "../hello-world-kubernetes/deployment.yaml" // unique namespace namespaceName := fmt.Sprintf("test-%s", strings.ToLower(random.UniqueId())) options := k8s.NewKubectlOptions("", "", namespaceName) k8s.CreateNamespace(t, options, namespaceName) defer k8s.DeleteNamespace(t, options, namespaceName) defer k8s.KubectlDelete(t, options, kubeResourcePath) k8s.KubectlApply(t, options, kubeResourcePath) k8s.WaitUntilServiceAvailable(t, options, "hello-world-service", 10, 3*time.Second) service := k8s.GetService(t, options, "hello-world-service") url := fmt.Sprintf("http://%s", k8s.GetServiceEndpoint(t, options, service, 5000)) http_helper.HttpGetWithRetry(t, url, nil, 200, "Hello world!", 30, 5*time.Second) } Ladislav Prskavec - Teststack conference, 4. 6. 2020 48

Slide 49

Slide 49 text

Ladislav Prskavec - Teststack conference, 4. 6. 2020 49

Slide 50

Slide 50 text

Example Hello World with docker-compose and stages Ladislav Prskavec - Teststack conference, 4. 6. 2020 50

Slide 51

Slide 51 text

1. Build docker images for nginx and webapp 2. Run docker-compose 3. Validate nginx and webapp 4. Destroy docker-compose Ladislav Prskavec - Teststack conference, 4. 6. 2020 51

Slide 52

Slide 52 text

version: "3" services: nginx: image: local/nginx:latest container_name: "production_nginx" networks: - "teststack-network" ports: - "80:80" go-webapp: image: go-webapp:latest container_name: "production_go-webapp" environment: - SERVER_TEXT=${SERVER_TEXT} expose: - "8080" networks: - "teststack-network" networks: "teststack-network": Ladislav Prskavec - Teststack conference, 4. 6. 2020 52

Slide 53

Slide 53 text

package main import (...) func helloHandler(res http.ResponseWriter, req *http.Request) { res.Header().Set( "Content-Type", "text/plain", ) io.WriteString( res, fmt.Sprintf("%s", os.Getenv("SERVER_TEXT")), ) } func defaultHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Go web app powered by Docker") } func main() { http.HandleFunc("/", defaultHandler) http.HandleFunc("/hello", helloHandler) err := http.ListenAndServe(":8080", nil) if err != nil { log.Fatal("ListenAndServe: ", err) return } } Ladislav Prskavec - Teststack conference, 4. 6. 2020 53

Slide 54

Slide 54 text

func TestDockerComposeLocal(t *testing.T) { tag := "go-webapp" buildargs := "-t local/nginx" buildOptions := &docker.BuildOptions{ Tags: []string{tag}, BuildArgs: []string{buildargs} } docker.Build(t, "../hello-world-docker-compose", buildOptions) tag := "local/nginx" buildargs := "-f Dockerfile.nginx" buildOptions := &docker.BuildOptions{ Tags: []string{tag}, BuildArgs: []string{buildargs} } docker.Build(t, "../hello-world-docker-compose", buildOptions) Ladislav Prskavec - Teststack conference, 4. 6. 2020 54

Slide 55

Slide 55 text

serverPort := 80 randomSuffix := random.UniqueId() expectedServerText := fmt.Sprintf("Hello, %s!", randomSuffix) dockerOptions := &docker.Options{ WorkingDir: "../hello-world-docker-compose", EnvVars: map[string]string{ "SERVER_TEXT": expectedServerText, "SERVER_PORT": strconv.Itoa(serverPort), "randomSuffix": randomSuffix, }, } defer docker.RunDockerCompose(t, dockerOptions, "down") docker.RunDockerCompose(t, dockerOptions, "up", "-d") maxRetries := 10 timeBetweenRetries := 5 * time.Second url := fmt.Sprintf("http://localhost:%d/hello", serverPort) tlsConfig := tls.Config{} http_helper.HttpGetWithRetry(t, url, &tlsConfig, 200, expectedServerText, maxRetries, timeBetweenRetries) } Ladislav Prskavec - Teststack conference, 4. 6. 2020 55

Slide 56

Slide 56 text

Ladislav Prskavec - Teststack conference, 4. 6. 2020 56

Slide 57

Slide 57 text

func TestDockerComposeWithStagesLocal(t *testing.T) { workingDir := "../hello-world-docker-compose" test_structure.RunTestStage(t, "build_docker_image", func() { buildImage(t, "go-webapp", "../demowebapp") buildImage(t, "local/nginx", "../nginx") }) test_structure.RunTestStage(t, "run_docker_compose", func() { runCompose(t, workingDir) }) } Ladislav Prskavec - Teststack conference, 4. 6. 2020 57

Slide 58

Slide 58 text

func buildImage(t *testing.T, tag string, workingDir string) { buildOptions := &docker.BuildOptions{ Tags: []string{tag}, } docker.Build(t, workingDir, buildOptions) } Ladislav Prskavec - Teststack conference, 4. 6. 2020 58

Slide 59

Slide 59 text

func runCompose(t *testing.T, workingDir string) { serverPort := 80 randomSuffix := random.UniqueId() expectedServerText := fmt.Sprintf("Hello, %s!", randomSuffix) dockerOptions := &docker.Options{ WorkingDir: workingDir, EnvVars: map[string]string{ "SERVER_TEXT": expectedServerText, "randomSuffix": randomSuffix, "SERVER_PORT": strconv.Itoa(serverPort), }, } defer docker.RunDockerCompose(t, dockerOptions, "down") docker.RunDockerCompose(t, dockerOptions, "up", "-d") opts := &docker.RunOptions{ Command: []string{"--retry", "5", "--retry-connrefused", "-s", "http://production_nginx:80/hello"}, OtherOptions: []string{"--network", "testdockercomposewithstageslocal_teststack-network"}, } tag := "appropriate/curl" output := docker.Run(t, tag, opts) assert.Equal(t, expectedServerText, output) } Ladislav Prskavec - Teststack conference, 4. 6. 2020 59

Slide 60

Slide 60 text

Ladislav Prskavec - Teststack conference, 4. 6. 2020 60

Slide 61

Slide 61 text

For iteration skip build stage Ladislav Prskavec - Teststack conference, 4. 6. 2020 61

Slide 62

Slide 62 text

Ladislav Prskavec - Teststack conference, 4. 6. 2020 62

Slide 63

Slide 63 text

E2E Tests Ladislav Prskavec - Teststack conference, 4. 6. 2020 63

Slide 64

Slide 64 text

Can we use this test strategy? 1. Deploy all the infrastruture 2. Validate it works 3. Undeploy all the infrastruture Ladislav Prskavec - Teststack conference, 4. 6. 2020 64

Slide 65

Slide 65 text

Test type Number of resources Chance of failure Unit test 10 1% Integration test 50 5% E2E test 500+ 40%+ For unit & integration you improve reliability with retries. Ladislav Prskavec - Teststack conference, 4. 6. 2020 65

Slide 66

Slide 66 text

Please don't write E2E tests in this way. Ladislav Prskavec - Teststack conference, 4. 6. 2020 66

Slide 67

Slide 67 text

Incremental testing Ladislav Prskavec - Teststack conference, 4. 6. 2020 67

Slide 68

Slide 68 text

Incremental testing 1. Deploy a persistent test environment and leave it running. 2. Each time you update a module, deploy & validate just that module. 3. Test your deployment process is zero-downtime too. Ladislav Prskavec - Teststack conference, 4. 6. 2020 68

Slide 69

Slide 69 text

Summary 4 Static Analysis (1-60 seconds) 4 Unit Tests (1-20 minutes) 4 Integration Tests (5-60 minutes) 4 E2E Tests (60-240+ minutes) Ladislav Prskavec - Teststack conference, 4. 6. 2020 69

Slide 70

Slide 70 text

What Good Worse Static Analysis Very Fast (until 1 min), Stable, Easy to use, No deploy Limited errors for catch, no reals checks with infrastructure Unit tests Fast (1-20 min), very good stability with retries, trust in components Need real deploy, non trivial test code, extra work for CI Integration tests Mostly stable with retries, High level confidence in multiple units Slow (5-60 min), Need real deploy, non trivial test code, extra work for CI E2E tests Build confidence in entire architecture Very slow (60-240+ min), Need real deploy, Still problem 0- deployments, Can be brittle (even with retries) Ladislav Prskavec - Teststack conference, 4. 6. 2020 70

Slide 71

Slide 71 text

Try make at least one test for your docker environment! Ladislav Prskavec - Teststack conference, 4. 6. 2020 71

Slide 72

Slide 72 text

Infrastructure code without tests is scary Ladislav Prskavec - Teststack conference, 4. 6. 2020 72

Slide 73

Slide 73 text

Q & A #test-infrastructure on Teststack Slack Ladislav Prskavec - Teststack conference, 4. 6. 2020 73