Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tests/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/api/meta"
)

// TestDefault loads the chart and checks metadata.
Expand All @@ -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")
}
})
}
}
26 changes: 5 additions & 21 deletions tests/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
}
}

Expand All @@ -340,27 +330,21 @@ 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)
}

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
}
Expand All @@ -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)
}
Expand Down