Skip to content

Commit

Permalink
Pass whole test as an arg to the executeStep method instead of passin…
Browse files Browse the repository at this point in the history
…g only steps

Signed-off-by: GLVS Kiriti <[email protected]>
  • Loading branch information
GLVSKiriti committed Aug 2, 2024
1 parent d8baeb5 commit bc2e070
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 19 deletions.
5 changes: 1 addition & 4 deletions cmd/declarative.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ func runTestSteps(test declarative.Test) error {
// spawn an alpine container
runner = &declarative.Containerrunner{Image: "golang"}
ctx = context.Background()
ctx = context.WithValue(ctx, declarative.ContextKey("ruleName"), test.Rule)
ctx = context.WithValue(ctx, declarative.ContextKey("beforeScript"), test.Before)
ctx = context.WithValue(ctx, declarative.ContextKey("afterScript"), test.After)
default:
return fmt.Errorf("unsupported runner: %v", test.Runner)
}
Expand All @@ -92,7 +89,7 @@ func runTestSteps(test declarative.Test) error {
}

// Execute each step in the test.
err := runner.ExecuteStep(ctx, test.Steps)
err := runner.ExecuteStep(ctx, test)
if err != nil {
return fmt.Errorf("error executing steps for the rule %v : %v", test.Rule, err)
}
Expand Down
13 changes: 3 additions & 10 deletions pkg/declarative/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,16 @@ func (r *Containerrunner) Setup(ctx context.Context, beforeScript string) error
return nil
}

func (r *Containerrunner) ExecuteStep(ctx context.Context, steps []SyscallStep) error {
func (r *Containerrunner) ExecuteStep(ctx context.Context, test Test) error {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return fmt.Errorf("error creating Docker Client: %v", err)
}

// Create a yaml structure for given step
test.Runner = "HostRunner" // Change container runner to host runner
testFile := Tests{
Tests: []Test{
{
Rule: ctx.Value(ContextKey("ruleName")).(string),
Runner: "HostRunner",
Before: ctx.Value(ContextKey("beforeScript")).(string),
Steps: steps,
After: ctx.Value(ContextKey("afterScript")).(string),
},
},
Tests: []Test{test},
}

// Marshall struct to yaml data
Expand Down
3 changes: 2 additions & 1 deletion pkg/declarative/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func (r *Hostrunner) Setup(ctx context.Context, beforeScript string) error {
return nil
}

func (r *Hostrunner) ExecuteStep(ctx context.Context, steps []SyscallStep) error {
func (r *Hostrunner) ExecuteStep(ctx context.Context, test Test) error {
steps := test.Steps
for _, step := range steps {
switch step.Syscall {
case "write":
Expand Down
5 changes: 1 addition & 4 deletions pkg/declarative/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import "context"
// Common runner interface for runners like hostrunner, container-runner etc..
type Runner interface {
Setup(ctx context.Context, beforeScript string) error
ExecuteStep(ctx context.Context, steps []SyscallStep) error
ExecuteStep(ctx context.Context, test Test) error
Cleanup(ctx context.Context, afterScript string) error
}

// A type which helps to store and retrive key-value pairs in context
type ContextKey string

0 comments on commit bc2e070

Please sign in to comment.