github.com/terraform-modules-krish/terratest@v0.29.0/modules/k8s/service_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 "crypto/tls" 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 http_helper "github.com/terraform-modules-krish/terratest/modules/http-helper" 22 "github.com/terraform-modules-krish/terratest/modules/random" 23 ) 24 25 func TestGetServiceEReturnsErrorForNonExistantService(t *testing.T) { 26 t.Parallel() 27 28 options := NewKubectlOptions("", "", "default") 29 _, err := GetServiceE(t, options, "nginx-service") 30 require.Error(t, err) 31 } 32 33 func TestGetServiceEReturnsCorrectServiceInCorrectNamespace(t *testing.T) { 34 t.Parallel() 35 36 uniqueID := strings.ToLower(random.UniqueId()) 37 options := NewKubectlOptions("", "", uniqueID) 38 configData := fmt.Sprintf(EXAMPLE_DEPLOYMENT_YAML_TEMPLATE, uniqueID, uniqueID, uniqueID) 39 KubectlApplyFromString(t, options, configData) 40 defer KubectlDeleteFromString(t, options, configData) 41 42 service := GetService(t, options, "nginx-service") 43 require.Equal(t, service.Name, "nginx-service") 44 require.Equal(t, service.Namespace, uniqueID) 45 } 46 47 func TestListServicesReturnsCorrectServiceInCorrectNamespace(t *testing.T) { 48 t.Parallel() 49 50 uniqueID := strings.ToLower(random.UniqueId()) 51 options := NewKubectlOptions("", "", uniqueID) 52 configData := fmt.Sprintf(EXAMPLE_DEPLOYMENT_YAML_TEMPLATE, uniqueID, uniqueID, uniqueID) 53 KubectlApplyFromString(t, options, configData) 54 defer KubectlDeleteFromString(t, options, configData) 55 56 services := ListServices(t, options, metav1.ListOptions{}) 57 require.Equal(t, len(services), 1) 58 59 service := services[0] 60 require.Equal(t, service.Name, "nginx-service") 61 require.Equal(t, service.Namespace, uniqueID) 62 } 63 64 func TestWaitUntilServiceAvailableReturnsSuccessfullyOnNodePortType(t *testing.T) { 65 t.Parallel() 66 67 uniqueID := strings.ToLower(random.UniqueId()) 68 options := NewKubectlOptions("", "", uniqueID) 69 configData := fmt.Sprintf(EXAMPLE_DEPLOYMENT_YAML_TEMPLATE, uniqueID, uniqueID, uniqueID) 70 KubectlApplyFromString(t, options, configData) 71 defer KubectlDeleteFromString(t, options, configData) 72 73 WaitUntilServiceAvailable(t, options, "nginx-service", 10, 1*time.Second) 74 } 75 76 func TestGetServiceEndpointEReturnsAccessibleEndpointForNodePort(t *testing.T) { 77 t.Parallel() 78 79 uniqueID := strings.ToLower(random.UniqueId()) 80 options := NewKubectlOptions("", "", uniqueID) 81 configData := fmt.Sprintf(EXAMPLE_DEPLOYMENT_YAML_TEMPLATE, uniqueID, uniqueID, uniqueID) 82 KubectlApplyFromString(t, options, configData) 83 defer KubectlDeleteFromString(t, options, configData) 84 85 service := GetService(t, options, "nginx-service") 86 endpoint := GetServiceEndpoint(t, options, service, 80) 87 88 // Setup a TLS configuration to submit with the helper, a blank struct is acceptable 89 tlsConfig := tls.Config{} 90 91 // Test up to 5 minutes 92 http_helper.HttpGetWithRetryWithCustomValidation( 93 t, 94 fmt.Sprintf("http://%s", endpoint), 95 &tlsConfig, 96 30, 97 10*time.Second, 98 func(statusCode int, body string) bool { 99 return statusCode == 200 100 }, 101 ) 102 } 103 104 const EXAMPLE_DEPLOYMENT_YAML_TEMPLATE = `--- 105 apiVersion: v1 106 kind: Namespace 107 metadata: 108 name: %s 109 --- 110 apiVersion: apps/v1 111 kind: Deployment 112 metadata: 113 name: nginx-deployment 114 namespace: %s 115 spec: 116 selector: 117 matchLabels: 118 app: nginx 119 replicas: 1 120 template: 121 metadata: 122 labels: 123 app: nginx 124 spec: 125 containers: 126 - name: nginx 127 image: nginx:1.15.7 128 ports: 129 - containerPort: 80 130 --- 131 kind: Service 132 apiVersion: v1 133 metadata: 134 name: nginx-service 135 namespace: %s 136 spec: 137 selector: 138 app: nginx 139 ports: 140 - protocol: TCP 141 targetPort: 80 142 port: 80 143 type: NodePort 144 `