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"
|
|
|
|
|
2019-08-27 11:01:29 +00:00
|
|
|
"github.com/laszlocph/woodpecker/model"
|
2017-05-05 16:59:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfig(t *testing.T) {
|
|
|
|
s := newTest()
|
|
|
|
defer func() {
|
2019-06-14 07:20:46 +00:00
|
|
|
s.Exec("delete from repos")
|
|
|
|
s.Exec("delete from builds")
|
|
|
|
s.Exec("delete from procs")
|
2017-05-05 16:59:37 +00:00
|
|
|
s.Exec("delete from config")
|
|
|
|
s.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
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/drone",
|
|
|
|
Owner: "bradrydzewski",
|
|
|
|
Name: "drone",
|
|
|
|
}
|
|
|
|
if err := s.CreateRepo(repo); err != nil {
|
|
|
|
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,
|
2019-06-11 08:50:50 +00:00
|
|
|
Data: data,
|
|
|
|
Hash: hash,
|
|
|
|
Name: "default",
|
|
|
|
}
|
|
|
|
if err := s.ConfigCreate(config); err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert config: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-14 07:20:46 +00:00
|
|
|
build := &model.Build{
|
|
|
|
RepoID: repo.ID,
|
|
|
|
Status: model.StatusRunning,
|
|
|
|
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
|
|
|
|
}
|
|
|
|
if err := s.CreateBuild(build); err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert build: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-11 08:50:50 +00:00
|
|
|
if err := s.BuildConfigCreate(
|
|
|
|
&model.BuildConfig{
|
|
|
|
ConfigID: config.ID,
|
2019-06-14 07:20:46 +00:00
|
|
|
BuildID: build.ID,
|
2017-05-05 16:59:37 +00:00
|
|
|
},
|
|
|
|
); err != nil {
|
2019-06-14 07:20:46 +00:00
|
|
|
t.Errorf("Unexpected error: insert build config: %s", err)
|
2017-05-05 16:59:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-14 07:20:46 +00:00
|
|
|
config, err := s.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 := config.Data, data; got != want {
|
|
|
|
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
|
|
|
|
2019-06-14 07:20:46 +00:00
|
|
|
loaded, err := s.ConfigsForBuild(build.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) {
|
|
|
|
s := newTest()
|
|
|
|
defer func() {
|
2017-05-23 15:53:40 +00:00
|
|
|
s.Exec("delete from repos")
|
2019-06-14 07:20:46 +00:00
|
|
|
s.Exec("delete from builds")
|
|
|
|
s.Exec("delete from procs")
|
|
|
|
s.Exec("delete from config")
|
2017-05-05 18:05:42 +00:00
|
|
|
s.Close()
|
|
|
|
}()
|
|
|
|
|
2017-05-23 15:53:40 +00:00
|
|
|
repo := &model.Repo{
|
|
|
|
UserID: 1,
|
|
|
|
FullName: "bradrydzewski/drone",
|
|
|
|
Owner: "bradrydzewski",
|
|
|
|
Name: "drone",
|
|
|
|
}
|
2019-06-14 07:20:46 +00:00
|
|
|
if err := s.CreateRepo(repo); err != nil {
|
|
|
|
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 (
|
2019-06-07 08:40:16 +00:00
|
|
|
data = "pipeline: [ { image: golang, commands: [ go build, go test ] } ]"
|
|
|
|
hash = "8d8647c9aa90d893bfb79dddbe901f03e258588121e5202632f8ae5738590b26"
|
|
|
|
buildBlocked = &model.Build{
|
2017-05-23 15:53:40 +00:00
|
|
|
RepoID: repo.ID,
|
2019-06-07 08:40:16 +00: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 18:05:42 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-06-14 07:20:46 +00:00
|
|
|
if err := s.CreateBuild(buildBlocked); err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert build: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := s.CreateBuild(buildPending); err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert build: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2019-06-07 08:40:16 +00:00
|
|
|
conf := &model.Config{
|
2019-06-11 08:50:50 +00:00
|
|
|
RepoID: repo.ID,
|
|
|
|
Data: data,
|
|
|
|
Hash: hash,
|
2019-06-07 08:40:16 +00:00
|
|
|
}
|
2019-06-14 07:20:46 +00:00
|
|
|
if err := s.ConfigCreate(conf); err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert config: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2019-06-11 08:50:50 +00:00
|
|
|
buildConfig := &model.BuildConfig{
|
2019-06-14 07:20:46 +00:00
|
|
|
ConfigID: conf.ID,
|
2019-06-11 08:50:50 +00:00
|
|
|
BuildID: buildBlocked.ID,
|
|
|
|
}
|
|
|
|
if err := s.BuildConfigCreate(buildConfig); err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert build_config: %s", err)
|
2017-05-05 16:59:37 +00:00
|
|
|
return
|
|
|
|
}
|
2019-06-07 08:40:16 +00:00
|
|
|
|
|
|
|
if approved, err := s.ConfigFindApproved(conf); approved != false || err != nil {
|
|
|
|
t.Errorf("Want config not approved, when blocked or pending. %v", err)
|
2017-05-05 18:05:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-07 08:40:16 +00:00
|
|
|
s.CreateBuild(buildRunning)
|
|
|
|
conf2 := &model.Config{
|
2019-06-11 08:50:50 +00:00
|
|
|
RepoID: repo.ID,
|
|
|
|
Data: data,
|
|
|
|
Hash: "xxx",
|
|
|
|
}
|
2019-06-14 07:20:46 +00:00
|
|
|
if err := s.ConfigCreate(conf2); err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert config: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2019-06-11 08:50:50 +00:00
|
|
|
buildConfig2 := &model.BuildConfig{
|
2019-06-14 07:20:46 +00:00
|
|
|
ConfigID: conf2.ID,
|
2019-06-11 08:50:50 +00:00
|
|
|
BuildID: buildRunning.ID,
|
2019-06-07 08:40:16 +00:00
|
|
|
}
|
2019-06-11 08:50:50 +00:00
|
|
|
if err := s.BuildConfigCreate(buildConfig2); 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
|
|
|
|
2019-06-07 08:40:16 +00:00
|
|
|
if approved, err := s.ConfigFindApproved(conf2); approved != true || err != nil {
|
|
|
|
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) {
|
|
|
|
s := newTest()
|
|
|
|
defer func() {
|
2019-06-14 07:20:46 +00:00
|
|
|
s.Exec("delete from repos")
|
|
|
|
s.Exec("delete from builds")
|
|
|
|
s.Exec("delete from procs")
|
2017-05-05 18:05:42 +00:00
|
|
|
s.Exec("delete from config")
|
|
|
|
s.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
var (
|
|
|
|
data = "pipeline: [ { image: golang, commands: [ go build, go test ] } ]"
|
|
|
|
hash = "8d8647c9aa90d893bfb79dddbe901f03e258588121e5202632f8ae5738590b26"
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := s.ConfigCreate(
|
|
|
|
&model.Config{
|
|
|
|
RepoID: 2,
|
|
|
|
Data: data,
|
|
|
|
Hash: hash,
|
|
|
|
},
|
|
|
|
); err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert config: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// fail due to duplicate sha
|
|
|
|
if err := s.ConfigCreate(
|
|
|
|
&model.Config{
|
|
|
|
RepoID: 2,
|
|
|
|
Data: data,
|
|
|
|
Hash: hash,
|
|
|
|
},
|
|
|
|
); err == nil {
|
|
|
|
t.Errorf("Unexpected error: dupliate sha")
|
|
|
|
}
|
|
|
|
}
|