Skip to content

Commit

Permalink
fix(test/integration): only override namespace if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm committed Jun 3, 2024
1 parent 3853395 commit 0cf832f
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions tests/integration/helm_ot_default_namespaceoverride_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ import (
"github.com/SumoLogic/sumologic-kubernetes-collection/tests/integration/internal"
"github.com/SumoLogic/sumologic-kubernetes-collection/tests/integration/internal/ctxopts"
"github.com/SumoLogic/sumologic-kubernetes-collection/tests/integration/internal/stepfuncs"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/features"
)

type ctxKey string

const originalNamespaceKey ctxKey = "originalNamespace"

func Test_Helm_Default_OT_NamespaceOverride(t *testing.T) {

expectedMetrics := []string{}
Expand Down Expand Up @@ -49,9 +53,10 @@ func Test_Helm_Default_OT_NamespaceOverride(t *testing.T) {

featTraces := GetTracesFeature()

var originalNamespaceKey ctxKey = "originalNamespace"

overrideNamespace := func(ctx context.Context, envConf *envconf.Config, t *testing.T, _ features.Feature) (context.Context, error) {
if !isNamespaceOverridden(t) {
return ctx, nil
}
originalNamespace := ctxopts.Namespace(ctx)
ctx = context.WithValue(ctx, originalNamespaceKey, originalNamespace)
kubectlOptions := ctxopts.KubectlOptions(ctx)
Expand All @@ -61,12 +66,27 @@ func Test_Helm_Default_OT_NamespaceOverride(t *testing.T) {
return ctx, nil
}
restoreOriginalNamespace := func(ctx context.Context, envConf *envconf.Config, t *testing.T, _ features.Feature) (context.Context, error) {
if !isNamespaceOverridden(t) {
return ctx, nil
}
originalNamespace := ctx.Value(originalNamespaceKey).(string)
kubectlOptions := ctxopts.KubectlOptions(ctx)
kubectlOptions.Namespace = originalNamespace
ctx = ctxopts.WithKubectlOptions(ctx, kubectlOptions)
ctx = ctxopts.WithNamespace(ctx, originalNamespace)
return ctx, nil
}

testenv.BeforeEachFeature(overrideNamespace).AfterEachFeature(restoreOriginalNamespace).Test(t, featInstall, featMetrics, featLogs, featMultilineLogs, featEvents, featTraces)
}

func isNamespaceOverridden(t *testing.T) bool {
valuesFileBytes := stepfuncs.GetHelmValuesForT(t)
var values struct {
NamespaceOverride string `yaml:"namespaceOverride"`
}

err := yaml.Unmarshal(valuesFileBytes, &values)
require.NoError(t, err)
return (values.NamespaceOverride != "")
}

0 comments on commit 0cf832f

Please sign in to comment.