Skip to content

Commit

Permalink
Add test for buildQuickSyncPipeline (#5227)
Browse files Browse the repository at this point in the history
Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>
  • Loading branch information
Warashi authored Sep 25, 2024
1 parent 31e91de commit c3386cf
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 3 deletions.
21 changes: 18 additions & 3 deletions pkg/app/pipedv1/plugin/kubernetes/deployment/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const (
// StageK8sTrafficRouting represents the state where the traffic to application
// should be splitted as the specified percentage to PRIMARY, CANARY, BASELINE variants.
StageK8sTrafficRouting Stage = "K8S_TRAFFIC_ROUTING"
// StageK8sRollback represents the state where all deployed resources should be rollbacked.
StageK8sRollback Stage = "K8S_ROLLBACK"
)

var AllStages = []Stage{
Expand All @@ -57,19 +59,32 @@ var AllStages = []Stage{
StageK8sBaselineRollout,
StageK8sBaselineClean,
StageK8sTrafficRouting,
StageK8sRollback,
}

func (s Stage) String() string {
return string(s)
}

const (
PredefinedStageK8sSync = "K8sSync"
PredefinedStageRollback = "Rollback"
PredefinedStageRollback = "K8sRollback"
)

var predefinedStages = map[string]config.PipelineStage{
PredefinedStageK8sSync: {
ID: PredefinedStageK8sSync,
Name: model.StageK8sSync,
ID: PredefinedStageK8sSync,
// TODO: we have to change config.PipelineStage.Name to string before releasing pipedv1?
// because we don't want to define stages at piped side. We want to define them at the plugin side.
// Or plugins should use the model.Stage type instead of string or some defined type.
Name: model.Stage(StageK8sSync),
Desc: "Sync by applying all manifests",
},
PredefinedStageRollback: {
ID: PredefinedStageRollback,
Name: model.Stage(StageK8sRollback),
Desc: "Rollback the deployment",
},
}

// GetPredefinedStage finds and returns the predefined stage for the given id.
Expand Down
89 changes: 89 additions & 0 deletions pkg/app/pipedv1/plugin/kubernetes/deployment/pipeline_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2024 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package deployment

import (
"testing"
"time"

"github.com/pipe-cd/pipecd/pkg/model"
"github.com/stretchr/testify/assert"
)

func TestBuildQuickSyncPipeline(t *testing.T) {
t.Parallel()

now := time.Now()

tests := []struct {
name string
autoRollback bool
expected []*model.PipelineStage
}{
{
name: "without auto rollback",
autoRollback: false,
expected: []*model.PipelineStage{
{
Id: PredefinedStageK8sSync,
Name: StageK8sSync.String(),
Desc: "Sync by applying all manifests",
Index: 0,
Predefined: true,
Visible: true,
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
Metadata: nil,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
},
},
},
{
name: "with auto rollback",
autoRollback: true,
expected: []*model.PipelineStage{
{
Id: PredefinedStageK8sSync,
Name: StageK8sSync.String(),
Desc: "Sync by applying all manifests",
Index: 0,
Predefined: true,
Visible: true,
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
Metadata: nil,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
},
{
Id: PredefinedStageRollback,
Name: StageK8sRollback.String(),
Desc: "Rollback the deployment",
Predefined: true,
Visible: false,
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := buildQuickSyncPipeline(tt.autoRollback, now)
assert.Equal(t, tt.expected, actual)
})
}
}

0 comments on commit c3386cf

Please sign in to comment.