woodpecker/server/store/datastore/config_test.go

233 lines
6.1 KiB
Go
Raw Normal View History

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"
"github.com/stretchr/testify/assert"
"go.woodpecker-ci.org/woodpecker/server/model"
2017-05-05 16:59:37 +00:00
)
func TestConfig(t *testing.T) {
store, closer := newTestStore(t, new(model.Config), new(model.PipelineConfig), new(model.Pipeline), new(model.Repo))
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,
FullName: "bradrydzewski/test",
2019-06-14 07:20:46 +00:00
Owner: "bradrydzewski",
Name: "test",
2019-06-14 07:20:46 +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
}
config := &model.Config{
2019-06-14 07:20:46 +00:00
RepoID: repo.ID,
Data: []byte(data),
Hash: hash,
Name: "default",
}
if err := store.ConfigCreate(config); err != nil {
t.Errorf("Unexpected error: insert config: %s", err)
return
}
pipeline := &model.Pipeline{
2019-06-14 07:20:46 +00:00
RepoID: repo.ID,
Status: model.StatusRunning,
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
}
if err := store.CreatePipeline(pipeline); err != nil {
t.Errorf("Unexpected error: insert pipeline: %s", err)
2019-06-14 07:20:46 +00:00
return
}
if err := store.PipelineConfigCreate(
&model.PipelineConfig{
ConfigID: config.ID,
PipelineID: pipeline.ID,
2017-05-05 16:59:37 +00:00
},
); err != nil {
t.Errorf("Unexpected error: insert pipeline config: %s", err)
2017-05-05 16:59:37 +00:00
return
}
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)
}
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
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) {
store, closer := newTestStore(t, new(model.Config), new(model.PipelineConfig), new(model.Pipeline), new(model.Repo))
defer closer()
2017-05-05 18:05:42 +00:00
2017-05-23 15:53:40 +00:00
repo := &model.Repo{
UserID: 1,
FullName: "bradrydzewski/test",
2017-05-23 15:53:40 +00:00
Owner: "bradrydzewski",
Name: "test",
2017-05-23 15:53:40 +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 (
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",
}
pipelinePending = &model.Pipeline{
2019-06-07 08:40:16 +00:00
RepoID: repo.ID,
Status: model.StatusPending,
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
}
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
}
)
if err := store.CreatePipeline(pipelineBlocked); err != nil {
t.Errorf("Unexpected error: insert pipeline: %s", err)
2019-06-14 07:20:46 +00:00
return
}
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{
RepoID: repo.ID,
Data: []byte(data),
Hash: hash,
2019-06-07 08:40:16 +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
}
pipelineConfig := &model.PipelineConfig{
ConfigID: conf.ID,
PipelineID: pipelineBlocked.ID,
}
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
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
}
assert.NoError(t, store.CreatePipeline(pipelineRunning))
2019-06-07 08:40:16 +00:00
conf2 := &model.Config{
RepoID: repo.ID,
Data: []byte(data),
Hash: "xxx",
}
if err := store.ConfigCreate(conf2); err != nil {
2019-06-14 07:20:46 +00:00
t.Errorf("Unexpected error: insert config: %s", err)
return
}
pipelineConfig2 := &model.PipelineConfig{
ConfigID: conf2.ID,
PipelineID: pipelineRunning.ID,
2019-06-07 08:40:16 +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
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) {
store, closer := newTestStore(t, new(model.Config), new(model.Step), new(model.Pipeline), new(model.Repo))
defer closer()
2017-05-05 18:05:42 +00:00
var (
data = "pipeline: [ { image: golang, commands: [ go build, go test ] } ]"
hash = "8d8647c9aa90d893bfb79dddbe901f03e258588121e5202632f8ae5738590b26"
)
if err := store.ConfigCreate(
2017-05-05 18:05:42 +00:00
&model.Config{
RepoID: 2,
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
if err := store.ConfigCreate(
2017-05-05 18:05:42 +00:00
&model.Config{
RepoID: 2,
Data: []byte(data),
2017-05-05 18:05:42 +00:00
Hash: hash,
},
); err == nil {
t.Errorf("Unexpected error: duplicate sha")
2017-05-05 18:05:42 +00:00
}
}