2022-10-28 15:38:53 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
|
|
|
// Copyright 2019 mhmxs
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2022-11-06 11:44:04 +00:00
|
|
|
package pipeline
|
2022-10-28 15:38:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-06-27 16:01:18 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-02-25 09:37:10 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
2023-06-27 16:01:18 +00:00
|
|
|
|
2024-03-15 17:00:25 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline"
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
2024-02-25 09:37:10 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store/mocks"
|
2022-10-28 15:38:53 +00:00
|
|
|
)
|
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
func mockStoreStep(t *testing.T) store.Store {
|
|
|
|
s := mocks.NewStore(t)
|
|
|
|
s.On("StepUpdate", mock.Anything).Return(nil)
|
|
|
|
return s
|
2022-10-28 15:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateStepStatusNotExited(t *testing.T) {
|
|
|
|
t.Parallel()
|
2024-01-09 17:34:55 +00:00
|
|
|
// step in db before update
|
|
|
|
step := &model.Step{}
|
2022-10-28 15:38:53 +00:00
|
|
|
|
2024-01-09 17:34:55 +00:00
|
|
|
// advertised step status
|
2022-10-28 15:38:53 +00:00
|
|
|
state := rpc.State{
|
|
|
|
Started: int64(42),
|
|
|
|
Exited: false,
|
|
|
|
// Dummy data
|
|
|
|
Finished: int64(1),
|
2024-03-15 17:00:25 +00:00
|
|
|
ExitCode: pipeline.ExitCodeKilled,
|
2022-10-28 15:38:53 +00:00
|
|
|
Error: "not an error",
|
|
|
|
}
|
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
err := UpdateStepStatus(mockStoreStep(t), step, state)
|
2024-01-09 17:34:55 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, model.StatusRunning, step.State)
|
|
|
|
assert.EqualValues(t, 42, step.Started)
|
|
|
|
assert.EqualValues(t, 0, step.Stopped)
|
|
|
|
assert.EqualValues(t, 0, step.ExitCode)
|
|
|
|
assert.EqualValues(t, "", step.Error)
|
2022-10-28 15:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateStepStatusNotExitedButStopped(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-01-09 17:34:55 +00:00
|
|
|
// step in db before update
|
|
|
|
step := &model.Step{Started: 42, Stopped: 64, State: model.StatusKilled}
|
2022-10-28 15:38:53 +00:00
|
|
|
|
2024-01-09 17:34:55 +00:00
|
|
|
// advertised step status
|
2022-10-28 15:38:53 +00:00
|
|
|
state := rpc.State{
|
|
|
|
Exited: false,
|
|
|
|
// Dummy data
|
|
|
|
Finished: int64(1),
|
2024-03-15 17:00:25 +00:00
|
|
|
ExitCode: pipeline.ExitCodeKilled,
|
2022-10-28 15:38:53 +00:00
|
|
|
Error: "not an error",
|
|
|
|
}
|
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
err := UpdateStepStatus(mockStoreStep(t), step, state)
|
2024-01-09 17:34:55 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, model.StatusKilled, step.State)
|
|
|
|
assert.EqualValues(t, 42, step.Started)
|
|
|
|
assert.EqualValues(t, 64, step.Stopped)
|
|
|
|
assert.EqualValues(t, 0, step.ExitCode)
|
|
|
|
assert.EqualValues(t, "", step.Error)
|
2022-10-28 15:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateStepStatusExited(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-01-09 17:34:55 +00:00
|
|
|
// step in db before update
|
|
|
|
step := &model.Step{Started: 42}
|
|
|
|
|
|
|
|
// advertised step status
|
2022-10-28 15:38:53 +00:00
|
|
|
state := rpc.State{
|
|
|
|
Started: int64(42),
|
|
|
|
Exited: true,
|
|
|
|
Finished: int64(34),
|
2024-03-15 17:00:25 +00:00
|
|
|
ExitCode: pipeline.ExitCodeKilled,
|
2022-10-28 15:38:53 +00:00
|
|
|
Error: "an error",
|
|
|
|
}
|
2023-06-27 16:01:18 +00:00
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
err := UpdateStepStatus(mockStoreStep(t), step, state)
|
2023-06-27 16:01:18 +00:00
|
|
|
assert.NoError(t, err)
|
2024-01-09 17:34:55 +00:00
|
|
|
assert.EqualValues(t, model.StatusKilled, step.State)
|
|
|
|
assert.EqualValues(t, 42, step.Started)
|
|
|
|
assert.EqualValues(t, 34, step.Stopped)
|
2024-03-15 17:00:25 +00:00
|
|
|
assert.EqualValues(t, pipeline.ExitCodeKilled, step.ExitCode)
|
2024-01-09 17:34:55 +00:00
|
|
|
assert.EqualValues(t, "an error", step.Error)
|
2022-10-28 15:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateStepStatusExitedButNot137(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-01-09 17:34:55 +00:00
|
|
|
// step in db before update
|
|
|
|
step := &model.Step{Started: 42}
|
|
|
|
|
|
|
|
// advertised step status
|
2022-10-28 15:38:53 +00:00
|
|
|
state := rpc.State{
|
|
|
|
Started: int64(42),
|
|
|
|
Exited: true,
|
|
|
|
Finished: int64(34),
|
|
|
|
Error: "an error",
|
|
|
|
}
|
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
err := UpdateStepStatus(mockStoreStep(t), step, state)
|
2024-01-09 17:34:55 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, model.StatusFailure, step.State)
|
|
|
|
assert.EqualValues(t, 42, step.Started)
|
|
|
|
assert.EqualValues(t, 34, step.Stopped)
|
|
|
|
assert.EqualValues(t, 0, step.ExitCode)
|
|
|
|
assert.EqualValues(t, "an error", step.Error)
|
2022-10-28 15:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateStepStatusExitedWithCode(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-01-09 17:34:55 +00:00
|
|
|
// advertised step status
|
2022-10-28 15:38:53 +00:00
|
|
|
state := rpc.State{
|
|
|
|
Started: int64(42),
|
|
|
|
Exited: true,
|
|
|
|
Finished: int64(34),
|
|
|
|
ExitCode: 1,
|
|
|
|
Error: "an error",
|
|
|
|
}
|
2023-06-27 16:01:18 +00:00
|
|
|
step := &model.Step{}
|
2024-02-25 09:37:10 +00:00
|
|
|
err := UpdateStepStatus(mockStoreStep(t), step, state)
|
2023-06-27 16:01:18 +00:00
|
|
|
assert.NoError(t, err)
|
2022-10-28 15:38:53 +00:00
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, model.StatusFailure, step.State)
|
|
|
|
assert.Equal(t, 1, step.ExitCode)
|
2022-10-28 15:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateStepPToStatusStarted(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
state := rpc.State{Started: int64(42)}
|
2024-02-25 09:37:10 +00:00
|
|
|
step, _ := UpdateStepToStatusStarted(mockStoreStep(t), model.Step{}, state)
|
2022-10-28 15:38:53 +00:00
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, model.StatusRunning, step.State)
|
|
|
|
assert.EqualValues(t, 42, step.Started)
|
2022-10-28 15:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateStepToStatusSkipped(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
step, _ := UpdateStepToStatusSkipped(mockStoreStep(t), model.Step{}, int64(1))
|
2022-10-28 15:38:53 +00:00
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, model.StatusSkipped, step.State)
|
|
|
|
assert.EqualValues(t, 0, step.Stopped)
|
2022-10-28 15:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateStepToStatusSkippedButStarted(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
step := &model.Step{
|
|
|
|
Started: int64(42),
|
|
|
|
}
|
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
step, _ = UpdateStepToStatusSkipped(mockStoreStep(t), *step, int64(1))
|
2022-10-28 15:38:53 +00:00
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, model.StatusSuccess, step.State)
|
|
|
|
assert.EqualValues(t, 1, step.Stopped)
|
2022-10-28 15:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateStepStatusToDoneSkipped(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
state := rpc.State{
|
|
|
|
Finished: int64(34),
|
|
|
|
}
|
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
step, _ := UpdateStepStatusToDone(mockStoreStep(t), model.Step{}, state)
|
2022-10-28 15:38:53 +00:00
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, model.StatusSkipped, step.State)
|
|
|
|
assert.EqualValues(t, 34, step.Stopped)
|
|
|
|
assert.Empty(t, step.Error)
|
|
|
|
assert.Equal(t, 0, step.ExitCode)
|
2022-10-28 15:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateStepStatusToDoneSuccess(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
state := rpc.State{
|
|
|
|
Started: int64(42),
|
|
|
|
Finished: int64(34),
|
|
|
|
}
|
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
step, _ := UpdateStepStatusToDone(mockStoreStep(t), model.Step{}, state)
|
2022-10-28 15:38:53 +00:00
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, model.StatusSuccess, step.State)
|
|
|
|
assert.EqualValues(t, 34, step.Stopped)
|
|
|
|
assert.Empty(t, step.Error)
|
|
|
|
assert.Equal(t, 0, step.ExitCode)
|
2022-10-28 15:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateStepStatusToDoneFailureWithError(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
state := rpc.State{Error: "an error"}
|
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
step, _ := UpdateStepStatusToDone(mockStoreStep(t), model.Step{}, state)
|
2022-10-28 15:38:53 +00:00
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, model.StatusFailure, step.State)
|
2022-10-28 15:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateStepStatusToDoneFailureWithExitCode(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
state := rpc.State{ExitCode: 43}
|
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
step, _ := UpdateStepStatusToDone(mockStoreStep(t), model.Step{}, state)
|
2022-10-28 15:38:53 +00:00
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, model.StatusFailure, step.State)
|
2022-10-28 15:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateStepToStatusKilledStarted(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
step, _ := UpdateStepToStatusKilled(mockStoreStep(t), model.Step{})
|
2022-10-28 15:38:53 +00:00
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, model.StatusKilled, step.State)
|
|
|
|
assert.LessOrEqual(t, now, step.Stopped)
|
|
|
|
assert.Equal(t, step.Stopped, step.Started)
|
|
|
|
assert.Equal(t, 137, step.ExitCode)
|
2022-10-28 15:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateStepToStatusKilledNotStarted(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
step, _ := UpdateStepToStatusKilled(mockStoreStep(t), model.Step{Started: int64(1)})
|
2022-10-28 15:38:53 +00:00
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.EqualValues(t, 1, step.Started)
|
2022-10-28 15:38:53 +00:00
|
|
|
}
|