woodpecker/server/store/datastore/config_test.go

231 lines
5.7 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/v2/server/model"
2017-05-05 16:59:37 +00:00
)
var (
data = []byte("pipeline: [ { image: golang, commands: [ go build, go test ] } ]")
hash = "8d8647c9aa90d893bfb79dddbe901f03e258588121e5202632f8ae5738590b26"
name = "test"
)
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
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
}
assert.NoError(t, store.CreateRepo(repo))
2019-06-14 07:20:46 +00:00
config := &model.Config{
2019-06-14 07:20:46 +00:00
RepoID: repo.ID,
Data: data,
Hash: hash,
Name: name,
}
assert.NoError(t, store.ConfigCreate(config))
pipeline := &model.Pipeline{
2019-06-14 07:20:46 +00:00
RepoID: repo.ID,
Status: model.StatusRunning,
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
}
assert.NoError(t, store.CreatePipeline(pipeline))
2019-06-14 07:20:46 +00:00
assert.NoError(t, store.PipelineConfigCreate(
&model.PipelineConfig{
ConfigID: config.ID,
PipelineID: pipeline.ID,
2017-05-05 16:59:37 +00:00
},
))
2017-05-05 16:59:37 +00:00
foundConfig, err := store.configFindIdentical(store.engine.NewSession(), repo.ID, hash, name)
assert.NoError(t, err)
assert.EqualValues(t, config, foundConfig)
2017-05-05 16:59:37 +00:00
loaded, err := store.ConfigsForPipeline(pipeline.ID)
assert.NoError(t, err)
assert.Equal(t, config.ID, loaded[0].ID)
2017-05-05 18:05:42 +00:00
}
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
}
assert.NoError(t, store.CreateRepo(repo))
2017-05-23 15:53:40 +00:00
2017-05-05 18:05:42 +00:00
var (
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
}
)
assert.NoError(t, store.CreatePipeline(pipelineBlocked))
assert.NoError(t, store.CreatePipeline(pipelinePending))
2019-06-07 08:40:16 +00:00
conf := &model.Config{
RepoID: repo.ID,
Data: data,
Hash: hash,
Name: name,
2019-06-07 08:40:16 +00:00
}
assert.NoError(t, store.ConfigCreate(conf))
pipelineConfig := &model.PipelineConfig{
ConfigID: conf.ID,
PipelineID: pipelineBlocked.ID,
}
assert.NoError(t, store.PipelineConfigCreate(pipelineConfig))
2019-06-07 08:40:16 +00:00
approved, err := store.ConfigFindApproved(conf)
if !assert.NoError(t, err) {
return
}
assert.False(t, approved, "want config not approved when blocked or pending.")
2017-05-05 18:05:42 +00:00
assert.NoError(t, store.CreatePipeline(pipelineRunning))
2019-06-07 08:40:16 +00:00
conf2 := &model.Config{
RepoID: repo.ID,
Data: data,
Hash: "xxx",
Name: "xxx",
}
assert.NoError(t, store.ConfigCreate(conf2))
pipelineConfig2 := &model.PipelineConfig{
ConfigID: conf2.ID,
PipelineID: pipelineRunning.ID,
2019-06-07 08:40:16 +00:00
}
assert.NoError(t, store.PipelineConfigCreate(pipelineConfig2))
2017-05-05 18:05:42 +00:00
approved, err = store.ConfigFindApproved(conf2)
assert.NoError(t, err)
assert.True(t, approved)
2017-05-05 16:59:37 +00:00
}
func TestConfigCreate(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
// fail due to missing name
assert.Error(t, store.ConfigCreate(
&model.Config{
RepoID: 2,
Data: data,
Hash: hash,
},
))
// fail due to missing hash
assert.Error(t, store.ConfigCreate(
&model.Config{
RepoID: 2,
Data: data,
Name: name,
},
))
2017-05-05 18:05:42 +00:00
assert.NoError(t, store.ConfigCreate(
2017-05-05 18:05:42 +00:00
&model.Config{
RepoID: 2,
Data: data,
2017-05-05 18:05:42 +00:00
Hash: hash,
Name: name,
2017-05-05 18:05:42 +00:00
},
))
2017-05-05 18:05:42 +00:00
// fail due to duplicate sha
assert.Error(t, store.ConfigCreate(
2017-05-05 18:05:42 +00:00
&model.Config{
RepoID: 2,
Data: data,
2017-05-05 18:05:42 +00:00
Hash: hash,
Name: name,
2017-05-05 18:05:42 +00:00
},
))
2017-05-05 18:05:42 +00:00
}
func TestConfigPersist(t *testing.T) {
store, closer := newTestStore(t, new(model.Config))
defer closer()
conf1 := &model.Config{
RepoID: 2,
Data: data,
Hash: hash,
Name: name,
}
conf2 := &model.Config{
RepoID: 2,
Data: []byte("steps: [ { image: golang, commands: [ go generate ] } ]"),
Name: "generate",
}
conf1, err := store.ConfigPersist(conf1)
assert.NoError(t, err)
assert.EqualValues(t, hash, conf1.Hash)
conf1secondInsert, err := store.ConfigPersist(conf1)
assert.NoError(t, err)
assert.EqualValues(t, conf1, conf1secondInsert)
count, err := store.engine.Count(new(model.Config))
assert.NoError(t, err)
assert.EqualValues(t, 1, count)
newConf2, err := store.ConfigPersist(conf2)
assert.NoError(t, err)
assert.EqualValues(t, "66f28f8d487a48aacf29d9feea13b0ab5dbb5025296b77a6addde93efcc4d82b", newConf2.Hash)
count, err = store.engine.Count(new(model.Config))
assert.NoError(t, err)
assert.EqualValues(t, 2, count)
// test for https://github.com/woodpecker-ci/woodpecker/issues/3093
_, err = store.ConfigPersist(&model.Config{
RepoID: 2,
Data: data,
Hash: hash,
Name: "some other",
})
assert.NoError(t, err)
count, err = store.engine.Count(new(model.Config))
assert.NoError(t, err)
assert.EqualValues(t, 3, count)
}