1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-12 08:23:48 +02:00
woodpecker/server/store/datastore/config_test.go

233 lines
6.0 KiB
Go
Raw Normal View History

2018-02-20 00:24:10 +02:00
// Copyright 2018 Drone.IO Inc.
2018-03-21 15:02:17 +02:00
//
2018-02-20 00:24:10 +02: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 15:02:17 +02:00
//
2018-02-20 00:24:10 +02:00
// http://www.apache.org/licenses/LICENSE-2.0
2018-03-21 15:02:17 +02:00
//
2018-02-20 00:24:10 +02: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 18:59:37 +02:00
package datastore
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/woodpecker-ci/woodpecker/server/model"
2017-05-05 18:59:37 +02:00
)
func TestConfig(t *testing.T) {
store, closer := newTestStore(t, new(model.Config), new(model.BuildConfig), new(model.Build), new(model.Repo))
defer closer()
2017-05-05 18:59:37 +02:00
var (
2019-06-14 09:20:46 +02:00
data = "pipeline: [ { image: golang, commands: [ go build, go test ] } ]"
hash = "8d8647c9aa90d893bfb79dddbe901f03e258588121e5202632f8ae5738590b26"
2017-05-05 18:59:37 +02:00
)
2019-06-14 09:20:46 +02:00
repo := &model.Repo{
UserID: 1,
FullName: "bradrydzewski/test",
2019-06-14 09:20:46 +02:00
Owner: "bradrydzewski",
Name: "test",
2019-06-14 09:20:46 +02:00
}
if err := store.CreateRepo(repo); err != nil {
2019-06-14 09:20:46 +02:00
t.Errorf("Unexpected error: insert repo: %s", err)
return
}
config := &model.Config{
2019-06-14 09:20:46 +02: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
}
2019-06-14 09:20:46 +02:00
build := &model.Build{
RepoID: repo.ID,
Status: model.StatusRunning,
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
}
if err := store.CreateBuild(build); err != nil {
2019-06-14 09:20:46 +02:00
t.Errorf("Unexpected error: insert build: %s", err)
return
}
if err := store.BuildConfigCreate(
&model.BuildConfig{
ConfigID: config.ID,
2019-06-14 09:20:46 +02:00
BuildID: build.ID,
2017-05-05 18:59:37 +02:00
},
); err != nil {
2019-06-14 09:20:46 +02:00
t.Errorf("Unexpected error: insert build config: %s", err)
2017-05-05 18:59:37 +02:00
return
}
config, err := store.ConfigFindIdentical(repo.ID, hash)
2017-05-05 18:59:37 +02: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 09:20:46 +02:00
if got, want := config.RepoID, repo.ID; got != want {
2017-05-05 18:59:37 +02:00
t.Errorf("Want config repo id %d, got %d", want, got)
}
if got, want := string(config.Data), data; got != want {
2017-05-05 18:59:37 +02: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 10:40:16 +02:00
if got, want := config.Name, "default"; got != want {
t.Errorf("Want config name %s, got %s", want, got)
}
2017-05-05 18:59:37 +02:00
loaded, err := store.ConfigsForBuild(build.ID)
2017-05-05 18:59:37 +02:00
if err != nil {
2017-05-05 20:05:42 +02:00
t.Errorf("Want config by id, got error %q", err)
2017-05-05 18:59:37 +02:00
return
}
2019-06-07 10:40:16 +02:00
if got, want := loaded[0].ID, config.ID; got != want {
2017-05-05 20:05:42 +02:00
t.Errorf("Want config by id %d, got %d", want, got)
}
}
2017-05-05 18:59:37 +02:00
2017-05-05 20:05:42 +02:00
func TestConfigApproved(t *testing.T) {
store, closer := newTestStore(t, new(model.Config), new(model.BuildConfig), new(model.Build), new(model.Repo))
defer closer()
2017-05-05 20:05:42 +02:00
2017-05-23 17:53:40 +02:00
repo := &model.Repo{
UserID: 1,
FullName: "bradrydzewski/test",
2017-05-23 17:53:40 +02:00
Owner: "bradrydzewski",
Name: "test",
2017-05-23 17:53:40 +02:00
}
if err := store.CreateRepo(repo); err != nil {
2019-06-14 09:20:46 +02:00
t.Errorf("Unexpected error: insert repo: %s", err)
return
}
2017-05-23 17:53:40 +02:00
2017-05-05 20:05:42 +02:00
var (
2019-06-07 10:40:16 +02:00
data = "pipeline: [ { image: golang, commands: [ go build, go test ] } ]"
hash = "8d8647c9aa90d893bfb79dddbe901f03e258588121e5202632f8ae5738590b26"
buildBlocked = &model.Build{
2017-05-23 17:53:40 +02:00
RepoID: repo.ID,
2019-06-07 10:40:16 +02:00
Status: model.StatusBlocked,
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
}
buildPending = &model.Build{
RepoID: repo.ID,
Status: model.StatusPending,
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
}
buildRunning = &model.Build{
RepoID: repo.ID,
Status: model.StatusRunning,
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
2017-05-05 20:05:42 +02:00
}
)
if err := store.CreateBuild(buildBlocked); err != nil {
2019-06-14 09:20:46 +02:00
t.Errorf("Unexpected error: insert build: %s", err)
return
}
if err := store.CreateBuild(buildPending); err != nil {
2019-06-14 09:20:46 +02:00
t.Errorf("Unexpected error: insert build: %s", err)
return
}
2019-06-07 10:40:16 +02:00
conf := &model.Config{
RepoID: repo.ID,
Data: []byte(data),
Hash: hash,
2019-06-07 10:40:16 +02:00
}
if err := store.ConfigCreate(conf); err != nil {
2019-06-14 09:20:46 +02:00
t.Errorf("Unexpected error: insert config: %s", err)
return
}
buildConfig := &model.BuildConfig{
2019-06-14 09:20:46 +02:00
ConfigID: conf.ID,
BuildID: buildBlocked.ID,
}
if err := store.BuildConfigCreate(buildConfig); err != nil {
t.Errorf("Unexpected error: insert build_config: %s", err)
2017-05-05 18:59:37 +02:00
return
}
2019-06-07 10:40:16 +02: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 20:05:42 +02:00
return
}
assert.NoError(t, store.CreateBuild(buildRunning))
2019-06-07 10:40:16 +02:00
conf2 := &model.Config{
RepoID: repo.ID,
Data: []byte(data),
Hash: "xxx",
}
if err := store.ConfigCreate(conf2); err != nil {
2019-06-14 09:20:46 +02:00
t.Errorf("Unexpected error: insert config: %s", err)
return
}
buildConfig2 := &model.BuildConfig{
2019-06-14 09:20:46 +02:00
ConfigID: conf2.ID,
BuildID: buildRunning.ID,
2019-06-07 10:40:16 +02:00
}
if err := store.BuildConfigCreate(buildConfig2); err != nil {
2019-06-07 10:40:16 +02:00
t.Errorf("Unexpected error: insert config: %s", err)
return
}
2017-05-05 20:05:42 +02:00
if approved, err := store.ConfigFindApproved(conf2); approved != true || err != nil {
2019-06-07 10:40:16 +02:00
t.Errorf("Want config approved, when running. %v", err)
2017-05-05 20:05:42 +02:00
return
2017-05-05 18:59:37 +02:00
}
}
2017-05-05 20:05:42 +02:00
func TestConfigIndexes(t *testing.T) {
store, closer := newTestStore(t, new(model.Config), new(model.Proc), new(model.Build), new(model.Repo))
defer closer()
2017-05-05 20:05:42 +02:00
var (
data = "pipeline: [ { image: golang, commands: [ go build, go test ] } ]"
hash = "8d8647c9aa90d893bfb79dddbe901f03e258588121e5202632f8ae5738590b26"
)
if err := store.ConfigCreate(
2017-05-05 20:05:42 +02:00
&model.Config{
RepoID: 2,
Data: []byte(data),
2017-05-05 20:05:42 +02: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 20:05:42 +02:00
&model.Config{
RepoID: 2,
Data: []byte(data),
2017-05-05 20:05:42 +02:00
Hash: hash,
},
); err == nil {
t.Errorf("Unexpected error: duplicate sha")
2017-05-05 20:05:42 +02:00
}
}