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