Skip to content

Commit 2fee3d1

Browse files
author
Jonathan Yu
authored
chore: enforce consistent namespace (#175)
Add a test to ensure that all objects are created in the designated release namespace.
1 parent 116a17e commit 2fee3d1

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

tests/defaults_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/require"
7+
"k8s.io/apimachinery/pkg/api/meta"
78
)
89

910
// TestDefault loads the chart and checks metadata.
@@ -20,3 +21,38 @@ func TestDefault(t *testing.T) {
2021
coderd := chart.OriginalValues.Coderd
2122
require.Equal(t, 1, *coderd.Replicas, "expected 1 replica by default")
2223
}
24+
25+
// TestNamespace checks that all objects are created in the specified
26+
// release namespace.
27+
func TestNamespace(t *testing.T) {
28+
t.Parallel()
29+
30+
chart := LoadChart(t)
31+
opts := DefaultReleaseOptions()
32+
namespaces := []string{
33+
opts.Namespace,
34+
"coder-test",
35+
}
36+
for _, namespace := range namespaces {
37+
namespace := namespace
38+
opts := opts
39+
opts.Namespace = namespace
40+
t.Run(namespace, func(t *testing.T) {
41+
t.Parallel()
42+
43+
// Render the chart with default values
44+
objs, err := chart.Render(chart.OriginalValues, &opts, nil)
45+
require.NoError(t, err, "chart render failed")
46+
47+
// Verify that all objects are using the supplied namespace
48+
for _, obj := range objs {
49+
metaObject, err := meta.Accessor(obj)
50+
require.NoError(t, err, "failed to get object metadata")
51+
52+
actualNamespace := metaObject.GetNamespace()
53+
require.Equal(t, namespace, actualNamespace,
54+
"deployed namespace does not match target")
55+
}
56+
})
57+
}
58+
}

tests/values.go

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ type Chart struct {
4949
// is intended to be read-only and should not be modified
5050
// by callers. Instead, modify the Values field.
5151
OriginalValues *CoderValues
52-
53-
// Values contains the effective chart values. This is a
54-
// deep copy of OriginalValues, and callers can modify
55-
// the values.
56-
Values *CoderValues
5752
}
5853

5954
// CoderValues is a typed Go representation of Coder's values
@@ -306,17 +301,12 @@ func LoadChart(t *testing.T) *Chart {
306301
originalValues, err := ConvertMapToCoderValues(chart.Values, true)
307302
require.NoError(t, err, "error parsing original values")
308303

309-
// Create another copy for users to modify
310-
values, err := ConvertMapToCoderValues(chart.Values, true)
311-
require.NoError(t, err, "error parsing original values")
312-
313304
return &Chart{
314305
chart: chart,
315306
Metadata: chart.Metadata,
316307
Files: chart.Files,
317308
Templates: chart.Templates,
318309
OriginalValues: originalValues,
319-
Values: values,
320310
}
321311
}
322312

@@ -340,27 +330,21 @@ func (c *Chart) Validate() error {
340330
return c.chart.Validate()
341331
}
342332

343-
// RenderChart applies the CoderValues to the chart, and returns a list
333+
// Render applies the CoderValues to the chart, and returns a list
344334
// of Kubernetes runtime objects, or an error.
345335
//
346336
// values, options, and capabilities may be nil, in which case the
347337
// function will simulate a fresh install to the "coder" namespace
348338
// using the "coder" release, default values, and capabilities.
349-
func RenderChart(chrt *chart.Chart, values *CoderValues, options *chartutil.ReleaseOptions, capabilities *chartutil.Capabilities) ([]runtime.Object, error) {
339+
func (c *Chart) Render(values *CoderValues, options *chartutil.ReleaseOptions, capabilities *chartutil.Capabilities) ([]runtime.Object, error) {
350340
vals, err := ConvertCoderValuesToMap(values)
351341
if err != nil {
352342
return nil, fmt.Errorf("failed to convert values to map: %w", err)
353343
}
354344

355345
var opts chartutil.ReleaseOptions
356346
if options == nil {
357-
opts = chartutil.ReleaseOptions{
358-
Name: "coder",
359-
Namespace: "coder",
360-
Revision: 1,
361-
IsInstall: true,
362-
IsUpgrade: false,
363-
}
347+
opts = DefaultReleaseOptions()
364348
} else {
365349
opts = *options
366350
}
@@ -369,12 +353,12 @@ func RenderChart(chrt *chart.Chart, values *CoderValues, options *chartutil.Rele
369353
capabilities = chartutil.DefaultCapabilities.Copy()
370354
}
371355

372-
vals, err = chartutil.ToRenderValues(chrt, vals, opts, capabilities)
356+
vals, err = chartutil.ToRenderValues(c.chart, vals, opts, capabilities)
373357
if err != nil {
374358
return nil, fmt.Errorf("failed to create render values: %w", err)
375359
}
376360

377-
manifests, err := engine.Render(chrt, vals)
361+
manifests, err := engine.Render(c.chart, vals)
378362
if err != nil {
379363
return nil, fmt.Errorf("failed to render Chart: %w", err)
380364
}

0 commit comments

Comments
 (0)