github.com/terraform-modules-krish/terratest@v0.29.0/modules/k8s/pod_test.go (about) 1 // +build kubeall kubernetes 2 3 // NOTE: we have build tags to differentiate kubernetes tests from non-kubernetes tests. This is done because minikube 4 // is heavy and can interfere with docker related tests in terratest. Specifically, many of the tests start to fail with 5 // `connection refused` errors from `minikube`. To avoid overloading the system, we run the kubernetes tests and helm 6 // tests separately from the others. This may not be necessary if you have a sufficiently powerful machine. We 7 // recommend at least 4 cores and 16GB of RAM if you want to run all the tests together. 8 9 package k8s 10 11 import ( 12 "fmt" 13 "strings" 14 "testing" 15 "time" 16 17 "github.com/stretchr/testify/require" 18 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 19 20 "github.com/terraform-modules-krish/terratest/modules/random" 21 ) 22 23 func TestListPodsReturnsPodsInNamespace(t *testing.T) { 24 t.Parallel() 25 26 uniqueID := strings.ToLower(random.UniqueId()) 27 options := NewKubectlOptions("", "", uniqueID) 28 configData := fmt.Sprintf(EXAMPLE_POD_YAML_TEMPLATE, uniqueID, uniqueID) 29 defer KubectlDeleteFromString(t, options, configData) 30 KubectlApplyFromString(t, options, configData) 31 32 pods := ListPods(t, options, metav1.ListOptions{}) 33 require.Equal(t, len(pods), 1) 34 pod := pods[0] 35 require.Equal(t, pod.Name, "nginx-pod") 36 require.Equal(t, pod.Namespace, uniqueID) 37 } 38 39 func TestGetPodEReturnsErrorForNonExistantPod(t *testing.T) { 40 t.Parallel() 41 42 options := NewKubectlOptions("", "", "default") 43 _, err := GetPodE(t, options, "nginx-pod") 44 require.Error(t, err) 45 } 46 47 func TestGetPodEReturnsCorrectPodInCorrectNamespace(t *testing.T) { 48 t.Parallel() 49 50 uniqueID := strings.ToLower(random.UniqueId()) 51 options := NewKubectlOptions("", "", uniqueID) 52 configData := fmt.Sprintf(EXAMPLE_POD_YAML_TEMPLATE, uniqueID, uniqueID) 53 defer KubectlDeleteFromString(t, options, configData) 54 KubectlApplyFromString(t, options, configData) 55 56 pod := GetPod(t, options, "nginx-pod") 57 require.Equal(t, pod.Name, "nginx-pod") 58 require.Equal(t, pod.Namespace, uniqueID) 59 } 60 61 func TestWaitUntilNumPodsCreatedReturnsSuccessfully(t *testing.T) { 62 t.Parallel() 63 64 uniqueID := strings.ToLower(random.UniqueId()) 65 options := NewKubectlOptions("", "", uniqueID) 66 configData := fmt.Sprintf(EXAMPLE_POD_YAML_TEMPLATE, uniqueID, uniqueID) 67 defer KubectlDeleteFromString(t, options, configData) 68 KubectlApplyFromString(t, options, configData) 69 70 WaitUntilNumPodsCreated(t, options, metav1.ListOptions{}, 1, 60, 1*time.Second) 71 } 72 73 func TestWaitUntilPodAvailableReturnsSuccessfully(t *testing.T) { 74 t.Parallel() 75 76 uniqueID := strings.ToLower(random.UniqueId()) 77 options := NewKubectlOptions("", "", uniqueID) 78 configData := fmt.Sprintf(EXAMPLE_POD_YAML_TEMPLATE, uniqueID, uniqueID) 79 defer KubectlDeleteFromString(t, options, configData) 80 KubectlApplyFromString(t, options, configData) 81 82 WaitUntilPodAvailable(t, options, "nginx-pod", 60, 1*time.Second) 83 } 84 85 func TestWaitUntilPodWithMultipleContainersAvailableReturnsSuccessfully(t *testing.T) { 86 t.Parallel() 87 88 uniqueID := strings.ToLower(random.UniqueId()) 89 options := NewKubectlOptions("", "", uniqueID) 90 configData := fmt.Sprintf(EXAMPLE_POD_WITH_MULTIPLE_CONTAINERS_YAML_TEMPLATE, uniqueID, uniqueID) 91 defer KubectlDeleteFromString(t, options, configData) 92 KubectlApplyFromString(t, options, configData) 93 94 WaitUntilPodAvailable(t, options, "nginx-pod", 60, 1*time.Second) 95 } 96 97 const EXAMPLE_POD_YAML_TEMPLATE = `--- 98 apiVersion: v1 99 kind: Namespace 100 metadata: 101 name: %s 102 --- 103 apiVersion: v1 104 kind: Pod 105 metadata: 106 name: nginx-pod 107 namespace: %s 108 spec: 109 containers: 110 - name: nginx 111 image: nginx:1.15.7 112 ports: 113 - containerPort: 80 114 ` 115 116 const EXAMPLE_POD_WITH_MULTIPLE_CONTAINERS_YAML_TEMPLATE = EXAMPLE_POD_YAML_TEMPLATE + ` 117 - name: nginx-two 118 image: nginx:1.15.7 119 ports: 120 - containerPort: 80 121 `