github.com/swisspost/terratest@v0.0.0-20230214120104-7ec6de2e1ae0/modules/helm/upgrade_test.go (about)

     1  //go:build kubeall || helm
     2  // +build kubeall helm
     3  
     4  // NOTE: we have build tags to differentiate kubernetes tests from non-kubernetes tests, and further differentiate helm
     5  // tests. This is done because minikube is heavy and can interfere with docker related tests in terratest. Similarly,
     6  // helm can overload the minikube system and thus interfere with the other kubernetes tests. To avoid overloading the
     7  // system, we run the kubernetes tests and helm tests separately from the others.
     8  
     9  package helm
    10  
    11  import (
    12  	"crypto/tls"
    13  	"fmt"
    14  	"strings"
    15  	"testing"
    16  	"time"
    17  
    18  	http_helper "github.com/gruntwork-io/terratest/modules/http-helper"
    19  	"github.com/gruntwork-io/terratest/modules/k8s"
    20  	"github.com/gruntwork-io/terratest/modules/random"
    21  )
    22  
    23  // Test that we can install, upgrade, and rollback a remote chart (e.g stable/chartmuseum)
    24  func TestRemoteChartInstallUpgradeRollback(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	namespaceName := fmt.Sprintf(
    28  		"%s-%s",
    29  		strings.ToLower(t.Name()),
    30  		strings.ToLower(random.UniqueId()),
    31  	)
    32  
    33  	// Use default kubectl options to create a new namespace for this test, and then update the namespace for kubectl
    34  	kubectlOptions := k8s.NewKubectlOptions("", "", namespaceName)
    35  
    36  	defer k8s.DeleteNamespace(t, kubectlOptions, namespaceName)
    37  	k8s.CreateNamespace(t, kubectlOptions, namespaceName)
    38  
    39  	// Override service type to node port
    40  	options := &Options{
    41  		KubectlOptions: kubectlOptions,
    42  		SetValues: map[string]string{
    43  			"service.type": "NodePort",
    44  		},
    45  	}
    46  
    47  	// Add the stable repo under a random name so as not to touch existing repo configs
    48  	uniqueName := strings.ToLower(fmt.Sprintf("terratest-%s", random.UniqueId()))
    49  	defer RemoveRepo(t, options, uniqueName)
    50  	AddRepo(t, options, uniqueName, remoteChartSource)
    51  	helmChart := fmt.Sprintf("%s/%s", uniqueName, remoteChartName)
    52  
    53  	// Generate a unique release name so we can defer the delete before installing
    54  	releaseName := fmt.Sprintf(
    55  		"%s-%s",
    56  		remoteChartName, strings.ToLower(random.UniqueId()),
    57  	)
    58  	defer Delete(t, options, releaseName, true)
    59  	Install(t, options, helmChart, releaseName)
    60  	waitForRemoteChartPods(t, kubectlOptions, releaseName, 1)
    61  
    62  	// Setting replica count to 2 to check the upgrade functionality.
    63  	// After successful upgrade, the count of pods should be equal to 2.
    64  	options.SetValues = map[string]string{
    65  		"replicaCount": "2",
    66  		"service.type": "NodePort",
    67  	}
    68  	// Test that passing extra arguments doesn't error, by changing default timeout
    69  	options.ExtraArgs = map[string][]string{"upgrade": []string{"--timeout", "5m1s"}}
    70  	options.ExtraArgs["rollback"] = []string{"--timeout", "5m1s"}
    71  	Upgrade(t, options, helmChart, releaseName)
    72  	waitForRemoteChartPods(t, kubectlOptions, releaseName, 2)
    73  
    74  	// Verify service is accessible. Wait for it to become available and then hit the endpoint.
    75  	serviceName := releaseName
    76  	k8s.WaitUntilServiceAvailable(t, kubectlOptions, serviceName, 10, 1*time.Second)
    77  	service := k8s.GetService(t, kubectlOptions, serviceName)
    78  	endpoint := k8s.GetServiceEndpoint(t, kubectlOptions, service, 80)
    79  
    80  	// Setup a TLS configuration to submit with the helper, a blank struct is acceptable
    81  	tlsConfig := tls.Config{}
    82  
    83  	http_helper.HttpGetWithRetryWithCustomValidation(
    84  		t,
    85  		fmt.Sprintf("http://%s", endpoint),
    86  		&tlsConfig,
    87  		30,
    88  		10*time.Second,
    89  		func(statusCode int, body string) bool {
    90  			return statusCode == 200
    91  		},
    92  	)
    93  
    94  	// Finally, test rollback functionality. When rolling back, we should see the pods go back down to 1.
    95  	Rollback(t, options, releaseName, "")
    96  	waitForRemoteChartPods(t, kubectlOptions, releaseName, 1)
    97  }