2015-05-14 17:52:45 +00:00
|
|
|
package builtin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2015-06-13 23:48:06 +00:00
|
|
|
|
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/franela/goblin"
|
|
|
|
"github.com/drone/drone/pkg/types"
|
2015-05-14 17:52:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuildstore(t *testing.T) {
|
|
|
|
db := mustConnectTest()
|
2015-06-19 00:36:52 +00:00
|
|
|
bs := NewJobstore(db)
|
2015-05-14 17:52:45 +00:00
|
|
|
cs := NewCommitstore(db)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
g := goblin.Goblin(t)
|
2015-06-19 00:36:52 +00:00
|
|
|
g.Describe("Jobstore", func() {
|
2015-05-14 17:52:45 +00:00
|
|
|
|
|
|
|
// before each test we purge the package table data from the database.
|
|
|
|
g.BeforeEach(func() {
|
2015-06-19 00:36:52 +00:00
|
|
|
db.Exec("DELETE FROM jobs")
|
2015-05-14 17:52:45 +00:00
|
|
|
db.Exec("DELETE FROM commits")
|
|
|
|
})
|
|
|
|
|
2015-06-19 00:36:52 +00:00
|
|
|
g.It("Should Set a job", func() {
|
|
|
|
job := &types.Job{
|
|
|
|
BuildID: 1,
|
|
|
|
Status: "pending",
|
2015-06-13 23:48:06 +00:00
|
|
|
ExitCode: 0,
|
2015-06-19 00:36:52 +00:00
|
|
|
Number: 1,
|
2015-05-15 05:31:18 +00:00
|
|
|
}
|
2015-06-19 00:36:52 +00:00
|
|
|
err1 := bs.AddJob(job)
|
2015-05-14 18:24:32 +00:00
|
|
|
g.Assert(err1 == nil).IsTrue()
|
2015-06-19 00:36:52 +00:00
|
|
|
g.Assert(job.ID != 0).IsTrue()
|
2015-06-13 23:48:06 +00:00
|
|
|
|
2015-06-19 00:36:52 +00:00
|
|
|
job.Status = "started"
|
|
|
|
err2 := bs.SetJob(job)
|
2015-05-15 05:31:18 +00:00
|
|
|
g.Assert(err2 == nil).IsTrue()
|
2015-06-13 23:48:06 +00:00
|
|
|
|
2015-06-19 00:36:52 +00:00
|
|
|
getjob, err3 := bs.Job(job.ID)
|
2015-06-13 23:48:06 +00:00
|
|
|
g.Assert(err3 == nil).IsTrue()
|
2015-06-19 00:36:52 +00:00
|
|
|
g.Assert(getjob.Status).Equal(job.Status)
|
2015-05-14 17:52:45 +00:00
|
|
|
})
|
|
|
|
|
2015-06-19 00:36:52 +00:00
|
|
|
g.It("Should Get a Job by ID", func() {
|
|
|
|
job := &types.Job{
|
|
|
|
BuildID: 1,
|
|
|
|
Status: "pending",
|
2015-06-13 23:48:06 +00:00
|
|
|
ExitCode: 1,
|
2015-06-19 00:36:52 +00:00
|
|
|
Number: 1,
|
2015-06-13 23:48:06 +00:00
|
|
|
Environment: map[string]string{"foo": "bar"},
|
2015-05-14 17:52:45 +00:00
|
|
|
}
|
2015-06-19 00:36:52 +00:00
|
|
|
err1 := bs.AddJob(job)
|
2015-05-14 18:24:32 +00:00
|
|
|
g.Assert(err1 == nil).IsTrue()
|
2015-06-19 00:36:52 +00:00
|
|
|
g.Assert(job.ID != 0).IsTrue()
|
2015-06-13 23:48:06 +00:00
|
|
|
|
2015-06-19 00:36:52 +00:00
|
|
|
getjob, err2 := bs.Job(job.ID)
|
2015-05-14 18:24:32 +00:00
|
|
|
g.Assert(err2 == nil).IsTrue()
|
2015-06-19 00:36:52 +00:00
|
|
|
g.Assert(getjob.ID).Equal(job.ID)
|
|
|
|
g.Assert(getjob.Status).Equal(job.Status)
|
|
|
|
g.Assert(getjob.ExitCode).Equal(job.ExitCode)
|
|
|
|
g.Assert(getjob.Environment).Equal(job.Environment)
|
|
|
|
g.Assert(getjob.Environment["foo"]).Equal("bar")
|
2015-05-14 17:52:45 +00:00
|
|
|
})
|
|
|
|
|
2015-06-19 00:36:52 +00:00
|
|
|
g.It("Should Get a Job by Number", func() {
|
|
|
|
job := &types.Job{
|
|
|
|
BuildID: 1,
|
|
|
|
Status: "pending",
|
2015-06-13 23:48:06 +00:00
|
|
|
ExitCode: 1,
|
2015-06-19 00:36:52 +00:00
|
|
|
Number: 1,
|
2015-05-14 17:52:45 +00:00
|
|
|
}
|
2015-06-19 00:36:52 +00:00
|
|
|
err1 := bs.AddJob(job)
|
2015-05-14 18:24:32 +00:00
|
|
|
g.Assert(err1 == nil).IsTrue()
|
2015-06-19 00:36:52 +00:00
|
|
|
g.Assert(job.ID != 0).IsTrue()
|
2015-06-13 23:48:06 +00:00
|
|
|
|
2015-06-19 00:36:52 +00:00
|
|
|
getjob, err2 := bs.JobNumber(&types.Commit{ID: 1}, 1)
|
2015-05-14 18:24:32 +00:00
|
|
|
g.Assert(err2 == nil).IsTrue()
|
2015-06-19 00:36:52 +00:00
|
|
|
g.Assert(getjob.ID).Equal(job.ID)
|
|
|
|
g.Assert(getjob.Status).Equal(job.Status)
|
2015-05-14 17:52:45 +00:00
|
|
|
})
|
|
|
|
|
2015-06-19 00:36:52 +00:00
|
|
|
g.It("Should Get a List of Jobs by Commit", func() {
|
|
|
|
|
2015-05-14 17:52:45 +00:00
|
|
|
//In order for buid to be populated,
|
|
|
|
//The AddCommit command will insert builds
|
|
|
|
//if the Commit.Builds array is populated
|
|
|
|
//Add Commit.
|
2015-06-19 00:36:52 +00:00
|
|
|
commit := types.Commit{
|
2015-05-14 17:52:45 +00:00
|
|
|
RepoID: 1,
|
2015-06-14 00:37:36 +00:00
|
|
|
State: types.StateSuccess,
|
2015-05-14 17:52:45 +00:00
|
|
|
Ref: "refs/heads/master",
|
|
|
|
Sha: "14710626f22791619d3b7e9ccf58b10374e5b76d",
|
2015-06-19 00:36:52 +00:00
|
|
|
Builds: []*types.Job{
|
|
|
|
&types.Job{
|
|
|
|
BuildID: 1,
|
|
|
|
Status: "success",
|
|
|
|
ExitCode: 0,
|
|
|
|
Number: 1,
|
|
|
|
},
|
|
|
|
&types.Job{
|
|
|
|
BuildID: 3,
|
|
|
|
Status: "error",
|
|
|
|
ExitCode: 1,
|
|
|
|
Number: 2,
|
|
|
|
},
|
|
|
|
&types.Job{
|
|
|
|
BuildID: 5,
|
|
|
|
Status: "pending",
|
|
|
|
ExitCode: 0,
|
|
|
|
Number: 3,
|
|
|
|
},
|
|
|
|
},
|
2015-05-14 17:52:45 +00:00
|
|
|
}
|
|
|
|
//
|
2015-06-19 00:36:52 +00:00
|
|
|
err1 := cs.AddCommit(&commit)
|
2015-05-14 18:24:32 +00:00
|
|
|
g.Assert(err1 == nil).IsTrue()
|
2015-06-19 00:36:52 +00:00
|
|
|
getjobs, err2 := bs.JobList(&commit)
|
2015-05-14 18:24:32 +00:00
|
|
|
g.Assert(err2 == nil).IsTrue()
|
2015-06-19 00:36:52 +00:00
|
|
|
g.Assert(len(getjobs)).Equal(3)
|
|
|
|
g.Assert(getjobs[0].Number).Equal(1)
|
|
|
|
g.Assert(getjobs[0].Status).Equal(types.StateSuccess)
|
2015-05-14 17:52:45 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|