2022-10-18 01:24:12 +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-18 01:24:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-02-25 09:37:10 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
2024-01-14 18:33:58 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"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-18 01:24:12 +00:00
|
|
|
)
|
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
func mockStorePipeline(t *testing.T) store.Store {
|
|
|
|
s := mocks.NewStore(t)
|
|
|
|
s.On("UpdatePipeline", mock.Anything).Return(nil)
|
|
|
|
return s
|
2022-10-18 01:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateToStatusRunning(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
pipeline, _ := UpdateToStatusRunning(mockStorePipeline(t), model.Pipeline{}, int64(1))
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, model.StatusRunning, pipeline.Status)
|
|
|
|
assert.EqualValues(t, 1, pipeline.Started)
|
2022-10-18 01:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateToStatusPending(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
pipeline, _ := UpdateToStatusPending(mockStorePipeline(t), model.Pipeline{}, "Reviewer")
|
2022-10-18 01:24:12 +00:00
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, model.StatusPending, pipeline.Status)
|
|
|
|
assert.Equal(t, "Reviewer", pipeline.Reviewer)
|
|
|
|
assert.LessOrEqual(t, now, pipeline.Reviewed)
|
2022-10-18 01:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateToStatusDeclined(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
pipeline, _ := UpdateToStatusDeclined(mockStorePipeline(t), model.Pipeline{}, "Reviewer")
|
2022-10-18 01:24:12 +00:00
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, model.StatusDeclined, pipeline.Status)
|
|
|
|
assert.Equal(t, "Reviewer", pipeline.Reviewer)
|
|
|
|
assert.LessOrEqual(t, now, pipeline.Reviewed)
|
2022-10-18 01:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateToStatusToDone(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
pipeline, _ := UpdateStatusToDone(mockStorePipeline(t), model.Pipeline{}, "status", int64(1))
|
2022-10-18 01:24:12 +00:00
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, model.StatusValue("status"), pipeline.Status)
|
|
|
|
assert.EqualValues(t, 1, pipeline.Finished)
|
2022-10-18 01:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateToStatusError(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
pipeline, _ := UpdateToStatusError(mockStorePipeline(t), model.Pipeline{}, errors.New("this is an error"))
|
2022-10-18 01:24:12 +00:00
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Len(t, pipeline.Errors, 1)
|
|
|
|
assert.Equal(t, "[generic] this is an error", pipeline.Errors[0].Error())
|
|
|
|
assert.Equal(t, model.StatusError, pipeline.Status)
|
|
|
|
assert.Equal(t, pipeline.Started, pipeline.Finished)
|
|
|
|
assert.LessOrEqual(t, now, pipeline.Started)
|
|
|
|
assert.Equal(t, pipeline.Started, pipeline.Finished)
|
2022-10-18 01:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateToStatusKilled(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
|
2024-02-25 09:37:10 +00:00
|
|
|
pipeline, _ := UpdateToStatusKilled(mockStorePipeline(t), model.Pipeline{})
|
2022-10-18 01:24:12 +00:00
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, model.StatusKilled, pipeline.Status)
|
|
|
|
assert.LessOrEqual(t, now, pipeline.Finished)
|
2022-10-18 01:24:12 +00:00
|
|
|
}
|