From 37e29fde30ac6d35ec5445f0f3679b4943f684ce Mon Sep 17 00:00:00 2001 From: Joe Date: Mon, 8 Nov 2021 17:24:11 +0000 Subject: [PATCH 1/6] feat: add helm test for ingress example From ad9e241a6ac233040322ffdfb94b0b03f5d84c5f Mon Sep 17 00:00:00 2001 From: Joe Date: Mon, 8 Nov 2021 17:49:45 +0000 Subject: [PATCH 2/6] wip --- tests/security_context_test.go | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/security_context_test.go b/tests/security_context_test.go index 7b8ed87a..1bb297be 100644 --- a/tests/security_context_test.go +++ b/tests/security_context_test.go @@ -77,3 +77,60 @@ func TestSecurityContext(t *testing.T) { }) } } + +func TestPostgresValues(t *testing.T) { + t.Parallel() + + chart, err := loader.LoadDir("..") + require.NoError(t, err, "loaded chart successfully") + exampleOpenShift, err := ReadValues("../examples/openshift/openshift.values.yaml") + require.NoError(t, err, "failed to OpenShift example values") + + tests := []struct { + Name string + Values *CoderValues + Postgres *corev1.Postgres + }{ + { + Name: "openshift", + Values: exampleOpenShift, + Postgres: &any{ + Default: &any{ + Resources: &any{ + Requests: &any{ + // Expect this to fail + CPU: "1m", + Memory: "33Mi", + }, + }, + }, + }, + }, + } + + for _, test := range tests { + test := test + t.Run(test.Name, func(t *testing.T) { + t.Parallel() + + objs, err := RenderChart(chart, test.Values, nil, nil) + require.NoError(t, err, "failed to render chart") + + // Find the coderd Deployment + var found bool + for _, obj := range objs { + deployment, ok := obj.(*appsv1.Deployment) + if ok && deployment.Name == "coderd" { + found = true + + expected := test.Postgres + // TODO@jsjoeio not sure what to do here. + actual := deployment.Spec.Template.Spec.SecurityContext + require.Equal(t, expected, actual, "expected matching PodSecurityContext") + break + } + } + require.True(t, found, "expected coderd deployment in manifests") + }) + } +} From 9066f1cd9d730fc724ae84dbd8e9231895f98069 Mon Sep 17 00:00:00 2001 From: Joe Date: Mon, 8 Nov 2021 18:19:30 +0000 Subject: [PATCH 3/6] wip: add postgres types --- tests/security_context_test.go | 24 ++++++++++++++++++++++-- tests/values.go | 17 +++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/tests/security_context_test.go b/tests/security_context_test.go index 1bb297be..6f5549bd 100644 --- a/tests/security_context_test.go +++ b/tests/security_context_test.go @@ -76,6 +76,26 @@ func TestSecurityContext(t *testing.T) { require.True(t, found, "expected coderd deployment in manifests") }) } + +} + +// TODO@jsjoeio +// there has to be a better way to do this. +type Postgres struct { + Default Default +} + +type Default struct { + Resources Resources +} + +type Resources struct { + Requests Requests +} + +type Requests struct { + CPU string + Memory string } func TestPostgresValues(t *testing.T) { @@ -89,12 +109,12 @@ func TestPostgresValues(t *testing.T) { tests := []struct { Name string Values *CoderValues - Postgres *corev1.Postgres + Postgres Postgres }{ { Name: "openshift", Values: exampleOpenShift, - Postgres: &any{ + Postgres: &Postgres{ Default: &any{ Resources: &any{ Requests: &any{ diff --git a/tests/values.go b/tests/values.go index 0357ad29..e677f38d 100644 --- a/tests/values.go +++ b/tests/values.go @@ -40,6 +40,23 @@ type CoderdValues struct { SecurityContext *CoderdSecurityContext `json:"securityContext" yaml:"securityContext"` } +// PostgresValues are values that apply to postgres. +type PostgresValues struct { + // TODO@jsjoeio + // There is something called NamespaceDefault in the corev1 type/s + // but I can't figure out how to import or use it. + Default *PostgresDefaultValues `json:"default" yaml:"default"` +} + +type PostgresDefaultValues struct { + Resources corev1.ResourceList +} + +type PostgresRequestsValues struct { + CPU string + Memory string +} + type CoderdServiceSpecValues struct { Type *string `json:"type" yaml:"type"` ExternalTrafficPolicy *string `json:"externalTrafficPolicy" yaml:"externalTrafficPolicy"` From 44d7c0ffbf80ebc1cf2e112a57c88cc9ce1de2fa Mon Sep 17 00:00:00 2001 From: Joe Date: Mon, 8 Nov 2021 20:27:32 +0000 Subject: [PATCH 4/6] wip: fix test and types --- tests/defaults_test.go | 73 +++++++++++++++++++++++++++++++- tests/security_context_test.go | 76 ---------------------------------- tests/values.go | 2 +- 3 files changed, 73 insertions(+), 78 deletions(-) diff --git a/tests/defaults_test.go b/tests/defaults_test.go index edee1998..e1d7da0a 100644 --- a/tests/defaults_test.go +++ b/tests/defaults_test.go @@ -5,18 +5,22 @@ import ( "github.com/stretchr/testify/require" "helm.sh/helm/v3/pkg/chart/loader" + appsv1 "k8s.io/api/apps/v1" + "k8s.io/utils/pointer" ) // TestDefault loads the chart and checks metadata. func TestDefault(t *testing.T) { t.Parallel() + // load default chart chart, err := loader.LoadDir("..") require.NoError(t, err, "loaded chart successfully") require.NotNil(t, chart, "chart must be non-nil") require.True(t, chart.IsRoot(), "chart must be a root chart") require.NoError(t, chart.Validate(), "chart has valid metadata") + // assert metadata metadata := chart.Metadata require.Equal(t, "coder", metadata.Name, "unexpected chart name") require.False(t, metadata.Deprecated, "chart should not be deprecated") @@ -25,5 +29,72 @@ func TestDefault(t *testing.T) { require.NoError(t, err, "converted map to coder values") require.NotNil(t, values, "values must be non-nil") coderd := values.Coderd - require.Equal(t, 1, *coderd.Replicas, "expected 1 replica by default") + require.Equal(t, int32(1), *coderd.Replicas, "expected 1 replica by default") + } + +func TestOverwriteReplica(t *testing.T) { + t.Parallel() + + // Given + // The default helm chart + chart, err := loader.LoadDir("..") + require.NoError(t, err, "loaded chart successfully") + require.NotNil(t, chart, "chart must be non-nil") + + // When + // We overwrite the replicas value and render the chart + var ValuesToOverwrite = &CoderValues{ + Coderd: &CoderdValues{ + Replicas: pointer.Int32(3), + }, + } + + objs, err := RenderChart(chart, ValuesToOverwrite, nil, nil) + require.NoError(t, err, "failed to render chart") + + // Find the coderd Deployment + var found bool + for _, obj := range objs { + deployment, ok := obj.(*appsv1.Deployment) + if ok && deployment.Name == "coderd" { + found = true + + // Then + // We expect the rendered chart to have the values we overwrote + expected := ValuesToOverwrite.Coderd.Replicas + actual := deployment.Spec.Replicas + require.Equal(t, expected, actual, "expected matching PodSecurityContext") + break + } + require.True(t, found, "expected coderd deployment in manifests") + } +} + +/* + +Notes + +1. load chart +2. change something: i.e. replicas to be more than 1 +3. app + +given: chart loaded +when: set replicas = 3 +then: should see in deployments 3. + + Values: &CoderValues{ + Coderd: &CoderdValues{ + Replicas: pointer.Int32(3), + }, + }, + we ran make lint to generate the /build dir + which is what you would get from helm running/using the templates. + + + objs, err := RenderChart(chart, test.Values, nil, nil) + but instead of test.Values, you're going to do your own struct literal + + Test overriding replicas + +*/ diff --git a/tests/security_context_test.go b/tests/security_context_test.go index 6f5549bd..35e19663 100644 --- a/tests/security_context_test.go +++ b/tests/security_context_test.go @@ -78,79 +78,3 @@ func TestSecurityContext(t *testing.T) { } } - -// TODO@jsjoeio -// there has to be a better way to do this. -type Postgres struct { - Default Default -} - -type Default struct { - Resources Resources -} - -type Resources struct { - Requests Requests -} - -type Requests struct { - CPU string - Memory string -} - -func TestPostgresValues(t *testing.T) { - t.Parallel() - - chart, err := loader.LoadDir("..") - require.NoError(t, err, "loaded chart successfully") - exampleOpenShift, err := ReadValues("../examples/openshift/openshift.values.yaml") - require.NoError(t, err, "failed to OpenShift example values") - - tests := []struct { - Name string - Values *CoderValues - Postgres Postgres - }{ - { - Name: "openshift", - Values: exampleOpenShift, - Postgres: &Postgres{ - Default: &any{ - Resources: &any{ - Requests: &any{ - // Expect this to fail - CPU: "1m", - Memory: "33Mi", - }, - }, - }, - }, - }, - } - - for _, test := range tests { - test := test - t.Run(test.Name, func(t *testing.T) { - t.Parallel() - - objs, err := RenderChart(chart, test.Values, nil, nil) - require.NoError(t, err, "failed to render chart") - - // Find the coderd Deployment - var found bool - for _, obj := range objs { - deployment, ok := obj.(*appsv1.Deployment) - if ok && deployment.Name == "coderd" { - found = true - - expected := test.Postgres - // TODO@jsjoeio not sure what to do here. - actual := deployment.Spec.Template.Spec.SecurityContext - require.Equal(t, expected, actual, "expected matching PodSecurityContext") - break - } - } - require.True(t, found, "expected coderd deployment in manifests") - }) - } -} diff --git a/tests/values.go b/tests/values.go index e677f38d..d0d606dd 100644 --- a/tests/values.go +++ b/tests/values.go @@ -34,7 +34,7 @@ type CoderValues struct { // CoderdValues are values that apply to coderd. type CoderdValues struct { Image *string `json:"image" yaml:"image"` - Replicas *int `json:"replicas" yaml:"replicas"` + Replicas *int32 `json:"replicas" yaml:"replicas"` ServiceSpec *CoderdServiceSpecValues `json:"serviceSpec" yaml:"serviceSpec"` PodSecurityContext *CoderdPodSecurityContext `json:"podSecurityContext" yaml:"podSecurityContext"` SecurityContext *CoderdSecurityContext `json:"securityContext" yaml:"securityContext"` From f721c94f24eab7eaa71938b1fbaa5f265738f03f Mon Sep 17 00:00:00 2001 From: Joe Date: Mon, 8 Nov 2021 20:33:50 +0000 Subject: [PATCH 5/6] almost --- tests/defaults_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/defaults_test.go b/tests/defaults_test.go index e1d7da0a..a114e191 100644 --- a/tests/defaults_test.go +++ b/tests/defaults_test.go @@ -51,6 +51,8 @@ func TestOverwriteReplica(t *testing.T) { } objs, err := RenderChart(chart, ValuesToOverwrite, nil, nil) + // TODO@jsjoeio - getting an error here + // error deserializing "coder/templates/coderd.yaml": yaml: line 9: mapping values are not allowed in this context require.NoError(t, err, "failed to render chart") // Find the coderd Deployment From 5a906ec092cc62a81a6e450102165ab1b2a5e520 Mon Sep 17 00:00:00 2001 From: Joe Date: Mon, 8 Nov 2021 20:38:22 +0000 Subject: [PATCH 6/6] fixup --- tests/defaults_test.go | 31 ------------------------------- tests/security_context_test.go | 1 - tests/values.go | 17 ----------------- 3 files changed, 49 deletions(-) diff --git a/tests/defaults_test.go b/tests/defaults_test.go index a114e191..b6a88f2a 100644 --- a/tests/defaults_test.go +++ b/tests/defaults_test.go @@ -13,14 +13,12 @@ import ( func TestDefault(t *testing.T) { t.Parallel() - // load default chart chart, err := loader.LoadDir("..") require.NoError(t, err, "loaded chart successfully") require.NotNil(t, chart, "chart must be non-nil") require.True(t, chart.IsRoot(), "chart must be a root chart") require.NoError(t, chart.Validate(), "chart has valid metadata") - // assert metadata metadata := chart.Metadata require.Equal(t, "coder", metadata.Name, "unexpected chart name") require.False(t, metadata.Deprecated, "chart should not be deprecated") @@ -30,7 +28,6 @@ func TestDefault(t *testing.T) { require.NotNil(t, values, "values must be non-nil") coderd := values.Coderd require.Equal(t, int32(1), *coderd.Replicas, "expected 1 replica by default") - } func TestOverwriteReplica(t *testing.T) { @@ -72,31 +69,3 @@ func TestOverwriteReplica(t *testing.T) { require.True(t, found, "expected coderd deployment in manifests") } } - -/* - -Notes - -1. load chart -2. change something: i.e. replicas to be more than 1 -3. app - -given: chart loaded -when: set replicas = 3 -then: should see in deployments 3. - - Values: &CoderValues{ - Coderd: &CoderdValues{ - Replicas: pointer.Int32(3), - }, - }, - we ran make lint to generate the /build dir - which is what you would get from helm running/using the templates. - - - objs, err := RenderChart(chart, test.Values, nil, nil) - but instead of test.Values, you're going to do your own struct literal - - Test overriding replicas - -*/ diff --git a/tests/security_context_test.go b/tests/security_context_test.go index 35e19663..7b8ed87a 100644 --- a/tests/security_context_test.go +++ b/tests/security_context_test.go @@ -76,5 +76,4 @@ func TestSecurityContext(t *testing.T) { require.True(t, found, "expected coderd deployment in manifests") }) } - } diff --git a/tests/values.go b/tests/values.go index d0d606dd..a26499f6 100644 --- a/tests/values.go +++ b/tests/values.go @@ -40,23 +40,6 @@ type CoderdValues struct { SecurityContext *CoderdSecurityContext `json:"securityContext" yaml:"securityContext"` } -// PostgresValues are values that apply to postgres. -type PostgresValues struct { - // TODO@jsjoeio - // There is something called NamespaceDefault in the corev1 type/s - // but I can't figure out how to import or use it. - Default *PostgresDefaultValues `json:"default" yaml:"default"` -} - -type PostgresDefaultValues struct { - Resources corev1.ResourceList -} - -type PostgresRequestsValues struct { - CPU string - Memory string -} - type CoderdServiceSpecValues struct { Type *string `json:"type" yaml:"type"` ExternalTrafficPolicy *string `json:"externalTrafficPolicy" yaml:"externalTrafficPolicy"`