github.com/mponton/terratest@v0.44.0/modules/k8s/deployment_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  	"fmt"
    14  	"time"
    15  
    16  	"strings"
    17  	"testing"
    18  
    19  	"github.com/mponton/terratest/modules/random"
    20  	"github.com/stretchr/testify/require"
    21  	appsv1 "k8s.io/api/apps/v1"
    22  	v1 "k8s.io/api/core/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  )
    25  
    26  func TestGetDeploymentEReturnsError(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	options := NewKubectlOptions("", "", "")
    30  	_, err := GetDeploymentE(t, options, "nginx-deployment")
    31  	require.Error(t, err)
    32  }
    33  
    34  func TestGetDeployments(t *testing.T) {
    35  	t.Parallel()
    36  
    37  	uniqueID := strings.ToLower(random.UniqueId())
    38  	options := NewKubectlOptions("", "", uniqueID)
    39  	configData := fmt.Sprintf(ExampleDeploymentYAMLTemplate, uniqueID)
    40  	KubectlApplyFromString(t, options, configData)
    41  	defer KubectlDeleteFromString(t, options, configData)
    42  
    43  	deployment := GetDeployment(t, options, "nginx-deployment")
    44  	require.Equal(t, deployment.Name, "nginx-deployment")
    45  	require.Equal(t, deployment.Namespace, uniqueID)
    46  }
    47  
    48  func TestListDeployments(t *testing.T) {
    49  	t.Parallel()
    50  
    51  	uniqueID := strings.ToLower(random.UniqueId())
    52  	options := NewKubectlOptions("", "", uniqueID)
    53  	configData := fmt.Sprintf(ExampleDeploymentYAMLTemplate, uniqueID)
    54  	KubectlApplyFromString(t, options, configData)
    55  	defer KubectlDeleteFromString(t, options, configData)
    56  
    57  	deployments := ListDeployments(t, options, metav1.ListOptions{})
    58  	require.Equal(t, len(deployments), 1)
    59  
    60  	deployment := deployments[0]
    61  	require.Equal(t, deployment.Name, "nginx-deployment")
    62  	require.Equal(t, deployment.Namespace, uniqueID)
    63  }
    64  
    65  func TestWaitUntilDeploymentAvailable(t *testing.T) {
    66  	t.Parallel()
    67  
    68  	uniqueID := strings.ToLower(random.UniqueId())
    69  	options := NewKubectlOptions("", "", uniqueID)
    70  	configData := fmt.Sprintf(ExampleDeploymentYAMLTemplate, uniqueID)
    71  	KubectlApplyFromString(t, options, configData)
    72  	defer KubectlDeleteFromString(t, options, configData)
    73  
    74  	WaitUntilDeploymentAvailable(t, options, "nginx-deployment", 60, 1*time.Second)
    75  }
    76  
    77  func TestTestIsDeploymentAvailable(t *testing.T) {
    78  	testCases := []struct {
    79  		title          string
    80  		deploy         *appsv1.Deployment
    81  		expectedResult bool
    82  	}{
    83  		{
    84  			title: "TestIsDeploymentAvailableWithProgressingNewReplicaSetAvailable",
    85  			deploy: &appsv1.Deployment{
    86  				Status: appsv1.DeploymentStatus{
    87  					Conditions: []appsv1.DeploymentCondition{
    88  						{
    89  							Type:   appsv1.DeploymentProgressing,
    90  							Status: v1.ConditionTrue,
    91  							Reason: "NewReplicaSetAvailable",
    92  						},
    93  					},
    94  				},
    95  			},
    96  			expectedResult: true,
    97  		},
    98  		{
    99  			title: "TestIsDeploymentAvailableWithoutProgressingNewReplicaSetAvailable",
   100  			deploy: &appsv1.Deployment{
   101  				Status: appsv1.DeploymentStatus{
   102  					Conditions: []appsv1.DeploymentCondition{
   103  						{
   104  							Type:   appsv1.DeploymentProgressing,
   105  							Status: v1.ConditionTrue,
   106  							Reason: "ReplicaSetUpdated",
   107  						},
   108  					},
   109  				},
   110  			},
   111  			expectedResult: false,
   112  		},
   113  	}
   114  
   115  	for _, tc := range testCases {
   116  		tc := tc
   117  		t.Run(tc.title, func(t *testing.T) {
   118  			t.Parallel()
   119  			actualResult := IsDeploymentAvailable(tc.deploy)
   120  			require.Equal(t, tc.expectedResult, actualResult)
   121  		})
   122  	}
   123  }
   124  
   125  const ExampleDeploymentYAMLTemplate = `---
   126  apiVersion: v1
   127  kind: Namespace
   128  metadata:
   129    name: %s
   130  ---
   131  apiVersion: apps/v1
   132  kind: Deployment
   133  metadata:
   134    name: nginx-deployment
   135    labels:
   136      app: nginx
   137  spec:
   138    strategy:
   139      rollingUpdate:
   140        maxSurge: 10%%
   141        maxUnavailable: 0
   142    replicas: 2
   143    selector:
   144      matchLabels:
   145        app: nginx
   146    template:
   147      metadata:
   148        labels:
   149          app: nginx
   150      spec:
   151        containers:
   152        - name: nginx
   153          image: nginx:1.15.7
   154          ports:
   155          - containerPort: 80
   156          readinessProbe:
   157            httpGet:
   158              path: /
   159              port: 80
   160  `