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.
|
|
|
|
|
2015-10-22 01:14:02 +02:00
|
|
|
package datastore
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
import (
|
2018-05-02 18:59:05 +02:00
|
|
|
"fmt"
|
2015-09-30 03:21:17 +02:00
|
|
|
"testing"
|
|
|
|
|
2015-10-22 01:14:02 +02:00
|
|
|
"github.com/drone/drone/model"
|
2015-09-30 03:21:17 +02:00
|
|
|
"github.com/franela/goblin"
|
|
|
|
)
|
|
|
|
|
2016-03-25 21:54:16 +02:00
|
|
|
func TestBuilds(t *testing.T) {
|
2017-05-23 12:43:58 +02:00
|
|
|
repo := &model.Repo{
|
|
|
|
UserID: 1,
|
|
|
|
FullName: "bradrydzewski/drone",
|
|
|
|
Owner: "bradrydzewski",
|
|
|
|
Name: "drone",
|
|
|
|
}
|
|
|
|
|
2017-05-23 17:53:40 +02:00
|
|
|
s := newTest()
|
|
|
|
defer s.Close()
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
g := goblin.Goblin(t)
|
|
|
|
g.Describe("Builds", func() {
|
2017-05-23 12:43:58 +02:00
|
|
|
g.Before(func() {
|
2017-05-23 17:53:40 +02:00
|
|
|
s.Exec("DELETE FROM repos")
|
2017-05-23 12:43:58 +02:00
|
|
|
s.CreateRepo(repo)
|
|
|
|
})
|
2017-05-23 17:53:40 +02:00
|
|
|
g.After(func() {
|
|
|
|
s.Exec("DELETE FROM repos")
|
|
|
|
})
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
// before each test be sure to purge the package
|
|
|
|
// table data from the database.
|
|
|
|
g.BeforeEach(func() {
|
2017-05-23 17:53:40 +02:00
|
|
|
s.Exec("DELETE FROM builds")
|
2017-07-15 18:51:02 +02:00
|
|
|
s.Exec("DELETE FROM procs")
|
2015-09-30 03:21:17 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Post a Build", func() {
|
2015-10-22 01:14:02 +02:00
|
|
|
build := model.Build{
|
2017-05-23 12:43:58 +02:00
|
|
|
RepoID: repo.ID,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusSuccess,
|
2015-09-30 03:21:17 +02:00
|
|
|
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
|
|
|
|
}
|
2017-05-23 12:43:58 +02:00
|
|
|
err := s.CreateBuild(&build)
|
2015-09-30 03:21:17 +02:00
|
|
|
g.Assert(err == nil).IsTrue()
|
|
|
|
g.Assert(build.ID != 0).IsTrue()
|
|
|
|
g.Assert(build.Number).Equal(1)
|
|
|
|
g.Assert(build.Commit).Equal("85f8c029b902ed9400bc600bac301a0aadb144ac")
|
2018-05-02 18:59:05 +02:00
|
|
|
|
|
|
|
count, err := s.GetBuildCount()
|
|
|
|
g.Assert(err == nil).IsTrue()
|
|
|
|
g.Assert(count > 0).IsTrue()
|
|
|
|
fmt.Println("GOT COUNT", count)
|
2015-09-30 03:21:17 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Put a Build", func() {
|
2015-10-22 01:14:02 +02:00
|
|
|
build := model.Build{
|
2017-05-23 12:43:58 +02:00
|
|
|
RepoID: repo.ID,
|
2015-09-30 03:21:17 +02:00
|
|
|
Number: 5,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusSuccess,
|
2015-09-30 03:21:17 +02:00
|
|
|
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
|
|
|
|
}
|
2017-05-23 12:43:58 +02:00
|
|
|
s.CreateBuild(&build)
|
2015-10-22 01:14:02 +02:00
|
|
|
build.Status = model.StatusRunning
|
2016-03-25 21:54:16 +02:00
|
|
|
err1 := s.UpdateBuild(&build)
|
|
|
|
getbuild, err2 := s.GetBuild(build.ID)
|
2015-09-30 03:21:17 +02:00
|
|
|
g.Assert(err1 == nil).IsTrue()
|
|
|
|
g.Assert(err2 == nil).IsTrue()
|
|
|
|
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() {
|
2015-10-22 01:14:02 +02:00
|
|
|
build := model.Build{
|
2017-05-23 12:43:58 +02:00
|
|
|
RepoID: repo.ID,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusSuccess,
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2017-04-02 16:13:26 +02:00
|
|
|
s.CreateBuild(&build, []*model.Proc{}...)
|
2016-03-25 21:54:16 +02:00
|
|
|
getbuild, err := s.GetBuild(build.ID)
|
2015-09-30 03:21:17 +02:00
|
|
|
g.Assert(err == nil).IsTrue()
|
|
|
|
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() {
|
2015-10-22 01:14:02 +02:00
|
|
|
build1 := &model.Build{
|
2017-05-23 12:43:58 +02:00
|
|
|
RepoID: repo.ID,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusPending,
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
build2 := &model.Build{
|
2017-05-23 12:43:58 +02:00
|
|
|
RepoID: repo.ID,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusPending,
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2017-04-02 16:13:26 +02:00
|
|
|
err1 := s.CreateBuild(build1, []*model.Proc{}...)
|
|
|
|
err2 := s.CreateBuild(build2, []*model.Proc{}...)
|
2016-03-25 21:54:16 +02:00
|
|
|
getbuild, err3 := s.GetBuildNumber(&model.Repo{ID: 1}, build2.Number)
|
2015-09-30 03:21:17 +02:00
|
|
|
g.Assert(err1 == nil).IsTrue()
|
|
|
|
g.Assert(err2 == nil).IsTrue()
|
|
|
|
g.Assert(err3 == nil).IsTrue()
|
|
|
|
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() {
|
2015-10-22 01:14:02 +02:00
|
|
|
build1 := &model.Build{
|
2017-05-23 12:43:58 +02:00
|
|
|
RepoID: repo.ID,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusPending,
|
2015-09-30 03:21:17 +02:00
|
|
|
Ref: "refs/pull/5",
|
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
build2 := &model.Build{
|
2017-05-23 12:43:58 +02:00
|
|
|
RepoID: repo.ID,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusPending,
|
2015-09-30 03:21:17 +02:00
|
|
|
Ref: "refs/pull/6",
|
|
|
|
}
|
2017-04-02 16:13:26 +02:00
|
|
|
err1 := s.CreateBuild(build1, []*model.Proc{}...)
|
|
|
|
err2 := s.CreateBuild(build2, []*model.Proc{}...)
|
2016-03-25 21:54:16 +02:00
|
|
|
getbuild, err3 := s.GetBuildRef(&model.Repo{ID: 1}, "refs/pull/6")
|
2015-09-30 03:21:17 +02:00
|
|
|
g.Assert(err1 == nil).IsTrue()
|
|
|
|
g.Assert(err2 == nil).IsTrue()
|
|
|
|
g.Assert(err3 == nil).IsTrue()
|
|
|
|
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() {
|
2015-10-22 01:14:02 +02:00
|
|
|
build1 := &model.Build{
|
2017-05-23 12:43:58 +02:00
|
|
|
RepoID: repo.ID,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusPending,
|
2015-09-30 03:21:17 +02:00
|
|
|
Ref: "refs/pull/5",
|
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
build2 := &model.Build{
|
2017-05-23 12:43:58 +02:00
|
|
|
RepoID: repo.ID,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusPending,
|
2015-09-30 03:21:17 +02:00
|
|
|
Ref: "refs/pull/6",
|
|
|
|
}
|
2017-04-02 16:13:26 +02:00
|
|
|
err1 := s.CreateBuild(build1, []*model.Proc{}...)
|
|
|
|
err2 := s.CreateBuild(build2, []*model.Proc{}...)
|
2016-03-25 21:54:16 +02:00
|
|
|
getbuild, err3 := s.GetBuildRef(&model.Repo{ID: 1}, "refs/pull/6")
|
2015-09-30 03:21:17 +02:00
|
|
|
g.Assert(err1 == nil).IsTrue()
|
|
|
|
g.Assert(err2 == nil).IsTrue()
|
|
|
|
g.Assert(err3 == nil).IsTrue()
|
|
|
|
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() {
|
2015-10-22 01:14:02 +02:00
|
|
|
build1 := &model.Build{
|
2017-05-23 12:43:58 +02:00
|
|
|
RepoID: repo.ID,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusPending,
|
2015-09-30 03:21:17 +02:00
|
|
|
Branch: "master",
|
|
|
|
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
|
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
build2 := &model.Build{
|
2017-05-23 12:43:58 +02:00
|
|
|
RepoID: repo.ID,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusPending,
|
2015-09-30 03:21:17 +02:00
|
|
|
Branch: "dev",
|
|
|
|
Commit: "85f8c029b902ed9400bc600bac301a0aadb144aa",
|
|
|
|
}
|
2017-04-02 16:13:26 +02:00
|
|
|
err1 := s.CreateBuild(build1, []*model.Proc{}...)
|
|
|
|
err2 := s.CreateBuild(build2, []*model.Proc{}...)
|
2016-03-25 21:54:16 +02:00
|
|
|
getbuild, err3 := s.GetBuildCommit(&model.Repo{ID: 1}, build2.Commit, build2.Branch)
|
2015-09-30 03:21:17 +02:00
|
|
|
g.Assert(err1 == nil).IsTrue()
|
|
|
|
g.Assert(err2 == nil).IsTrue()
|
|
|
|
g.Assert(err3 == nil).IsTrue()
|
|
|
|
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 02:53:28 +02:00
|
|
|
g.It("Should Get the last Build", func() {
|
2015-10-22 01:14:02 +02:00
|
|
|
build1 := &model.Build{
|
2017-05-23 12:43:58 +02:00
|
|
|
RepoID: repo.ID,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusFailure,
|
2015-09-30 03:21:17 +02:00
|
|
|
Branch: "master",
|
|
|
|
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
|
2015-10-28 20:50:11 +02:00
|
|
|
Event: model.EventPush,
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
build2 := &model.Build{
|
2017-05-23 12:43:58 +02:00
|
|
|
RepoID: repo.ID,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusSuccess,
|
2015-09-30 03:21:17 +02:00
|
|
|
Branch: "master",
|
|
|
|
Commit: "85f8c029b902ed9400bc600bac301a0aadb144aa",
|
2015-10-28 20:50:11 +02:00
|
|
|
Event: model.EventPush,
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2017-04-02 16:13:26 +02:00
|
|
|
err1 := s.CreateBuild(build1, []*model.Proc{}...)
|
|
|
|
err2 := s.CreateBuild(build2, []*model.Proc{}...)
|
2016-03-25 21:54:16 +02:00
|
|
|
getbuild, err3 := s.GetBuildLast(&model.Repo{ID: 1}, build2.Branch)
|
2015-09-30 03:21:17 +02:00
|
|
|
g.Assert(err1 == nil).IsTrue()
|
|
|
|
g.Assert(err2 == nil).IsTrue()
|
|
|
|
g.Assert(err3 == nil).IsTrue()
|
|
|
|
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 02:53:28 +02:00
|
|
|
g.It("Should Get the last Build Before Build N", func() {
|
2015-10-22 01:14:02 +02:00
|
|
|
build1 := &model.Build{
|
2017-05-23 12:43:58 +02:00
|
|
|
RepoID: repo.ID,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusFailure,
|
2015-10-08 02:53:28 +02:00
|
|
|
Branch: "master",
|
|
|
|
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
|
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
build2 := &model.Build{
|
2017-05-23 12:43:58 +02:00
|
|
|
RepoID: repo.ID,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusSuccess,
|
2015-10-08 02:53:28 +02:00
|
|
|
Branch: "master",
|
|
|
|
Commit: "85f8c029b902ed9400bc600bac301a0aadb144aa",
|
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
build3 := &model.Build{
|
2017-05-23 12:43:58 +02:00
|
|
|
RepoID: repo.ID,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusRunning,
|
2015-10-08 02:53:28 +02:00
|
|
|
Branch: "master",
|
|
|
|
Commit: "85f8c029b902ed9400bc600bac301a0aadb144aa",
|
|
|
|
}
|
2017-04-02 16:13:26 +02:00
|
|
|
err1 := s.CreateBuild(build1, []*model.Proc{}...)
|
|
|
|
err2 := s.CreateBuild(build2, []*model.Proc{}...)
|
|
|
|
err3 := s.CreateBuild(build3, []*model.Proc{}...)
|
2016-03-25 21:54:16 +02:00
|
|
|
getbuild, err4 := s.GetBuildLastBefore(&model.Repo{ID: 1}, build3.Branch, build3.ID)
|
2015-10-08 02:53:28 +02:00
|
|
|
g.Assert(err1 == nil).IsTrue()
|
|
|
|
g.Assert(err2 == nil).IsTrue()
|
|
|
|
g.Assert(err3 == nil).IsTrue()
|
|
|
|
g.Assert(err4 == nil).IsTrue()
|
|
|
|
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 03:21:17 +02:00
|
|
|
g.It("Should get recent Builds", func() {
|
2015-10-22 01:14:02 +02:00
|
|
|
build1 := &model.Build{
|
2017-05-23 12:43:58 +02:00
|
|
|
RepoID: repo.ID,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusFailure,
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
build2 := &model.Build{
|
2017-05-23 12:43:58 +02:00
|
|
|
RepoID: repo.ID,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusSuccess,
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2017-04-02 16:13:26 +02:00
|
|
|
s.CreateBuild(build1, []*model.Proc{}...)
|
|
|
|
s.CreateBuild(build2, []*model.Proc{}...)
|
2018-02-02 21:03:40 +02:00
|
|
|
builds, err := s.GetBuildList(&model.Repo{ID: 1}, 1)
|
2015-09-30 03:21:17 +02:00
|
|
|
g.Assert(err == nil).IsTrue()
|
|
|
|
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 12:43:58 +02:00
|
|
|
|
|
|
|
func TestBuildIncrement(t *testing.T) {
|
|
|
|
s := newTest()
|
|
|
|
defer func() {
|
|
|
|
s.Exec("delete from repos")
|
|
|
|
s.Exec("delete from builds")
|
|
|
|
s.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
repo := &model.Repo{
|
|
|
|
UserID: 1,
|
|
|
|
FullName: "bradrydzewski/drone",
|
|
|
|
Owner: "bradrydzewski",
|
|
|
|
Name: "drone",
|
|
|
|
}
|
|
|
|
if err := s.CreateRepo(repo); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
num, err := s.incrementRepo(repo.ID, 0, 1)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := num, 1; got != want {
|
|
|
|
t.Errorf("Want repository counter incremented to %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
num, err = s.incrementRepo(repo.ID, 1, 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := num, 2; got != want {
|
|
|
|
t.Errorf("Want repository counter incremented to %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
// this block tests incrementing the repository counter
|
|
|
|
// should fail when attempting to increment the counter
|
|
|
|
// from a stale base.
|
|
|
|
num, _ = s.incrementRepo(repo.ID, 1, 2)
|
|
|
|
if num != 0 {
|
|
|
|
t.Errorf("Want error when trying to increment stale number")
|
|
|
|
}
|
|
|
|
|
|
|
|
// this block tests incrementing the repository counter
|
|
|
|
// using the given repository id with backoff.
|
|
|
|
num, err = s.incrementRepoRetry(repo.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if got, want := num, 3; got != want {
|
|
|
|
t.Errorf("Want repository counter incremented to %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
}
|