github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/test/helm_basic_example_integration_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 test
     9  
    10  import (
    11  	"crypto/tls"
    12  	"fmt"
    13  	"path/filepath"
    14  	"strings"
    15  	"testing"
    16  	"time"
    17  
    18  	"github.com/stretchr/testify/require"
    19  
    20  	"github.com/gruntwork-io/terratest/modules/helm"
    21  	http_helper "github.com/gruntwork-io/terratest/modules/http-helper"
    22  	"github.com/gruntwork-io/terratest/modules/k8s"
    23  	"github.com/gruntwork-io/terratest/modules/random"
    24  )
    25  
    26  // This file contains examples of how to use terratest to test helm charts by deploying the chart and verifying the
    27  // deployment by hitting the service endpoint.
    28  func TestHelmBasicExampleDeployment(t *testing.T) {
    29  	t.Parallel()
    30  
    31  	// Path to the helm chart we will test
    32  	helmChartPath, err := filepath.Abs("../examples/helm-basic-example")
    33  	require.NoError(t, err)
    34  
    35  	// To ensure we can reuse the resource config on the same cluster to test different scenarios, we setup a unique
    36  	// namespace for the resources for this test.
    37  	// Note that namespaces must be lowercase.
    38  	namespaceName := fmt.Sprintf("helm-basic-example-%s", strings.ToLower(random.UniqueId()))
    39  
    40  	// Setup the kubectl config and context. Here we choose to use the defaults, which is:
    41  	// - HOME/.kube/config for the kubectl config file
    42  	// - Current context of the kubectl config file
    43  	kubectlOptions := k8s.NewKubectlOptions("", "", namespaceName)
    44  
    45  	k8s.CreateNamespace(t, kubectlOptions, namespaceName)
    46  	// ... and make sure to delete the namespace at the end of the test
    47  	defer k8s.DeleteNamespace(t, kubectlOptions, namespaceName)
    48  
    49  	// Setup the args. For this test, we will set the following input values:
    50  	// - containerImageRepo=nginx
    51  	// - containerImageTag=1.15.8
    52  	options := &helm.Options{
    53  		KubectlOptions: kubectlOptions,
    54  		SetValues: map[string]string{
    55  			"containerImageRepo": "nginx",
    56  			"containerImageTag":  "1.15.8",
    57  		},
    58  	}
    59  
    60  	// We generate a unique release name so that we can refer to after deployment.
    61  	// By doing so, we can schedule the delete call here so that at the end of the test, we run
    62  	// `helm delete RELEASE_NAME` to clean up any resources that were created.
    63  	releaseName := fmt.Sprintf(
    64  		"nginx-service-%s",
    65  		strings.ToLower(random.UniqueId()),
    66  	)
    67  	defer helm.Delete(t, options, releaseName, true)
    68  
    69  	// Deploy the chart using `helm install`. Note that we use the version without `E`, since we want to assert the
    70  	// install succeeds without any errors.
    71  	helm.Install(t, options, helmChartPath, releaseName)
    72  
    73  	// Now let's verify the deployment. We will get the service endpoint and try to access it.
    74  
    75  	// First we need to get the service name. We will use domain knowledge of the chart here, where the name is
    76  	// RELEASE_NAME-CHART_NAME
    77  	serviceName := fmt.Sprintf("%s-helm-basic-example", releaseName)
    78  
    79  	// Next we wait until the service is available. This will wait up to 10 seconds for the service to become available,
    80  	// to ensure that we can access it.
    81  	k8s.WaitUntilServiceAvailable(t, kubectlOptions, serviceName, 10, 1*time.Second)
    82  
    83  	// Now we verify that the service will successfully boot and start serving requests
    84  	service := k8s.GetService(t, kubectlOptions, serviceName)
    85  	endpoint := k8s.GetServiceEndpoint(t, kubectlOptions, service, 80)
    86  
    87  	// Setup a TLS configuration to submit with the helper, a blank struct is acceptable
    88  	tlsConfig := tls.Config{}
    89  
    90  	// Test the endpoint for up to 5 minutes. This will only fail if we timeout waiting for the service to return a 200
    91  	// response.
    92  	http_helper.HttpGetWithRetryWithCustomValidation(
    93  		t,
    94  		fmt.Sprintf("http://%s", endpoint),
    95  		&tlsConfig,
    96  		30,
    97  		10*time.Second,
    98  		func(statusCode int, body string) bool {
    99  			return statusCode == 200
   100  		},
   101  	)
   102  }