diff --git a/src/cmd/dev/get-resources.go b/src/cmd/dev/get-resources.go index 0a5509575..fa72bc88c 100644 --- a/src/cmd/dev/get-resources.go +++ b/src/cmd/dev/get-resources.go @@ -35,7 +35,7 @@ var getResourcesCmd = &cobra.Command{ Example: getResourcesHelp, Run: func(cmd *cobra.Command, args []string) { spinnerMessage := fmt.Sprintf("Getting Resources from %s", getResourcesOpts.InputFile) - spinner := message.NewProgressSpinner(spinnerMessage) + spinner := message.NewProgressSpinner("%s", spinnerMessage) defer spinner.Stop() ctx := context.Background() diff --git a/src/cmd/dev/validate.go b/src/cmd/dev/validate.go index 4bd3d256d..051c9a28f 100644 --- a/src/cmd/dev/validate.go +++ b/src/cmd/dev/validate.go @@ -45,7 +45,7 @@ var validateCmd = &cobra.Command{ Example: validateHelp, Run: func(cmd *cobra.Command, args []string) { spinnerMessage := fmt.Sprintf("Validating %s", validateOpts.InputFile) - spinner := message.NewProgressSpinner(spinnerMessage) + spinner := message.NewProgressSpinner("%s", spinnerMessage) defer spinner.Stop() ctx := context.Background() @@ -60,7 +60,7 @@ var validateCmd = &cobra.Command{ } // Reset the spinner message - spinner.Updatef(spinnerMessage) + spinner.Updatef("%s", spinnerMessage) // If a resources file is provided, read the resources file if validateOpts.ResourcesFile != "" { diff --git a/src/internal/template/template.go b/src/internal/template/template.go index 40ea2f9e4..dbefedba1 100644 --- a/src/internal/template/template.go +++ b/src/internal/template/template.go @@ -1,6 +1,7 @@ package template import ( + "errors" "fmt" "os" "regexp" @@ -217,7 +218,7 @@ func CollectTemplatingData(constants map[string]interface{}, variables []Variabl variablesMissing.WriteString(fmt.Sprintf("sensitive variable %s is missing a value;\n", k)) } } - message.Debugf(variablesMissing.String()) + message.Debug(variablesMissing.String()) return templateData, nil } @@ -346,14 +347,14 @@ func returnUniqueMatches(matches [][]string, captures int) map[string][]string { // checkForInvalidKeys checks for invalid characters in keys for go text/template // cannot contain '-' or '.' func checkForInvalidKeys(constants map[string]interface{}, variables []VariableConfig) error { - var errors strings.Builder + var errs strings.Builder containsInvalidChars := func(key string) { if strings.Contains(key, "-") { - errors.WriteString(fmt.Sprintf("invalid key %s - cannot contain '-';", key)) + errs.WriteString(fmt.Sprintf("invalid key %s - cannot contain '-';", key)) } if strings.Contains(key, ".") { - errors.WriteString(fmt.Sprintf("invalid key %s - cannot contain '.';", key)) + errs.WriteString(fmt.Sprintf("invalid key %s - cannot contain '.';", key)) } } @@ -375,8 +376,8 @@ func checkForInvalidKeys(constants map[string]interface{}, variables []VariableC containsInvalidChars(variable.Key) } - if errors.Len() > 0 { - return fmt.Errorf(errors.String()[:len(errors.String())-1]) + if errs.Len() > 0 { + return errors.New(errs.String()[:len(errs.String())-1]) } return nil diff --git a/src/pkg/common/oscal/assessment-results.go b/src/pkg/common/oscal/assessment-results.go index 16d76147a..c11bacd3c 100644 --- a/src/pkg/common/oscal/assessment-results.go +++ b/src/pkg/common/oscal/assessment-results.go @@ -212,33 +212,6 @@ func MakeAssessmentResultsDeterministic(assessment *oscalTypes_1_1_2.AssessmentR } -// findAndSortResults takes a map of results and returns a list of thresholds and a sorted list of results in order of time -func findAndSortResults(resultMap map[string]*oscalTypes_1_1_2.AssessmentResults) ([]*oscalTypes_1_1_2.Result, []*oscalTypes_1_1_2.Result) { - - thresholds := make([]*oscalTypes_1_1_2.Result, 0) - sortedResults := make([]*oscalTypes_1_1_2.Result, 0) - - for _, assessment := range resultMap { - for _, result := range assessment.Results { - if result.Props != nil { - for _, prop := range *result.Props { - if prop.Name == "threshold" && prop.Value == "true" { - thresholds = append(thresholds, &result) - } - } - } - // Store all results in a non-sorted list - sortedResults = append(sortedResults, &result) - } - } - - // Sort the results by start time - slices.SortFunc(sortedResults, func(a, b *oscalTypes_1_1_2.Result) int { return a.Start.Compare(b.Start) }) - slices.SortFunc(thresholds, func(a, b *oscalTypes_1_1_2.Result) int { return a.Start.Compare(b.Start) }) - - return thresholds, sortedResults -} - // filterResults consumes many assessment-results objects and builds out a map of EvalResults filtered by target // this function looks at the target prop as the key in the map func FilterResults(resultMap map[string]*oscalTypes_1_1_2.AssessmentResults) map[string]EvalResult { diff --git a/src/pkg/common/validation-store/validation-store.go b/src/pkg/common/validation-store/validation-store.go index eb89c66f6..f573efbcb 100644 --- a/src/pkg/common/validation-store/validation-store.go +++ b/src/pkg/common/validation-store/validation-store.go @@ -112,7 +112,7 @@ func (v *ValidationStore) RunValidations(ctx context.Context, confirmExecution, for k, val := range v.validationMap { completedText := "evaluated" spinnerMessage := fmt.Sprintf("Running validation %s", k) - spinner := message.NewProgressSpinner(spinnerMessage) + spinner := message.NewProgressSpinner("%s", spinnerMessage) defer spinner.Stop() err := val.Validate(ctx, types.ExecutionAllowed(confirmExecution)) if err != nil { diff --git a/src/pkg/message/message.go b/src/pkg/message/message.go index a19e744a8..b1305d7bc 100644 --- a/src/pkg/message/message.go +++ b/src/pkg/message/message.go @@ -153,7 +153,7 @@ func Warnf(format string, a ...any) { // WarnErr prints an error message as a warning. func WarnErr(err any, message string) { debugPrinter(2, err) - Warnf(message) + Warnf("%s", message) } // WarnErrf prints an error message as a warning with a given format. @@ -261,7 +261,7 @@ func HeaderInfof(format string, a ...any) { WithBackgroundStyle(pterm.NewStyle(pterm.BgDarkGray)). WithTextStyle(pterm.NewStyle(pterm.FgLightWhite)). WithMargin(2). - Printfln(message + strings.Repeat(" ", padding)) + Printfln("%s", message+strings.Repeat(" ", padding)) } // HorizontalRule prints a white horizontal rule to separate the terminal diff --git a/src/pkg/message/spinner.go b/src/pkg/message/spinner.go index 16cfbf0df..c3b26e60e 100644 --- a/src/pkg/message/spinner.go +++ b/src/pkg/message/spinner.go @@ -113,7 +113,7 @@ func (p *Spinner) Stop() { // Success prints a success message and stops the spinner. func (p *Spinner) Success() { - p.Successf(p.startText) + p.Successf("%s", p.startText) } // Successf prints a success message with the spinner and stops it. @@ -145,7 +145,7 @@ func (p *Spinner) Errorf(err error, format string, a ...any) { // Fatal calls message.Fatalf with the given error. func (p *Spinner) Fatal(err error) { - p.Fatalf(err, p.startText) + p.Fatalf(err, "%s", p.startText) } // Fatalf calls message.Fatalf with the given error and format. diff --git a/src/test/e2e/api_validation_test.go b/src/test/e2e/api_validation_test.go index 1b43e4c90..84e3a3839 100644 --- a/src/test/e2e/api_validation_test.go +++ b/src/test/e2e/api_validation_test.go @@ -2,17 +2,13 @@ package test import ( "context" + "testing" + "time" "github.com/defenseunicorns/lula/src/pkg/common/validation" "github.com/defenseunicorns/lula/src/pkg/message" "github.com/defenseunicorns/lula/src/test/util" corev1 "k8s.io/api/core/v1" - - // netv1 "k8s.io/api/networking/v1" - // "sigs.k8s.io/e2e-framework/klient/k8s" - "testing" - "time" - "sigs.k8s.io/e2e-framework/klient/wait" "sigs.k8s.io/e2e-framework/klient/wait/conditions" "sigs.k8s.io/e2e-framework/pkg/envconf" @@ -20,6 +16,10 @@ import ( ) func TestApiValidation(t *testing.T) { + const ( + ckAPIFieldConfigMap contextKey = "api-field-configmap" + ckApiFieldPod contextKey = "api-field-pod" + ) featureTrueValidation := features.New("Check API Validation - Success"). Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { // Create the configmap @@ -30,7 +30,7 @@ func TestApiValidation(t *testing.T) { if err = config.Client().Resources().Create(ctx, configMap); err != nil { t.Fatal(err) } - ctx = context.WithValue(ctx, "api-field-configmap", configMap) + ctx = context.WithValue(ctx, ckAPIFieldConfigMap, configMap) // Create the pod pod, err := util.GetPod("./scenarios/api-field/pod.yaml") @@ -47,7 +47,7 @@ func TestApiValidation(t *testing.T) { if err != nil { t.Fatal(err) } - ctx = context.WithValue(ctx, "api-field-pod", pod) + ctx = context.WithValue(ctx, ckApiFieldPod, pod) return ctx }). @@ -85,7 +85,7 @@ func TestApiValidation(t *testing.T) { return ctx }). Teardown(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { - pod := ctx.Value("api-field-pod").(*corev1.Pod) + pod := ctx.Value(ckApiFieldPod).(*corev1.Pod) if err := config.Client().Resources().Delete(ctx, pod); err != nil { t.Fatal(err) } @@ -97,7 +97,7 @@ func TestApiValidation(t *testing.T) { t.Fatal(err) } - configMap := ctx.Value("api-field-configmap").(*corev1.ConfigMap) + configMap := ctx.Value(ckAPIFieldConfigMap).(*corev1.ConfigMap) if err := config.Client().Resources().Delete(ctx, configMap); err != nil { t.Fatal(err) } @@ -121,7 +121,7 @@ func TestApiValidation(t *testing.T) { if err = config.Client().Resources().Create(ctx, configMap); err != nil { t.Fatal(err) } - ctx = context.WithValue(ctx, "api-field-configmap", configMap) + ctx = context.WithValue(ctx, ckAPIFieldConfigMap, configMap) pod, err := util.GetPod("./scenarios/api-field/pod.yaml") if err != nil { @@ -137,7 +137,7 @@ func TestApiValidation(t *testing.T) { if err != nil { t.Fatal(err) } - ctx = context.WithValue(ctx, "api-field-pod", pod) + ctx = context.WithValue(ctx, ckApiFieldPod, pod) return ctx }). Assess("Validate API response field", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { @@ -174,7 +174,7 @@ func TestApiValidation(t *testing.T) { return ctx }). Teardown(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { - pod := ctx.Value("api-field-pod").(*corev1.Pod) + pod := ctx.Value(ckApiFieldPod).(*corev1.Pod) if err := config.Client().Resources().Delete(ctx, pod); err != nil { t.Fatal(err) } @@ -186,7 +186,7 @@ func TestApiValidation(t *testing.T) { t.Fatal(err) } - configMap := ctx.Value("api-field-configmap").(*corev1.ConfigMap) + configMap := ctx.Value(ckAPIFieldConfigMap).(*corev1.ConfigMap) if err := config.Client().Resources().Delete(ctx, configMap); err != nil { t.Fatal(err) } diff --git a/src/test/e2e/dev_get_resources_test.go b/src/test/e2e/dev_get_resources_test.go index 311274aaf..8581b2fa3 100644 --- a/src/test/e2e/dev_get_resources_test.go +++ b/src/test/e2e/dev_get_resources_test.go @@ -17,6 +17,10 @@ import ( ) func TestGetResources(t *testing.T) { + const ( + ckPodGetResources contextKey = "pod-get-resources" + ckCfgGetResources contextKey = "config-get-resources" + ) featureTrueGetResources := features.New("Check dev get-resources"). Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { // Create the pod @@ -31,7 +35,7 @@ func TestGetResources(t *testing.T) { if err != nil { t.Fatal(err) } - ctx = context.WithValue(ctx, "pod-get-resources", pod) + ctx = context.WithValue(ctx, ckPodGetResources, pod) // Create the configmap configMap, err := util.GetConfigMap("./scenarios/dev-get-resources/configmap.yaml") @@ -41,7 +45,7 @@ func TestGetResources(t *testing.T) { if err = config.Client().Resources().Create(ctx, configMap); err != nil { t.Fatal(err) } - ctx = context.WithValue(ctx, "configmap-get-resources", configMap) + ctx = context.WithValue(ctx, ckCfgGetResources, configMap) return ctx }). @@ -75,13 +79,13 @@ func TestGetResources(t *testing.T) { t.Fatal("The nginx-conf resource was not found in the collection") } - message.Infof("Successfully validated dev get-resources command") + message.Info("Successfully validated dev get-resources command") return ctx }). Teardown(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { // Delete the configmap - configMap := ctx.Value("configmap-get-resources").(*corev1.ConfigMap) + configMap := ctx.Value(ckCfgGetResources).(*corev1.ConfigMap) if err := config.Client().Resources().Delete(ctx, configMap); err != nil { t.Fatal(err) } @@ -94,7 +98,7 @@ func TestGetResources(t *testing.T) { } // Delete the pod - pod := ctx.Value("pod-get-resources").(*corev1.Pod) + pod := ctx.Value(ckPodGetResources).(*corev1.Pod) if err := config.Client().Resources().Delete(ctx, pod); err != nil { t.Fatal(err) } diff --git a/src/test/e2e/dev_validation_test.go b/src/test/e2e/dev_validation_test.go index 0a69759aa..ed403c0dc 100644 --- a/src/test/e2e/dev_validation_test.go +++ b/src/test/e2e/dev_validation_test.go @@ -17,6 +17,8 @@ import ( ) func TestDevValidation(t *testing.T) { + const ckPodDevValidate contextKey = "pod-dev-validate" + featureTrueDevValidate := features.New("Check dev validate"). Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { // Create the pod @@ -31,7 +33,7 @@ func TestDevValidation(t *testing.T) { if err != nil { t.Fatal(err) } - ctx = context.WithValue(ctx, "pod-dev-validate", pod) + ctx = context.WithValue(ctx, ckPodDevValidate, pod) return ctx }). @@ -131,7 +133,7 @@ func TestDevValidation(t *testing.T) { Teardown(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { // Delete the pod - pod := ctx.Value("pod-dev-validate").(*corev1.Pod) + pod := ctx.Value(ckPodDevValidate).(*corev1.Pod) if err := config.Client().Resources().Delete(ctx, pod); err != nil { t.Fatal(err) } diff --git a/src/test/e2e/main_test.go b/src/test/e2e/main_test.go index 96193e6b3..669952075 100644 --- a/src/test/e2e/main_test.go +++ b/src/test/e2e/main_test.go @@ -26,7 +26,7 @@ func TestMain(m *testing.M) { envfuncs.CreateClusterWithConfig( kind.NewProvider(), kindClusterName, - "kind-config.yaml"), + "./testdata/kind-config.yaml"), envfuncs.CreateNamespace(namespace), ) diff --git a/src/test/e2e/multi_resource_validation_test.go b/src/test/e2e/multi_resource_validation_test.go index 33b98067b..64bc29c70 100644 --- a/src/test/e2e/multi_resource_validation_test.go +++ b/src/test/e2e/multi_resource_validation_test.go @@ -16,6 +16,14 @@ import ( ) func TestMultiResourceValidation(t *testing.T) { + const ( + ckValidationTest2 contextKey = "validation-test2-ns" + ckValidationTest1 contextKey = "validation-test1-ns" + ckAPIFieldConfigMap contextKey = "api-field-configmap" + ckApiFieldPod contextKey = "api-field-pod" + ckPodvt1 contextKey = "podvt1" + ckPodvt2 contextKey = "podvt2" + ) featureTrueAPIValidation := features.New("Check Multi-Resource API Validation - Success"). Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { // Create the configmap @@ -26,7 +34,7 @@ func TestMultiResourceValidation(t *testing.T) { if err = config.Client().Resources().Create(ctx, configMap); err != nil { t.Fatal(err) } - ctx = context.WithValue(ctx, "api-field-configmap", configMap) + ctx = context.WithValue(ctx, ckAPIFieldConfigMap, configMap) // Create the pod pod, err := util.GetPod("./scenarios/multi-resource/pod.yaml") @@ -43,7 +51,7 @@ func TestMultiResourceValidation(t *testing.T) { if err != nil { t.Fatal(err) } - ctx = context.WithValue(ctx, "api-field-pod", pod) + ctx = context.WithValue(ctx, ckApiFieldPod, pod) // Create additional Namespace nsvt1, err := util.GetNamespace("validation-test1") if err != nil { @@ -52,7 +60,7 @@ func TestMultiResourceValidation(t *testing.T) { if err = config.Client().Resources().Create(ctx, nsvt1); err != nil { t.Fatal(err) } - ctx = context.WithValue(ctx, "validation-test1-ns", nsvt1) + ctx = context.WithValue(ctx, ckValidationTest1, nsvt1) // Create the pod podvt1, err := util.GetPod("./scenarios/multi-resource/podvt1.yaml") @@ -69,7 +77,7 @@ func TestMultiResourceValidation(t *testing.T) { if err != nil { t.Fatal(err) } - ctx = context.WithValue(ctx, "podvt1", podvt1) + ctx = context.WithValue(ctx, ckPodvt1, podvt1) // Create additional Namespace nsvt2, err := util.GetNamespace("validation-test2") if err != nil { @@ -78,7 +86,7 @@ func TestMultiResourceValidation(t *testing.T) { if err = config.Client().Resources().Create(ctx, nsvt2); err != nil { t.Fatal(err) } - ctx = context.WithValue(ctx, "validation-test2-ns", nsvt2) + ctx = context.WithValue(ctx, ckValidationTest2, nsvt2) // Create the pod podvt2, err := util.GetPod("./scenarios/multi-resource/podvt2.yaml") @@ -95,7 +103,7 @@ func TestMultiResourceValidation(t *testing.T) { if err != nil { t.Fatal(err) } - ctx = context.WithValue(ctx, "podvt2", podvt2) + ctx = context.WithValue(ctx, ckPodvt2, podvt2) return ctx }). @@ -132,7 +140,7 @@ func TestMultiResourceValidation(t *testing.T) { return ctx }). Teardown(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { - podvt2 := ctx.Value("podvt2").(*corev1.Pod) + podvt2 := ctx.Value(ckPodvt2).(*corev1.Pod) if err := config.Client().Resources().Delete(ctx, podvt2); err != nil { t.Fatal(err) } @@ -144,12 +152,12 @@ func TestMultiResourceValidation(t *testing.T) { t.Fatal(err) } - nsvt2 := ctx.Value("validation-test2-ns").(*corev1.Namespace) + nsvt2 := ctx.Value(ckValidationTest2).(*corev1.Namespace) if err := config.Client().Resources().Delete(ctx, nsvt2); err != nil { t.Fatal(err) } - podvt1 := ctx.Value("podvt1").(*corev1.Pod) + podvt1 := ctx.Value(ckPodvt1).(*corev1.Pod) if err := config.Client().Resources().Delete(ctx, podvt1); err != nil { t.Fatal(err) } @@ -161,12 +169,12 @@ func TestMultiResourceValidation(t *testing.T) { t.Fatal(err) } - nsvt1 := ctx.Value("validation-test1-ns").(*corev1.Namespace) + nsvt1 := ctx.Value(ckValidationTest1).(*corev1.Namespace) if err := config.Client().Resources().Delete(ctx, nsvt1); err != nil { t.Fatal(err) } - pod := ctx.Value("api-field-pod").(*corev1.Pod) + pod := ctx.Value(ckApiFieldPod).(*corev1.Pod) if err := config.Client().Resources().Delete(ctx, pod); err != nil { t.Fatal(err) } @@ -178,7 +186,7 @@ func TestMultiResourceValidation(t *testing.T) { t.Fatal(err) } - configMap := ctx.Value("api-field-configmap").(*corev1.ConfigMap) + configMap := ctx.Value(ckAPIFieldConfigMap).(*corev1.ConfigMap) if err := config.Client().Resources().Delete(ctx, configMap); err != nil { t.Fatal(err) } diff --git a/src/test/e2e/outputs_test.go b/src/test/e2e/outputs_test.go index abe4cefb4..13a7a8c55 100644 --- a/src/test/e2e/outputs_test.go +++ b/src/test/e2e/outputs_test.go @@ -20,6 +20,7 @@ import ( ) func TestOutputs(t *testing.T) { + const ckTestPodOutputs contextKey = "test-pod-outputs" featureTrueOutputs := features.New("Check Outputs"). Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { pod, err := util.GetPod("./scenarios/outputs/pod.yaml") @@ -33,7 +34,7 @@ func TestOutputs(t *testing.T) { if err != nil { t.Fatal(err) } - return context.WithValue(ctx, "test-pod-outputs", pod) + return context.WithValue(ctx, ckTestPodOutputs, pod) }). Assess("Validate Outputs", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { oscalPath := "./scenarios/outputs/oscal-component.yaml" @@ -91,12 +92,12 @@ func TestOutputs(t *testing.T) { } } - message.Infof("Successfully validated payload.output structure") + message.Info("Successfully validated payload.output structure") return ctx }). Teardown(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { - pod := ctx.Value("test-pod-outputs").(*corev1.Pod) + pod := ctx.Value(ckTestPodOutputs).(*corev1.Pod) if err := config.Client().Resources().Delete(ctx, pod); err != nil { t.Fatal(err) } diff --git a/src/test/e2e/pod_validation_test.go b/src/test/e2e/pod_validation_test.go index 8f8902a42..3b8df7d17 100644 --- a/src/test/e2e/pod_validation_test.go +++ b/src/test/e2e/pod_validation_test.go @@ -29,6 +29,7 @@ import ( ) func TestPodLabelValidation(t *testing.T) { + const ckTestPodLabel contextKey = "test-pod-label" featureTrueValidation := features.New("Check Pod Validation - Success"). Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { pod, err := util.GetPod("./scenarios/pod-label/pod.pass.yaml") @@ -42,22 +43,22 @@ func TestPodLabelValidation(t *testing.T) { if err != nil { t.Fatal(err) } - return context.WithValue(ctx, "test-pod-label", pod) + return context.WithValue(ctx, ckTestPodLabel, pod) }). Assess("Validate pod label", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { oscalPath := "./scenarios/pod-label/oscal-component.yaml" - return validatePodLabelPass(ctx, t, config, oscalPath) + return validatePodLabelPass(ctx, t, oscalPath) }). Assess("Validate pod label (Kyverno)", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { oscalPath := "./scenarios/pod-label/oscal-component-kyverno.yaml" - return validatePodLabelPass(ctx, t, config, oscalPath) + return validatePodLabelPass(ctx, t, oscalPath) }). Assess("Validate pod label (save-resources=remote)", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { oscalPath := "./scenarios/pod-label/oscal-component.yaml" return validateSaveResources(ctx, t, oscalPath) }). Teardown(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { - pod := ctx.Value("test-pod-label").(*corev1.Pod) + pod := ctx.Value(ckTestPodLabel).(*corev1.Pod) if err := config.Client().Resources().Delete(ctx, pod); err != nil { t.Fatal(err) } @@ -88,7 +89,7 @@ func TestPodLabelValidation(t *testing.T) { if err != nil { t.Fatal(err) } - return context.WithValue(ctx, "test-pod-label", pod) + return context.WithValue(ctx, ckTestPodLabel, pod) }). Assess("Validate pod label", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { oscalPath := "./scenarios/pod-label/oscal-component.yaml" @@ -101,7 +102,7 @@ func TestPodLabelValidation(t *testing.T) { return ctx }). Teardown(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { - pod := ctx.Value("test-pod-label").(*corev1.Pod) + pod := ctx.Value(ckTestPodLabel).(*corev1.Pod) if err := config.Client().Resources().Delete(ctx, pod); err != nil { t.Fatal(err) } @@ -126,7 +127,7 @@ func TestPodLabelValidation(t *testing.T) { if err != nil { t.Fatal(err) } - return context.WithValue(ctx, "test-pod-label", pod) + return context.WithValue(ctx, ckTestPodLabel, pod) }). Assess("All not-satisfied", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { oscalPath := "./scenarios/pod-label/oscal-component-all-bad.yaml" @@ -134,7 +135,7 @@ func TestPodLabelValidation(t *testing.T) { observationRemarksMap := generateObservationRemarksMap(*observations) for _, f := range *findings { - // relatedobservations should have len = 1 + // related observations should have len = 1 relatedObs := *f.RelatedObservations if f.RelatedObservations == nil || len(relatedObs) != 1 { t.Fatal("RelatedObservations should have len = 1") @@ -190,7 +191,7 @@ func TestPodLabelValidation(t *testing.T) { return ctx }). Teardown(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { - pod := ctx.Value("test-pod-label").(*corev1.Pod) + pod := ctx.Value(ckTestPodLabel).(*corev1.Pod) if err := config.Client().Resources().Delete(ctx, pod); err != nil { t.Fatal(err) } @@ -205,7 +206,7 @@ func TestPodLabelValidation(t *testing.T) { testEnv.Test(t, featureTrueValidation, featureFalseValidation, featureBadValidation) } -func validatePodLabelPass(ctx context.Context, t *testing.T, config *envconf.Config, oscalPath string) context.Context { +func validatePodLabelPass(ctx context.Context, t *testing.T, oscalPath string) context.Context { message.NoProgress = true tempDir := t.TempDir() diff --git a/src/test/e2e/pod_wait_test.go b/src/test/e2e/pod_wait_test.go index 13a3d5220..534f856d3 100644 --- a/src/test/e2e/pod_wait_test.go +++ b/src/test/e2e/pod_wait_test.go @@ -13,6 +13,7 @@ import ( ) func TestPodWaitValidation(t *testing.T) { + const ckTestPodLabel contextKey = "test-pod-label" featureTrueValidation := features.New("Check Pod Wait for Ready - Success"). Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { pod, err := util.GetPod("./scenarios/wait-field/pod.yaml") @@ -26,7 +27,7 @@ func TestPodWaitValidation(t *testing.T) { // We are purposefully not going to wait until the pod is ready and start Assess - return context.WithValue(ctx, "test-pod-label", pod) + return context.WithValue(ctx, ckTestPodLabel, pod) }). Assess("Validate pod label", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { oscalPath := "./scenarios/wait-field/oscal-component.yaml" @@ -61,7 +62,7 @@ func TestPodWaitValidation(t *testing.T) { return ctx }). Teardown(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { - pod := ctx.Value("test-pod-label").(*corev1.Pod) + pod := ctx.Value(ckTestPodLabel).(*corev1.Pod) if err := config.Client().Resources().Delete(ctx, pod); err != nil { t.Fatal(err) } diff --git a/src/test/e2e/remote_validation_test.go b/src/test/e2e/remote_validation_test.go index cc8c94448..87280bb7b 100644 --- a/src/test/e2e/remote_validation_test.go +++ b/src/test/e2e/remote_validation_test.go @@ -15,6 +15,7 @@ import ( ) func TestRemoteValidation(t *testing.T) { + const ckPodDevValidate contextKey = "pod-dev-validate" featureRemoteValidation := features.New("Check dev validate"). Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { // Create the pod @@ -29,7 +30,7 @@ func TestRemoteValidation(t *testing.T) { if err != nil { t.Fatal(err) } - ctx = context.WithValue(ctx, "pod-dev-validate", pod) + ctx = context.WithValue(ctx, ckPodDevValidate, pod) return ctx }). @@ -68,7 +69,7 @@ func TestRemoteValidation(t *testing.T) { Teardown(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { // Delete the pod - pod := ctx.Value("pod-dev-validate").(*corev1.Pod) + pod := ctx.Value(ckPodDevValidate).(*corev1.Pod) if err := config.Client().Resources().Delete(ctx, pod); err != nil { t.Fatal(err) } diff --git a/src/test/e2e/resource_data_test.go b/src/test/e2e/resource_data_test.go index 13f4ae7b0..202372f9b 100644 --- a/src/test/e2e/resource_data_test.go +++ b/src/test/e2e/resource_data_test.go @@ -16,6 +16,12 @@ import ( ) func TestResourceDataValidation(t *testing.T) { + const ( + ckCfgMapYaml contextKey = "configmap-yaml" + ckCfgMapJSON contextKey = "configmap-json" + ckSecret contextKey = "secret" + ckPod contextKey = "pod" + ) featureTrueDataValidation := features.New("Check Resource Data Validation - Success"). Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { // Create the json configmap @@ -26,7 +32,7 @@ func TestResourceDataValidation(t *testing.T) { if err = config.Client().Resources().Create(ctx, configMapJson); err != nil { t.Fatal(err) } - ctx = context.WithValue(ctx, "configmap-json", configMapJson) + ctx = context.WithValue(ctx, ckCfgMapJSON, configMapJson) // Create the configmap with yaml data configMapYaml, err := util.GetConfigMap("./scenarios/resource-data/configmap_yaml.yaml") @@ -36,7 +42,7 @@ func TestResourceDataValidation(t *testing.T) { if err = config.Client().Resources().Create(ctx, configMapYaml); err != nil { t.Fatal(err) } - ctx = context.WithValue(ctx, "configmap-yaml", configMapYaml) + ctx = context.WithValue(ctx, ckCfgMapYaml, configMapYaml) // Create the secret secret, err := util.GetSecret("./scenarios/resource-data/secret.yaml") @@ -46,7 +52,7 @@ func TestResourceDataValidation(t *testing.T) { if err = config.Client().Resources().Create(ctx, secret); err != nil { t.Fatal(err) } - ctx = context.WithValue(ctx, "secret", secret) + ctx = context.WithValue(ctx, ckSecret, secret) // Create the pod pod, err := util.GetPod("./scenarios/resource-data/pod.yaml") @@ -63,7 +69,7 @@ func TestResourceDataValidation(t *testing.T) { if err != nil { t.Fatal(err) } - ctx = context.WithValue(ctx, "pod", pod) + ctx = context.WithValue(ctx, ckPod, pod) return ctx }). @@ -101,7 +107,7 @@ func TestResourceDataValidation(t *testing.T) { }). Teardown(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { // Delete the json configmap - configMapJson := ctx.Value("configmap-json").(*corev1.ConfigMap) + configMapJson := ctx.Value(ckCfgMapJSON).(*corev1.ConfigMap) if err := config.Client().Resources().Delete(ctx, configMapJson); err != nil { t.Fatal(err) } @@ -114,7 +120,7 @@ func TestResourceDataValidation(t *testing.T) { } // Delete the yaml configmap - configMapYaml := ctx.Value("configmap-yaml").(*corev1.ConfigMap) + configMapYaml := ctx.Value(ckCfgMapYaml).(*corev1.ConfigMap) if err := config.Client().Resources().Delete(ctx, configMapYaml); err != nil { t.Fatal(err) } @@ -127,7 +133,7 @@ func TestResourceDataValidation(t *testing.T) { } // Delete the secret - secret := ctx.Value("secret").(*corev1.Secret) + secret := ctx.Value(ckSecret).(*corev1.Secret) if err := config.Client().Resources().Delete(ctx, secret); err != nil { t.Fatal(err) } @@ -140,7 +146,7 @@ func TestResourceDataValidation(t *testing.T) { } // Delete the pod - pod := ctx.Value("pod").(*corev1.Pod) + pod := ctx.Value(ckPod).(*corev1.Pod) if err := config.Client().Resources().Delete(ctx, pod); err != nil { t.Fatal(err) } diff --git a/src/test/e2e/template_validation_test.go b/src/test/e2e/template_validation_test.go index 2e830af70..7eda2cf02 100644 --- a/src/test/e2e/template_validation_test.go +++ b/src/test/e2e/template_validation_test.go @@ -24,6 +24,7 @@ import ( // check pass and fail?... func TestTemplateValidation(t *testing.T) { + const ckPodTmplValidation contextKey = "pod-template-validation" featureTemplateValidation := features.New("Check Template Validation"). Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { // Create the pod @@ -38,7 +39,7 @@ func TestTemplateValidation(t *testing.T) { if err != nil { t.Fatal(err) } - ctx = context.WithValue(ctx, "pod-template-validation", pod) + ctx = context.WithValue(ctx, ckPodTmplValidation, pod) return ctx }). @@ -149,7 +150,7 @@ func TestTemplateValidation(t *testing.T) { return ctx }). Teardown(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context { - pod := ctx.Value("pod-template-validation").(*corev1.Pod) + pod := ctx.Value(ckPodTmplValidation).(*corev1.Pod) if err := config.Client().Resources().Delete(ctx, pod); err != nil { t.Fatal(err) } diff --git a/src/test/e2e/kind-config.yaml b/src/test/e2e/testdata/kind-config.yaml similarity index 100% rename from src/test/e2e/kind-config.yaml rename to src/test/e2e/testdata/kind-config.yaml