2015-10-21 23:14:02 +00:00
|
|
|
package datastore
|
2015-09-30 01:21:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2015-10-21 23:14:02 +00:00
|
|
|
"github.com/drone/drone/model"
|
2015-09-30 01:21:17 +00:00
|
|
|
"github.com/franela/goblin"
|
|
|
|
)
|
|
|
|
|
2016-03-25 19:54:16 +00:00
|
|
|
func TestJobs(t *testing.T) {
|
2015-10-21 23:14:02 +00:00
|
|
|
db := openTest()
|
2015-09-30 01:21:17 +00:00
|
|
|
defer db.Close()
|
|
|
|
|
2015-10-21 23:14:02 +00:00
|
|
|
s := From(db)
|
2015-09-30 01:21:17 +00:00
|
|
|
g := goblin.Goblin(t)
|
|
|
|
g.Describe("Job", func() {
|
|
|
|
|
|
|
|
// before each test we purge the package table data from the database.
|
|
|
|
g.BeforeEach(func() {
|
|
|
|
db.Exec("DELETE FROM jobs")
|
|
|
|
db.Exec("DELETE FROM builds")
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Set a job", func() {
|
2015-10-21 23:14:02 +00:00
|
|
|
job := &model.Job{
|
2015-09-30 01:21:17 +00:00
|
|
|
BuildID: 1,
|
|
|
|
Status: "pending",
|
|
|
|
ExitCode: 0,
|
|
|
|
Number: 1,
|
|
|
|
}
|
2016-03-25 19:54:16 +00:00
|
|
|
err1 := s.CreateJob(job)
|
2015-09-30 01:21:17 +00:00
|
|
|
g.Assert(err1 == nil).IsTrue()
|
|
|
|
g.Assert(job.ID != 0).IsTrue()
|
|
|
|
|
|
|
|
job.Status = "started"
|
2016-03-25 19:54:16 +00:00
|
|
|
err2 := s.UpdateJob(job)
|
2015-09-30 01:21:17 +00:00
|
|
|
g.Assert(err2 == nil).IsTrue()
|
|
|
|
|
2016-03-25 19:54:16 +00:00
|
|
|
getjob, err3 := s.GetJob(job.ID)
|
2015-09-30 01:21:17 +00:00
|
|
|
g.Assert(err3 == nil).IsTrue()
|
|
|
|
g.Assert(getjob.Status).Equal(job.Status)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Get a Job by ID", func() {
|
2015-10-21 23:14:02 +00:00
|
|
|
job := &model.Job{
|
2015-09-30 01:21:17 +00:00
|
|
|
BuildID: 1,
|
|
|
|
Status: "pending",
|
|
|
|
ExitCode: 1,
|
|
|
|
Number: 1,
|
|
|
|
Environment: map[string]string{"foo": "bar"},
|
|
|
|
}
|
2016-03-25 19:54:16 +00:00
|
|
|
err1 := s.CreateJob(job)
|
2015-09-30 01:21:17 +00:00
|
|
|
g.Assert(err1 == nil).IsTrue()
|
|
|
|
g.Assert(job.ID != 0).IsTrue()
|
|
|
|
|
2016-03-25 19:54:16 +00:00
|
|
|
getjob, err2 := s.GetJob(job.ID)
|
2015-09-30 01:21:17 +00:00
|
|
|
g.Assert(err2 == nil).IsTrue()
|
|
|
|
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")
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Get a Job by Number", func() {
|
2015-10-21 23:14:02 +00:00
|
|
|
job := &model.Job{
|
2015-09-30 01:21:17 +00:00
|
|
|
BuildID: 1,
|
|
|
|
Status: "pending",
|
|
|
|
ExitCode: 1,
|
|
|
|
Number: 1,
|
|
|
|
}
|
2016-03-25 19:54:16 +00:00
|
|
|
err1 := s.CreateJob(job)
|
2015-09-30 01:21:17 +00:00
|
|
|
g.Assert(err1 == nil).IsTrue()
|
|
|
|
g.Assert(job.ID != 0).IsTrue()
|
|
|
|
|
2016-03-25 19:54:16 +00:00
|
|
|
getjob, err2 := s.GetJobNumber(&model.Build{ID: 1}, 1)
|
2015-09-30 01:21:17 +00:00
|
|
|
g.Assert(err2 == nil).IsTrue()
|
|
|
|
g.Assert(getjob.ID).Equal(job.ID)
|
|
|
|
g.Assert(getjob.Status).Equal(job.Status)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should Get a List of Jobs by Commit", func() {
|
|
|
|
|
2015-10-21 23:14:02 +00:00
|
|
|
build := model.Build{
|
2015-09-30 01:21:17 +00:00
|
|
|
RepoID: 1,
|
2015-10-21 23:14:02 +00:00
|
|
|
Status: model.StatusSuccess,
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
2015-10-21 23:14:02 +00:00
|
|
|
jobs := []*model.Job{
|
2016-03-27 00:24:09 +00:00
|
|
|
{
|
2015-09-30 01:21:17 +00:00
|
|
|
BuildID: 1,
|
|
|
|
Status: "success",
|
|
|
|
ExitCode: 0,
|
|
|
|
Number: 1,
|
|
|
|
},
|
2016-03-27 00:24:09 +00:00
|
|
|
{
|
2015-09-30 01:21:17 +00:00
|
|
|
BuildID: 3,
|
|
|
|
Status: "error",
|
|
|
|
ExitCode: 1,
|
|
|
|
Number: 2,
|
|
|
|
},
|
2016-03-27 00:24:09 +00:00
|
|
|
{
|
2015-09-30 01:21:17 +00:00
|
|
|
BuildID: 5,
|
|
|
|
Status: "pending",
|
|
|
|
ExitCode: 0,
|
|
|
|
Number: 3,
|
|
|
|
},
|
|
|
|
}
|
2015-10-21 23:14:02 +00:00
|
|
|
|
2016-03-25 19:54:16 +00:00
|
|
|
err1 := s.CreateBuild(&build, jobs...)
|
2015-09-30 01:21:17 +00:00
|
|
|
g.Assert(err1 == nil).IsTrue()
|
2016-03-25 19:54:16 +00:00
|
|
|
getjobs, err2 := s.GetJobList(&build)
|
2015-09-30 01:21:17 +00:00
|
|
|
g.Assert(err2 == nil).IsTrue()
|
|
|
|
g.Assert(len(getjobs)).Equal(3)
|
|
|
|
g.Assert(getjobs[0].Number).Equal(1)
|
2015-10-21 23:14:02 +00:00
|
|
|
g.Assert(getjobs[0].Status).Equal(model.StatusSuccess)
|
2015-09-30 01:21:17 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|