forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubernetes_basic_example_test.go
47 lines (37 loc) · 1.9 KB
/
kubernetes_basic_example_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
package test
import (
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/gruntwork-io/terratest/modules/random"
)
// An example of how to test the Kubernetes resource config in examples/kubernetes-basic-example using Terratest.
func TestKubernetesBasicExample(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 get the service resource and verify that it exists and was retrieved successfully. This function will
// fail the test if the there is an error retrieving the service resource from Kubernetes.
service := k8s.GetService(t, options, "nginx-service")
require.Equal(t, service.Name, "nginx-service")
}