forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubernetes_basic_example_service_check_test.go
77 lines (62 loc) · 3.13 KB
/
kubernetes_basic_example_service_check_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// +build kubeall kubernetes
// NOTE: we have build tags to differentiate kubernetes tests from non-kubernetes tests. This is done because minikube
// is heavy and can interfere with docker related tests in terratest. Specifically, many of the tests start to fail with
// `connection refused` errors from `minikube`. To avoid overloading the system, we run the kubernetes tests and helm
// tests separately from the others. This may not be necessary if you have a sufficiently powerful machine. We
// recommend at least 4 cores and 16GB of RAM if you want to run all the tests together.
package test
import (
"crypto/tls"
"fmt"
"path/filepath"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
http_helper "github.com/gruntwork-io/terratest/modules/http-helper"
"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/gruntwork-io/terratest/modules/random"
)
// An example of how to do more expanded verification of the Kubernetes resource config in examples/kubernetes-basic-example using Terratest.
func TestKubernetesBasicExampleServiceCheck(t *testing.T) {
t.Parallel()
// Path to the Kubernetes resource config we will test
kubeResourcePath, err := filepath.Abs("../examples/kubernetes-basic-example/nginx-deployment.yml")
require.NoError(t, err)
// Setup the kubectl config and context. Here we choose to use the defaults, which is:
// - HOME/.kube/config for the kubectl config file
// - Current context of the kubectl config file
options := k8s.NewKubectlOptions("", "")
// To ensure we can reuse the resource config on the same cluster to test different scenarios, we setup a unique
// namespace for the resources for this test.
// Note that namespaces must be lowercase.
namespaceName := strings.ToLower(random.UniqueId())
k8s.CreateNamespace(t, options, namespaceName)
// Make sure we set the namespace on the options
options.Namespace = namespaceName
// ... and make sure to delete the namespace at the end of the test
defer k8s.DeleteNamespace(t, options, namespaceName)
// At the end of the test, run `kubectl delete -f RESOURCE_CONFIG` to clean up any resources that were created.
defer k8s.KubectlDelete(t, options, kubeResourcePath)
// This will run `kubectl apply -f RESOURCE_CONFIG` and fail the test if there are any errors
k8s.KubectlApply(t, options, kubeResourcePath)
// This will wait up to 10 seconds for the service to become available, to ensure that we can access it.
k8s.WaitUntilServiceAvailable(t, options, "nginx-service", 10, 1*time.Second)
// Now we verify that the service will successfully boot and start serving requests
service := k8s.GetService(t, options, "nginx-service")
endpoint := k8s.GetServiceEndpoint(t, options, service, 80)
// Setup a TLS configuration to submit with the helper, a blank struct is acceptable
tlsConfig := tls.Config{}
// Test the endpoint for up to 5 minutes. This will only fail if we timeout waiting for the service to return a 200
// response.
http_helper.HttpGetWithRetryWithCustomValidation(
t,
fmt.Sprintf("http://%s", endpoint),
&tlsConfig,
30,
10*time.Second,
func(statusCode int, body string) bool {
return statusCode == 200
},
)
}