github.com/mponton/terratest@v0.44.0/modules/k8s/ingress_test.go (about) 1 //go:build kubernetes 2 // +build kubernetes 3 4 // NOTE: we have build tags to differentiate kubernetes tests from non-kubernetes tests. This is done because minikube 5 // is heavy and can interfere with docker related tests in terratest. Specifically, many of the tests start to fail with 6 // `connection refused` errors from `minikube`. To avoid overloading the system, we run the kubernetes tests and helm 7 // tests separately from the others. This may not be necessary if you have a sufficiently powerful machine. We 8 // recommend at least 4 cores and 16GB of RAM if you want to run all the tests together. 9 10 package k8s 11 12 import ( 13 "fmt" 14 "strings" 15 "testing" 16 "time" 17 18 "github.com/stretchr/testify/require" 19 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 20 21 "github.com/mponton/terratest/modules/random" 22 ) 23 24 const ExampleIngressName = "nginx-service-ingress" 25 26 func TestGetIngressEReturnsErrorForNonExistantIngress(t *testing.T) { 27 t.Parallel() 28 29 options := NewKubectlOptions("", "", "default") 30 _, err := GetIngressV1Beta1E(t, options, "i-dont-exist") 31 require.Error(t, err) 32 } 33 34 func TestGetIngressEReturnsCorrectIngressInCorrectNamespace(t *testing.T) { 35 t.Parallel() 36 37 uniqueID := strings.ToLower(random.UniqueId()) 38 options := NewKubectlOptions("", "", uniqueID) 39 configData := fmt.Sprintf(exampleIngressDeploymentYamlTemplate, uniqueID, uniqueID, uniqueID, uniqueID, uniqueID) 40 KubectlApplyFromString(t, options, configData) 41 defer KubectlDeleteFromString(t, options, configData) 42 43 service := GetIngressV1Beta1(t, options, "nginx-service-ingress") 44 require.Equal(t, service.Name, "nginx-service-ingress") 45 require.Equal(t, service.Namespace, uniqueID) 46 } 47 48 func TestListIngressesReturnsCorrectIngressInCorrectNamespace(t *testing.T) { 49 t.Parallel() 50 51 uniqueID := strings.ToLower(random.UniqueId()) 52 options := NewKubectlOptions("", "", uniqueID) 53 configData := fmt.Sprintf(exampleIngressDeploymentYamlTemplate, uniqueID, uniqueID, uniqueID, uniqueID, uniqueID) 54 KubectlApplyFromString(t, options, configData) 55 defer KubectlDeleteFromString(t, options, configData) 56 57 ingresses := ListIngressesV1Beta1(t, options, metav1.ListOptions{}) 58 require.Equal(t, len(ingresses), 1) 59 60 ingress := ingresses[0] 61 require.Equal(t, ingress.Name, ExampleIngressName) 62 require.Equal(t, ingress.Namespace, uniqueID) 63 } 64 65 func TestWaitUntilIngressAvailableReturnsSuccessfully(t *testing.T) { 66 t.Parallel() 67 68 uniqueID := strings.ToLower(random.UniqueId()) 69 options := NewKubectlOptions("", "", uniqueID) 70 configData := fmt.Sprintf(exampleIngressDeploymentYamlTemplate, uniqueID, uniqueID, uniqueID, uniqueID, uniqueID) 71 KubectlApplyFromString(t, options, configData) 72 defer KubectlDeleteFromString(t, options, configData) 73 74 WaitUntilIngressAvailableV1Beta1(t, options, ExampleIngressName, 60, 5*time.Second) 75 } 76 77 const exampleIngressDeploymentYamlTemplate = `--- 78 apiVersion: v1 79 kind: Namespace 80 metadata: 81 name: %s 82 --- 83 apiVersion: apps/v1 84 kind: Deployment 85 metadata: 86 name: nginx-deployment 87 namespace: %s 88 spec: 89 selector: 90 matchLabels: 91 app: nginx 92 replicas: 1 93 template: 94 metadata: 95 labels: 96 app: nginx 97 spec: 98 containers: 99 - name: nginx 100 image: nginx:1.15.7 101 ports: 102 - containerPort: 80 103 --- 104 kind: Service 105 apiVersion: v1 106 metadata: 107 name: nginx-service 108 namespace: %s 109 spec: 110 selector: 111 app: nginx 112 ports: 113 - protocol: TCP 114 targetPort: 80 115 port: 80 116 type: NodePort 117 --- 118 kind: Ingress 119 apiVersion: extensions/v1beta1 120 metadata: 121 name: nginx-service-ingress 122 namespace: %s 123 spec: 124 rules: 125 - http: 126 paths: 127 - path: /app-%s 128 backend: 129 serviceName: nginx-service 130 servicePort: 80 131 `