Skip to content

Commit

Permalink
Default RequisiteStageRefIds to empty slice (#31)
Browse files Browse the repository at this point in the history
* Default RequisiteStageRefIds to empty slice

* Add builder tests for pipeline stages
  • Loading branch information
bitsofdave authored and shraykay committed Mar 30, 2018
1 parent 2514e39 commit e231879
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pipeline/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (b *Builder) Pipeline() (*types.SpinnakerPipeline, error) {
if err != nil {
return nil, err
}

sp.Stages = append(sp.Stages, s)
}

Expand Down Expand Up @@ -213,7 +214,11 @@ func (b *Builder) buildManualJudgementStage(index int, s config.Stage) (*types.M

func buildStageMetadata(s config.Stage, t string, index int, linear bool) types.StageMetadata {
refID := s.RefID
if s.ReliesOn == nil {
s.ReliesOn = []string{}
}
reliesOn := s.ReliesOn

if linear {
refID = fmt.Sprintf("%d", index)
if index > 0 {
Expand Down
177 changes: 177 additions & 0 deletions pipeline/builder/builder_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package builder_test

import (
"os"
"path/filepath"
"testing"

"github.com/namely/k8s-pipeliner/pipeline/builder"
"github.com/namely/k8s-pipeliner/pipeline/builder/types"
"github.com/namely/k8s-pipeliner/pipeline/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -48,3 +51,177 @@ func TestBuilderAssignsPipelineConfiguration(t *testing.T) {
assert.True(t, spinnaker.LimitConcurrent)
assert.Equal(t, pipeline.Description, spinnaker.Description)
}

func TestBuilderPipelineStages(t *testing.T) {
wd, _ := os.Getwd()
file := filepath.Join(wd, "testdata", "deployment.full.yml")

t.Run("Deploy stage is parsed correctly", func(t *testing.T) {
t.Run("Clusters are assigned", func(t *testing.T) {
pipeline := &config.Pipeline{
Stages: []config.Stage{
{
Name: "Test Deploy Stage",
Deploy: &config.DeployStage{
Groups: []config.Group{
{
ManifestFile: file,
},
},
},
},
},
}

builder := builder.New(pipeline)
spinnaker, err := builder.Pipeline()
require.NoError(t, err, "error generating pipeline json")

assert.Equal(t, "Test Deploy Stage", spinnaker.Stages[0].(*types.DeployStage).Name)
assert.Len(t, spinnaker.Stages[0].(*types.DeployStage).Clusters, 1)
})

t.Run("RequisiteStageRefIds defaults to an empty slice", func(t *testing.T) {
pipeline := &config.Pipeline{
Stages: []config.Stage{
{
Deploy: &config.DeployStage{},
},
},
}

builder := builder.New(pipeline)
spinnaker, err := builder.Pipeline()
require.NoError(t, err, "error generating pipeline json")

assert.Equal(t, []string{}, spinnaker.Stages[0].(*types.DeployStage).StageMetadata.RequisiteStageRefIds)
})

t.Run("RequisiteStageRefIds is assigned when ReliesOn is provided", func(t *testing.T) {
pipeline := &config.Pipeline{
Stages: []config.Stage{
{
ReliesOn: []string{"2"},
Deploy: &config.DeployStage{},
},
},
}

builder := builder.New(pipeline)
spinnaker, err := builder.Pipeline()
require.NoError(t, err, "error generating pipeline json")

assert.Equal(t, []string{"2"}, spinnaker.Stages[0].(*types.DeployStage).StageMetadata.RequisiteStageRefIds)
})
})

t.Run("RunJob stage is parsed correctly", func(t *testing.T) {
t.Run("Name is assigned", func(t *testing.T) {
pipeline := &config.Pipeline{
Stages: []config.Stage{
{
Name: "Test RunJob Stage",
RunJob: &config.RunJobStage{
ManifestFile: file,
},
},
},
}

builder := builder.New(pipeline)
spinnaker, err := builder.Pipeline()
require.NoError(t, err, "error generating pipeline json")

assert.Equal(t, "Test RunJob Stage", spinnaker.Stages[0].(*types.RunJobStage).Name)
})

t.Run("RequisiteStageRefIds defaults to an empty slice", func(t *testing.T) {
pipeline := &config.Pipeline{
Stages: []config.Stage{
{
RunJob: &config.RunJobStage{
ManifestFile: file,
},
},
},
}

builder := builder.New(pipeline)
spinnaker, err := builder.Pipeline()
require.NoError(t, err, "error generating pipeline json")

assert.Equal(t, []string{}, spinnaker.Stages[0].(*types.RunJobStage).StageMetadata.RequisiteStageRefIds)
})

t.Run("RequisiteStageRefIds is assigned when ReliesOn is provided", func(t *testing.T) {
pipeline := &config.Pipeline{
Stages: []config.Stage{
{
ReliesOn: []string{"2"},
RunJob: &config.RunJobStage{
ManifestFile: file,
},
},
},
}

builder := builder.New(pipeline)
spinnaker, err := builder.Pipeline()
require.NoError(t, err, "error generating pipeline json")

assert.Equal(t, []string{"2"}, spinnaker.Stages[0].(*types.RunJobStage).StageMetadata.RequisiteStageRefIds)
})
})

t.Run("ManualJudgement stage is parsed correctly", func(t *testing.T) {
t.Run("Name is assigned", func(t *testing.T) {
pipeline := &config.Pipeline{
Stages: []config.Stage{
{
Name: "Test ManualJudgementStage Stage",
ManualJudgement: &config.ManualJudgementStage{},
},
},
}

builder := builder.New(pipeline)
spinnaker, err := builder.Pipeline()
require.NoError(t, err, "error generating pipeline json")

assert.Equal(t, "Test ManualJudgementStage Stage", spinnaker.Stages[0].(*types.ManualJudgementStage).Name)
})

t.Run("RequisiteStageRefIds defaults to an empty slice", func(t *testing.T) {
pipeline := &config.Pipeline{
Stages: []config.Stage{
{
ManualJudgement: &config.ManualJudgementStage{},
},
},
}

builder := builder.New(pipeline)
spinnaker, err := builder.Pipeline()
require.NoError(t, err, "error generating pipeline json")

assert.Equal(t, []string{}, spinnaker.Stages[0].(*types.ManualJudgementStage).StageMetadata.RequisiteStageRefIds)
})

t.Run("RequisiteStageRefIds is assigned when ReliesOn is provided", func(t *testing.T) {
pipeline := &config.Pipeline{
Stages: []config.Stage{
{
ReliesOn: []string{"2"},
ManualJudgement: &config.ManualJudgementStage{},
},
},
}

builder := builder.New(pipeline)
spinnaker, err := builder.Pipeline()
require.NoError(t, err, "error generating pipeline json")

assert.Equal(t, []string{"2"}, spinnaker.Stages[0].(*types.ManualJudgementStage).StageMetadata.RequisiteStageRefIds)
})
})
}
2 changes: 1 addition & 1 deletion pipeline/builder/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Trigger interface {
// StageMetadata is the common components of a stage in spinnaker such as name
type StageMetadata struct {
RefID string `json:"refId,omitempty"`
RequisiteStageRefIds []string `json:"requisiteStageRefIds,omitempty"`
RequisiteStageRefIds []string `json:"requisiteStageRefIds"`
Name string `json:"name"`
Type string `json:"type"`
Notifications []Notification `json:"notifications,omitempty"`
Expand Down

0 comments on commit e231879

Please sign in to comment.