Skip to content

feat: add test for overwriting replica values #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 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
75 changes: 74 additions & 1 deletion tests/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -25,5 +29,74 @@ 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)
// 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")
Comment on lines +50 to +53
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jawnsy I feel like I'm so close. I'm doing something wrong though and getting this error. Any ideas?

image


// 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

*/
1 change: 1 addition & 0 deletions tests/security_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@ func TestSecurityContext(t *testing.T) {
require.True(t, found, "expected coderd deployment in manifests")
})
}

}
19 changes: 18 additions & 1 deletion tests/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,29 @@ 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"`
}

// 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"`
Expand Down