github.com/terraform-modules-krish/terratest@v0.29.0/modules/k8s/tunnel_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 "crypto/tls" 13 "fmt" 14 "strings" 15 "testing" 16 "time" 17 18 http_helper "github.com/terraform-modules-krish/terratest/modules/http-helper" 19 "github.com/terraform-modules-krish/terratest/modules/random" 20 ) 21 22 func TestTunnelOpensAPortForwardTunnelToPod(t *testing.T) { 23 t.Parallel() 24 25 uniqueID := strings.ToLower(random.UniqueId()) 26 options := NewKubectlOptions("", "", uniqueID) 27 configData := fmt.Sprintf(EXAMPLE_POD_YAML_TEMPLATE, uniqueID, uniqueID) 28 defer KubectlDeleteFromString(t, options, configData) 29 KubectlApplyFromString(t, options, configData) 30 WaitUntilPodAvailable(t, options, "nginx-pod", 60, 1*time.Second) 31 32 // Open a tunnel to pod from any available port locally 33 tunnel := NewTunnel(options, ResourceTypePod, "nginx-pod", 0, 80) 34 defer tunnel.Close() 35 tunnel.ForwardPort(t) 36 37 // Setup a TLS configuration to submit with the helper, a blank struct is acceptable 38 tlsConfig := tls.Config{} 39 40 // Try to access the nginx service on the local port, retrying until we get a good response for up to 5 minutes 41 http_helper.HttpGetWithRetryWithCustomValidation( 42 t, 43 fmt.Sprintf("http://%s", tunnel.Endpoint()), 44 &tlsConfig, 45 60, 46 5*time.Second, 47 verifyNginxWelcomePage, 48 ) 49 } 50 51 func TestTunnelOpensAPortForwardTunnelToService(t *testing.T) { 52 t.Parallel() 53 54 uniqueID := strings.ToLower(random.UniqueId()) 55 options := NewKubectlOptions("", "", uniqueID) 56 configData := fmt.Sprintf(EXAMPLE_POD_WITH_SERVICE_YAML_TEMPLATE, uniqueID, uniqueID, uniqueID) 57 defer KubectlDeleteFromString(t, options, configData) 58 KubectlApplyFromString(t, options, configData) 59 WaitUntilPodAvailable(t, options, "nginx-pod", 60, 1*time.Second) 60 WaitUntilServiceAvailable(t, options, "nginx-service", 60, 1*time.Second) 61 62 // Open a tunnel from any available port locally 63 tunnel := NewTunnel(options, ResourceTypeService, "nginx-service", 0, 80) 64 defer tunnel.Close() 65 tunnel.ForwardPort(t) 66 67 // Setup a TLS configuration to submit with the helper, a blank struct is acceptable 68 tlsConfig := tls.Config{} 69 70 // Try to access the nginx service on the local port, retrying until we get a good response for up to 5 minutes 71 http_helper.HttpGetWithRetryWithCustomValidation( 72 t, 73 fmt.Sprintf("http://%s", tunnel.Endpoint()), 74 &tlsConfig, 75 60, 76 5*time.Second, 77 verifyNginxWelcomePage, 78 ) 79 } 80 81 func verifyNginxWelcomePage(statusCode int, body string) bool { 82 if statusCode != 200 { 83 return false 84 } 85 return strings.Contains(body, "Welcome to nginx") 86 } 87 88 const EXAMPLE_POD_WITH_SERVICE_YAML_TEMPLATE = `--- 89 apiVersion: v1 90 kind: Namespace 91 metadata: 92 name: %s 93 --- 94 apiVersion: v1 95 kind: Pod 96 metadata: 97 name: nginx-pod 98 namespace: %s 99 labels: 100 app: nginx 101 spec: 102 containers: 103 - name: nginx 104 image: nginx:1.15.7 105 ports: 106 - containerPort: 80 107 readinessProbe: 108 httpGet: 109 path: / 110 port: 80 111 --- 112 apiVersion: v1 113 kind: Service 114 metadata: 115 name: nginx-service 116 namespace: %s 117 spec: 118 selector: 119 app: nginx 120 ports: 121 - protocol: TCP 122 targetPort: 80 123 port: 80 124 `