Skip to content

Commit

Permalink
Add tests for pipedv1 executor package
Browse files Browse the repository at this point in the history
Signed-off-by: khanhtc1202 <[email protected]>
  • Loading branch information
khanhtc1202 committed Oct 31, 2024
1 parent 8b49a84 commit 9b61c4a
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 10 deletions.
23 changes: 13 additions & 10 deletions pkg/app/pipedv1/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,19 @@ type Input struct {
// Deploy source at target commit
TargetDSP deploysource.Provider
// Deploy source at running commit
RunningDSP deploysource.Provider
GitClient GitClient
CommandLister CommandLister
LogPersister LogPersister
MetadataStore metadatastore.MetadataStore
AppManifestsCache cache.Cache
AnalysisResultStore AnalysisResultStore
Logger *zap.Logger
Notifier Notifier
RunningDSP deploysource.Provider
GitClient GitClient
CommandLister CommandLister
LogPersister LogPersister
MetadataStore metadatastore.MetadataStore
AppManifestsCache cache.Cache
AnalysisResultStore AnalysisResultStore
Logger *zap.Logger
Notifier Notifier
}

// DetermineStageStatus determines the final status of the stage based on the given stop signal.
// Normal is the case when the stop signal is StopSignalNone.
func DetermineStageStatus(sig StopSignalType, ori, got model.StageStatus) model.StageStatus {
switch sig {
case StopSignalNone:
Expand All @@ -93,6 +95,7 @@ func DetermineStageStatus(sig StopSignalType, ori, got model.StageStatus) model.
return model.StageStatus_STAGE_CANCELLED
case StopSignalTimeout:
return model.StageStatus_STAGE_FAILURE
default:
return model.StageStatus_STAGE_FAILURE
}
return model.StageStatus_STAGE_FAILURE
}
72 changes: 72 additions & 0 deletions pkg/app/pipedv1/executor/executor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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 executor

import (
"testing"

"github.com/stretchr/testify/assert"

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

func TestDetermineStageStatus(t *testing.T) {
testcases := []struct {
name string
sig StopSignalType
ori model.StageStatus
got model.StageStatus
expected model.StageStatus
}{
{
name: "No stop signal, should get got status",
sig: StopSignalNone,
ori: model.StageStatus_STAGE_RUNNING,
got: model.StageStatus_STAGE_SUCCESS,
expected: model.StageStatus_STAGE_SUCCESS,
}, {
name: "Terminated signal given, should get original status",
sig: StopSignalTerminate,
ori: model.StageStatus_STAGE_RUNNING,
got: model.StageStatus_STAGE_SKIPPED,
expected: model.StageStatus_STAGE_RUNNING,
}, {
name: "Timeout signal given, should get failed status",
sig: StopSignalTimeout,
ori: model.StageStatus_STAGE_RUNNING,
got: model.StageStatus_STAGE_RUNNING,
expected: model.StageStatus_STAGE_FAILURE,
}, {
name: "Cancel signal given, should get cancelled status",
sig: StopSignalCancel,
ori: model.StageStatus_STAGE_RUNNING,
got: model.StageStatus_STAGE_RUNNING,
expected: model.StageStatus_STAGE_CANCELLED,
}, {
name: "Unknown signal type given, should get failed status",
sig: StopSignalType("unknown"),
ori: model.StageStatus_STAGE_RUNNING,
got: model.StageStatus_STAGE_RUNNING,
expected: model.StageStatus_STAGE_FAILURE,
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
tc.got = DetermineStageStatus(tc.sig, tc.ori, tc.got)
assert.Equal(t, tc.expected, tc.got)
})
}
}
58 changes: 58 additions & 0 deletions pkg/app/pipedv1/executor/stopsignal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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 executor

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewStopSignal(t *testing.T) {
signal, handler := NewStopSignal()
assert.NotNil(t, signal)
assert.NotNil(t, signal.Context())
assert.NotNil(t, signal.Ch())
assert.NotNil(t, handler)
assert.Equal(t, StopSignalNone, signal.Signal())
}

func TestCancel(t *testing.T) {
signal, handler := NewStopSignal()
handler.Cancel()
assert.Equal(t, StopSignalCancel, signal.Signal())
assert.Equal(t, StopSignalCancel, <-signal.Ch())
}

func TestTimeout(t *testing.T) {
signal, handler := NewStopSignal()
handler.Timeout()
assert.Equal(t, StopSignalTimeout, signal.Signal())
assert.Equal(t, StopSignalTimeout, <-signal.Ch())
}

func TestTerminate(t *testing.T) {
signal, handler := NewStopSignal()
handler.Terminate()
assert.Equal(t, StopSignalTerminate, signal.Signal())
assert.Equal(t, StopSignalTerminate, <-signal.Ch())
}

func TestTerminated(t *testing.T) {
signal, handler := NewStopSignal()
assert.False(t, signal.Terminated())
handler.Terminate()
assert.True(t, signal.Terminated())
}

0 comments on commit 9b61c4a

Please sign in to comment.