Skip to content

Commit

Permalink
Adding some additional tests for inspect
Browse files Browse the repository at this point in the history
Fixing naming issue with inspect options, easier to read
  • Loading branch information
janboll committed Nov 25, 2024
1 parent 228914f commit 06782e5
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 18 deletions.
9 changes: 2 additions & 7 deletions tooling/templatize/cmd/pipeline/inspect/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
35 changes: 24 additions & 11 deletions tooling/templatize/pkg/pipeline/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,41 @@ 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{
"vars": inspectVars,
}
}

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
Expand All @@ -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 {
Expand Down
126 changes: 126 additions & 0 deletions tooling/templatize/pkg/pipeline/inspect_test.go
Original file line number Diff line number Diff line change
@@ -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\"")
}

0 comments on commit 06782e5

Please sign in to comment.