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-05-05 16:59:37 +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"
|
2017-05-05 16:59:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfig(t *testing.T) {
|
2022-10-18 01:24:12 +00:00
|
|
|
store, closer := newTestStore(t, new(model.Config), new(model.PipelineConfig), new(model.Pipeline), new(model.Repo))
|
2021-11-13 19:18:06 +00:00
|
|
|
defer closer()
|
2017-05-05 16:59:37 +00:00
|
|
|
|
|
|
|
var (
|
2019-06-14 07:20:46 +00:00
|
|
|
data = "pipeline: [ { image: golang, commands: [ go build, go test ] } ]"
|
|
|
|
hash = "8d8647c9aa90d893bfb79dddbe901f03e258588121e5202632f8ae5738590b26"
|
2017-05-05 16:59:37 +00:00
|
|
|
)
|
|
|
|
|
2019-06-14 07:20:46 +00:00
|
|
|
repo := &model.Repo{
|
|
|
|
UserID: 1,
|
2021-10-02 08:59:34 +00:00
|
|
|
FullName: "bradrydzewski/test",
|
2019-06-14 07:20:46 +00:00
|
|
|
Owner: "bradrydzewski",
|
2021-10-02 08:59:34 +00:00
|
|
|
Name: "test",
|
2019-06-14 07:20:46 +00:00
|
|
|
}
|
2021-11-13 19:18:06 +00:00
|
|
|
if err := store.CreateRepo(repo); err != nil {
|
2019-06-14 07:20:46 +00:00
|
|
|
t.Errorf("Unexpected error: insert repo: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-11 08:50:50 +00:00
|
|
|
config := &model.Config{
|
2019-06-14 07:20:46 +00:00
|
|
|
RepoID: repo.ID,
|
2021-11-13 19:18:06 +00:00
|
|
|
Data: []byte(data),
|
2019-06-11 08:50:50 +00:00
|
|
|
Hash: hash,
|
|
|
|
Name: "default",
|
|
|
|
}
|
2021-11-13 19:18:06 +00:00
|
|
|
if err := store.ConfigCreate(config); err != nil {
|
2019-06-11 08:50:50 +00:00
|
|
|
t.Errorf("Unexpected error: insert config: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline := &model.Pipeline{
|
2019-06-14 07:20:46 +00:00
|
|
|
RepoID: repo.ID,
|
|
|
|
Status: model.StatusRunning,
|
|
|
|
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
if err := store.CreatePipeline(pipeline); err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert pipeline: %s", err)
|
2019-06-14 07:20:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
if err := store.PipelineConfigCreate(
|
|
|
|
&model.PipelineConfig{
|
|
|
|
ConfigID: config.ID,
|
|
|
|
PipelineID: pipeline.ID,
|
2017-05-05 16:59:37 +00:00
|
|
|
},
|
|
|
|
); err != nil {
|
2022-10-18 01:24:12 +00:00
|
|
|
t.Errorf("Unexpected error: insert pipeline config: %s", err)
|
2017-05-05 16:59:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
config, err := store.ConfigFindIdentical(repo.ID, hash)
|
2017-05-05 16:59:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got, want := config.ID, int64(1); got != want {
|
|
|
|
t.Errorf("Want config id %d, got %d", want, got)
|
|
|
|
}
|
2019-06-14 07:20:46 +00:00
|
|
|
if got, want := config.RepoID, repo.ID; got != want {
|
2017-05-05 16:59:37 +00:00
|
|
|
t.Errorf("Want config repo id %d, got %d", want, got)
|
|
|
|
}
|
2021-11-13 19:18:06 +00:00
|
|
|
if got, want := string(config.Data), data; got != want {
|
2017-05-05 16:59:37 +00:00
|
|
|
t.Errorf("Want config data %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := config.Hash, hash; got != want {
|
|
|
|
t.Errorf("Want config hash %s, got %s", want, got)
|
|
|
|
}
|
2019-06-07 08:40:16 +00:00
|
|
|
if got, want := config.Name, "default"; got != want {
|
|
|
|
t.Errorf("Want config name %s, got %s", want, got)
|
|
|
|
}
|
2017-05-05 16:59:37 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
loaded, err := store.ConfigsForPipeline(pipeline.ID)
|
2017-05-05 16:59:37 +00:00
|
|
|
if err != nil {
|
2017-05-05 18:05:42 +00:00
|
|
|
t.Errorf("Want config by id, got error %q", err)
|
2017-05-05 16:59:37 +00:00
|
|
|
return
|
|
|
|
}
|
2019-06-07 08:40:16 +00:00
|
|
|
if got, want := loaded[0].ID, config.ID; got != want {
|
2017-05-05 18:05:42 +00:00
|
|
|
t.Errorf("Want config by id %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
}
|
2017-05-05 16:59:37 +00:00
|
|
|
|
2017-05-05 18:05:42 +00:00
|
|
|
func TestConfigApproved(t *testing.T) {
|
2022-10-18 01:24:12 +00:00
|
|
|
store, closer := newTestStore(t, new(model.Config), new(model.PipelineConfig), new(model.Pipeline), new(model.Repo))
|
2021-11-13 19:18:06 +00:00
|
|
|
defer closer()
|
2017-05-05 18:05:42 +00:00
|
|
|
|
2017-05-23 15:53:40 +00:00
|
|
|
repo := &model.Repo{
|
|
|
|
UserID: 1,
|
2021-10-02 08:59:34 +00:00
|
|
|
FullName: "bradrydzewski/test",
|
2017-05-23 15:53:40 +00:00
|
|
|
Owner: "bradrydzewski",
|
2021-10-02 08:59:34 +00:00
|
|
|
Name: "test",
|
2017-05-23 15:53:40 +00:00
|
|
|
}
|
2021-11-13 19:18:06 +00:00
|
|
|
if err := store.CreateRepo(repo); err != nil {
|
2019-06-14 07:20:46 +00:00
|
|
|
t.Errorf("Unexpected error: insert repo: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2017-05-23 15:53:40 +00:00
|
|
|
|
2017-05-05 18:05:42 +00:00
|
|
|
var (
|
2022-10-18 01:24:12 +00:00
|
|
|
data = "pipeline: [ { image: golang, commands: [ go build, go test ] } ]"
|
|
|
|
hash = "8d8647c9aa90d893bfb79dddbe901f03e258588121e5202632f8ae5738590b26"
|
|
|
|
pipelineBlocked = &model.Pipeline{
|
2017-05-23 15:53:40 +00:00
|
|
|
RepoID: repo.ID,
|
2019-06-07 08:40:16 +00:00
|
|
|
Status: model.StatusBlocked,
|
|
|
|
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
pipelinePending = &model.Pipeline{
|
2019-06-07 08:40:16 +00:00
|
|
|
RepoID: repo.ID,
|
|
|
|
Status: model.StatusPending,
|
|
|
|
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
pipelineRunning = &model.Pipeline{
|
2019-06-07 08:40:16 +00:00
|
|
|
RepoID: repo.ID,
|
|
|
|
Status: model.StatusRunning,
|
|
|
|
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
|
2017-05-05 18:05:42 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
if err := store.CreatePipeline(pipelineBlocked); err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert pipeline: %s", err)
|
2019-06-14 07:20:46 +00:00
|
|
|
return
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
if err := store.CreatePipeline(pipelinePending); err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert pipeline: %s", err)
|
2019-06-14 07:20:46 +00:00
|
|
|
return
|
|
|
|
}
|
2019-06-07 08:40:16 +00:00
|
|
|
conf := &model.Config{
|
2019-06-11 08:50:50 +00:00
|
|
|
RepoID: repo.ID,
|
2021-11-13 19:18:06 +00:00
|
|
|
Data: []byte(data),
|
2019-06-11 08:50:50 +00:00
|
|
|
Hash: hash,
|
2019-06-07 08:40:16 +00:00
|
|
|
}
|
2021-11-13 19:18:06 +00:00
|
|
|
if err := store.ConfigCreate(conf); err != nil {
|
2019-06-14 07:20:46 +00:00
|
|
|
t.Errorf("Unexpected error: insert config: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
pipelineConfig := &model.PipelineConfig{
|
|
|
|
ConfigID: conf.ID,
|
|
|
|
PipelineID: pipelineBlocked.ID,
|
2019-06-11 08:50:50 +00:00
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
if err := store.PipelineConfigCreate(pipelineConfig); err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert pipeline_config: %s", err)
|
2017-05-05 16:59:37 +00:00
|
|
|
return
|
|
|
|
}
|
2019-06-07 08:40:16 +00:00
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
approved, err := store.ConfigFindApproved(conf)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if approved != false {
|
|
|
|
t.Errorf("Want config not approved, when blocked or pending.")
|
2017-05-05 18:05:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
assert.NoError(t, store.CreatePipeline(pipelineRunning))
|
2019-06-07 08:40:16 +00:00
|
|
|
conf2 := &model.Config{
|
2019-06-11 08:50:50 +00:00
|
|
|
RepoID: repo.ID,
|
2021-11-13 19:18:06 +00:00
|
|
|
Data: []byte(data),
|
2019-06-11 08:50:50 +00:00
|
|
|
Hash: "xxx",
|
|
|
|
}
|
2021-11-13 19:18:06 +00:00
|
|
|
if err := store.ConfigCreate(conf2); err != nil {
|
2019-06-14 07:20:46 +00:00
|
|
|
t.Errorf("Unexpected error: insert config: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
pipelineConfig2 := &model.PipelineConfig{
|
|
|
|
ConfigID: conf2.ID,
|
|
|
|
PipelineID: pipelineRunning.ID,
|
2019-06-07 08:40:16 +00:00
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
if err := store.PipelineConfigCreate(pipelineConfig2); err != nil {
|
2019-06-07 08:40:16 +00:00
|
|
|
t.Errorf("Unexpected error: insert config: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2017-05-05 18:05:42 +00:00
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
if approved, err := store.ConfigFindApproved(conf2); approved != true || err != nil {
|
2019-06-07 08:40:16 +00:00
|
|
|
t.Errorf("Want config approved, when running. %v", err)
|
2017-05-05 18:05:42 +00:00
|
|
|
return
|
2017-05-05 16:59:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-05 18:05:42 +00:00
|
|
|
func TestConfigIndexes(t *testing.T) {
|
2022-10-28 15:38:53 +00:00
|
|
|
store, closer := newTestStore(t, new(model.Config), new(model.Step), new(model.Pipeline), new(model.Repo))
|
2021-11-13 19:18:06 +00:00
|
|
|
defer closer()
|
2017-05-05 18:05:42 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
data = "pipeline: [ { image: golang, commands: [ go build, go test ] } ]"
|
|
|
|
hash = "8d8647c9aa90d893bfb79dddbe901f03e258588121e5202632f8ae5738590b26"
|
|
|
|
)
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
if err := store.ConfigCreate(
|
2017-05-05 18:05:42 +00:00
|
|
|
&model.Config{
|
|
|
|
RepoID: 2,
|
2021-11-13 19:18:06 +00:00
|
|
|
Data: []byte(data),
|
2017-05-05 18:05:42 +00:00
|
|
|
Hash: hash,
|
|
|
|
},
|
|
|
|
); err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert config: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// fail due to duplicate sha
|
2021-11-13 19:18:06 +00:00
|
|
|
if err := store.ConfigCreate(
|
2017-05-05 18:05:42 +00:00
|
|
|
&model.Config{
|
|
|
|
RepoID: 2,
|
2021-11-13 19:18:06 +00:00
|
|
|
Data: []byte(data),
|
2017-05-05 18:05:42 +00:00
|
|
|
Hash: hash,
|
|
|
|
},
|
|
|
|
); err == nil {
|
2021-10-08 16:35:56 +00:00
|
|
|
t.Errorf("Unexpected error: duplicate sha")
|
2017-05-05 18:05:42 +00:00
|
|
|
}
|
|
|
|
}
|