From c3386cf0fcb37c7eebce7177feea16771796e3dc Mon Sep 17 00:00:00 2001 From: Shinnosuke Sawada-Dazai Date: Wed, 25 Sep 2024 15:03:54 +0900 Subject: [PATCH] Add test for buildQuickSyncPipeline (#5227) Signed-off-by: Shinnosuke Sawada-Dazai --- .../plugin/kubernetes/deployment/pipeline.go | 21 ++++- .../kubernetes/deployment/pipeline_test.go | 89 +++++++++++++++++++ 2 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 pkg/app/pipedv1/plugin/kubernetes/deployment/pipeline_test.go diff --git a/pkg/app/pipedv1/plugin/kubernetes/deployment/pipeline.go b/pkg/app/pipedv1/plugin/kubernetes/deployment/pipeline.go index e4292bac00..388aa5f959 100644 --- a/pkg/app/pipedv1/plugin/kubernetes/deployment/pipeline.go +++ b/pkg/app/pipedv1/plugin/kubernetes/deployment/pipeline.go @@ -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{ @@ -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. diff --git a/pkg/app/pipedv1/plugin/kubernetes/deployment/pipeline_test.go b/pkg/app/pipedv1/plugin/kubernetes/deployment/pipeline_test.go new file mode 100644 index 0000000000..26f608ed77 --- /dev/null +++ b/pkg/app/pipedv1/plugin/kubernetes/deployment/pipeline_test.go @@ -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) + }) + } +}