Skip to content

Commit

Permalink
Helm diff test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
denis256 committed May 23, 2024
1 parent 7c2939e commit 7b047ad
Showing 1 changed file with 28 additions and 40 deletions.
68 changes: 28 additions & 40 deletions modules/helm/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,33 @@ func TestRemoteChartRender(t *testing.T) {
// Test that we can dump all the manifest locally a remote chart (e.g bitnami/nginx)
// so that I can use them later to compare between two versions of the same chart for example
func TestRemoteChartRenderDump(t *testing.T) {
t.Parallel()
renderChartDump(t, "13.2.20", t.TempDir())
}

// Test that we can diff all the manifest to a local snapshot using a remote chart (e.g bitnami/nginx)
func TestRemoteChartRenderDiff(t *testing.T) {
t.Parallel()

initialSnapshot := t.TempDir()
updatedSnapshot := t.TempDir()
renderChartDump(t, "5.0.0", initialSnapshot)
output := renderChartDump(t, "5.1.0", updatedSnapshot)

options := &Options{
Logger: logger.Default,
SnapshotPath: initialSnapshot,
}
// diff in: spec.initContainers.preserve-logs-symlinks.imag, spec.containers.nginx.image, tls certificates
require.Equal(t, 5, DiffAgainstSnapshot(t, options, output, "nginx"))
}

// render chart dump and return the rendered output
func renderChartDump(t *testing.T, remoteChartVersion, snapshotDir string) string {
const (
remoteChartSource = "https://charts.bitnami.com/bitnami"
remoteChartName = "nginx"
remoteChartVersion = "13.2.20"
// need to set a fix name for the namespace so it is not flag as a difference
remoteChartSource = "https://charts.bitnami.com/bitnami"
remoteChartName = "nginx"
// need to set a fix name for the namespace, so it is not flag as a difference
namespaceName = "dump-ns"
)

Expand Down Expand Up @@ -106,42 +128,8 @@ func TestRemoteChartRenderDump(t *testing.T) {
// write chart manifest to a local filesystem directory
options = &Options{
Logger: logger.Default,
SnapshotPath: "__chart_render_dump_manifests_snapshot__",
SnapshotPath: snapshotDir,
}
UpdateSnapshot(t, options, output, releaseName)
}

// Test that we can diff all the manifest to a local snapshot using a remote chart (e.g bitnami/nginx)
func TestRemoteChartRenderDiff(t *testing.T) {
const (
remoteChartSource = "https://charts.bitnami.com/bitnami"
remoteChartName = "nginx"
remoteChartVersion = "13.2.24"
// need to set a fix name for the namespace, so it is not flag as a difference
namespaceName = "dump-ns"
)

releaseName := remoteChartName
options := &Options{
SetValues: map[string]string{
"image.repository": remoteChartName,
"image.registry": "",
"image.tag": remoteChartVersion,
},
KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
Logger: logger.Discard,
SnapshotPath: "__chart_remote_manifests_snapshot__",
}

// 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.
output := RenderRemoteTemplate(t, options, remoteChartSource, releaseName, []string{})

// 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
UnmarshalK8SYaml(t, output, &deployment)

// run the diff and assert there is only one difference: the image name
require.Equal(t, 1, DiffAgainstSnapshot(t, options, output, releaseName))
return output
}

0 comments on commit 7b047ad

Please sign in to comment.