2015-10-22 01:14:02 +02:00
|
|
|
package datastore
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2015-10-22 01:14:02 +02:00
|
|
|
func Test_buildstore(t *testing.T) {
|
|
|
|
db := openTest()
|
2015-09-30 03:21:17 +02:00
|
|
|
defer db.Close()
|
|
|
|
|
2015-10-22 01:14:02 +02:00
|
|
|
s := From(db)
|
2015-09-30 03:21:17 +02:00
|
|
|
g := goblin.Goblin(t)
|
|
|
|
g.Describe("Builds", func() {
|
|
|
|
|
|
|
|
// before each test be sure to purge the package
|
|
|
|
// table data from the database.
|
|
|
|
g.BeforeEach(func() {
|
|
|
|
db.Exec("DELETE FROM builds")
|
|
|
|
db.Exec("DELETE FROM jobs")
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Post a Build", func() {
|
2015-10-22 01:14:02 +02:00
|
|
|
build := model.Build{
|
2015-09-30 03:21:17 +02:00
|
|
|
RepoID: 1,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusSuccess,
|
2015-09-30 03:21:17 +02:00
|
|
|
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
|
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
err := s.Builds().Create(&build, []*model.Job{}...)
|
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")
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Put a Build", func() {
|
2015-10-22 01:14:02 +02:00
|
|
|
build := model.Build{
|
2015-09-30 03:21:17 +02:00
|
|
|
RepoID: 1,
|
|
|
|
Number: 5,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusSuccess,
|
2015-09-30 03:21:17 +02:00
|
|
|
Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac",
|
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
s.Builds().Create(&build, []*model.Job{}...)
|
|
|
|
build.Status = model.StatusRunning
|
|
|
|
err1 := s.Builds().Update(&build)
|
|
|
|
getbuild, err2 := s.Builds().Get(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{
|
2015-09-30 03:21:17 +02:00
|
|
|
RepoID: 1,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusSuccess,
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
s.Builds().Create(&build, []*model.Job{}...)
|
|
|
|
getbuild, err := s.Builds().Get(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{
|
2015-09-30 03:21:17 +02:00
|
|
|
RepoID: 1,
|
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{
|
2015-09-30 03:21:17 +02:00
|
|
|
RepoID: 1,
|
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
|
|
|
err1 := s.Builds().Create(build1, []*model.Job{}...)
|
|
|
|
err2 := s.Builds().Create(build2, []*model.Job{}...)
|
|
|
|
getbuild, err3 := s.Builds().GetNumber(&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{
|
2015-09-30 03:21:17 +02:00
|
|
|
RepoID: 1,
|
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{
|
2015-09-30 03:21:17 +02:00
|
|
|
RepoID: 1,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusPending,
|
2015-09-30 03:21:17 +02:00
|
|
|
Ref: "refs/pull/6",
|
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
err1 := s.Builds().Create(build1, []*model.Job{}...)
|
|
|
|
err2 := s.Builds().Create(build2, []*model.Job{}...)
|
|
|
|
getbuild, err3 := s.Builds().GetRef(&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{
|
2015-09-30 03:21:17 +02:00
|
|
|
RepoID: 1,
|
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{
|
2015-09-30 03:21:17 +02:00
|
|
|
RepoID: 1,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusPending,
|
2015-09-30 03:21:17 +02:00
|
|
|
Ref: "refs/pull/6",
|
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
err1 := s.Builds().Create(build1, []*model.Job{}...)
|
|
|
|
err2 := s.Builds().Create(build2, []*model.Job{}...)
|
|
|
|
getbuild, err3 := s.Builds().GetRef(&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{
|
2015-09-30 03:21:17 +02:00
|
|
|
RepoID: 1,
|
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{
|
2015-09-30 03:21:17 +02:00
|
|
|
RepoID: 1,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusPending,
|
2015-09-30 03:21:17 +02:00
|
|
|
Branch: "dev",
|
|
|
|
Commit: "85f8c029b902ed9400bc600bac301a0aadb144aa",
|
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
err1 := s.Builds().Create(build1, []*model.Job{}...)
|
|
|
|
err2 := s.Builds().Create(build2, []*model.Job{}...)
|
|
|
|
getbuild, err3 := s.Builds().GetCommit(&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{
|
2015-09-30 03:21:17 +02:00
|
|
|
RepoID: 1,
|
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-22 01:14:02 +02:00
|
|
|
build2 := &model.Build{
|
2015-09-30 03:21:17 +02:00
|
|
|
RepoID: 1,
|
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-22 01:14:02 +02:00
|
|
|
err1 := s.Builds().Create(build1, []*model.Job{}...)
|
|
|
|
err2 := s.Builds().Create(build2, []*model.Job{}...)
|
|
|
|
getbuild, err3 := s.Builds().GetLast(&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{
|
2015-10-08 02:53:28 +02:00
|
|
|
RepoID: 1,
|
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{
|
2015-10-08 02:53:28 +02:00
|
|
|
RepoID: 1,
|
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{
|
2015-10-08 02:53:28 +02:00
|
|
|
RepoID: 1,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusRunning,
|
2015-10-08 02:53:28 +02:00
|
|
|
Branch: "master",
|
|
|
|
Commit: "85f8c029b902ed9400bc600bac301a0aadb144aa",
|
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
err1 := s.Builds().Create(build1, []*model.Job{}...)
|
|
|
|
err2 := s.Builds().Create(build2, []*model.Job{}...)
|
|
|
|
err3 := s.Builds().Create(build3, []*model.Job{}...)
|
|
|
|
getbuild, err4 := s.Builds().GetLastBefore(&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{
|
2015-09-30 03:21:17 +02:00
|
|
|
RepoID: 1,
|
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{
|
2015-09-30 03:21:17 +02:00
|
|
|
RepoID: 1,
|
2015-10-22 01:14:02 +02:00
|
|
|
Status: model.StatusSuccess,
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
s.Builds().Create(build1, []*model.Job{}...)
|
|
|
|
s.Builds().Create(build2, []*model.Job{}...)
|
|
|
|
builds, err := s.Builds().GetList(&model.Repo{ID: 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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|