github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/test/kubernetes_hello_world_example_test.go (about) 1 // +build kubeall kubernetes 2 // NOTE: See the notes in the other Kubernetes example tests for why this build tag is included. 3 4 package test 5 6 import ( 7 "fmt" 8 "testing" 9 "time" 10 11 http_helper "github.com/gruntwork-io/terratest/modules/http-helper" 12 "github.com/gruntwork-io/terratest/modules/k8s" 13 ) 14 15 func TestKubernetesHelloWorldExample(t *testing.T) { 16 t.Parallel() 17 18 // website::tag::1:: Path to the Kubernetes resource config we will test. 19 kubeResourcePath := "../examples/kubernetes-hello-world-example/hello-world-deployment.yml" 20 21 // website::tag::2:: Setup the kubectl config and context. 22 options := k8s.NewKubectlOptions("", "", "default") 23 24 // website::tag::6:: At the end of the test, run "kubectl delete" to clean up any resources that were created. 25 defer k8s.KubectlDelete(t, options, kubeResourcePath) 26 27 // website::tag::3:: Run `kubectl apply` to deploy. Fail the test if there are any errors. 28 k8s.KubectlApply(t, options, kubeResourcePath) 29 30 // website::tag::4:: Verify the service is available and get the URL for it. 31 k8s.WaitUntilServiceAvailable(t, options, "hello-world-service", 10, 1*time.Second) 32 service := k8s.GetService(t, options, "hello-world-service") 33 url := fmt.Sprintf("http://%s", k8s.GetServiceEndpoint(t, options, service, 5000)) 34 35 // website::tag::5:: Make an HTTP request to the URL and make sure it returns a 200 OK with the body "Hello, World". 36 http_helper.HttpGetWithRetry(t, url, nil, 200, "Hello world!", 30, 3*time.Second) 37 }