woodpecker/server/store/datastore/build_test.go

308 lines
9.3 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.
package datastore
2015-09-30 01:21:17 +00:00
import (
2018-05-02 16:59:05 +00:00
"fmt"
2015-09-30 01:21:17 +00:00
"testing"
"github.com/franela/goblin"
"github.com/stretchr/testify/assert"
"github.com/woodpecker-ci/woodpecker/server/model"
2015-09-30 01:21:17 +00:00
)
func TestBuilds(t *testing.T) {
2017-05-23 10:43:58 +00:00
repo := &model.Repo{
UserID: 1,
FullName: "bradrydzewski/test",
2017-05-23 10:43:58 +00:00
Owner: "bradrydzewski",
Name: "test",
2017-05-23 10:43:58 +00:00
}
store, closer := newTestStore(t, new(model.Repo), new(model.Proc), new(model.Build))
defer closer()
2015-09-30 01:21:17 +00:00
g := goblin.Goblin(t)
g.Describe("Builds", func() {
2017-05-23 10:43:58 +00:00
g.Before(func() {
_, err := store.engine.Exec("DELETE FROM repos")
g.Assert(err).IsNil()
g.Assert(store.CreateRepo(repo)).IsNil()
2017-05-23 10:43:58 +00:00
})
2017-05-23 15:53:40 +00:00
g.After(func() {
_, err := store.engine.Exec("DELETE FROM repos")
g.Assert(err).IsNil()
2017-05-23 15:53:40 +00:00
})
2015-09-30 01:21:17 +00:00
// before each test be sure to purge the package
// table data from the database.
g.BeforeEach(func() {
_, err := store.engine.Exec("DELETE FROM builds")
g.Assert(err).IsNil()
_, err = store.engine.Exec("DELETE FROM procs")
g.Assert(err).IsNil()
2015-09-30 01:21:17 +00:00
})
g.It("Should Post a Build", func() {
build := model.Build{
2017-05-23 10:43:58 +00:00
RepoID: repo.ID,
Status: model.StatusSuccess,
2015-09-30 01:21:17 +00:00
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
}
err := store.CreateBuild(&build)
g.Assert(err).IsNil()
2015-09-30 01:21:17 +00:00
g.Assert(build.ID != 0).IsTrue()
g.Assert(build.Number).Equal(int64(1))
2015-09-30 01:21:17 +00:00
g.Assert(build.Commit).Equal("85f8c029b902ed9400bc600bac301a0aadb144ac")
2018-05-02 16:59:05 +00:00
count, err := store.GetBuildCount()
g.Assert(err).IsNil()
2018-05-02 16:59:05 +00:00
g.Assert(count > 0).IsTrue()
fmt.Println("GOT COUNT", count)
2015-09-30 01:21:17 +00:00
})
g.It("Should Put a Build", func() {
build := model.Build{
2017-05-23 10:43:58 +00:00
RepoID: repo.ID,
2015-09-30 01:21:17 +00:00
Number: 5,
Status: model.StatusSuccess,
2015-09-30 01:21:17 +00:00
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
}
err := store.CreateBuild(&build)
g.Assert(err).IsNil()
build.Status = model.StatusRunning
err1 := store.UpdateBuild(&build)
getbuild, err2 := store.GetBuild(build.ID)
g.Assert(err1).IsNil()
g.Assert(err2).IsNil()
2015-09-30 01:21:17 +00:00
g.Assert(build.ID).Equal(getbuild.ID)
g.Assert(build.RepoID).Equal(getbuild.RepoID)
g.Assert(build.Status).Equal(getbuild.Status)
g.Assert(build.Number).Equal(getbuild.Number)
})
g.It("Should Get a Build", func() {
build := model.Build{
2017-05-23 10:43:58 +00:00
RepoID: repo.ID,
Status: model.StatusSuccess,
2015-09-30 01:21:17 +00:00
}
err := store.CreateBuild(&build, []*model.Proc{}...)
g.Assert(err).IsNil()
getbuild, err := store.GetBuild(build.ID)
g.Assert(err).IsNil()
2015-09-30 01:21:17 +00:00
g.Assert(build.ID).Equal(getbuild.ID)
g.Assert(build.RepoID).Equal(getbuild.RepoID)
g.Assert(build.Status).Equal(getbuild.Status)
})
g.It("Should Get a Build by Number", func() {
build1 := &model.Build{
2017-05-23 10:43:58 +00:00
RepoID: repo.ID,
Status: model.StatusPending,
2015-09-30 01:21:17 +00:00
}
build2 := &model.Build{
2017-05-23 10:43:58 +00:00
RepoID: repo.ID,
Status: model.StatusPending,
2015-09-30 01:21:17 +00:00
}
err1 := store.CreateBuild(build1, []*model.Proc{}...)
g.Assert(err1).IsNil()
err2 := store.CreateBuild(build2, []*model.Proc{}...)
g.Assert(err2).IsNil()
getbuild, err3 := store.GetBuildNumber(&model.Repo{ID: 1}, build2.Number)
g.Assert(err3).IsNil()
2015-09-30 01:21:17 +00:00
g.Assert(build2.ID).Equal(getbuild.ID)
g.Assert(build2.RepoID).Equal(getbuild.RepoID)
g.Assert(build2.Number).Equal(getbuild.Number)
})
g.It("Should Get a Build by Ref", func() {
build1 := &model.Build{
2017-05-23 10:43:58 +00:00
RepoID: repo.ID,
Status: model.StatusPending,
2015-09-30 01:21:17 +00:00
Ref: "refs/pull/5",
}
build2 := &model.Build{
2017-05-23 10:43:58 +00:00
RepoID: repo.ID,
Status: model.StatusPending,
2015-09-30 01:21:17 +00:00
Ref: "refs/pull/6",
}
err1 := store.CreateBuild(build1, []*model.Proc{}...)
g.Assert(err1).IsNil()
err2 := store.CreateBuild(build2, []*model.Proc{}...)
g.Assert(err2).IsNil()
getbuild, err3 := store.GetBuildRef(&model.Repo{ID: 1}, "refs/pull/6")
g.Assert(err3).IsNil()
2015-09-30 01:21:17 +00:00
g.Assert(build2.ID).Equal(getbuild.ID)
g.Assert(build2.RepoID).Equal(getbuild.RepoID)
g.Assert(build2.Number).Equal(getbuild.Number)
g.Assert(build2.Ref).Equal(getbuild.Ref)
})
g.It("Should Get a Build by Ref", func() {
build1 := &model.Build{
2017-05-23 10:43:58 +00:00
RepoID: repo.ID,
Status: model.StatusPending,
2015-09-30 01:21:17 +00:00
Ref: "refs/pull/5",
}
build2 := &model.Build{
2017-05-23 10:43:58 +00:00
RepoID: repo.ID,
Status: model.StatusPending,
2015-09-30 01:21:17 +00:00
Ref: "refs/pull/6",
}
err1 := store.CreateBuild(build1, []*model.Proc{}...)
g.Assert(err1).IsNil()
err2 := store.CreateBuild(build2, []*model.Proc{}...)
g.Assert(err2).IsNil()
getbuild, err3 := store.GetBuildRef(&model.Repo{ID: 1}, "refs/pull/6")
g.Assert(err3).IsNil()
2015-09-30 01:21:17 +00:00
g.Assert(build2.ID).Equal(getbuild.ID)
g.Assert(build2.RepoID).Equal(getbuild.RepoID)
g.Assert(build2.Number).Equal(getbuild.Number)
g.Assert(build2.Ref).Equal(getbuild.Ref)
})
g.It("Should Get a Build by Commit", func() {
build1 := &model.Build{
2017-05-23 10:43:58 +00:00
RepoID: repo.ID,
Status: model.StatusPending,
2015-09-30 01:21:17 +00:00
Branch: "master",
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
}
build2 := &model.Build{
2017-05-23 10:43:58 +00:00
RepoID: repo.ID,
Status: model.StatusPending,
2015-09-30 01:21:17 +00:00
Branch: "dev",
Commit: "85f8c029b902ed9400bc600bac301a0aadb144aa",
}
err1 := store.CreateBuild(build1, []*model.Proc{}...)
g.Assert(err1).IsNil()
err2 := store.CreateBuild(build2, []*model.Proc{}...)
g.Assert(err2).IsNil()
getbuild, err3 := store.GetBuildCommit(&model.Repo{ID: 1}, build2.Commit, build2.Branch)
g.Assert(err3).IsNil()
2015-09-30 01:21:17 +00:00
g.Assert(build2.ID).Equal(getbuild.ID)
g.Assert(build2.RepoID).Equal(getbuild.RepoID)
g.Assert(build2.Number).Equal(getbuild.Number)
g.Assert(build2.Commit).Equal(getbuild.Commit)
g.Assert(build2.Branch).Equal(getbuild.Branch)
})
2015-10-08 00:53:28 +00:00
g.It("Should Get the last Build", func() {
build1 := &model.Build{
2017-05-23 10:43:58 +00:00
RepoID: repo.ID,
Status: model.StatusFailure,
2015-09-30 01:21:17 +00:00
Branch: "master",
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
Event: model.EventPush,
2015-09-30 01:21:17 +00:00
}
build2 := &model.Build{
2017-05-23 10:43:58 +00:00
RepoID: repo.ID,
Status: model.StatusSuccess,
2015-09-30 01:21:17 +00:00
Branch: "master",
Commit: "85f8c029b902ed9400bc600bac301a0aadb144aa",
Event: model.EventPush,
2015-09-30 01:21:17 +00:00
}
err1 := store.CreateBuild(build1, []*model.Proc{}...)
err2 := store.CreateBuild(build2, []*model.Proc{}...)
getbuild, err3 := store.GetBuildLast(&model.Repo{ID: 1}, build2.Branch)
g.Assert(err1).IsNil()
g.Assert(err2).IsNil()
g.Assert(err3).IsNil()
2015-09-30 01:21:17 +00:00
g.Assert(build2.ID).Equal(getbuild.ID)
g.Assert(build2.RepoID).Equal(getbuild.RepoID)
g.Assert(build2.Number).Equal(getbuild.Number)
g.Assert(build2.Status).Equal(getbuild.Status)
g.Assert(build2.Branch).Equal(getbuild.Branch)
g.Assert(build2.Commit).Equal(getbuild.Commit)
})
2015-10-08 00:53:28 +00:00
g.It("Should Get the last Build Before Build N", func() {
build1 := &model.Build{
2017-05-23 10:43:58 +00:00
RepoID: repo.ID,
Status: model.StatusFailure,
2015-10-08 00:53:28 +00:00
Branch: "master",
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
}
build2 := &model.Build{
2017-05-23 10:43:58 +00:00
RepoID: repo.ID,
Status: model.StatusSuccess,
2015-10-08 00:53:28 +00:00
Branch: "master",
Commit: "85f8c029b902ed9400bc600bac301a0aadb144aa",
}
build3 := &model.Build{
2017-05-23 10:43:58 +00:00
RepoID: repo.ID,
Status: model.StatusRunning,
2015-10-08 00:53:28 +00:00
Branch: "master",
Commit: "85f8c029b902ed9400bc600bac301a0aadb144aa",
}
err1 := store.CreateBuild(build1, []*model.Proc{}...)
g.Assert(err1).IsNil()
err2 := store.CreateBuild(build2, []*model.Proc{}...)
g.Assert(err2).IsNil()
err3 := store.CreateBuild(build3, []*model.Proc{}...)
g.Assert(err3).IsNil()
getbuild, err4 := store.GetBuildLastBefore(&model.Repo{ID: 1}, build3.Branch, build3.ID)
g.Assert(err4).IsNil()
2015-10-08 00:53:28 +00:00
g.Assert(build2.ID).Equal(getbuild.ID)
g.Assert(build2.RepoID).Equal(getbuild.RepoID)
g.Assert(build2.Number).Equal(getbuild.Number)
g.Assert(build2.Status).Equal(getbuild.Status)
g.Assert(build2.Branch).Equal(getbuild.Branch)
g.Assert(build2.Commit).Equal(getbuild.Commit)
})
2015-09-30 01:21:17 +00:00
g.It("Should get recent Builds", func() {
build1 := &model.Build{
2017-05-23 10:43:58 +00:00
RepoID: repo.ID,
Status: model.StatusFailure,
2015-09-30 01:21:17 +00:00
}
build2 := &model.Build{
2017-05-23 10:43:58 +00:00
RepoID: repo.ID,
Status: model.StatusSuccess,
2015-09-30 01:21:17 +00:00
}
err1 := store.CreateBuild(build1, []*model.Proc{}...)
g.Assert(err1).IsNil()
err2 := store.CreateBuild(build2, []*model.Proc{}...)
g.Assert(err2).IsNil()
builds, err3 := store.GetBuildList(&model.Repo{ID: 1}, 1)
g.Assert(err3).IsNil()
2015-09-30 01:21:17 +00:00
g.Assert(len(builds)).Equal(2)
g.Assert(builds[0].ID).Equal(build2.ID)
g.Assert(builds[0].RepoID).Equal(build2.RepoID)
g.Assert(builds[0].Status).Equal(build2.Status)
})
})
}
2017-05-23 10:43:58 +00:00
func TestBuildIncrement(t *testing.T) {
store, closer := newTestStore(t, new(model.Build))
defer closer()
2017-05-23 10:43:58 +00:00
buildA := &model.Build{RepoID: 1}
if !assert.NoError(t, store.CreateBuild(buildA)) {
return
2017-05-23 10:43:58 +00:00
}
assert.EqualValues(t, 1, buildA.Number)
2017-05-23 10:43:58 +00:00
buildB := &model.Build{RepoID: 1}
assert.NoError(t, store.CreateBuild(buildB))
assert.EqualValues(t, 2, buildB.Number)
2017-05-23 10:43:58 +00:00
buildC := &model.Build{RepoID: 2}
assert.NoError(t, store.CreateBuild(buildC))
assert.EqualValues(t, 1, buildC.Number)
2017-05-23 10:43:58 +00:00
}