From 8fc687c2964339e27f9ca03679c554e955d2293b Mon Sep 17 00:00:00 2001 From: Jonathan Yu Date: Mon, 22 Nov 2021 22:37:31 +0000 Subject: [PATCH] chore: enforce consistent namespace Add a test to ensure that all objects are created in the designated release namespace. --- tests/defaults_test.go | 36 ++++++++++++++++++++++++++++++++++++ tests/values.go | 26 +++++--------------------- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/tests/defaults_test.go b/tests/defaults_test.go index 4da026f8..5bcd178a 100644 --- a/tests/defaults_test.go +++ b/tests/defaults_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/stretchr/testify/require" + "k8s.io/apimachinery/pkg/api/meta" ) // TestDefault loads the chart and checks metadata. @@ -20,3 +21,38 @@ func TestDefault(t *testing.T) { coderd := chart.OriginalValues.Coderd require.Equal(t, 1, *coderd.Replicas, "expected 1 replica by default") } + +// TestNamespace checks that all objects are created in the specified +// release namespace. +func TestNamespace(t *testing.T) { + t.Parallel() + + chart := LoadChart(t) + opts := DefaultReleaseOptions() + namespaces := []string{ + opts.Namespace, + "coder-test", + } + for _, namespace := range namespaces { + namespace := namespace + opts := opts + opts.Namespace = namespace + t.Run(namespace, func(t *testing.T) { + t.Parallel() + + // Render the chart with default values + objs, err := chart.Render(chart.OriginalValues, &opts, nil) + require.NoError(t, err, "chart render failed") + + // Verify that all objects are using the supplied namespace + for _, obj := range objs { + metaObject, err := meta.Accessor(obj) + require.NoError(t, err, "failed to get object metadata") + + actualNamespace := metaObject.GetNamespace() + require.Equal(t, namespace, actualNamespace, + "deployed namespace does not match target") + } + }) + } +} diff --git a/tests/values.go b/tests/values.go index 0b8539c3..139fd6ec 100644 --- a/tests/values.go +++ b/tests/values.go @@ -49,11 +49,6 @@ type Chart struct { // is intended to be read-only and should not be modified // by callers. Instead, modify the Values field. OriginalValues *CoderValues - - // Values contains the effective chart values. This is a - // deep copy of OriginalValues, and callers can modify - // the values. - Values *CoderValues } // CoderValues is a typed Go representation of Coder's values @@ -306,17 +301,12 @@ func LoadChart(t *testing.T) *Chart { originalValues, err := ConvertMapToCoderValues(chart.Values, true) require.NoError(t, err, "error parsing original values") - // Create another copy for users to modify - values, err := ConvertMapToCoderValues(chart.Values, true) - require.NoError(t, err, "error parsing original values") - return &Chart{ chart: chart, Metadata: chart.Metadata, Files: chart.Files, Templates: chart.Templates, OriginalValues: originalValues, - Values: values, } } @@ -340,13 +330,13 @@ func (c *Chart) Validate() error { return c.chart.Validate() } -// RenderChart applies the CoderValues to the chart, and returns a list +// Render applies the CoderValues to the chart, and returns a list // of Kubernetes runtime objects, or an error. // // values, options, and capabilities may be nil, in which case the // function will simulate a fresh install to the "coder" namespace // using the "coder" release, default values, and capabilities. -func RenderChart(chrt *chart.Chart, values *CoderValues, options *chartutil.ReleaseOptions, capabilities *chartutil.Capabilities) ([]runtime.Object, error) { +func (c *Chart) Render(values *CoderValues, options *chartutil.ReleaseOptions, capabilities *chartutil.Capabilities) ([]runtime.Object, error) { vals, err := ConvertCoderValuesToMap(values) if err != nil { return nil, fmt.Errorf("failed to convert values to map: %w", err) @@ -354,13 +344,7 @@ func RenderChart(chrt *chart.Chart, values *CoderValues, options *chartutil.Rele var opts chartutil.ReleaseOptions if options == nil { - opts = chartutil.ReleaseOptions{ - Name: "coder", - Namespace: "coder", - Revision: 1, - IsInstall: true, - IsUpgrade: false, - } + opts = DefaultReleaseOptions() } else { opts = *options } @@ -369,12 +353,12 @@ func RenderChart(chrt *chart.Chart, values *CoderValues, options *chartutil.Rele capabilities = chartutil.DefaultCapabilities.Copy() } - vals, err = chartutil.ToRenderValues(chrt, vals, opts, capabilities) + vals, err = chartutil.ToRenderValues(c.chart, vals, opts, capabilities) if err != nil { return nil, fmt.Errorf("failed to create render values: %w", err) } - manifests, err := engine.Render(chrt, vals) + manifests, err := engine.Render(c.chart, vals) if err != nil { return nil, fmt.Errorf("failed to render Chart: %w", err) }