<p>Is there a way to test that the templating works fine for all the possible values?<br>
(note: this is different from helm test which is used for testing the deployed chart through arbitrary code ran in a job).</p>
<p>What I would like to achieve is iterating over a set of values and checking the generated K8s resources for each.</p>
<p>Lets say we want to test whether our chart is correctly written:</p>
<p><strong>The chart:</strong><br>
Values.yaml</p>
<pre><code>app:
  port: 8081

pod2:
   enabled: true
</code></pre>
<p>AppPod.yaml</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: AppPod
  labels:
    app: nginx
spec:
...
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: {{ $.Values.app.port| default 8080  }}
</code></pre>
<p>Pod2.yaml</p>
<pre><code>{{- if $.Values.pod2.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: Pod2
  labels:
    app: nginx2
spec:
...
{{- end}}
</code></pre>
<p><strong>We want to run the following tests:</strong></p>
<ul>
<li>with the default Values.yaml -&gt; assert port=8081 and Pod2 is created</li>
<li>With app.port missing  -&gt; assert port=8080</li>
<li>With pod2.enabled false -&gt; assert Pod2 is not created</li>
<li>With pod2 missing  -&gt; test will fail because the key 'pod2' is mandatory</li>
</ul>
<p>So basically to test the templating logic.</p>
<p><strong>What I am doing right now:</strong><br>
Whenever I modify something in the chart I just run helm template for different Values.yaml and check the results by hand. Doing this by hand is error prone and it becomes more time consuming the more template the chart contains.</p>
<p>Is there any builtin helm feature or a separate framework for this?</p>

<p>Yes, we do that with <a href="https://www.openpolicyagent.org/docs/latest/policy-language/" rel="nofollow noreferrer">rego policy rules</a>. The set-up is not complicated, this is how it looks as part of one of the pipeline of ours (this is a very simplified example to get you started):</p>
<pre><code># install conftest to be able to run helm unit tests
wget https://github.com/open-policy-agent/conftest/releases/download/v0.28.1/conftest_0.28.1_Linux_x86_64.tar.gz
tar xzf conftest_0.28.1_Linux_x86_64.tar.gz
chmod +x conftest

# you can call &quot;helm template&quot; with other override values of course, too
helm template src/main/helm/my-service/ &gt; all.yaml

echo &quot;running opa policies tests&quot;
if ! ./conftest test -p src/main/helm/my-service/ all.yaml; then
  echo &quot;failure&quot;
  exit 1
fi
</code></pre>
<p>inside <code>my-service</code> directory there is a <code>policy</code> folder that holds the &quot;rules&quot; for testing (though this can be passed as an argument). Here is an example of two rules that I had to very recently write:</p>
<pre><code>package main

deny_app_version_must_be_present[msg] {
    input.kind == &quot;Deployment&quot;
    env := input.spec.template.spec.containers[_].env[_]
    msg := sprintf(&quot;env property with name '%v' must not be empty&quot;, [env.name])
    &quot;APP_VERSION&quot; == env.name ; &quot;&quot; == env.value
}

deny_app_version_env_variable_must_be_present[msg] {
    input.kind == &quot;Deployment&quot;
    app_version_names := { envs | envs := input.spec.template.spec.containers[_].env[_]; envs.name == &quot;APP_VERSION&quot;}
    count(app_version_names) != 1
    msg := sprintf(&quot;'%v' env variable must be preset once&quot;, [&quot;APP_VERSION&quot;])
}
</code></pre>
<p>This validates that the container in the <code>Deployment</code> has an env variable called <code>APP_VERSION</code> that must be unique and must be non-empty.</p>
