Skip to content

chore: update example tests #174

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

Merged
merged 2 commits into from
Nov 22, 2021
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
11 changes: 2 additions & 9 deletions tests/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,19 @@ import (
"testing"

"github.com/stretchr/testify/require"
"helm.sh/helm/v3/pkg/chart/loader"
)

// TestDefault loads the chart and checks metadata.
func TestDefault(t *testing.T) {
t.Parallel()

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")
chart := LoadChart(t)
require.NoError(t, chart.Validate(), "chart has valid metadata")

metadata := chart.Metadata
require.Equal(t, "coder", metadata.Name, "unexpected chart name")
require.False(t, metadata.Deprecated, "chart should not be deprecated")

values, err := ConvertMapToCoderValues(chart.Values, false)
require.NoError(t, err, "converted map to coder values")
require.NotNil(t, values, "values must be non-nil")
coderd := values.Coderd
coderd := chart.OriginalValues.Coderd
require.Equal(t, 1, *coderd.Replicas, "expected 1 replica by default")
}
152 changes: 152 additions & 0 deletions tests/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package tests

import (
"testing"

"github.com/stretchr/testify/require"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/engine"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/pointer"
)

// TestExamples loads the example values files and performs
// some basic checks.
func TestExamples(t *testing.T) {
t.Parallel()

chart, err := loader.LoadDir("..")
require.NoError(t, err, "loaded chart successfully")
require.NotNil(t, chart, "chart must be non-nil")

exampleOpenShift, err := ReadValuesAsMap("../examples/openshift/openshift.values.yaml")
require.NoError(t, err, "failed to load OpenShift example values")

exampleKind, err := ReadValuesAsMap("../examples/kind/kind.values.yaml")
require.NoError(t, err, "failed to load Kind example values")

tests := []struct {
Name string
Values map[string]interface{}
PodSecurityContext *corev1.PodSecurityContext
ContainerSecurityContext *corev1.SecurityContext
}{
{
Name: "default",
Values: nil,
PodSecurityContext: &corev1.PodSecurityContext{
RunAsUser: pointer.Int64(1000),
RunAsGroup: nil,
RunAsNonRoot: pointer.Bool(true),
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
LocalhostProfile: nil,
},
},
ContainerSecurityContext: &corev1.SecurityContext{
RunAsUser: nil,
RunAsGroup: nil,
RunAsNonRoot: nil,
Capabilities: nil,
Privileged: nil,
SELinuxOptions: nil,
WindowsOptions: nil,
ReadOnlyRootFilesystem: pointer.Bool(true),
AllowPrivilegeEscalation: pointer.Bool(false),
ProcMount: nil,
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
LocalhostProfile: nil,
},
},
}, {
Name: "openshift",
Values: exampleOpenShift,
PodSecurityContext: &corev1.PodSecurityContext{
RunAsUser: nil,
RunAsGroup: nil,
RunAsNonRoot: pointer.Bool(true),
SeccompProfile: nil,
},
ContainerSecurityContext: &corev1.SecurityContext{
RunAsUser: nil,
RunAsGroup: nil,
RunAsNonRoot: nil,
Capabilities: nil,
Privileged: nil,
SELinuxOptions: nil,
WindowsOptions: nil,
ReadOnlyRootFilesystem: pointer.Bool(true),
AllowPrivilegeEscalation: pointer.Bool(false),
ProcMount: nil,
SeccompProfile: nil,
},
},
{
Name: "kind",
Values: exampleKind,
PodSecurityContext: &corev1.PodSecurityContext{
RunAsUser: pointer.Int64(1000),
RunAsNonRoot: pointer.Bool(true),
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
},
ContainerSecurityContext: &corev1.SecurityContext{
RunAsUser: pointer.Int64(1000),
RunAsGroup: pointer.Int64(1000),
RunAsNonRoot: pointer.Bool(true),
Capabilities: nil,
Privileged: nil,
SELinuxOptions: nil,
WindowsOptions: nil,
ReadOnlyRootFilesystem: pointer.Bool(true),
AllowPrivilegeEscalation: pointer.Bool(false),
ProcMount: nil,
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
LocalhostProfile: nil,
},
},
},
}

for _, test := range tests {
test := test
t.Run(test.Name, func(t *testing.T) {
t.Parallel()

values, err := chartutil.ToRenderValues(chart, test.Values, DefaultReleaseOptions(), chartutil.DefaultCapabilities.Copy())
require.NoError(t, err, "failed to generate render values")

manifests, err := engine.Render(chart, values)
require.NoError(t, err, "failed to render chart")

objs, err := LoadObjectsFromManifests(manifests)
require.NoError(t, err, "failed to convert manifests to objects")

// Find the coderd Deployment
var found bool
for _, obj := range objs {
deployment, ok := obj.(*appsv1.Deployment)
if ok && deployment.Name == "coderd" {
found = true

require.Equal(t, test.PodSecurityContext,
deployment.Spec.Template.Spec.SecurityContext,
"expected matching pod securityContext")
require.Len(t, deployment.Spec.Template.Spec.Containers, 1,
"expected one container")
require.Equal(t, test.ContainerSecurityContext,
deployment.Spec.Template.Spec.Containers[0].SecurityContext,
"expected matching container securityContext")

break
}
}
require.True(t, found, "expected coderd deployment in manifests")
})
}
}
91 changes: 0 additions & 91 deletions tests/security_context_test.go

This file was deleted.

Loading