2022-10-18 01:24:12 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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.
|
|
|
|
|
2017-03-28 08:53:06 +00:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2023-11-07 07:04:33 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/server/store/types"
|
2017-03-28 08:53:06 +00:00
|
|
|
)
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
func TestStepFind(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Step), new(model.Pipeline))
|
2023-06-27 16:01:18 +00:00
|
|
|
sess := store.engine.NewSession()
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
defer closer()
|
2017-03-28 08:53:06 +00:00
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
steps := []*model.Step{
|
2017-03-28 08:53:06 +00:00
|
|
|
{
|
2023-06-06 07:52:08 +00:00
|
|
|
UUID: "8d89104f-d44e-4b45-b86e-17f8b5e74a0e",
|
2022-10-18 01:24:12 +00:00
|
|
|
PipelineID: 1000,
|
|
|
|
PID: 1,
|
|
|
|
PPID: 2,
|
|
|
|
Name: "build",
|
|
|
|
State: model.StatusSuccess,
|
|
|
|
Error: "pc load letter",
|
|
|
|
ExitCode: 255,
|
2017-03-28 08:53:06 +00:00
|
|
|
},
|
|
|
|
}
|
2023-06-27 16:01:18 +00:00
|
|
|
assert.NoError(t, store.stepCreate(sess, steps))
|
2022-10-28 15:38:53 +00:00
|
|
|
assert.EqualValues(t, 1, steps[0].ID)
|
2023-06-27 16:01:18 +00:00
|
|
|
assert.Error(t, store.stepCreate(sess, steps))
|
|
|
|
assert.NoError(t, sess.Close())
|
2017-03-28 08:53:06 +00:00
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
step, err := store.StepFind(&model.Pipeline{ID: 1000}, 1)
|
2021-11-13 19:18:06 +00:00
|
|
|
if !assert.NoError(t, err) {
|
2017-03-28 08:53:06 +00:00
|
|
|
return
|
|
|
|
}
|
2022-10-28 15:38:53 +00:00
|
|
|
assert.Equal(t, steps[0], step)
|
2017-03-28 08:53:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
func TestStepChild(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Step), new(model.Pipeline))
|
2021-11-13 19:18:06 +00:00
|
|
|
defer closer()
|
2017-03-28 08:53:06 +00:00
|
|
|
|
2023-06-27 16:01:18 +00:00
|
|
|
sess := store.engine.NewSession()
|
|
|
|
err := store.stepCreate(sess, []*model.Step{
|
2017-03-28 08:53:06 +00:00
|
|
|
{
|
2023-06-06 07:52:08 +00:00
|
|
|
UUID: "ea6d4008-8ace-4f8a-ad03-53f1756465d9",
|
2022-10-18 01:24:12 +00:00
|
|
|
PipelineID: 1,
|
|
|
|
PID: 1,
|
|
|
|
PPID: 1,
|
|
|
|
State: "success",
|
2017-03-28 08:53:06 +00:00
|
|
|
},
|
|
|
|
{
|
2023-06-06 07:52:08 +00:00
|
|
|
UUID: "2bf387f7-2913-4907-814c-c9ada88707c0",
|
2022-10-18 01:24:12 +00:00
|
|
|
PipelineID: 1,
|
|
|
|
PID: 2,
|
|
|
|
PPID: 1,
|
|
|
|
Name: "build",
|
|
|
|
State: "success",
|
2017-03-28 08:53:06 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-10-28 15:38:53 +00:00
|
|
|
t.Errorf("Unexpected error: insert steps: %s", err)
|
2017-03-28 08:53:06 +00:00
|
|
|
return
|
|
|
|
}
|
2023-06-27 16:01:18 +00:00
|
|
|
_ = sess.Commit()
|
2022-10-28 15:38:53 +00:00
|
|
|
step, err := store.StepChild(&model.Pipeline{ID: 1}, 1, "build")
|
2017-03-28 08:53:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
if got, want := step.PID, 2; got != want {
|
|
|
|
t.Errorf("Want step pid %d, got %d", want, got)
|
2017-03-28 08:53:06 +00:00
|
|
|
}
|
2022-10-28 15:38:53 +00:00
|
|
|
if got, want := step.Name, "build"; got != want {
|
|
|
|
t.Errorf("Want step name %s, got %s", want, got)
|
2017-03-28 08:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
func TestStepList(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Step), new(model.Pipeline))
|
2021-11-13 19:18:06 +00:00
|
|
|
defer closer()
|
2017-03-28 08:53:06 +00:00
|
|
|
|
2023-06-27 16:01:18 +00:00
|
|
|
sess := store.engine.NewSession()
|
|
|
|
err := store.stepCreate(sess, []*model.Step{
|
2017-03-28 08:53:06 +00:00
|
|
|
{
|
2023-06-06 07:52:08 +00:00
|
|
|
UUID: "2bf387f7-2913-4907-814c-c9ada88707c0",
|
2022-10-18 01:24:12 +00:00
|
|
|
PipelineID: 2,
|
|
|
|
PID: 1,
|
|
|
|
PPID: 1,
|
|
|
|
State: "success",
|
2017-03-28 08:53:06 +00:00
|
|
|
},
|
|
|
|
{
|
2023-06-06 07:52:08 +00:00
|
|
|
UUID: "4b04073c-1827-4aa4-a5f5-c7b21c5e44a6",
|
2022-10-18 01:24:12 +00:00
|
|
|
PipelineID: 1,
|
|
|
|
PID: 1,
|
|
|
|
PPID: 1,
|
|
|
|
State: "success",
|
2017-03-28 08:53:06 +00:00
|
|
|
},
|
|
|
|
{
|
2023-06-06 07:52:08 +00:00
|
|
|
UUID: "40aab045-970b-4892-b6df-6f825a7ec97a",
|
2022-10-18 01:24:12 +00:00
|
|
|
PipelineID: 1,
|
|
|
|
PID: 2,
|
|
|
|
PPID: 1,
|
|
|
|
Name: "build",
|
|
|
|
State: "success",
|
2017-03-28 08:53:06 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-10-28 15:38:53 +00:00
|
|
|
t.Errorf("Unexpected error: insert steps: %s", err)
|
2017-03-28 08:53:06 +00:00
|
|
|
return
|
|
|
|
}
|
2023-06-27 16:01:18 +00:00
|
|
|
_ = sess.Commit()
|
2022-10-28 15:38:53 +00:00
|
|
|
steps, err := store.StepList(&model.Pipeline{ID: 1})
|
2017-03-28 08:53:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2022-10-28 15:38:53 +00:00
|
|
|
if got, want := len(steps), 2; got != want {
|
|
|
|
t.Errorf("Want %d steps, got %d", want, got)
|
2017-03-28 08:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
func TestStepUpdate(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Step), new(model.Pipeline))
|
2021-11-13 19:18:06 +00:00
|
|
|
defer closer()
|
2017-03-28 08:53:06 +00:00
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
step := &model.Step{
|
2023-06-06 07:52:08 +00:00
|
|
|
UUID: "fc7c7fd6-553e-480b-8ed7-30d8563d0b79",
|
2022-10-18 01:24:12 +00:00
|
|
|
PipelineID: 1,
|
|
|
|
PID: 1,
|
|
|
|
PPID: 2,
|
|
|
|
Name: "build",
|
|
|
|
State: "pending",
|
|
|
|
Error: "pc load letter",
|
|
|
|
ExitCode: 255,
|
2017-03-28 08:53:06 +00:00
|
|
|
}
|
2023-06-27 16:01:18 +00:00
|
|
|
sess := store.engine.NewSession()
|
|
|
|
if err := store.stepCreate(sess, []*model.Step{step}); err != nil {
|
2022-10-28 15:38:53 +00:00
|
|
|
t.Errorf("Unexpected error: insert step: %s", err)
|
2017-03-28 08:53:06 +00:00
|
|
|
return
|
|
|
|
}
|
2023-06-27 16:01:18 +00:00
|
|
|
_ = sess.Commit()
|
2022-10-28 15:38:53 +00:00
|
|
|
step.State = "running"
|
|
|
|
if err := store.StepUpdate(step); err != nil {
|
|
|
|
t.Errorf("Unexpected error: update step: %s", err)
|
2017-03-28 08:53:06 +00:00
|
|
|
return
|
|
|
|
}
|
2022-10-28 15:38:53 +00:00
|
|
|
updated, err := store.StepFind(&model.Pipeline{ID: 1}, 1)
|
2017-03-28 08:53:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2021-11-22 11:55:13 +00:00
|
|
|
if got, want := updated.State, model.StatusRunning; got != want {
|
2022-10-28 15:38:53 +00:00
|
|
|
t.Errorf("Want step name %s, got %s", want, got)
|
2017-03-28 08:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
func TestStepIndexes(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Step), new(model.Pipeline))
|
2021-11-13 19:18:06 +00:00
|
|
|
defer closer()
|
2017-03-28 08:53:06 +00:00
|
|
|
|
2023-06-27 16:01:18 +00:00
|
|
|
sess := store.engine.NewSession()
|
2023-07-08 18:09:53 +00:00
|
|
|
defer sess.Close()
|
|
|
|
|
2023-06-27 16:01:18 +00:00
|
|
|
if err := store.stepCreate(sess, []*model.Step{
|
2017-03-28 08:53:06 +00:00
|
|
|
{
|
2023-06-06 07:52:08 +00:00
|
|
|
UUID: "4db7e5fc-5312-4d02-9e14-b51b9e3242cc",
|
2022-10-18 01:24:12 +00:00
|
|
|
PipelineID: 1,
|
|
|
|
PID: 1,
|
|
|
|
PPID: 1,
|
|
|
|
State: "running",
|
|
|
|
Name: "build",
|
2017-03-28 08:53:06 +00:00
|
|
|
},
|
|
|
|
}); err != nil {
|
2022-10-28 15:38:53 +00:00
|
|
|
t.Errorf("Unexpected error: insert steps: %s", err)
|
2017-03-28 08:53:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// fail due to duplicate pid
|
2023-06-27 16:01:18 +00:00
|
|
|
if err := store.stepCreate(sess, []*model.Step{
|
2017-03-28 08:53:06 +00:00
|
|
|
{
|
2023-06-06 07:52:08 +00:00
|
|
|
UUID: "c1f33a9e-2a02-4579-95ec-90255d785a12",
|
2022-10-18 01:24:12 +00:00
|
|
|
PipelineID: 1,
|
|
|
|
PID: 1,
|
|
|
|
PPID: 1,
|
|
|
|
State: "success",
|
|
|
|
Name: "clone",
|
2017-03-28 08:53:06 +00:00
|
|
|
},
|
|
|
|
}); err == nil {
|
2021-10-08 16:35:56 +00:00
|
|
|
t.Errorf("Unexpected error: duplicate pid")
|
2017-03-28 08:53:06 +00:00
|
|
|
}
|
2023-06-06 07:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepByUUID(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Step), new(model.Pipeline))
|
|
|
|
defer closer()
|
|
|
|
|
2023-06-27 16:01:18 +00:00
|
|
|
sess := store.engine.NewSession()
|
|
|
|
assert.NoError(t, store.stepCreate(sess, []*model.Step{
|
2023-06-06 07:52:08 +00:00
|
|
|
{
|
|
|
|
UUID: "4db7e5fc-5312-4d02-9e14-b51b9e3242cc",
|
|
|
|
PipelineID: 1,
|
|
|
|
PID: 1,
|
|
|
|
PPID: 1,
|
|
|
|
State: "running",
|
|
|
|
Name: "build",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
UUID: "fc7c7fd6-553e-480b-8ed7-30d8563d0b79",
|
|
|
|
PipelineID: 4,
|
|
|
|
PID: 6,
|
|
|
|
PPID: 7,
|
|
|
|
Name: "build",
|
|
|
|
State: "pending",
|
|
|
|
Error: "pc load letter",
|
|
|
|
ExitCode: 255,
|
|
|
|
},
|
|
|
|
}))
|
2023-06-27 16:01:18 +00:00
|
|
|
_ = sess.Close()
|
2023-06-06 07:52:08 +00:00
|
|
|
|
|
|
|
step, err := store.StepByUUID("4db7e5fc-5312-4d02-9e14-b51b9e3242cc")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, step)
|
|
|
|
|
|
|
|
step, err = store.StepByUUID("52feb6f5-8ce2-40c0-9937-9d0e3349c98c")
|
|
|
|
assert.ErrorIs(t, err, types.RecordNotExist)
|
|
|
|
assert.Empty(t, step)
|
2017-03-28 08:53:06 +00:00
|
|
|
}
|
|
|
|
|
2023-10-28 11:37:54 +00:00
|
|
|
func TestStepLoad(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Step))
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
sess := store.engine.NewSession()
|
|
|
|
assert.NoError(t, store.stepCreate(sess, []*model.Step{
|
|
|
|
{
|
|
|
|
UUID: "4db7e5fc-5312-4d02-9e14-b51b9e3242cc",
|
|
|
|
PipelineID: 1,
|
|
|
|
PID: 1,
|
|
|
|
PPID: 1,
|
|
|
|
State: "running",
|
|
|
|
Name: "build",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
UUID: "fc7c7fd6-553e-480b-8ed7-30d8563d0b79",
|
|
|
|
PipelineID: 4,
|
|
|
|
PID: 6,
|
|
|
|
PPID: 7,
|
|
|
|
Name: "build",
|
|
|
|
State: "pending",
|
|
|
|
Error: "pc load letter",
|
|
|
|
ExitCode: 255,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
_ = sess.Close()
|
|
|
|
|
|
|
|
step, err := store.StepLoad(1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotEmpty(t, step)
|
|
|
|
assert.Equal(t, step.UUID, "4db7e5fc-5312-4d02-9e14-b51b9e3242cc")
|
|
|
|
|
|
|
|
step, err = store.StepLoad(5)
|
|
|
|
assert.ErrorIs(t, err, types.RecordNotExist)
|
|
|
|
assert.Empty(t, step)
|
|
|
|
}
|