Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: could not find template when using an enabled switch #1328

Open
2 tasks done
Mo0rBy opened this issue Aug 4, 2023 · 0 comments
Open
2 tasks done

Error: could not find template when using an enabled switch #1328

Mo0rBy opened this issue Aug 4, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@Mo0rBy
Copy link

Mo0rBy commented Aug 4, 2023

Describe the bug
I have a template yaml that uses an enabled switch in the values.yaml that determines if the template should be rendered or not:

{{- if .Values.security.authorizationPolicy.enabled }}
---
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: {{ .Values.serviceName }}
# This continues with more yaml
{{- end }}

The values.yaml has security.authorizationPolicy.enabled which is set to false when I don't want to include an authorization-policy resource and it is set to true when I do want that resource.

When the enabled switch is set to false I get a exit status 1; Error: could not find template templates/authorization-policy.yaml in chart.

I was actually wanting the rendering to fail if I set the enabled switch to disabled and then I could assert that the render failed or that it produced an empty string or something similar, but this error is sort of wrong? It would indicate to me that the yaml template doesn't exist within the chart at all.

To Reproduce
Here's the part of my code where I set the enabled switch option and then attempt to get the Helm output for the template:

options := &helm.Options{ SetValues: map[string]string{ "security.authorizationPolicy.enabled": "true" }}

output := helm.RenderTemplate(GinkgoT(), options, helmChartPath, "authorization-policy", []string{"templates/authorization-policy.yaml"})

I imagine that this could be reproduced with any kubernetes resource, you just need to have an enabled switch at the top of the yaml template.

Expected behavior
When I realised why this was erroring, it sort of made sense, clearly the yaml template is not rendered because the switch is not enabled, but the error message is confusing. I think it would be better if this error message stated that the Helm template was rendered successfully, but the output was nothing, or empty, or something else indicating that the render itself worked, but no output was produced.

In the helm helm-unittest plugin it is possible to perform this kind of assertion using the hasDocuments assertion (see assertion types here).

For now, I am just going to use helm.RenderTemplateE and assert the error, because I know for a fact that the yaml template is there and my other tests will prove this when I set the switch to enabled, but I do think that a new error should be created to more effectively state what is wrong with the test, or this functionality should be implemented that allows you to assert that no documents were generated in the Helm output.

Here is some code and terminal screenshots to help show what I am describing:

Context("enabled switch", func() {
		It("should not render when set to 'disabled'", func() {
			options := &helm.Options{ SetValues: map[string]string{ "security.authorizationPolicy.enabled": "true" }} # This value is what is modified

			output := helm.RenderTemplate(GinkgoT(), options, helmChartPath, "authorization-policy", []string{"templates/authorization-policy.yaml"})
			outputNoSourceString := regexp.ReplaceAllString(output, "")

			helm.UnmarshalK8SYaml(GinkgoT(), outputNoSourceString, &authorizationPolicy)
		})
	})

When the enabled switch is true:
Screenshot 2023-08-04 at 15 58 44

When the enabled switch is false:
Screenshot 2023-08-04 at 16 04 08

Nice to have

  • Terminal output
  • Screenshots

Versions

  • Terratest version: v0.43.11
  • Environment details (Apple M1, macOS Ventura 13.4.1):

Additional context
Add any other context about the problem here.

@Mo0rBy Mo0rBy added the bug Something isn't working label Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant