From 06782e57520c94f254d1fa52596be048321949de Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Boll Date: Mon, 25 Nov 2024 15:27:51 +0100 Subject: [PATCH] Adding some additional tests for inspect Fixing naming issue with inspect options, easier to read --- .../cmd/pipeline/inspect/options.go | 9 +- tooling/templatize/pkg/pipeline/inspect.go | 35 +++-- .../templatize/pkg/pipeline/inspect_test.go | 126 ++++++++++++++++++ 3 files changed, 152 insertions(+), 18 deletions(-) create mode 100644 tooling/templatize/pkg/pipeline/inspect_test.go diff --git a/tooling/templatize/cmd/pipeline/inspect/options.go b/tooling/templatize/cmd/pipeline/inspect/options.go index d78516d4d..774a43e18 100644 --- a/tooling/templatize/cmd/pipeline/inspect/options.go +++ b/tooling/templatize/cmd/pipeline/inspect/options.go @@ -112,11 +112,6 @@ func (o *InspectOptions) RunInspect(ctx context.Context) error { if err != nil { return err } - return o.PipelineOptions.Pipeline.Inspect(ctx, &pipeline.PipelineInspectOptions{ - Vars: variables, - Region: rolloutOptions.Region, - Step: o.PipelineOptions.Step, - Scope: o.Scope, - Format: o.Format, - }, os.Stdout) + inspectOptions := pipeline.NewInspectOptions(variables, rolloutOptions.Region, o.PipelineOptions.Step, o.Scope, o.Format) + return o.PipelineOptions.Pipeline.Inspect(ctx, inspectOptions, os.Stdout) } diff --git a/tooling/templatize/pkg/pipeline/inspect.go b/tooling/templatize/pkg/pipeline/inspect.go index 739b7c50b..2374ce84f 100644 --- a/tooling/templatize/pkg/pipeline/inspect.go +++ b/tooling/templatize/pkg/pipeline/inspect.go @@ -8,7 +8,7 @@ import ( "github.com/Azure/ARO-HCP/tooling/templatize/pkg/config" ) -type StepInspectScope func(*step, *PipelineInspectOptions, io.Writer) error +type StepInspectScope func(*step, *InspectOptions, io.Writer) error func NewStepInspectScopes() map[string]StepInspectScope { return map[string]StepInspectScope{ @@ -16,20 +16,33 @@ func NewStepInspectScopes() map[string]StepInspectScope { } } -type PipelineInspectOptions struct { - Scope string - Format string - Step string - Region string - Vars config.Variables +// InspectOptions contains the options for the Inspect method +type InspectOptions struct { + Scope string + Format string + Step string + Region string + Vars config.Variables + ScopeFunctions map[string]StepInspectScope } -func (p *Pipeline) Inspect(ctx context.Context, options *PipelineInspectOptions, writer io.Writer) error { - stepInspectScopes := NewStepInspectScopes() +// NewInspectOptions creates a new PipelineInspectOptions struct +func NewInspectOptions(vars config.Variables, region, step, scope, format string) *InspectOptions { + return &InspectOptions{ + Scope: scope, + Format: format, + Step: step, + Region: region, + Vars: vars, + ScopeFunctions: NewStepInspectScopes(), + } +} + +func (p *Pipeline) Inspect(ctx context.Context, options *InspectOptions, writer io.Writer) error { for _, rg := range p.ResourceGroups { for _, step := range rg.Steps { if step.Name == options.Step { - if inspectFunc, ok := stepInspectScopes[options.Scope]; ok { + if inspectFunc, ok := options.ScopeFunctions[options.Scope]; ok { err := inspectFunc(step, options, writer) if err != nil { return err @@ -44,7 +57,7 @@ func (p *Pipeline) Inspect(ctx context.Context, options *PipelineInspectOptions, return fmt.Errorf("step %q not found", options.Step) } -func inspectVars(s *step, options *PipelineInspectOptions, writer io.Writer) error { +func inspectVars(s *step, options *InspectOptions, writer io.Writer) error { var envVars map[string]string var err error switch s.Action { diff --git a/tooling/templatize/pkg/pipeline/inspect_test.go b/tooling/templatize/pkg/pipeline/inspect_test.go new file mode 100644 index 000000000..f8be4c7b3 --- /dev/null +++ b/tooling/templatize/pkg/pipeline/inspect_test.go @@ -0,0 +1,126 @@ +package pipeline + +import ( + "bytes" + "context" + "io" + "testing" + + "gotest.tools/v3/assert" + + "github.com/Azure/ARO-HCP/tooling/templatize/pkg/config" +) + +func TestInspectVars(t *testing.T) { + testCases := []struct { + name string + caseStep *step + options *InspectOptions + expected string + err string + }{ + { + name: "basic", + caseStep: &step{ + Action: "Shell", + Env: []EnvVar{ + { + Name: "FOO", + ConfigRef: "foo", + }, + }, + }, + options: &InspectOptions{ + Vars: config.Variables{ + "foo": "bar", + }, + Format: "shell", + }, + expected: "export FOO=\"bar\"\n", + }, + { + name: "makefile", + caseStep: &step{ + Action: "Shell", + Env: []EnvVar{ + { + Name: "FOO", + ConfigRef: "foo", + }, + }, + }, + options: &InspectOptions{ + Vars: config.Variables{ + "foo": "bar", + }, + Format: "makefile", + }, + expected: "FOO ?= bar\n", + }, + { + name: "failed action", + caseStep: &step{Action: "Unknown"}, + err: "inspecting step variables not implemented for action type Unknown", + }, + { + name: "failed format", + caseStep: &step{Action: "Shell"}, + options: &InspectOptions{Format: "unknown"}, + err: "unknown output format \"unknown\"", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + buf := new(bytes.Buffer) + err := inspectVars(tc.caseStep, tc.options, buf) + if tc.err == "" { + assert.NilError(t, err) + assert.Equal(t, buf.String(), tc.expected) + } else { + assert.ErrorContains(t, err, tc.err) + } + }) + } +} + +func TestInspect(t *testing.T) { + p := Pipeline{ + ResourceGroups: []*resourceGroup{{ + Steps: []*step{ + { + Name: "step1", + }, + }, + }, + }, + } + opts := NewInspectOptions(config.Variables{}, "", "step1", "scope", "format") + + opts.ScopeFunctions = map[string]StepInspectScope{ + "scope": func(s *step, o *InspectOptions, w io.Writer) error { + assert.Equal(t, s.Name, "step1") + return nil + }, + } + + err := p.Inspect(context.Background(), opts, nil) + assert.NilError(t, err) +} + +func TestInspectWrongScope(t *testing.T) { + p := Pipeline{ + ResourceGroups: []*resourceGroup{{ + Steps: []*step{ + { + Name: "step1", + }, + }, + }, + }, + } + opts := NewInspectOptions(config.Variables{}, "", "step1", "foo", "format") + + err := p.Inspect(context.Background(), opts, nil) + assert.Error(t, err, "unknown inspect scope \"foo\"") +}