Skip to content

Commit

Permalink
fix: test cleanup, renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
meganwolf0 committed Oct 9, 2024
1 parent 588388c commit 4836e03
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/cmd/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func ValidateCommand() *cobra.Command {

// Set up the validation context
validationCtx, err := validation.New(
validation.WithCompositionContext(compositionCtx, inputFile),
validation.WithComposition(compositionCtx, inputFile),
validation.WithResourcesDir(saveResources, filepath.Dir(outputFile)),
validation.WithAllowExecution(confirmExecution, runNonInteractively),
)
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/common/validation/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

type Option func(*ValidationContext) error

func WithCompositionContext(cctx *composition.CompositionContext, path string) Option {
func WithComposition(cctx *composition.CompositionContext, path string) Option {
return func(ctx *ValidationContext) error {
var err error
if cctx == nil {
Expand Down
24 changes: 12 additions & 12 deletions src/pkg/common/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func New(opts ...Option) (*ValidationContext, error) {
return &validationCtx, nil
}

func (vctx *ValidationContext) ValidateOnPath(ctx context.Context, path, target string) (assessmentResult *oscalTypes_1_1_2.AssessmentResults, err error) {
func (vc *ValidationContext) ValidateOnPath(ctx context.Context, path, target string) (assessmentResult *oscalTypes_1_1_2.AssessmentResults, err error) {
var oscalModel *oscalTypes_1_1_2.OscalCompleteSchema
if vctx.cctx == nil {
if vc.cctx == nil {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("error getting path: %v", err)
Expand All @@ -44,7 +44,7 @@ func (vctx *ValidationContext) ValidateOnPath(ctx context.Context, path, target
return nil, fmt.Errorf("error creating oscal model from path: %v", err)
}
} else {
oscalModel, err = vctx.cctx.ComposeFromPath(ctx, path)
oscalModel, err = vc.cctx.ComposeFromPath(ctx, path)
if err != nil {
return nil, fmt.Errorf("error composing model: %v", err)
}
Expand All @@ -54,7 +54,7 @@ func (vctx *ValidationContext) ValidateOnPath(ctx context.Context, path, target
return assessmentResult, fmt.Errorf("component definition is nil")
}

results, err := vctx.ValidateOnCompDef(oscalModel.ComponentDefinition, target)
results, err := vc.ValidateOnCompDef(ctx, oscalModel.ComponentDefinition, target)
if err != nil {
return assessmentResult, err
}
Expand All @@ -67,7 +67,7 @@ func (vctx *ValidationContext) ValidateOnPath(ctx context.Context, path, target
return assessmentResult, nil
}

func (vctx *ValidationContext) ValidateOnCompDef(compDef *oscalTypes_1_1_2.ComponentDefinition, target string) (results []oscalTypes_1_1_2.Result, err error) {
func (vc *ValidationContext) ValidateOnCompDef(ctx context.Context, compDef *oscalTypes_1_1_2.ComponentDefinition, target string) (results []oscalTypes_1_1_2.Result, err error) {
// TODO: Should we execute the validation even if there are no comp-def/components, e.g., create an empty assessment-results object?

if compDef == nil {
Expand All @@ -93,7 +93,7 @@ func (vctx *ValidationContext) ValidateOnCompDef(compDef *oscalTypes_1_1_2.Compo
results = make([]oscalTypes_1_1_2.Result, 0)
if target != "" {
if controlImplementation, ok := controlImplementations[target]; ok {
findings, observations, err := vctx.ValidateOnControlImplementations(&controlImplementation, validationStore, target)
findings, observations, err := vc.ValidateOnControlImplementations(ctx, &controlImplementation, validationStore, target)
if err != nil {
return nil, err
}
Expand All @@ -112,7 +112,7 @@ func (vctx *ValidationContext) ValidateOnCompDef(compDef *oscalTypes_1_1_2.Compo
// loop over the controlImplementations map & validate
// we lose context of source if not contained within the loop
for source, controlImplementation := range controlImplementations {
findings, observations, err := vctx.ValidateOnControlImplementations(&controlImplementation, validationStore, source)
findings, observations, err := vc.ValidateOnControlImplementations(ctx, &controlImplementation, validationStore, source)
if err != nil {
return nil, err
}
Expand All @@ -129,7 +129,7 @@ func (vctx *ValidationContext) ValidateOnCompDef(compDef *oscalTypes_1_1_2.Compo
return results, nil
}

func (vctx *ValidationContext) ValidateOnControlImplementations(controlImplementations *[]oscalTypes_1_1_2.ControlImplementationSet, validationStore *validationstore.ValidationStore, target string) (map[string]oscalTypes_1_1_2.Finding, []oscalTypes_1_1_2.Observation, error) {
func (vc *ValidationContext) ValidateOnControlImplementations(ctx context.Context, controlImplementations *[]oscalTypes_1_1_2.ControlImplementationSet, validationStore *validationstore.ValidationStore, target string) (map[string]oscalTypes_1_1_2.Finding, []oscalTypes_1_1_2.Observation, error) {
// Create requirement store for all implemented requirements
requirementStore := requirementstore.NewRequirementStore(controlImplementations)
message.Title("\n🔍 Collecting Requirements and Validations for Target: ", target)
Expand All @@ -140,25 +140,25 @@ func (vctx *ValidationContext) ValidateOnControlImplementations(controlImplement

// Check if validations perform execution actions
if reqtStats.ExecutableValidations {
if !vctx.runExecutableValidations && vctx.requestExecutionConfirmation {
if !vc.runExecutableValidations && vc.requestExecutionConfirmation {
confirmExecution := message.PromptForConfirmation(nil)
if !confirmExecution {
message.Infof("Validations requiring execution will NOT be run")
} else {
vctx.runExecutableValidations = true
vc.runExecutableValidations = true
}
}
}

// Set values for saving resources
saveResources := false
if vctx.resourcesDir != "" {
if vc.resourcesDir != "" {
saveResources = true
}

// Run Lula validations and generate observations & findings
message.Title("\n📐 Running Validations", "")
observations := validationStore.RunValidations(vctx.runExecutableValidations, saveResources, vctx.resourcesDir)
observations := validationStore.RunValidations(ctx, vc.runExecutableValidations, saveResources, vc.resourcesDir)
message.Title("\n💡 Findings", "")
findings := requirementStore.GenerateFindings(validationStore)

Expand Down
8 changes: 4 additions & 4 deletions src/test/e2e/api_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ func TestApiValidation(t *testing.T) {
oscalPath := "./scenarios/api-field/oscal-component.yaml"
message.NoProgress = true

validationCtx, err := validation.New(validation.WithCompositionContext(nil, oscalPath))
validationCtx, err := validation.New()
if err != nil {
t.Errorf("error creating validation context: %v", err)
}

assessment, err := validationCtx.ValidateOnPath(ctx, oscalPath, "")
assessment, err := validationCtx.ValidateOnPath(context.Background(), oscalPath, "")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -144,12 +144,12 @@ func TestApiValidation(t *testing.T) {
oscalPath := "./scenarios/api-field/oscal-component.yaml"
message.NoProgress = true

validationCtx, err := validation.New(validation.WithCompositionContext(nil, oscalPath))
validationCtx, err := validation.New()
if err != nil {
t.Errorf("error creating validation context: %v", err)
}

assessment, err := validationCtx.ValidateOnPath(ctx, oscalPath, "")
assessment, err := validationCtx.ValidateOnPath(context.Background(), oscalPath, "")
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/e2e/composition_component_def_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ func TestComponentDefinitionComposition(t *testing.T) {
Assess("Validate local composition file", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
compDefPath := "../../test/unit/common/composition/component-definition-import-multi-compdef.yaml"

validationCtx, err := validation.New(validation.WithCompositionContext(nil, compDefPath))
validationCtx, err := validation.New(validation.WithComposition(nil, compDefPath))
if err != nil {
t.Errorf("error creating validation context: %v", err)
}

assessment, err := validationCtx.ValidateOnPath(ctx, compDefPath, "")
assessment, err := validationCtx.ValidateOnPath(context.Background(), compDefPath, "")
if err != nil {
t.Errorf("Error validating component definition: %v", err)
}
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestComponentDefinitionComposition(t *testing.T) {
t.Errorf("component definition is nil")
}

composeResults, err := validationCtx.ValidateOnCompDef(oscalModel.ComponentDefinition, "")
composeResults, err := validationCtx.ValidateOnCompDef(context.Background(), oscalModel.ComponentDefinition, "")
if err != nil {
t.Error(err)
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/e2e/create_resource_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ func TestCreateResourceDataValidation(t *testing.T) {
message.NoProgress = true

validationCtx, err := validation.New(
validation.WithCompositionContext(nil, oscalPath),
validation.WithComposition(nil, oscalPath),
validation.WithAllowExecution(true, true),
)
if err != nil {
t.Errorf("error creating validation context: %v", err)
}

assessment, err := validationCtx.ValidateOnPath(ctx, oscalPath, "")
assessment, err := validationCtx.ValidateOnPath(context.Background(), oscalPath, "")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -115,14 +115,14 @@ func TestDeniedCreateResources(t *testing.T) {
message.NoProgress = true

validationCtx, err := validation.New(
validation.WithCompositionContext(nil, oscalPath),
validation.WithComposition(nil, oscalPath),
validation.WithAllowExecution(false, true),
)
if err != nil {
t.Errorf("error creating validation context: %v", err)
}

assessment, err := validationCtx.ValidateOnPath(ctx, oscalPath, "")
assessment, err := validationCtx.ValidateOnPath(context.Background(), oscalPath, "")
if err != nil {
t.Fatal(err)
}
Expand Down
32 changes: 26 additions & 6 deletions src/test/e2e/file_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"testing"

"github.com/defenseunicorns/lula/src/cmd/validate"
"github.com/defenseunicorns/lula/src/pkg/common/validation"
"github.com/defenseunicorns/lula/src/types"
)

Expand All @@ -16,7 +16,11 @@ func TestFileValidation(t *testing.T) {

t.Run("success - opa", func(t *testing.T) {
ctx := context.WithValue(context.Background(), types.LulaValidationWorkDir, passDir)
assessment, err := validate.ValidateOnPath(ctx, passDir+oscalFile, "")
validationCtx, err := validation.New()
if err != nil {
t.Errorf("error creating validation context: %v", err)
}
assessment, err := validationCtx.ValidateOnPath(ctx, passDir+oscalFile, "")
if err != nil {
t.Fatal(err)
}
Expand All @@ -39,7 +43,11 @@ func TestFileValidation(t *testing.T) {
})
t.Run("success - kyverno", func(t *testing.T) {
ctx := context.WithValue(context.Background(), types.LulaValidationWorkDir, passDir)
assessment, err := validate.ValidateOnPath(ctx, passDir+kyvernoFile, "")
validationCtx, err := validation.New()
if err != nil {
t.Errorf("error creating validation context: %v", err)
}
assessment, err := validationCtx.ValidateOnPath(ctx, passDir+kyvernoFile, "")
if err != nil {
t.Fatal(err)
}
Expand All @@ -62,7 +70,11 @@ func TestFileValidation(t *testing.T) {
})
t.Run("fail - opa", func(t *testing.T) {
ctx := context.WithValue(context.Background(), types.LulaValidationWorkDir, failDir)
assessment, err := validate.ValidateOnPath(ctx, failDir+oscalFile, "")
validationCtx, err := validation.New()
if err != nil {
t.Errorf("error creating validation context: %v", err)
}
assessment, err := validationCtx.ValidateOnPath(ctx, failDir+oscalFile, "")
if err != nil {
t.Fatal(err)
}
Expand All @@ -85,7 +97,11 @@ func TestFileValidation(t *testing.T) {
})
t.Run("fail - kyverno", func(t *testing.T) {
ctx := context.WithValue(context.Background(), types.LulaValidationWorkDir, failDir)
assessment, err := validate.ValidateOnPath(ctx, failDir+kyvernoFile, "")
validationCtx, err := validation.New()
if err != nil {
t.Errorf("error creating validation context: %v", err)
}
assessment, err := validationCtx.ValidateOnPath(ctx, failDir+kyvernoFile, "")
if err != nil {
t.Fatal(err)
}
Expand All @@ -109,7 +125,11 @@ func TestFileValidation(t *testing.T) {

t.Run("invalid input", func(t *testing.T) {
ctx := context.WithValue(context.Background(), types.LulaValidationWorkDir, "scenarios/file-validations/invalid")
_, err := validate.ValidateOnPath(ctx, "scenarios/file-validations/invalid/oscal-component.yaml", "")
validationCtx, err := validation.New()
if err != nil {
t.Errorf("error creating validation context: %v", err)
}
_, err = validationCtx.ValidateOnPath(ctx, "scenarios/file-validations/invalid/oscal-component.yaml", "")
if err == nil {
t.Fatal("expected error, got success")
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/e2e/multi_resource_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ func TestMultiResourceValidation(t *testing.T) {
oscalPath := "./scenarios/multi-resource/oscal-component.yaml"
message.NoProgress = true

validationCtx, err := validation.New(validation.WithCompositionContext(nil, oscalPath))
validationCtx, err := validation.New()
if err != nil {
t.Errorf("error creating validation context: %v", err)
}

assessment, err := validationCtx.ValidateOnPath(ctx, oscalPath, "")
assessment, err := validationCtx.ValidateOnPath(context.Background(), oscalPath, "")
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/e2e/outputs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ func TestOutputs(t *testing.T) {
components := *compDef.Components
validationStore := validationstore.NewValidationStoreFromBackMatter(*compDef.BackMatter)

validationCtx, err := validation.New(validation.WithCompositionContext(nil, oscalPath))
validationCtx, err := validation.New()
if err != nil {
t.Errorf("error creating validation context: %v", err)
}

findingMap, observations, err := validationCtx.ValidateOnControlImplementations(components[0].ControlImplementations, validationStore, "")
findingMap, observations, err := validationCtx.ValidateOnControlImplementations(context.Background(), components[0].ControlImplementations, validationStore, "")
if err != nil {
t.Fatal(err)
}
Expand Down
18 changes: 7 additions & 11 deletions src/test/e2e/pod_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,14 @@ func validatePodLabelPass(ctx context.Context, t *testing.T, config *envconf.Con
}
message.Infof("Successfully upgraded %s to %s with OSCAL version %s %s\n", oscalPath, revisionOptions.OutputFile, revisionResponse.Reviser.GetSchemaVersion(), revisionResponse.Reviser.GetModelType())

validationCtx, err := validation.New(validation.WithCompositionContext(nil, oscalPath))
validationCtx, err := validation.New()
if err != nil {
t.Errorf("error creating validation context: %v", err)
}

assessment, err := validationCtx.ValidateOnPath(ctx, oscalPath, "")
assessment, err := validationCtx.ValidateOnPath(context.Background(), revisionOptions.OutputFile, "")
if err != nil {
t.Fatalf("Failed to validate oscal file: %s", oscalPath)
t.Fatalf("Failed to validate oscal file: %s", revisionOptions.OutputFile)
}

if len(assessment.Results) == 0 {
Expand Down Expand Up @@ -322,14 +322,12 @@ func validatePodLabelPass(ctx context.Context, t *testing.T, config *envconf.Con
func validatePodLabelFail(ctx context.Context, t *testing.T, oscalPath string) (*[]oscalTypes_1_1_2.Finding, *[]oscalTypes_1_1_2.Observation) {
message.NoProgress = true

validationCtx, err := validation.New(
validation.WithCompositionContext(nil, oscalPath),
validation.WithAllowExecution(false, true))
validationCtx, err := validation.New(validation.WithAllowExecution(false, true))
if err != nil {
t.Errorf("error creating validation context: %v", err)
}

assessment, err := validationCtx.ValidateOnPath(ctx, oscalPath, "")
assessment, err := validationCtx.ValidateOnPath(context.Background(), oscalPath, "")
if err != nil {
t.Fatalf("Failed to validate oscal file: %s", oscalPath)
}
Expand Down Expand Up @@ -372,14 +370,12 @@ func validateSaveResources(ctx context.Context, t *testing.T, oscalPath string)
message.NoProgress = true
tempDir := t.TempDir()

validationCtx, err := validation.New(
validation.WithCompositionContext(nil, oscalPath),
validation.WithResourcesDir(true, tempDir))
validationCtx, err := validation.New(validation.WithResourcesDir(true, tempDir))
if err != nil {
t.Errorf("error creating validation context: %v", err)
}

assessment, err := validationCtx.ValidateOnPath(ctx, oscalPath, "")
assessment, err := validationCtx.ValidateOnPath(context.Background(), oscalPath, "")
if err != nil {
t.Fatalf("Failed to validate oscal file: %s", oscalPath)
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/e2e/pod_wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ func TestPodWaitValidation(t *testing.T) {
oscalPath := "./scenarios/wait-field/oscal-component.yaml"
message.NoProgress = true

validationCtx, err := validation.New(validation.WithCompositionContext(nil, oscalPath))
validationCtx, err := validation.New()
if err != nil {
t.Errorf("error creating validation context: %v", err)
}

assessment, err := validationCtx.ValidateOnPath(ctx, oscalPath, "")
assessment, err := validationCtx.ValidateOnPath(context.Background(), oscalPath, "")
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/e2e/remote_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ func TestRemoteValidation(t *testing.T) {
Assess("Validate local validation file", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
oscalPath := "./scenarios/remote-validations/component-definition.yaml"

validationCtx, err := validation.New(validation.WithCompositionContext(nil, oscalPath))
validationCtx, err := validation.New(validation.WithComposition(nil, oscalPath))
if err != nil {
t.Errorf("error creating validation context: %v", err)
}

assessment, err := validationCtx.ValidateOnPath(ctx, oscalPath, "")
assessment, err := validationCtx.ValidateOnPath(context.Background(), oscalPath, "")
if err != nil {
t.Fatal(err)
}
Expand Down
Loading

0 comments on commit 4836e03

Please sign in to comment.