Skip to content

Instantly share code, notes, and snippets.

@jwillker
Created May 8, 2022 21:33
Show Gist options
  • Save jwillker/d13e95845dd73d6808edd3d51569ee1d to your computer and use it in GitHub Desktop.
Save jwillker/d13e95845dd73d6808edd3d51569ee1d to your computer and use it in GitHub Desktop.
// An example of how to verify the rendered template object of a Helm Chart given various inputs.
func TestHelmBasicExampleTemplateRenderedDeployment(t *testing.T) {
t.Parallel()
// Path to the helm chart we will test
helmChartPath, err := filepath.Abs("../examples/helm-basic-example")
releaseName := "helm-basic"
require.NoError(t, err)
// Since we aren't deploying any resources, there is no need to setup kubectl authentication or helm home.
// Set up the namespace; confirm that the template renders the expected value for the namespace.
namespaceName := "medieval-" + strings.ToLower(random.UniqueId())
logger.Logf(t, "Namespace: %s\n", namespaceName)
// Setup the args. For this test, we will set the following input values:
// - containerImageRepo=nginx
// - containerImageTag=1.15.8
options := &helm.Options{
SetValues: map[string]string{
"containerImageRepo": "nginx",
"containerImageTag": "1.15.8",
},
KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
}
// Run RenderTemplate to render the template and capture the output. Note that we use the version without `E`, since
// we want to assert that the template renders without any errors.
// Additionally, although we know there is only one yaml file in the template, we deliberately path a templateFiles
// arg to demonstrate how to select individual templates to render.
output := helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/deployment.yaml"})
// Now we use kubernetes/client-go library to render the template output into the Deployment struct. This will
// ensure the Deployment resource is rendered correctly.
var deployment appsv1.Deployment
helm.UnmarshalK8SYaml(t, output, &deployment)
// Verify the namespace matches the expected supplied namespace.
require.Equal(t, namespaceName, deployment.Namespace)
// Finally, we verify the deployment pod template spec is set to the expected container image value
expectedContainerImage := "nginx:1.15.8"
deploymentContainers := deployment.Spec.Template.Spec.Containers
require.Equal(t, len(deploymentContainers), 1)
require.Equal(t, deploymentContainers[0].Image, expectedContainerImage)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment