github.com/terraform-modules-krish/terratest@v0.29.0/modules/helm/upgrade_test.go (about)

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