From d13c1caebf5cf8f93af81485a8e7cd3511a7d015 Mon Sep 17 00:00:00 2001 From: Daniel Oliveira Date: Wed, 22 Apr 2015 19:49:16 -0600 Subject: [PATCH] About this commit: 1. server/builds.go:92 uses SetStatus(). 2. The other APIs we talked about: Status(), StatusList(), we were able to move out. 3. The repo_del_test.go code merge into repo_test.go 4. Unit tests for the other build APIs added. - We are facing a crash in: github.com/drone/drone/datastore/bolt.(*DB).SetBuildTask(0xc208056080, 0x5e15d0, 0x15, 0x1, 0xc208036940, 0x0, 0x0) which seems to be related to the note in: build.go:207 (// TODO check index to prevent nil pointer / panic) 5. With these new tests we get over 86% plus test cover for bolt package. --- datastore/bolt/build.go | 6 ++-- datastore/bolt/build_test.go | 56 +++++++++++++++++++++++++++-- datastore/bolt/repo_del_test.go | 63 --------------------------------- datastore/bolt/repo_test.go | 53 +++++++++++++++++++++++++++ datastore/datastore.go | 4 +-- 5 files changed, 111 insertions(+), 71 deletions(-) delete mode 100644 datastore/bolt/repo_del_test.go diff --git a/datastore/bolt/build.go b/datastore/bolt/build.go index 3d20a1be0..eb3b49e0a 100644 --- a/datastore/bolt/build.go +++ b/datastore/bolt/build.go @@ -1,7 +1,7 @@ package bolt import ( - "bytes" + //"bytes" "encoding/binary" "strconv" "time" @@ -114,6 +114,7 @@ func (db *DB) SetBuild(repo string, build *common.Build) error { }) } +/* // Status returns the status for the given repository // and build number. func (db *DB) Status(repo string, build int, status string) (*common.Status, error) { @@ -147,7 +148,7 @@ func (db *DB) StatusList(repo string, build int) ([]*common.Status, error) { }) return statuses, err } - +*/ // SetStatus inserts a new build status for the // named repository and build number. If the status already // exists an error is returned. @@ -160,7 +161,6 @@ func (db *DB) SetStatus(repo string, build int, status *common.Status) error { } // Experimental - func (db *DB) SetBuildState(repo string, build *common.Build) error { key := []byte(repo + "/" + strconv.Itoa(build.Number)) diff --git a/datastore/bolt/build_test.go b/datastore/bolt/build_test.go index 8f9d78b12..f26efcc61 100644 --- a/datastore/bolt/build_test.go +++ b/datastore/bolt/build_test.go @@ -1,11 +1,10 @@ package bolt import ( - "os" - "testing" - "github.com/drone/drone/common" . "github.com/franela/goblin" + "os" + "testing" ) func TestBuild(t *testing.T) { @@ -13,6 +12,11 @@ func TestBuild(t *testing.T) { g.Describe("Build", func() { var db *DB // temporary database repo := string("github.com/octopod/hq") + //testUser := &common.User{Login: "octocat"} + //testRepo := &common.Repo{FullName: "github.com/octopod/hq"} + testUser := "octocat" + testRepo := "github.com/octopod/hq" + //testBuild := 1 // create a new database before each unit // test and destroy afterwards. @@ -62,5 +66,51 @@ func TestBuild(t *testing.T) { g.Assert(err).Equal(nil) g.Assert(len(builds)).Equal(3) }) + + 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) + }) + + g.It("Should set build task: SetBuildTask()", 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.SetBuildTask(repo, 1, &common.Task{Number: 1}) + g.Assert(err_).Equal(nil) + err_ = db.SetBuildTask(repo, 1, &common.Task{Number: 2}) + g.Assert(err_).Equal(nil) + err_ = db.SetBuildTask(repo, 2, &common.Task{Number: 1}) + g.Assert(err_).Equal(nil) + }) }) } diff --git a/datastore/bolt/repo_del_test.go b/datastore/bolt/repo_del_test.go deleted file mode 100644 index 22e101087..000000000 --- a/datastore/bolt/repo_del_test.go +++ /dev/null @@ -1,63 +0,0 @@ -package bolt - -import ( - "io/ioutil" - "os" - "testing" - - "github.com/drone/drone/common" - . "github.com/franela/goblin" -) - -func TestRepoDel(t *testing.T) { - g := Goblin(t) - g.Describe("Delete repo", func() { - - var db *DB // temporary database - - user := &common.User{Login: "freya"} - repoUri := string("github.com/octopod/hq") - - // create a new database before each unit - // test and destroy afterwards. - g.BeforeEach(func() { - file, err := ioutil.TempFile(os.TempDir(), "drone-bolt") - if err != nil { - panic(err) - } - - db = Must(file.Name()) - }) - g.AfterEach(func() { - os.Remove(db.Path()) - }) - - g.It("should cleanup", func() { - repo := &common.Repo{FullName: repoUri} - err := db.SetRepoNotExists(user, repo) - g.Assert(err).Equal(nil) - - db.SetBuild(repoUri, &common.Build{State: "success"}) - db.SetBuild(repoUri, &common.Build{State: "success"}) - db.SetBuild(repoUri, &common.Build{State: "pending"}) - - db.SetBuildStatus(repoUri, 1, &common.Status{Context: "success"}) - db.SetBuildStatus(repoUri, 2, &common.Status{Context: "success"}) - db.SetBuildStatus(repoUri, 3, &common.Status{Context: "pending"}) - - // first a little sanity to validate our test conditions - _, err = db.BuildLast(repoUri) - g.Assert(err).Equal(nil) - - // now run our specific test suite - // 1. ensure that we can delete the repo - err = db.DelRepo(repo) - g.Assert(err).Equal(nil) - - // 2. ensure that deleting the repo cleans up other references - _, err = db.Build(repoUri, 1) - g.Assert(err).Equal(ErrKeyNotFound) - }) - }) - -} diff --git a/datastore/bolt/repo_test.go b/datastore/bolt/repo_test.go index ab45babc7..ed53a10f6 100644 --- a/datastore/bolt/repo_test.go +++ b/datastore/bolt/repo_test.go @@ -3,6 +3,7 @@ package bolt import ( "github.com/drone/drone/common" . "github.com/franela/goblin" + "io/ioutil" "os" "testing" ) @@ -128,3 +129,55 @@ func TestRepo(t *testing.T) { }) } + +func TestRepoDel(t *testing.T) { + g := Goblin(t) + g.Describe("Delete repo", func() { + + var db *DB // temporary database + + user := &common.User{Login: "freya"} + repoUri := string("github.com/octopod/hq") + + // create a new database before each unit + // test and destroy afterwards. + g.BeforeEach(func() { + file, err := ioutil.TempFile(os.TempDir(), "drone-bolt") + if err != nil { + panic(err) + } + + db = Must(file.Name()) + }) + g.AfterEach(func() { + os.Remove(db.Path()) + }) + + g.It("should cleanup", func() { + repo := &common.Repo{FullName: repoUri} + err := db.SetRepoNotExists(user, repo) + g.Assert(err).Equal(nil) + + db.SetBuild(repoUri, &common.Build{State: "success"}) + db.SetBuild(repoUri, &common.Build{State: "success"}) + db.SetBuild(repoUri, &common.Build{State: "pending"}) + + db.SetBuildStatus(repoUri, 1, &common.Status{Context: "success"}) + db.SetBuildStatus(repoUri, 2, &common.Status{Context: "success"}) + db.SetBuildStatus(repoUri, 3, &common.Status{Context: "pending"}) + + // first a little sanity to validate our test conditions + _, err = db.BuildLast(repoUri) + g.Assert(err).Equal(nil) + + // now run our specific test suite + // 1. ensure that we can delete the repo + err = db.DelRepo(repo) + g.Assert(err).Equal(nil) + + // 2. ensure that deleting the repo cleans up other references + _, err = db.Build(repoUri, 1) + g.Assert(err).Equal(ErrKeyNotFound) + }) + }) +} diff --git a/datastore/datastore.go b/datastore/datastore.go index 32d744984..ed4f121c7 100644 --- a/datastore/datastore.go +++ b/datastore/datastore.go @@ -108,11 +108,11 @@ type Datastore interface { // Status returns the status for the given repository // and build number. - Status(string, int, string) (*common.Status, error) + ////Status(string, int, string) (*common.Status, error) // StatusList returned a list of all build statues for // the given repository and build number. - StatusList(string, int) ([]*common.Status, error) + ////StatusList(string, int) ([]*common.Status, error) // SetStatus inserts a new build status for the // named repository and build number. If the status already