2015-04-07 08:20:55 +00:00
|
|
|
package bolt
|
2015-04-10 16:14:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/drone/drone/common"
|
|
|
|
. "github.com/franela/goblin"
|
2015-04-23 01:49:16 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
2015-04-10 16:14:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuild(t *testing.T) {
|
|
|
|
g := Goblin(t)
|
|
|
|
g.Describe("Build", func() {
|
|
|
|
var db *DB // temporary database
|
|
|
|
repo := string("github.com/octopod/hq")
|
2015-04-23 01:49:16 +00:00
|
|
|
//testUser := &common.User{Login: "octocat"}
|
|
|
|
//testRepo := &common.Repo{FullName: "github.com/octopod/hq"}
|
|
|
|
testUser := "octocat"
|
|
|
|
testRepo := "github.com/octopod/hq"
|
|
|
|
//testBuild := 1
|
2015-04-10 16:14:02 +00:00
|
|
|
|
|
|
|
// create a new database before each unit
|
|
|
|
// test and destroy afterwards.
|
|
|
|
g.BeforeEach(func() {
|
|
|
|
db = Must("/tmp/drone.test.db")
|
|
|
|
})
|
|
|
|
g.AfterEach(func() {
|
|
|
|
os.Remove(db.Path())
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should sequence builds", func() {
|
2015-04-15 05:04:38 +00:00
|
|
|
err := db.SetBuild(repo, &common.Build{State: "pending"})
|
2015-04-10 16:14:02 +00:00
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
|
|
|
|
// the first build should always be numero 1
|
2015-04-15 05:04:38 +00:00
|
|
|
build, err := db.Build(repo, 1)
|
2015-04-10 16:14:02 +00:00
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
g.Assert(build.State).Equal("pending")
|
|
|
|
|
|
|
|
// add another build, just for fun
|
2015-04-15 05:04:38 +00:00
|
|
|
err = db.SetBuild(repo, &common.Build{State: "success"})
|
2015-04-10 16:14:02 +00:00
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
|
|
|
|
// get the next build
|
2015-04-15 05:04:38 +00:00
|
|
|
build, err = db.Build(repo, 2)
|
2015-04-10 16:14:02 +00:00
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
g.Assert(build.State).Equal("success")
|
|
|
|
})
|
|
|
|
|
2015-04-11 05:22:55 +00:00
|
|
|
g.It("Should get the latest builds", func() {
|
2015-04-15 05:04:38 +00:00
|
|
|
db.SetBuild(repo, &common.Build{State: "success"})
|
|
|
|
db.SetBuild(repo, &common.Build{State: "success"})
|
|
|
|
db.SetBuild(repo, &common.Build{State: "pending"})
|
2015-04-11 05:22:55 +00:00
|
|
|
|
2015-04-15 05:04:38 +00:00
|
|
|
build, err := db.BuildLast(repo)
|
2015-04-11 05:22:55 +00:00
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
g.Assert(build.State).Equal("pending")
|
|
|
|
g.Assert(build.Number).Equal(3)
|
|
|
|
})
|
2015-04-11 06:11:41 +00:00
|
|
|
|
|
|
|
g.It("Should get the recent list of builds", func() {
|
2015-04-15 05:04:38 +00:00
|
|
|
db.SetBuild(repo, &common.Build{State: "success"})
|
|
|
|
db.SetBuild(repo, &common.Build{State: "success"})
|
|
|
|
db.SetBuild(repo, &common.Build{State: "pending"})
|
2015-04-11 06:11:41 +00:00
|
|
|
|
2015-04-15 05:04:38 +00:00
|
|
|
builds, err := db.BuildList(repo)
|
2015-04-11 06:11:41 +00:00
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
g.Assert(len(builds)).Equal(3)
|
|
|
|
})
|
2015-04-23 01:49:16 +00:00
|
|
|
|
|
|
|
g.It("Should set build status: SetBuildStatus()", func() {
|
|
|
|
//err := db.SetRepoNotExists(testUser, testRepo)
|
|
|
|
err := db.SetRepoNotExists(&common.User{Login: testUser}, &common.Repo{FullName: testRepo})
|
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
|
|
|
|
db.SetBuild(repo, &common.Build{State: "error"})
|
|
|
|
db.SetBuild(repo, &common.Build{State: "pending"})
|
|
|
|
db.SetBuild(repo, &common.Build{State: "success"})
|
|
|
|
err_ := db.SetBuildStatus(repo, 1, &common.Status{Context: "pending"})
|
|
|
|
g.Assert(err_).Equal(nil)
|
|
|
|
err_ = db.SetBuildStatus(repo, 2, &common.Status{Context: "running"})
|
|
|
|
g.Assert(err_).Equal(nil)
|
|
|
|
err_ = db.SetBuildStatus(repo, 3, &common.Status{Context: "success"})
|
|
|
|
g.Assert(err_).Equal(nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should set build state: SetBuildState()", func() {
|
|
|
|
err := db.SetRepoNotExists(&common.User{Login: testUser}, &common.Repo{FullName: testRepo})
|
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
|
|
|
|
db.SetBuild(repo, &common.Build{State: "error"})
|
|
|
|
db.SetBuild(repo, &common.Build{State: "pending"})
|
|
|
|
db.SetBuild(repo, &common.Build{State: "success"})
|
|
|
|
err_ := db.SetBuildState(repo, &common.Build{Number: 1})
|
|
|
|
g.Assert(err_).Equal(nil)
|
|
|
|
err_ = db.SetBuildState(repo, &common.Build{Number: 2})
|
|
|
|
g.Assert(err_).Equal(nil)
|
|
|
|
err_ = db.SetBuildState(repo, &common.Build{Number: 3})
|
|
|
|
g.Assert(err_).Equal(nil)
|
|
|
|
})
|
|
|
|
|
2015-04-23 18:15:17 +00:00
|
|
|
g.It("Should set build task: SetBuildTask()", func() {
|
2015-04-27 18:34:33 +00:00
|
|
|
err_ := db.SetRepoNotExists(&common.User{Login: testUser}, &common.Repo{FullName: testRepo})
|
|
|
|
g.Assert(err_).Equal(nil)
|
|
|
|
// setting up tasks.
|
|
|
|
tasks := []*common.Task{
|
|
|
|
&common.Task{
|
|
|
|
Number: 1,
|
|
|
|
State: "pending",
|
|
|
|
ExitCode: 0,
|
|
|
|
},
|
|
|
|
&common.Task{
|
|
|
|
Number: 2,
|
|
|
|
State: "running",
|
|
|
|
ExitCode: 0,
|
|
|
|
},
|
|
|
|
&common.Task{
|
|
|
|
Number: 3,
|
|
|
|
State: "success",
|
|
|
|
ExitCode: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// setting up builds.
|
|
|
|
err_ = db.SetBuild(repo, &common.Build{Number: 1, State: "failed", Tasks: tasks})
|
|
|
|
g.Assert(err_).Equal(nil)
|
|
|
|
err_ = db.SetBuildTask(repo, 1, &common.Task{Number: 1, State: "error", ExitCode: -1})
|
|
|
|
g.Assert(err_).Equal(nil)
|
|
|
|
db.SetBuild(repo, &common.Build{Number: 2, State: "success", Tasks: tasks})
|
|
|
|
err_ = db.SetBuildTask(repo, 2, &common.Task{Number: 1, State: "success", ExitCode: 0})
|
2015-04-23 18:15:17 +00:00
|
|
|
g.Assert(err_).Equal(nil)
|
|
|
|
})
|
2015-04-10 16:14:02 +00:00
|
|
|
})
|
|
|
|
}
|