1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2026-06-03 16:35:37 +02:00
Files
woodpecker/server/store/datastore/config_test.go
T

121 lines
3.2 KiB
Go
Raw Normal View History

2018-02-19 14:24:10 -08:00
// Copyright 2018 Drone.IO Inc.
2018-03-21 14:02:17 +01:00
//
2018-02-19 14:24:10 -08: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 14:02:17 +01:00
//
2018-02-19 14:24:10 -08:00
// http://www.apache.org/licenses/LICENSE-2.0
2018-03-21 14:02:17 +01:00
//
2018-02-19 14:24:10 -08: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"
2021-11-13 20:18:06 +01:00
"github.com/stretchr/testify/assert"
2024-12-22 11:44:34 +02:00
"go.woodpecker-ci.org/woodpecker/v3/server/model"
2017-05-05 18:59:37 +02:00
)
2024-01-18 22:50:29 +01:00
var (
data = []byte("pipeline: [ { image: golang, commands: [ go build, go test ] } ]")
hash = "8d8647c9aa90d893bfb79dddbe901f03e258588121e5202632f8ae5738590b26"
name = "test"
)
2017-05-05 18:59:37 +02:00
func TestConfig(t *testing.T) {
2022-10-18 03:24:12 +02:00
store, closer := newTestStore(t, new(model.Config), new(model.PipelineConfig), new(model.Pipeline), new(model.Repo))
2021-11-13 20:18:06 +01:00
defer closer()
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
}
2024-01-14 19:33:58 +01:00
assert.NoError(t, store.CreateRepo(repo))
2019-06-14 09:20:46 +02:00
config := &model.Config{
2019-06-14 09:20:46 +02:00
RepoID: repo.ID,
2024-01-18 22:50:29 +01:00
Data: data,
Hash: hash,
2024-01-18 22:50:29 +01:00
Name: name,
}
2024-02-25 10:37:10 +01:00
_, err := store.ConfigPersist(config)
assert.NoError(t, err)
2022-10-18 03:24:12 +02:00
pipeline := &model.Pipeline{
2019-06-14 09:20:46 +02:00
RepoID: repo.ID,
Status: model.StatusRunning,
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
}
2024-01-14 19:33:58 +01:00
assert.NoError(t, store.CreatePipeline(pipeline))
2019-06-14 09:20:46 +02:00
2024-01-14 19:33:58 +01:00
assert.NoError(t, store.PipelineConfigCreate(
2022-10-18 03:24:12 +02:00
&model.PipelineConfig{
ConfigID: config.ID,
PipelineID: pipeline.ID,
2017-05-05 18:59:37 +02:00
},
2024-01-14 19:33:58 +01:00
))
2017-05-05 18:59:37 +02:00
2024-01-18 22:50:29 +01:00
foundConfig, err := store.configFindIdentical(store.engine.NewSession(), repo.ID, hash, name)
2024-01-14 19:33:58 +01:00
assert.NoError(t, err)
2024-01-18 22:50:29 +01:00
assert.EqualValues(t, config, foundConfig)
2017-05-05 18:59:37 +02:00
2022-10-18 03:24:12 +02:00
loaded, err := store.ConfigsForPipeline(pipeline.ID)
2024-01-14 19:33:58 +01:00
assert.NoError(t, err)
assert.Equal(t, config.ID, loaded[0].ID)
2017-05-05 20:05:42 +02:00
}
2017-05-05 18:59:37 +02:00
2024-01-18 22:50:29 +01: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)
}