github.com/terraform-modules-krish/terratest@v0.29.0/modules/k8s/ingress_test.go (about) 1 // +build 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 const ExampleIngressName = "nginx-service-ingress" 24 25 func TestGetIngressEReturnsErrorForNonExistantIngress(t *testing.T) { 26 t.Parallel() 27 28 options := NewKubectlOptions("", "", "default") 29 _, err := GetIngressE(t, options, "i-dont-exist") 30 require.Error(t, err) 31 } 32 33 func TestGetIngressEReturnsCorrectIngressInCorrectNamespace(t *testing.T) { 34 t.Parallel() 35 36 uniqueID := strings.ToLower(random.UniqueId()) 37 options := NewKubectlOptions("", "", uniqueID) 38 configData := fmt.Sprintf(EXAMPLE_INGRESS_DEPLOYMENT_YAML_TEMPLATE, uniqueID, uniqueID, uniqueID, uniqueID) 39 KubectlApplyFromString(t, options, configData) 40 defer KubectlDeleteFromString(t, options, configData) 41 42 service := GetIngress(t, options, "nginx-service-ingress") 43 require.Equal(t, service.Name, "nginx-service-ingress") 44 require.Equal(t, service.Namespace, uniqueID) 45 } 46 47 func TestListIngressesReturnsCorrectIngressInCorrectNamespace(t *testing.T) { 48 t.Parallel() 49 50 uniqueID := strings.ToLower(random.UniqueId()) 51 options := NewKubectlOptions("", "", uniqueID) 52 configData := fmt.Sprintf(EXAMPLE_INGRESS_DEPLOYMENT_YAML_TEMPLATE, uniqueID, uniqueID, uniqueID, uniqueID) 53 KubectlApplyFromString(t, options, configData) 54 defer KubectlDeleteFromString(t, options, configData) 55 56 ingresses := ListIngresses(t, options, metav1.ListOptions{}) 57 require.Equal(t, len(ingresses), 1) 58 59 ingress := ingresses[0] 60 require.Equal(t, ingress.Name, ExampleIngressName) 61 require.Equal(t, ingress.Namespace, uniqueID) 62 } 63 64 func TestWaitUntilIngressAvailableReturnsSuccessfully(t *testing.T) { 65 t.Parallel() 66 67 uniqueID := strings.ToLower(random.UniqueId()) 68 options := NewKubectlOptions("", "", uniqueID) 69 configData := fmt.Sprintf(EXAMPLE_INGRESS_DEPLOYMENT_YAML_TEMPLATE, uniqueID, uniqueID, uniqueID, uniqueID) 70 KubectlApplyFromString(t, options, configData) 71 defer KubectlDeleteFromString(t, options, configData) 72 73 WaitUntilIngressAvailable(t, options, ExampleIngressName, 60, 5*time.Second) 74 } 75 76 const EXAMPLE_INGRESS_DEPLOYMENT_YAML_TEMPLATE = `--- 77 apiVersion: v1 78 kind: Namespace 79 metadata: 80 name: %s 81 --- 82 apiVersion: apps/v1 83 kind: Deployment 84 metadata: 85 name: nginx-deployment 86 namespace: %s 87 spec: 88 selector: 89 matchLabels: 90 app: nginx 91 replicas: 1 92 template: 93 metadata: 94 labels: 95 app: nginx 96 spec: 97 containers: 98 - name: nginx 99 image: nginx:1.15.7 100 ports: 101 - containerPort: 80 102 --- 103 kind: Service 104 apiVersion: v1 105 metadata: 106 name: nginx-service 107 namespace: %s 108 spec: 109 selector: 110 app: nginx 111 ports: 112 - protocol: TCP 113 targetPort: 80 114 port: 80 115 type: NodePort 116 --- 117 kind: Ingress 118 apiVersion: extensions/v1beta1 119 metadata: 120 name: nginx-service-ingress 121 namespace: %s 122 spec: 123 rules: 124 - http: 125 paths: 126 - path: /app 127 backend: 128 serviceName: nginx-service 129 servicePort: 80 130 `