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"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
2022-10-18 01:24:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockUpdatePipelineStore struct{}
|
|
|
|
|
2023-03-18 19:35:27 +00:00
|
|
|
func (m *mockUpdatePipelineStore) UpdatePipeline(_ *model.Pipeline) error {
|
2022-10-18 01:24:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateToStatusRunning(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
pipeline, _ := UpdateToStatusRunning(&mockUpdatePipelineStore{}, model.Pipeline{}, int64(1))
|
|
|
|
|
|
|
|
if model.StatusRunning != pipeline.Status {
|
|
|
|
t.Errorf("Pipeline status not equals '%s' != '%s'", model.StatusRunning, pipeline.Status)
|
|
|
|
} else if int64(1) != pipeline.Started {
|
|
|
|
t.Errorf("Pipeline started not equals 1 != %d", pipeline.Started)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateToStatusPending(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
|
|
|
|
pipeline, _ := UpdateToStatusPending(&mockUpdatePipelineStore{}, model.Pipeline{}, "Reviewer")
|
|
|
|
|
2024-01-10 14:34:44 +00:00
|
|
|
switch {
|
|
|
|
case model.StatusPending != pipeline.Status:
|
2022-10-18 01:24:12 +00:00
|
|
|
t.Errorf("Pipeline status not equals '%s' != '%s'", model.StatusPending, pipeline.Status)
|
2024-01-10 14:34:44 +00:00
|
|
|
case pipeline.Reviewer != "Reviewer":
|
2022-10-18 01:24:12 +00:00
|
|
|
t.Errorf("Reviewer not equals 'Reviewer' != '%s'", pipeline.Reviewer)
|
2024-01-10 14:34:44 +00:00
|
|
|
case now > pipeline.Reviewed:
|
2022-10-18 01:24:12 +00:00
|
|
|
t.Errorf("Reviewed not updated %d !< %d", now, pipeline.Reviewed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateToStatusDeclined(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
|
|
|
|
pipeline, _ := UpdateToStatusDeclined(&mockUpdatePipelineStore{}, model.Pipeline{}, "Reviewer")
|
|
|
|
|
2024-01-10 14:34:44 +00:00
|
|
|
switch {
|
|
|
|
case model.StatusDeclined != pipeline.Status:
|
2022-10-18 01:24:12 +00:00
|
|
|
t.Errorf("Pipeline status not equals '%s' != '%s'", model.StatusDeclined, pipeline.Status)
|
2024-01-10 14:34:44 +00:00
|
|
|
case pipeline.Reviewer != "Reviewer":
|
2022-10-18 01:24:12 +00:00
|
|
|
t.Errorf("Reviewer not equals 'Reviewer' != '%s'", pipeline.Reviewer)
|
2024-01-10 14:34:44 +00:00
|
|
|
case now > pipeline.Reviewed:
|
2022-10-18 01:24:12 +00:00
|
|
|
t.Errorf("Reviewed not updated %d !< %d", now, pipeline.Reviewed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateToStatusToDone(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
pipeline, _ := UpdateStatusToDone(&mockUpdatePipelineStore{}, model.Pipeline{}, "status", int64(1))
|
|
|
|
|
|
|
|
if pipeline.Status != "status" {
|
|
|
|
t.Errorf("Pipeline status not equals 'status' != '%s'", pipeline.Status)
|
|
|
|
} else if int64(1) != pipeline.Finished {
|
|
|
|
t.Errorf("Pipeline finished not equals 1 != %d", pipeline.Finished)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateToStatusError(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
|
2023-11-03 10:44:03 +00:00
|
|
|
pipeline, _ := UpdateToStatusError(&mockUpdatePipelineStore{}, model.Pipeline{}, errors.New("this is an error"))
|
2022-10-18 01:24:12 +00:00
|
|
|
|
2024-01-10 14:34:44 +00:00
|
|
|
switch {
|
|
|
|
case len(pipeline.Errors) != 1:
|
2023-11-03 10:44:03 +00:00
|
|
|
t.Errorf("Expected one error, got %d", len(pipeline.Errors))
|
2024-01-10 14:34:44 +00:00
|
|
|
case pipeline.Errors[0].Error() != "[generic] this is an error":
|
2023-11-03 10:44:03 +00:00
|
|
|
t.Errorf("Pipeline error not equals '[generic] this is an error' != '%s'", pipeline.Errors[0].Error())
|
2024-01-10 14:34:44 +00:00
|
|
|
case model.StatusError != pipeline.Status:
|
2022-10-18 01:24:12 +00:00
|
|
|
t.Errorf("Pipeline status not equals '%s' != '%s'", model.StatusError, pipeline.Status)
|
2024-01-10 14:34:44 +00:00
|
|
|
case now > pipeline.Started:
|
2022-10-18 01:24:12 +00:00
|
|
|
t.Errorf("Started not updated %d !< %d", now, pipeline.Started)
|
2024-01-10 14:34:44 +00:00
|
|
|
case pipeline.Started != pipeline.Finished:
|
2022-10-18 01:24:12 +00:00
|
|
|
t.Errorf("Pipeline started and finished not equals %d != %d", pipeline.Started, pipeline.Finished)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateToStatusKilled(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
now := time.Now().Unix()
|
|
|
|
|
|
|
|
pipeline, _ := UpdateToStatusKilled(&mockUpdatePipelineStore{}, model.Pipeline{})
|
|
|
|
|
|
|
|
if model.StatusKilled != pipeline.Status {
|
|
|
|
t.Errorf("Pipeline status not equals '%s' != '%s'", model.StatusKilled, pipeline.Status)
|
|
|
|
} else if now > pipeline.Finished {
|
|
|
|
t.Errorf("Finished not updated %d !< %d", now, pipeline.Finished)
|
|
|
|
}
|
|
|
|
}
|