github.com/mponton/terratest@v0.44.0/modules/k8s/tunnel_test.go (about) 1 //go:build kubeall || kubernetes 2 // +build kubeall 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 "crypto/tls" 14 "fmt" 15 "strings" 16 "testing" 17 "time" 18 19 http_helper "github.com/mponton/terratest/modules/http-helper" 20 "github.com/mponton/terratest/modules/random" 21 ) 22 23 func TestTunnelOpensAPortForwardTunnelToPod(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 WaitUntilPodAvailable(t, options, "nginx-pod", 60, 1*time.Second) 32 33 // Open a tunnel to pod from any available port locally 34 tunnel := NewTunnel(options, ResourceTypePod, "nginx-pod", 0, 80) 35 defer tunnel.Close() 36 tunnel.ForwardPort(t) 37 38 // Setup a TLS configuration to submit with the helper, a blank struct is acceptable 39 tlsConfig := tls.Config{} 40 41 // Try to access the nginx service on the local port, retrying until we get a good response for up to 5 minutes 42 http_helper.HttpGetWithRetryWithCustomValidation( 43 t, 44 fmt.Sprintf("http://%s", tunnel.Endpoint()), 45 &tlsConfig, 46 60, 47 5*time.Second, 48 verifyNginxWelcomePage, 49 ) 50 } 51 52 func TestTunnelOpensAPortForwardTunnelToDeployment(t *testing.T) { 53 t.Parallel() 54 55 uniqueID := strings.ToLower(random.UniqueId()) 56 options := NewKubectlOptions("", "", uniqueID) 57 configData := fmt.Sprintf(ExampleDeploymentYAMLTemplate, uniqueID) 58 KubectlApplyFromString(t, options, configData) 59 defer KubectlDeleteFromString(t, options, configData) 60 WaitUntilDeploymentAvailable(t, options, "nginx-deployment", 60, 1*time.Second) 61 62 // Open a tunnel to pod from any available port locally 63 tunnel := NewTunnel(options, ResourceTypeDeployment, "nginx-deployment", 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 TestTunnelOpensAPortForwardTunnelToService(t *testing.T) { 82 t.Parallel() 83 84 uniqueID := strings.ToLower(random.UniqueId()) 85 options := NewKubectlOptions("", "", uniqueID) 86 configData := fmt.Sprintf(ExamplePodWithServiceYAMLTemplate, uniqueID, uniqueID, uniqueID) 87 defer KubectlDeleteFromString(t, options, configData) 88 KubectlApplyFromString(t, options, configData) 89 WaitUntilPodAvailable(t, options, "nginx-pod", 60, 1*time.Second) 90 WaitUntilServiceAvailable(t, options, "nginx-service", 60, 1*time.Second) 91 92 // Open a tunnel from any available port locally 93 tunnel := NewTunnel(options, ResourceTypeService, "nginx-service", 0, 80) 94 defer tunnel.Close() 95 tunnel.ForwardPort(t) 96 97 // Setup a TLS configuration to submit with the helper, a blank struct is acceptable 98 tlsConfig := tls.Config{} 99 100 // Try to access the nginx service on the local port, retrying until we get a good response for up to 5 minutes 101 http_helper.HttpGetWithRetryWithCustomValidation( 102 t, 103 fmt.Sprintf("http://%s", tunnel.Endpoint()), 104 &tlsConfig, 105 60, 106 5*time.Second, 107 verifyNginxWelcomePage, 108 ) 109 } 110 111 func verifyNginxWelcomePage(statusCode int, body string) bool { 112 if statusCode != 200 { 113 return false 114 } 115 return strings.Contains(body, "Welcome to nginx") 116 } 117 118 const ExamplePodWithServiceYAMLTemplate = `--- 119 apiVersion: v1 120 kind: Namespace 121 metadata: 122 name: %s 123 --- 124 apiVersion: v1 125 kind: Pod 126 metadata: 127 name: nginx-pod 128 namespace: %s 129 labels: 130 app: nginx 131 spec: 132 containers: 133 - name: nginx 134 image: nginx:1.15.7 135 ports: 136 - containerPort: 80 137 readinessProbe: 138 httpGet: 139 path: / 140 port: 80 141 --- 142 apiVersion: v1 143 kind: Service 144 metadata: 145 name: nginx-service 146 namespace: %s 147 spec: 148 selector: 149 app: nginx 150 ports: 151 - protocol: TCP 152 targetPort: 80 153 port: 80 154 `