Skip to content

Commit

Permalink
Add tests for run methods
Browse files Browse the repository at this point in the history
  • Loading branch information
janboll committed Nov 27, 2024
1 parent dad219d commit f45e661
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 22 deletions.
34 changes: 17 additions & 17 deletions tooling/templatize/pkg/pipeline/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ type PipelineRunOptions struct {
func (p *Pipeline) Run(ctx context.Context, options *PipelineRunOptions) error {
logger := logr.FromContextOrDiscard(ctx)

if p.subsciptionLookupFunc == nil {
p.subsciptionLookupFunc = lookupSubscriptionID
}

// set working directory to the pipeline file directory for the
// duration of the execution so that all commands and file references
// within the pipeline file are resolved relative to the pipeline file
Expand All @@ -70,28 +74,24 @@ func (p *Pipeline) Run(ctx context.Context, options *PipelineRunOptions) error {
}()

for _, rg := range p.ResourceGroups {
err := rg.run(ctx, options)
// prepare execution context
subscriptionID, err := p.subsciptionLookupFunc(ctx, rg.Subscription)

Check failure on line 78 in tooling/templatize/pkg/pipeline/run.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)
executionTarget := executionTargetImpl{
subscriptionName: rg.Subscription,
subscriptionID: subscriptionID,
region: options.Region,
resourceGroup: rg.Name,
aksClusterName: rg.AKSCluster,
}
err = rg.run(ctx, options, &executionTarget)
if err != nil {
return err
}
}
return nil
}

func (rg *ResourceGroup) run(ctx context.Context, options *PipelineRunOptions) error {
// prepare execution context
subscriptionID, err := lookupSubscriptionID(ctx, rg.Subscription)
if err != nil {
return err
}
executionTarget := executionTargetImpl{
subscriptionName: rg.Subscription,
subscriptionID: subscriptionID,
region: options.Region,
resourceGroup: rg.Name,
aksClusterName: rg.AKSCluster,
}

func (rg *ResourceGroup) run(ctx context.Context, options *PipelineRunOptions, executionTarget ExecutionTarget) error {
logger := logr.FromContextOrDiscard(ctx)

kubeconfigFile, err := executionTarget.KubeConfig(ctx)
Expand All @@ -101,7 +101,7 @@ func (rg *ResourceGroup) run(ctx context.Context, options *PipelineRunOptions) e
logger.V(5).Error(err, "failed to delete kubeconfig file", "kubeconfig", kubeconfigFile)
}
}()
} else if err != nil || kubeconfigFile == "" && executionTarget.GetAkSClusterName() != "" {
} else if err != nil {
return fmt.Errorf("failed to prepare kubeconfig: %w", err)
}

Expand All @@ -118,7 +118,7 @@ func (rg *ResourceGroup) run(ctx context.Context, options *PipelineRunOptions) e
),
),
kubeconfigFile,
&executionTarget, options,
executionTarget, options,
)
if err != nil {
return err
Expand Down
106 changes: 105 additions & 1 deletion tooling/templatize/pkg/pipeline/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ import (
)

func TestStepRun(t *testing.T) {
fooundOutput := ""
s := &Step{
Name: "test",
Action: "Shell",
Command: []string{"echo", "hello"},
outputFunc: func(output string) {
assert.Equal(t, output, "hello\n")
fooundOutput = output
},
}
err := s.run(context.Background(), "", &executionTargetImpl{}, &PipelineRunOptions{})
assert.NilError(t, err)
assert.Equal(t, fooundOutput, "hello\n")
}

func TestStepRunSkip(t *testing.T) {
Expand Down Expand Up @@ -80,3 +82,105 @@ func TestPipelineValidate(t *testing.T) {
err := p.Validate()
assert.Error(t, err, "resource group name is required")
}

func TestResourceGroupRun(t *testing.T) {
foundOutput := ""
rg := &ResourceGroup{
Steps: []*Step{
{
Name: "step",
Action: "Shell",
Command: []string{"echo", "hello"},
outputFunc: func(output string) {
foundOutput = output
},
},
},
}
err := rg.run(context.Background(), &PipelineRunOptions{}, &executionTargetImpl{})
assert.NilError(t, err)
assert.Equal(t, foundOutput, "hello\n")
}

func TestResourceGroupError(t *testing.T) {
tmpVals := make([]string, 0)
rg := &ResourceGroup{
Steps: []*Step{
{
Name: "step",
Action: "Shell",
Command: []string{"echo", "hello"},
outputFunc: func(output string) {
tmpVals = append(tmpVals, output)
},
},
{
Name: "step",
Action: "Shell",
Command: []string{"faaaaafffaa"},
outputFunc: func(output string) {
tmpVals = append(tmpVals, output)
},
},
{
Name: "step",
Action: "Shell",
Command: []string{"echo", "hallo"},
outputFunc: func(output string) {
tmpVals = append(tmpVals, output)
},
},
},
}
err := rg.run(context.Background(), &PipelineRunOptions{}, &executionTargetImpl{})
assert.Error(t, err, "failed to execute shell command: exec: \"faaaaafffaa\": executable file not found in $PATH")
// Test processing ends after first error
assert.Equal(t, len(tmpVals), 1)
}

type testExecutionTarget struct{}

func (t *testExecutionTarget) KubeConfig(_ context.Context) (string, error) {
return "", nil
}
func (t *testExecutionTarget) GetSubscriptionID() string { return "test" }
func (t *testExecutionTarget) GetAkSClusterName() string { return "test" }
func (t *testExecutionTarget) GetResourceGroup() string { return "test" }
func (t *testExecutionTarget) GetRegion() string { return "test" }

func TestResourceGroupRunRequireKubeconfig(t *testing.T) {

rg := &ResourceGroup{Steps: []*Step{}}
err := rg.run(context.Background(), &PipelineRunOptions{}, &testExecutionTarget{})
assert.NilError(t, err)
}

func TestPipelineRun(t *testing.T) {
foundOutput := ""
pipeline := &Pipeline{
ResourceGroups: []*ResourceGroup{
{
Name: "test",
Subscription: "test",
Steps: []*Step{
{
Name: "step",
Action: "Shell",
Command: []string{"echo", "hello"},
outputFunc: func(output string) {
foundOutput = output
},
},
},
},
},
subsciptionLookupFunc: func(_ context.Context, _ string) (string, error) {
return "test", nil
},
}

err := pipeline.Run(context.Background(), &PipelineRunOptions{})

assert.NilError(t, err)
assert.Equal(t, foundOutput, "hello\n")
}
13 changes: 9 additions & 4 deletions tooling/templatize/pkg/pipeline/types.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package pipeline

import "context"

type subsciptionLookup func(context.Context, string) (string, error)

type Pipeline struct {
pipelineFilePath string
ServiceGroup string `yaml:"serviceGroup"`
RolloutName string `yaml:"rolloutName"`
ResourceGroups []*ResourceGroup `yaml:"resourceGroups"`
pipelineFilePath string
ServiceGroup string `yaml:"serviceGroup"`
RolloutName string `yaml:"rolloutName"`
ResourceGroups []*ResourceGroup `yaml:"resourceGroups"`
subsciptionLookupFunc subsciptionLookup
}

type ResourceGroup struct {
Expand Down

0 comments on commit f45e661

Please sign in to comment.