From d70a6bb5f7343ffe1c8a08600b58cde1f6012b8f Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Tue, 11 Mar 2014 23:00:06 -0700 Subject: [PATCH 1/7] adding github testing --- pkg/database/testing/testing.go | 2 +- pkg/handler/repos.go | 15 +- pkg/handler/testing/github_test.go | 440 +++++++++++++++++++++++++++++ 3 files changed, 447 insertions(+), 10 deletions(-) create mode 100644 pkg/handler/testing/github_test.go diff --git a/pkg/database/testing/testing.go b/pkg/database/testing/testing.go index 69fde3787..e7deff75c 100644 --- a/pkg/database/testing/testing.go +++ b/pkg/database/testing/testing.go @@ -100,7 +100,7 @@ func Setup() { database.SaveMember(user1.ID, team2.ID, RoleOwner) database.SaveMember(user2.ID, team2.ID, RoleAdmin) database.SaveMember(user3.ID, team2.ID, RoleWrite) - database.SaveMember(user1.ID, team3.ID, RoleOwner) + database.SaveMember(user1.ID, team3.ID, RoleRead) // create dummy repo data repo1 := Repo{ diff --git a/pkg/handler/repos.go b/pkg/handler/repos.go index c49116b12..de232b8e5 100644 --- a/pkg/handler/repos.go +++ b/pkg/handler/repos.go @@ -2,7 +2,6 @@ package handler import ( "fmt" - "log" "net/http" "github.com/drone/drone/pkg/channel" @@ -87,7 +86,7 @@ func RepoCreateGithub(w http.ResponseWriter, r *http.Request, u *User) error { client.ApiUrl = settings.GitHubApiUrl githubRepo, err := client.Repos.Find(owner, name) if err != nil { - return err + return fmt.Errorf("Unable to find GitHub repository %s/%s.", owner, name) } repo, err := NewGitHubRepo(settings.GitHubDomain, owner, name, githubRepo.Private) @@ -104,13 +103,12 @@ func RepoCreateGithub(w http.ResponseWriter, r *http.Request, u *User) error { if len(teamName) > 0 { team, err := database.GetTeamSlug(teamName) if err != nil { - log.Printf("error retrieving team %s", teamName) - return err + return fmt.Errorf("Unable to find Team %s.", teamName) } // user must be an admin member of the team if ok, _ := database.IsMemberAdmin(u.ID, team.ID); !ok { - return fmt.Errorf("Forbidden") + return fmt.Errorf("Invalid permission to access Team %s.", teamName) } repo.TeamID = team.ID @@ -125,7 +123,7 @@ func RepoCreateGithub(w http.ResponseWriter, r *http.Request, u *User) error { // create the github key, or update if one already exists _, err := client.RepoKeys.CreateUpdate(owner, name, repo.PublicKey, keyName) if err != nil { - return fmt.Errorf("Unable to add Public Key to your GitHub repository") + return fmt.Errorf("Unable to add Public Key to your GitHub repository.") } } else { @@ -137,13 +135,12 @@ func RepoCreateGithub(w http.ResponseWriter, r *http.Request, u *User) error { // add the hook if _, err := client.Hooks.CreateUpdate(owner, name, link); err != nil { - return fmt.Errorf("Unable to add Hook to your GitHub repository. %s", err.Error()) + return fmt.Errorf("Unable to add Hook to your GitHub repository.") } // Save to the database if err := database.SaveRepo(repo); err != nil { - log.Print("error saving new repository to the database") - return err + return fmt.Errorf("Error saving repository to the database. %s", err) } return RenderText(w, http.StatusText(http.StatusOK), http.StatusOK) diff --git a/pkg/handler/testing/github_test.go b/pkg/handler/testing/github_test.go new file mode 100644 index 000000000..abcf84a15 --- /dev/null +++ b/pkg/handler/testing/github_test.go @@ -0,0 +1,440 @@ +package testing + +import ( + "database/sql" + "fmt" + "net/http" + "net/http/httptest" + "net/url" + "testing" + + "github.com/drone/drone/pkg/database" + "github.com/drone/drone/pkg/handler" + "github.com/drone/drone/pkg/model" + + dbtest "github.com/drone/drone/pkg/database/testing" + . "github.com/smartystreets/goconvey/convey" +) + +// Tests the ability to create GitHub repositories. +func Test_GitHubCreate(t *testing.T) { + // seed the database with values + SetupFixtures() + defer TeardownFixtures() + + // mock request + req := http.Request{} + req.Form = url.Values{} + + // get user that will add repositories + user, _ := database.GetUser(1) + settings := database.SettingsMust() + + Convey("Given request to setup github repo", t, func() { + + Convey("When repository is public", func() { + req.Form.Set("owner", "example") + req.Form.Set("name", "public") + req.Form.Set("team", "") + res := httptest.NewRecorder() + err := handler.RepoCreateGithub(res, &req, user) + repo, _ := database.GetRepoSlug(settings.GitHubDomain + "/example/public") + + Convey("The repository is created", func() { + So(err, ShouldBeNil) + So(repo, ShouldNotBeNil) + So(repo.ID, ShouldNotEqual, 0) + So(repo.Owner, ShouldEqual, "example") + So(repo.Name, ShouldEqual, "public") + So(repo.Host, ShouldEqual, settings.GitHubDomain) + So(repo.TeamID, ShouldEqual, 0) + So(repo.UserID, ShouldEqual, user.ID) + So(repo.Private, ShouldEqual, false) + So(repo.SCM, ShouldEqual, "git") + }) + Convey("The repository is public", func() { + So(repo.Private, ShouldEqual, false) + }) + }) + + Convey("When repository is private", func() { + req.Form.Set("owner", "example") + req.Form.Set("name", "private") + req.Form.Set("team", "") + res := httptest.NewRecorder() + err := handler.RepoCreateGithub(res, &req, user) + repo, _ := database.GetRepoSlug(settings.GitHubDomain + "/example/private") + + Convey("The repository is created", func() { + So(err, ShouldBeNil) + So(repo, ShouldNotBeNil) + So(repo.ID, ShouldNotEqual, 0) + }) + Convey("The repository is private", func() { + So(repo.Private, ShouldEqual, true) + }) + }) + + Convey("When repository is not found", func() { + req.Form.Set("owner", "example") + req.Form.Set("name", "notfound") + req.Form.Set("team", "") + res := httptest.NewRecorder() + err := handler.RepoCreateGithub(res, &req, user) + + Convey("The result is an error", func() { + So(err, ShouldNotBeNil) + So(err.Error(), ShouldEqual, "Unable to find GitHub repository example/notfound.") + }) + + Convey("The repository is not created", func() { + _, err := database.GetRepoSlug("example/notfound") + So(err, ShouldNotBeNil) + So(err, ShouldEqual, sql.ErrNoRows) + }) + }) + + Convey("When repository hook is not writable", func() { + req.Form.Set("owner", "example") + req.Form.Set("name", "hookerr") + req.Form.Set("team", "") + res := httptest.NewRecorder() + err := handler.RepoCreateGithub(res, &req, user) + + Convey("The result is an error", func() { + So(err, ShouldNotBeNil) + So(err.Error(), ShouldEqual, "Unable to add Hook to your GitHub repository.") + }) + + Convey("The repository is not created", func() { + _, err := database.GetRepoSlug("example/hookerr") + So(err, ShouldNotBeNil) + So(err, ShouldEqual, sql.ErrNoRows) + }) + }) + + Convey("When repository ssh key is not writable", func() { + req.Form.Set("owner", "example") + req.Form.Set("name", "keyerr") + req.Form.Set("team", "") + res := httptest.NewRecorder() + err := handler.RepoCreateGithub(res, &req, user) + + Convey("The result is an error", func() { + So(err, ShouldNotBeNil) + So(err.Error(), ShouldEqual, "Unable to add Public Key to your GitHub repository.") + }) + + Convey("The repository is not created", func() { + _, err := database.GetRepoSlug("example/keyerr") + So(err, ShouldNotBeNil) + So(err, ShouldEqual, sql.ErrNoRows) + }) + }) + + Convey("When a team is provided", func() { + req.Form.Set("owner", "example") + req.Form.Set("name", "team") + req.Form.Set("team", "drone") + res := httptest.NewRecorder() + + // invoke handler + err := handler.RepoCreateGithub(res, &req, user) + team, _ := database.GetTeamSlug("drone") + repo, _ := database.GetRepoSlug(settings.GitHubDomain + "/example/team") + + Convey("The repository is created", func() { + So(err, ShouldBeNil) + So(repo, ShouldNotBeNil) + So(repo.ID, ShouldNotEqual, 0) + }) + + Convey("The team should be set", func() { + So(repo.TeamID, ShouldEqual, team.ID) + }) + }) + + Convey("When a team is not found", func() { + req.Form.Set("owner", "example") + req.Form.Set("name", "public") + req.Form.Set("team", "faketeam") + res := httptest.NewRecorder() + err := handler.RepoCreateGithub(res, &req, user) + + Convey("The result is an error", func() { + So(err, ShouldNotBeNil) + So(err.Error(), ShouldEqual, "Unable to find Team faketeam.") + }) + }) + + Convey("When a team is forbidden", func() { + req.Form.Set("owner", "example") + req.Form.Set("name", "public") + req.Form.Set("team", "golang") + res := httptest.NewRecorder() + err := handler.RepoCreateGithub(res, &req, user) + + Convey("The result is an error", func() { + So(err, ShouldNotBeNil) + So(err.Error(), ShouldEqual, "Invalid permission to access Team golang.") + }) + }) + }) +} + +// this code should be refactored and centralized, but for now +// it is just proof-of-concepting a testing strategy, so we'll +// revisit later. + +// mux is the HTTP request multiplexer used with the test server. +var mux *http.ServeMux + +// server is a test HTTP server used to provide mock API responses. +var server *httptest.Server + +func SetupFixtures() { + dbtest.Setup() + + // test server + mux = http.NewServeMux() + server = httptest.NewServer(mux) + url, _ := url.Parse(server.URL) + + // set database to use a localhost url for GitHub + settings := model.Settings{} + settings.GitHubKey = "123" + settings.GitHubSecret = "abc" + settings.GitHubApiUrl = url.String() // normall would be "https://api.github.com" + settings.GitHubDomain = url.Host // normally would be "github.com" + settings.Scheme = url.Scheme + settings.Domain = "localhost" + database.SaveSettings(&settings) + + /* + mux.HandleFunc("/repos/octocat/", func(w http.ResponseWriter, r *http.Request) { + + switch { + case r.URL.Path == "/repos/octocat/notfound": + http.NotFound(w, r) + + case r.URL.Path == "/repos/ocotcat/hookfail": + fmt.Fprint(w, `{"id":1296269,"name":"hookfail","full_name":"octocat/hookfail","owner":{"login":"octocat","id":583231,"avatar_url":"https://gravatar.com/avatar/7194e8d48fa1d2b689f99443b767316c?d=https%3A%2F%2Fidenticons.github.com%2Fb295bf8043975e176de44c2617751f8b.png&r=x","gravatar_id":"7194e8d48fa1d2b689f99443b767316c","url":"https://api.github.com/users/octocat","html_url":"https://github.com/octocat","followers_url":"https://api.github.com/users/octocat/followers","following_url":"https://api.github.com/users/octocat/following{/other_user}","gists_url":"https://api.github.com/users/octocat/gists{/gist_id}","starred_url":"https://api.github.com/users/octocat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/octocat/subscriptions","organizations_url":"https://api.github.com/users/octocat/orgs","repos_url":"https://api.github.com/users/octocat/repos","events_url":"https://api.github.com/users/octocat/events{/privacy}","received_events_url":"https://api.github.com/users/octocat/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/octocat/hookfail","description":"This your first repo!","fork":false,"url":"https://api.github.com/repos/octocat/hookfail","forks_url":"https://api.github.com/repos/octocat/hookfail/forks","keys_url":"https://api.github.com/repos/octocat/hookfail/keys{/key_id}","collaborators_url":"https://api.github.com/repos/octocat/hookfail/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/octocat/hookfail/teams","hooks_url":"https://api.github.com/repos/octocat/hookfail/hooks","issue_events_url":"https://api.github.com/repos/octocat/hookfail/issues/events{/number}","events_url":"https://api.github.com/repos/octocat/hookfail/events","assignees_url":"https://api.github.com/repos/octocat/hookfail/assignees{/user}","branches_url":"https://api.github.com/repos/octocat/hookfail/branches{/branch}","tags_url":"https://api.github.com/repos/octocat/hookfail/tags","blobs_url":"https://api.github.com/repos/octocat/hookfail/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/octocat/hookfail/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/octocat/hookfail/git/refs{/sha}","trees_url":"https://api.github.com/repos/octocat/hookfail/git/trees{/sha}","statuses_url":"https://api.github.com/repos/octocat/hookfail/statuses/{sha}","languages_url":"https://api.github.com/repos/octocat/hookfail/languages","stargazers_url":"https://api.github.com/repos/octocat/hookfail/stargazers","contributors_url":"https://api.github.com/repos/octocat/hookfail/contributors","subscribers_url":"https://api.github.com/repos/octocat/hookfail/subscribers","subscription_url":"https://api.github.com/repos/octocat/hookfail/subscription","commits_url":"https://api.github.com/repos/octocat/hookfail/commits{/sha}","git_commits_url":"https://api.github.com/repos/octocat/hookfail/git/commits{/sha}","comments_url":"https://api.github.com/repos/octocat/hookfail/comments{/number}","issue_comment_url":"https://api.github.com/repos/octocat/hookfail/issues/comments/{number}","contents_url":"https://api.github.com/repos/octocat/hookfail/contents/{+path}","compare_url":"https://api.github.com/repos/octocat/hookfail/compare/{base}...{head}","merges_url":"https://api.github.com/repos/octocat/hookfail/merges","archive_url":"https://api.github.com/repos/octocat/hookfail/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/octocat/hookfail/downloads","issues_url":"https://api.github.com/repos/octocat/hookfail/issues{/number}","pulls_url":"https://api.github.com/repos/octocat/hookfail/pulls{/number}","milestones_url":"https://api.github.com/repos/octocat/hookfail/milestones{/number}","notifications_url":"https://api.github.com/repos/octocat/hookfail/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/octocat/hookfail/labels{/name}","releases_url":"https://api.github.com/repos/octocat/hookfail/releases{/id}","created_at":"2011-01-26T19:01:12Z","updated_at":"2014-01-14T20:21:44Z","pushed_at":"2012-03-06T23:06:51Z","git_url":"git://github.com/octocat/hookfail.git","ssh_url":"git@github.com:octocat/hookfail.git","clone_url":"https://github.com/octocat/hookfail.git","svn_url":"https://github.com/octocat/hookfail","homepage":"","size":263,"stargazers_count":1365,"watchers_count":1365,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"forks_count":867,"mirror_url":null,"open_issues_count":87,"forks":867,"open_issues":87,"watchers":1365,"default_branch":"master","master_branch":"master","network_count":867,"subscribers_count":1396}`) + + // ------------------------------------------------------------------------ + // Repository that should fail to add an SSH key + case r.URL.Path == "/repos/ocotcat/keyfail": + fmt.Fprint(w, `{"id":1296269,"name":"keyfail","full_name":"octocat/keyfail","owner":{"login":"octocat","id":583231,"avatar_url":"https://gravatar.com/avatar/7194e8d48fa1d2b689f99443b767316c?d=https%3A%2F%2Fidenticons.github.com%2Fb295bf8043975e176de44c2617751f8b.png&r=x","gravatar_id":"7194e8d48fa1d2b689f99443b767316c","url":"https://api.github.com/users/octocat","html_url":"https://github.com/octocat","followers_url":"https://api.github.com/users/octocat/followers","following_url":"https://api.github.com/users/octocat/following{/other_user}","gists_url":"https://api.github.com/users/octocat/gists{/gist_id}","starred_url":"https://api.github.com/users/octocat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/octocat/subscriptions","organizations_url":"https://api.github.com/users/octocat/orgs","repos_url":"https://api.github.com/users/octocat/repos","events_url":"https://api.github.com/users/octocat/events{/privacy}","received_events_url":"https://api.github.com/users/octocat/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/octocat/keyfail","description":"This your first repo!","fork":false,"url":"https://api.github.com/repos/octocat/keyfail","forks_url":"https://api.github.com/repos/octocat/keyfail/forks","keys_url":"https://api.github.com/repos/octocat/keyfail/keys{/key_id}","collaborators_url":"https://api.github.com/repos/octocat/keyfail/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/octocat/keyfail/teams","hooks_url":"https://api.github.com/repos/octocat/keyfail/hooks","issue_events_url":"https://api.github.com/repos/octocat/keyfail/issues/events{/number}","events_url":"https://api.github.com/repos/octocat/keyfail/events","assignees_url":"https://api.github.com/repos/octocat/keyfail/assignees{/user}","branches_url":"https://api.github.com/repos/octocat/keyfail/branches{/branch}","tags_url":"https://api.github.com/repos/octocat/keyfail/tags","blobs_url":"https://api.github.com/repos/octocat/keyfail/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/octocat/keyfail/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/octocat/keyfail/git/refs{/sha}","trees_url":"https://api.github.com/repos/octocat/keyfail/git/trees{/sha}","statuses_url":"https://api.github.com/repos/octocat/keyfail/statuses/{sha}","languages_url":"https://api.github.com/repos/octocat/keyfail/languages","stargazers_url":"https://api.github.com/repos/octocat/keyfail/stargazers","contributors_url":"https://api.github.com/repos/octocat/keyfail/contributors","subscribers_url":"https://api.github.com/repos/octocat/keyfail/subscribers","subscription_url":"https://api.github.com/repos/octocat/keyfail/subscription","commits_url":"https://api.github.com/repos/octocat/keyfail/commits{/sha}","git_commits_url":"https://api.github.com/repos/octocat/keyfail/git/commits{/sha}","comments_url":"https://api.github.com/repos/octocat/keyfail/comments{/number}","issue_comment_url":"https://api.github.com/repos/octocat/keyfail/issues/comments/{number}","contents_url":"https://api.github.com/repos/octocat/keyfail/contents/{+path}","compare_url":"https://api.github.com/repos/octocat/keyfail/compare/{base}...{head}","merges_url":"https://api.github.com/repos/octocat/keyfail/merges","archive_url":"https://api.github.com/repos/octocat/keyfail/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/octocat/keyfail/downloads","issues_url":"https://api.github.com/repos/octocat/keyfail/issues{/number}","pulls_url":"https://api.github.com/repos/octocat/keyfail/pulls{/number}","milestones_url":"https://api.github.com/repos/octocat/keyfail/milestones{/number}","notifications_url":"https://api.github.com/repos/octocat/keyfail/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/octocat/keyfail/labels{/name}","releases_url":"https://api.github.com/repos/octocat/keyfail/releases{/id}","created_at":"2011-01-26T19:01:12Z","updated_at":"2014-01-14T20:21:44Z","pushed_at":"2012-03-06T23:06:51Z","git_url":"git://github.com/octocat/keyfail.git","ssh_url":"git@github.com:octocat/keyfail.git","clone_url":"https://github.com/octocat/keyfail.git","svn_url":"https://github.com/octocat/keyfail","homepage":"","size":263,"stargazers_count":1365,"watchers_count":1365,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"forks_count":867,"mirror_url":null,"open_issues_count":87,"forks":867,"open_issues":87,"watchers":1365,"default_branch":"master","master_branch":"master","network_count":867,"subscribers_count":1396}`) + + case r.URL.Path == "/repos/octocat/hookfail/hooks": + http.Error(w, "Forbidden", http.StatusForbidden) + + case r.URL.Path == "/repos/octocat/hookfail/hooks": + http.Error(w, "Forbidden", http.StatusForbidden) + } + + }) + + // github repository not found + mux.HandleFunc("/repos/octocat/notfound", func(w http.ResponseWriter, r *http.Request) { + http.NotFound(w, r) + }) + + // user does not have permissions to create the hook + mux.HandleFunc("/repos/octocat/hookfail/hooks", func(w http.ResponseWriter, r *http.Request) { + http.Error(w, "Forbidden", http.StatusForbidden) + }) + + // github repository found, but forbidden to create hooks + mux.HandleFunc("/repos/octocat/hookfail", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, `{"id":1296269,"name":"hookfail","full_name":"octocat/hookfail","owner":{"login":"octocat","id":583231,"avatar_url":"https://gravatar.com/avatar/7194e8d48fa1d2b689f99443b767316c?d=https%3A%2F%2Fidenticons.github.com%2Fb295bf8043975e176de44c2617751f8b.png&r=x","gravatar_id":"7194e8d48fa1d2b689f99443b767316c","url":"https://api.github.com/users/octocat","html_url":"https://github.com/octocat","followers_url":"https://api.github.com/users/octocat/followers","following_url":"https://api.github.com/users/octocat/following{/other_user}","gists_url":"https://api.github.com/users/octocat/gists{/gist_id}","starred_url":"https://api.github.com/users/octocat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/octocat/subscriptions","organizations_url":"https://api.github.com/users/octocat/orgs","repos_url":"https://api.github.com/users/octocat/repos","events_url":"https://api.github.com/users/octocat/events{/privacy}","received_events_url":"https://api.github.com/users/octocat/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/octocat/hookfail","description":"This your first repo!","fork":false,"url":"https://api.github.com/repos/octocat/hookfail","forks_url":"https://api.github.com/repos/octocat/hookfail/forks","keys_url":"https://api.github.com/repos/octocat/hookfail/keys{/key_id}","collaborators_url":"https://api.github.com/repos/octocat/hookfail/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/octocat/hookfail/teams","hooks_url":"https://api.github.com/repos/octocat/hookfail/hooks","issue_events_url":"https://api.github.com/repos/octocat/hookfail/issues/events{/number}","events_url":"https://api.github.com/repos/octocat/hookfail/events","assignees_url":"https://api.github.com/repos/octocat/hookfail/assignees{/user}","branches_url":"https://api.github.com/repos/octocat/hookfail/branches{/branch}","tags_url":"https://api.github.com/repos/octocat/hookfail/tags","blobs_url":"https://api.github.com/repos/octocat/hookfail/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/octocat/hookfail/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/octocat/hookfail/git/refs{/sha}","trees_url":"https://api.github.com/repos/octocat/hookfail/git/trees{/sha}","statuses_url":"https://api.github.com/repos/octocat/hookfail/statuses/{sha}","languages_url":"https://api.github.com/repos/octocat/hookfail/languages","stargazers_url":"https://api.github.com/repos/octocat/hookfail/stargazers","contributors_url":"https://api.github.com/repos/octocat/hookfail/contributors","subscribers_url":"https://api.github.com/repos/octocat/hookfail/subscribers","subscription_url":"https://api.github.com/repos/octocat/hookfail/subscription","commits_url":"https://api.github.com/repos/octocat/hookfail/commits{/sha}","git_commits_url":"https://api.github.com/repos/octocat/hookfail/git/commits{/sha}","comments_url":"https://api.github.com/repos/octocat/hookfail/comments{/number}","issue_comment_url":"https://api.github.com/repos/octocat/hookfail/issues/comments/{number}","contents_url":"https://api.github.com/repos/octocat/hookfail/contents/{+path}","compare_url":"https://api.github.com/repos/octocat/hookfail/compare/{base}...{head}","merges_url":"https://api.github.com/repos/octocat/hookfail/merges","archive_url":"https://api.github.com/repos/octocat/hookfail/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/octocat/hookfail/downloads","issues_url":"https://api.github.com/repos/octocat/hookfail/issues{/number}","pulls_url":"https://api.github.com/repos/octocat/hookfail/pulls{/number}","milestones_url":"https://api.github.com/repos/octocat/hookfail/milestones{/number}","notifications_url":"https://api.github.com/repos/octocat/hookfail/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/octocat/hookfail/labels{/name}","releases_url":"https://api.github.com/repos/octocat/hookfail/releases{/id}","created_at":"2011-01-26T19:01:12Z","updated_at":"2014-01-14T20:21:44Z","pushed_at":"2012-03-06T23:06:51Z","git_url":"git://github.com/octocat/hookfail.git","ssh_url":"git@github.com:octocat/hookfail.git","clone_url":"https://github.com/octocat/hookfail.git","svn_url":"https://github.com/octocat/hookfail","homepage":"","size":263,"stargazers_count":1365,"watchers_count":1365,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"forks_count":867,"mirror_url":null,"open_issues_count":87,"forks":867,"open_issues":87,"watchers":1365,"default_branch":"master","master_branch":"master","network_count":867,"subscribers_count":1396}`) + }) + + // github repository found, but forbidden to create hooks + mux.HandleFunc("/repos/octocat/hookfail", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, `{"id":1296269,"name":"hookfail","full_name":"octocat/hookfail","owner":{"login":"octocat","id":583231,"avatar_url":"https://gravatar.com/avatar/7194e8d48fa1d2b689f99443b767316c?d=https%3A%2F%2Fidenticons.github.com%2Fb295bf8043975e176de44c2617751f8b.png&r=x","gravatar_id":"7194e8d48fa1d2b689f99443b767316c","url":"https://api.github.com/users/octocat","html_url":"https://github.com/octocat","followers_url":"https://api.github.com/users/octocat/followers","following_url":"https://api.github.com/users/octocat/following{/other_user}","gists_url":"https://api.github.com/users/octocat/gists{/gist_id}","starred_url":"https://api.github.com/users/octocat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/octocat/subscriptions","organizations_url":"https://api.github.com/users/octocat/orgs","repos_url":"https://api.github.com/users/octocat/repos","events_url":"https://api.github.com/users/octocat/events{/privacy}","received_events_url":"https://api.github.com/users/octocat/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/octocat/hookfail","description":"This your first repo!","fork":false,"url":"https://api.github.com/repos/octocat/hookfail","forks_url":"https://api.github.com/repos/octocat/hookfail/forks","keys_url":"https://api.github.com/repos/octocat/hookfail/keys{/key_id}","collaborators_url":"https://api.github.com/repos/octocat/hookfail/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/octocat/hookfail/teams","hooks_url":"https://api.github.com/repos/octocat/hookfail/hooks","issue_events_url":"https://api.github.com/repos/octocat/hookfail/issues/events{/number}","events_url":"https://api.github.com/repos/octocat/hookfail/events","assignees_url":"https://api.github.com/repos/octocat/hookfail/assignees{/user}","branches_url":"https://api.github.com/repos/octocat/hookfail/branches{/branch}","tags_url":"https://api.github.com/repos/octocat/hookfail/tags","blobs_url":"https://api.github.com/repos/octocat/hookfail/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/octocat/hookfail/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/octocat/hookfail/git/refs{/sha}","trees_url":"https://api.github.com/repos/octocat/hookfail/git/trees{/sha}","statuses_url":"https://api.github.com/repos/octocat/hookfail/statuses/{sha}","languages_url":"https://api.github.com/repos/octocat/hookfail/languages","stargazers_url":"https://api.github.com/repos/octocat/hookfail/stargazers","contributors_url":"https://api.github.com/repos/octocat/hookfail/contributors","subscribers_url":"https://api.github.com/repos/octocat/hookfail/subscribers","subscription_url":"https://api.github.com/repos/octocat/hookfail/subscription","commits_url":"https://api.github.com/repos/octocat/hookfail/commits{/sha}","git_commits_url":"https://api.github.com/repos/octocat/hookfail/git/commits{/sha}","comments_url":"https://api.github.com/repos/octocat/hookfail/comments{/number}","issue_comment_url":"https://api.github.com/repos/octocat/hookfail/issues/comments/{number}","contents_url":"https://api.github.com/repos/octocat/hookfail/contents/{+path}","compare_url":"https://api.github.com/repos/octocat/hookfail/compare/{base}...{head}","merges_url":"https://api.github.com/repos/octocat/hookfail/merges","archive_url":"https://api.github.com/repos/octocat/hookfail/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/octocat/hookfail/downloads","issues_url":"https://api.github.com/repos/octocat/hookfail/issues{/number}","pulls_url":"https://api.github.com/repos/octocat/hookfail/pulls{/number}","milestones_url":"https://api.github.com/repos/octocat/hookfail/milestones{/number}","notifications_url":"https://api.github.com/repos/octocat/hookfail/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/octocat/hookfail/labels{/name}","releases_url":"https://api.github.com/repos/octocat/hookfail/releases{/id}","created_at":"2011-01-26T19:01:12Z","updated_at":"2014-01-14T20:21:44Z","pushed_at":"2012-03-06T23:06:51Z","git_url":"git://github.com/octocat/hookfail.git","ssh_url":"git@github.com:octocat/hookfail.git","clone_url":"https://github.com/octocat/hookfail.git","svn_url":"https://github.com/octocat/hookfail","homepage":"","size":263,"stargazers_count":1365,"watchers_count":1365,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"forks_count":867,"mirror_url":null,"open_issues_count":87,"forks":867,"open_issues":87,"watchers":1365,"default_branch":"master","master_branch":"master","network_count":867,"subscribers_count":1396}`) + }) + */ + // ----------------------------------------------------------------------------------- + // fixture to return a public repository and successfully + // create a commit hook. + + mux.HandleFunc("/repos/example/public", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, `{ + "name": "public", + "full_name": "example/public", + "private": false + }`) + }) + + mux.HandleFunc("/repos/example/public/hooks", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, `{ + "url": "https://api.github.com/repos/example/public/hooks/1", + "name": "web", + "events": [ "push", "pull_request" ], + "id": 1 + }`) + }) + + // ----------------------------------------------------------------------------------- + // fixture to return a private repository and successfully + // create a commit hook and ssh deploy key + + mux.HandleFunc("/repos/example/private", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, `{ + "name": "private", + "full_name": "example/private", + "private": true + }`) + }) + + mux.HandleFunc("/repos/example/private/hooks", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, `{ + "url": "https://api.github.com/repos/example/private/hooks/1", + "name": "web", + "events": [ "push", "pull_request" ], + "id": 1 + }`) + }) + + mux.HandleFunc("/repos/example/private/keys", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, `{ + "id": 1, + "key": "ssh-rsa AAA...", + "url": "https://api.github.com/user/keys/1", + "title": "octocat@octomac" + }`) + }) + + // ----------------------------------------------------------------------------------- + // fixture to return a not found when accessing a github repository. + + mux.HandleFunc("/repos/example/notfound", func(w http.ResponseWriter, r *http.Request) { + http.NotFound(w, r) + }) + + // ----------------------------------------------------------------------------------- + // fixture to return a public repository and successfully + // create a commit hook. + + mux.HandleFunc("/repos/example/hookerr", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, `{ + "name": "hookerr", + "full_name": "example/hookerr", + "private": false + }`) + }) + + mux.HandleFunc("/repos/example/hookerr/hooks", func(w http.ResponseWriter, r *http.Request) { + http.Error(w, "Forbidden", http.StatusForbidden) + }) + + // ----------------------------------------------------------------------------------- + // fixture to return a private repository and successfully + // create a commit hook and ssh deploy key + + mux.HandleFunc("/repos/example/keyerr", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, `{ + "name": "keyerr", + "full_name": "example/keyerr", + "private": true + }`) + }) + + mux.HandleFunc("/repos/example/keyerr/hooks", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, `{ + "url": "https://api.github.com/repos/example/keyerr/hooks/1", + "name": "web", + "events": [ "push", "pull_request" ], + "id": 1 + }`) + }) + + mux.HandleFunc("/repos/example/keyerr/keys", func(w http.ResponseWriter, r *http.Request) { + http.Error(w, "Forbidden", http.StatusForbidden) + }) + + // ----------------------------------------------------------------------------------- + // fixture to return a public repository and successfully to + // test adding a team. + + mux.HandleFunc("/repos/example/team", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, `{ + "name": "team", + "full_name": "example/team", + "private": false + }`) + }) + + mux.HandleFunc("/repos/example/team/hooks", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, `{ + "url": "https://api.github.com/repos/example/team/hooks/1", + "name": "web", + "events": [ "push", "pull_request" ], + "id": 1 + }`) + }) +} + +func TeardownFixtures() { + dbtest.Teardown() + server.Close() +} + +/* + +// response for querying a repo +var repoGet = `{ + "name": "Hello-World", + "full_name": "octocat/Hello-World", + "owner": { + "login": "octocat" + }, + "private": false, + "git_url": "git://github.com/octocat/Hello-World.git", + "ssh_url": "git@github.com:octocat/Hello-World.git", + "clone_url": "https://github.com/octocat/Hello-World.git" +}` + +// response for querying a private repo +var repoPrivateGet = `{ + "name": "Hello-World", + "full_name": "octocat/Hello-World", + "owner": { + "login": "octocat" + }, + "private": true, + "git_url": "git://github.com/octocat/Hello-World.git", + "ssh_url": "git@github.com:octocat/Hello-World.git", + "clone_url": "https://github.com/octocat/Hello-World.git" +}` + +// response for creating a key +var keyAdd = ` +{ + "id": 1, + "key": "ssh-rsa AAA...", + "url": "https://api.github.com/user/keys/1", + "title": "octocat@octomac" +} +` + +// response for creating a hook +var hookAdd = ` +{ + "url": "https://api.github.com/repos/octocat/Hello-World/hooks/1", + "updated_at": "2011-09-06T20:39:23Z", + "created_at": "2011-09-06T17:26:27Z", + "name": "web", + "events": [ + "push", + "pull_request" + ], + "active": true, + "config": { + "url": "http://example.com", + "content_type": "json" + }, + "id": 1 +} +` +*/ From 17318d5623743aece7a1f18382a6ebab5e9d9fa4 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Thu, 13 Mar 2014 16:49:47 -0700 Subject: [PATCH 2/7] updated .gitignore to exclude coverage reports --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 956ba27ba..dd54dc627 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ deb/drone/usr/local/bin/drone deb/drone/usr/local/bin/droned .vagrant +*.out \ No newline at end of file From 62ec8634f6cb1b66919143451d65ac27b2350609 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Thu, 13 Mar 2014 16:50:01 -0700 Subject: [PATCH 3/7] added code for testing buildfiles --- pkg/build/buildfile/buildfile_test.go | 49 +++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 pkg/build/buildfile/buildfile_test.go diff --git a/pkg/build/buildfile/buildfile_test.go b/pkg/build/buildfile/buildfile_test.go new file mode 100644 index 000000000..aca239290 --- /dev/null +++ b/pkg/build/buildfile/buildfile_test.go @@ -0,0 +1,49 @@ +package buildfile + +import ( + "testing" +) + +func TestWrite(t *testing.T) { + + var f = New() + var got, want = f.String(), base + if got != want { + t.Errorf("Exepected New() returned %s, got %s", want, got) + } + + f = &Buildfile{} + f.WriteCmd("echo hi") + got, want = f.String(), "echo '#DRONE:6563686f206869'\necho hi\n" + if got != want { + t.Errorf("Exepected WriteCmd returned %s, got %s", want, got) + } + + f = &Buildfile{} + f.WriteCmdSilent("echo hi") + got, want = f.String(), "echo hi\n" + if got != want { + t.Errorf("Exepected WriteCmdSilent returned %s, got %s", want, got) + } + + f = &Buildfile{} + f.WriteComment("this is a comment") + got, want = f.String(), "#this is a comment\n" + if got != want { + t.Errorf("Exepected WriteComment returned %s, got %s", want, got) + } + + f = &Buildfile{} + f.WriteEnv("FOO", "BAR") + got, want = f.String(), "export FOO=BAR\n" + if got != want { + t.Errorf("Exepected WriteEnv returned %s, got %s", want, got) + } + + f = &Buildfile{} + f.WriteHost("127.0.0.1") + got, want = f.String(), "[ -f /usr/bin/sudo ] || echo \"127.0.0.1\" | tee -a /etc/hosts\n[ -f /usr/bin/sudo ] && echo \"127.0.0.1\" | sudo tee -a /etc/hosts\n" + if got != want { + t.Errorf("Exepected WriteHost returned %s, got %s", want, got) + } +} \ No newline at end of file From 1a153b8e80eaec71727c80be54ba613a8425d408 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Thu, 13 Mar 2014 17:07:33 -0700 Subject: [PATCH 4/7] test coverage for dockerfile package --- pkg/build/dockerfile/dockerfile_test.go | 63 +++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 pkg/build/dockerfile/dockerfile_test.go diff --git a/pkg/build/dockerfile/dockerfile_test.go b/pkg/build/dockerfile/dockerfile_test.go new file mode 100644 index 000000000..fb1b017d5 --- /dev/null +++ b/pkg/build/dockerfile/dockerfile_test.go @@ -0,0 +1,63 @@ +package dockerfile + +import ( + "testing" +) + +func TestWrite(t *testing.T) { + + var f = New("ubuntu") + var got, want = f.String(), "FROM ubuntu\n" + if got != want { + t.Errorf("Exepected New() returned %s, got %s", want, got) + } + + f = &Dockerfile{} + f.WriteAdd("src", "target") + got, want = f.String(), "ADD src target\n" + if got != want { + t.Errorf("Exepected WriteAdd returned %s, got %s", want, got) + } + + f = &Dockerfile{} + f.WriteFrom("ubuntu") + got, want = f.String(), "FROM ubuntu\n" + if got != want { + t.Errorf("Exepected WriteFrom returned %s, got %s", want, got) + } + + f = &Dockerfile{} + f.WriteRun("whoami") + got, want = f.String(), "RUN whoami\n" + if got != want { + t.Errorf("Exepected WriteRun returned %s, got %s", want, got) + } + + f = &Dockerfile{} + f.WriteUser("root") + got, want = f.String(), "USER root\n" + if got != want { + t.Errorf("Exepected WriteUser returned %s, got %s", want, got) + } + + f = &Dockerfile{} + f.WriteEnv("FOO", "BAR") + got, want = f.String(), "ENV FOO BAR\n" + if got != want { + t.Errorf("Exepected WriteEnv returned %s, got %s", want, got) + } + + f = &Dockerfile{} + f.WriteWorkdir("/home/ubuntu") + got, want = f.String(), "WORKDIR /home/ubuntu\n" + if got != want { + t.Errorf("Exepected WriteWorkdir returned %s, got %s", want, got) + } + + f = &Dockerfile{} + f.WriteEntrypoint("/root") + got, want = f.String(), "ENTRYPOINT /root\n" + if got != want { + t.Errorf("Exepected WriteEntrypoint returned %s, got %s", want, got) + } +} \ No newline at end of file From 3d4e3168b79807907b9402165ef97082365d1f45 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Thu, 13 Mar 2014 18:49:49 -0700 Subject: [PATCH 5/7] ran gofmt --- pkg/build/buildfile/buildfile_test.go | 2 +- pkg/build/dockerfile/dockerfile_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/build/buildfile/buildfile_test.go b/pkg/build/buildfile/buildfile_test.go index aca239290..c4a48c993 100644 --- a/pkg/build/buildfile/buildfile_test.go +++ b/pkg/build/buildfile/buildfile_test.go @@ -46,4 +46,4 @@ func TestWrite(t *testing.T) { if got != want { t.Errorf("Exepected WriteHost returned %s, got %s", want, got) } -} \ No newline at end of file +} diff --git a/pkg/build/dockerfile/dockerfile_test.go b/pkg/build/dockerfile/dockerfile_test.go index fb1b017d5..cb9a63a22 100644 --- a/pkg/build/dockerfile/dockerfile_test.go +++ b/pkg/build/dockerfile/dockerfile_test.go @@ -60,4 +60,4 @@ func TestWrite(t *testing.T) { if got != want { t.Errorf("Exepected WriteEntrypoint returned %s, got %s", want, got) } -} \ No newline at end of file +} From 6f8bbf1d0402b699878b033909990dd2ad9a3e71 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Fri, 14 Mar 2014 11:52:24 -0700 Subject: [PATCH 6/7] adding mocks for Builder.setup() --- pkg/build/build.go | 2 +- pkg/build/build_test.go | 360 +++++++++++++++++++++++++++++ pkg/handler/testing/github_test.go | 44 ---- 3 files changed, 361 insertions(+), 45 deletions(-) create mode 100644 pkg/build/build_test.go diff --git a/pkg/build/build.go b/pkg/build/build.go index 65f3a73eb..8b39698b1 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -474,7 +474,7 @@ func (b *Builder) writeBuildScript(dir string) error { f.WriteEnv("DRONE_PR", b.Repo.PR) f.WriteEnv("DRONE_BUILD_DIR", b.Repo.Dir) - // add /etc/hosts entries + // add /etc/hosts entries for _, mapping := range b.Build.Hosts { f.WriteHost(mapping) } diff --git a/pkg/build/build_test.go b/pkg/build/build_test.go new file mode 100644 index 000000000..0457a4f21 --- /dev/null +++ b/pkg/build/build_test.go @@ -0,0 +1,360 @@ +package build + +import ( + "io/ioutil" + "net/http" + "net/http/httptest" + "net/url" + "os" + "path/filepath" + "testing" + + "github.com/drone/drone/pkg/build/buildfile" + "github.com/drone/drone/pkg/build/docker" + "github.com/drone/drone/pkg/build/proxy" + "github.com/drone/drone/pkg/build/repo" + "github.com/drone/drone/pkg/build/script" +) + +var ( + // mux is the HTTP request multiplexer used with the test server. + mux *http.ServeMux + + // server is a test HTTP server used to provide mock API responses. + server *httptest.Server + + // docker client + client *docker.Client +) + +// setup a mock docker client for testing purposes. This will use +// a test http server that can return mock responses to the docker client. +func setup() { + mux = http.NewServeMux() + server = httptest.NewServer(mux) + + url, _ := url.Parse(server.URL) + url.Scheme = "tcp" + os.Setenv("DOCKER_HOST", url.String()) + client = docker.New() +} + +func teardown() { + server.Close() +} + +// TestSetup will test our ability to successfully create a Docker +// image for the build. +func TestSetup(t *testing.T) { + setup() + defer teardown() + + // Handles a request to inspect the Go 1.2 image + // This will return a dummy image ID, so that the system knows + // the build image exists, and doens't need to be downloaded. + mux.HandleFunc("/v1.9/images/bradrydzewski/go:1.2/json", func(w http.ResponseWriter, r *http.Request) { + body := `[{ "id": "7bf9ce0ffb7236ca68da0f9fed0e1682053b393db3c724ff3c5a4e8c0793b34c" }]` + w.Write([]byte(body)) + }) + + // Handles a request to create the build image, with the build + // script injected. This will return a dummy stream. + mux.HandleFunc("/v1.9/build", func(w http.ResponseWriter, r *http.Request) { + body := `{"stream":"Step 1..."}` + w.Write([]byte(body)) + }) + + // Handles a request to inspect the newly created build image. Note + // that we are doing a "wildcard" url match here, since the name of + // the image will be random. This will return a dummy image ID + // to confirm the build image was created successfully. + mux.HandleFunc("/v1.9/images/", func(w http.ResponseWriter, r *http.Request) { + body := `{ "id": "7bf9ce0ffb7236ca68da0f9fed0e1682053b393db3c724ff3c5a4e8c0793b34c" }` + w.Write([]byte(body)) + }) + + b := Builder{} + b.Repo = &repo.Repo{} + b.Repo.Path = "git://github.com/drone/drone.git" + b.Build = &script.Build{} + b.Build.Image = "go1.2" + b.dockerClient = client + + if err := b.setup(); err != nil { + t.Errorf("Expected success, got %s", err) + } + + // verify the Image is being correctly set + if b.image == nil { + t.Errorf("Expected image not nil") + } + + expectedID := "7bf9ce0ffb7236ca68da0f9fed0e1682053b393db3c724ff3c5a4e8c0793b34c" + if b.image.ID != expectedID { + t.Errorf("Expected image.ID %s, got %s", expectedID, b.image.ID) + } +} + +// Expected behavior is that a build script with no docker image +// specified will thrown an error. +func TestSetupEmptyImage(t *testing.T) { + b := Builder{Build: &script.Build{}} + var got, want = b.setup(), "Error: missing Docker image" + + if got == nil || got.Error() != want { + t.Errorf("Expected error %s, got %s", want, got) + } +} + +// Expected behavior is that a build script with an unknown +// service (ie mysql) +func TestSetupUnknownService(t *testing.T) { + b := Builder{} + b.Repo = &repo.Repo{} + b.Repo.Path = "git://github.com/drone/drone.git" + b.Build = &script.Build{} + b.Build.Image = "go1.2" + b.Build.Services = append(b.Build.Services, "not-found") + + var got, want = b.setup(), "Error: Invalid or unknown service not-found" + if got == nil || got.Error() != want { + t.Errorf("Expected error %s, got %s", want, got) + } +} + +func TestSetupErrorRunDaemonPorts(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/v1.9/containers/create", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusBadRequest) + }) + + b := Builder{} + b.Repo = &repo.Repo{} + b.Repo.Path = "git://github.com/drone/drone.git" + b.Build = &script.Build{} + b.Build.Image = "go1.2" + b.Build.Services = append(b.Build.Services, "mysql") + b.dockerClient = client + + var got, want = b.setup(), docker.ErrBadRequest + if got == nil || got != want { + t.Errorf("Expected error %s, got %s", want, got) + } +} + +func TestSetupErrorServiceInspect(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/v1.9/containers/create", func(w http.ResponseWriter, r *http.Request) { + body := `{ "Id":"e90e34656806", "Warnings":[] }` + w.Write([]byte(body)) + }) + + mux.HandleFunc("/v1.9/containers/e90e34656806/start", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNoContent) + }) + + mux.HandleFunc("/v1.9/containers/e90e34656806/json", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusBadRequest) + }) + + b := Builder{} + b.Repo = &repo.Repo{} + b.Repo.Path = "git://github.com/drone/drone.git" + b.Build = &script.Build{} + b.Build.Image = "go1.2" + b.Build.Services = append(b.Build.Services, "mysql") + b.dockerClient = client + + var got, want = b.setup(), docker.ErrBadRequest + if got == nil || got != want { + t.Errorf("Expected error %s, got %s", want, got) + } +} + +func TestSetupErrorImagePull(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/v1.9/images/bradrydzewski/go:1.2/json", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + }) + + mux.HandleFunc("/v1.9/images/create", func(w http.ResponseWriter, r *http.Request) { + // validate ?fromImage=bradrydzewski/go&tag=1.2 + w.WriteHeader(http.StatusBadRequest) + }) + + b := Builder{} + b.Repo = &repo.Repo{} + b.Repo.Path = "git://github.com/drone/drone.git" + b.Build = &script.Build{} + b.Build.Image = "go1.2" + b.dockerClient = client + + var got, want = b.setup(), docker.ErrBadRequest + if got == nil || got != want { + t.Errorf("Expected error %s, got %s", want, got) + } +} + +func TestSetupErrorBuild(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/v1.9/images/bradrydzewski/go:1.2/json", func(w http.ResponseWriter, r *http.Request) { + body := `[{ "id": "7bf9ce0ffb7236ca68da0f9fed0e1682053b393db3c724ff3c5a4e8c0793b34c" }]` + w.Write([]byte(body)) + }) + + mux.HandleFunc("/v1.9/build", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusBadRequest) + }) + + b := Builder{} + b.Repo = &repo.Repo{} + b.Repo.Path = "git://github.com/drone/drone.git" + b.Build = &script.Build{} + b.Build.Image = "go1.2" + b.dockerClient = client + + var got, want = b.setup(), docker.ErrBadRequest + if got == nil || got != want { + t.Errorf("Expected error %s, got %s", want, got) + } +} + +func TestSetupErrorBuildInspect(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/v1.9/images/bradrydzewski/go:1.2/json", func(w http.ResponseWriter, r *http.Request) { + body := `[{ "id": "7bf9ce0ffb7236ca68da0f9fed0e1682053b393db3c724ff3c5a4e8c0793b34c" }]` + w.Write([]byte(body)) + }) + + mux.HandleFunc("/v1.9/build", func(w http.ResponseWriter, r *http.Request) { + body := `{"stream":"Step 1..."}` + w.Write([]byte(body)) + }) + + mux.HandleFunc("/v1.9/images/", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusBadRequest) + }) + + b := Builder{} + b.Repo = &repo.Repo{} + b.Repo.Path = "git://github.com/drone/drone.git" + b.Build = &script.Build{} + b.Build.Image = "go1.2" + b.dockerClient = client + + var got, want = b.setup(), docker.ErrBadRequest + if got == nil || got != want { + t.Errorf("Expected error %s, got %s", want, got) + } +} + +func TestTeardown(t *testing.T) {} + +func TestTeardownContainerFail(t *testing.T) {} + +func TestTeardownImageFail(t *testing.T) {} + +func TestWriteIdentifyFile(t *testing.T) { + // temporary directory to store file + dir, _ := ioutil.TempDir("", "drone-test-") + defer os.RemoveAll(dir) + + b := Builder{} + b.Key = []byte("ssh-rsa AAA...") + b.writeIdentifyFile(dir) + + // persist a dummy id_rsa keyfile to disk + keyfile, err := ioutil.ReadFile(filepath.Join(dir, "id_rsa")) + if err != nil { + t.Errorf("Expected id_rsa file saved to disk") + } + + if string(keyfile) != string(b.Key) { + t.Errorf("Expected id_rsa value saved as %s, got %s", b.Key, keyfile) + } +} + +func TestWriteProxyScript(t *testing.T) { + // temporary directory to store file + dir, _ := ioutil.TempDir("", "drone-test-") + defer os.RemoveAll(dir) + + // fake service container that we'll assume was part of the yaml + // and should be attached to the build container. + c := docker.Container{ + NetworkSettings: &docker.NetworkSettings{ + IPAddress: "172.1.4.5", + Ports: map[docker.Port][]docker.PortBinding{ + docker.NewPort("tcp", "3306"): nil, + }, + }, + } + + // this should generate the following proxy file + p := proxy.Proxy{} + p.Set("3306", "172.1.4.5") + want := p.String() + + b := Builder{} + b.services = append(b.services, &c) + b.writeProxyScript(dir) + + // persist a dummy proxy script to disk + got, err := ioutil.ReadFile(filepath.Join(dir, "proxy.sh")) + if err != nil { + t.Errorf("Expected proxy.sh file saved to disk") + } + + if string(got) != want { + t.Errorf("Expected proxy.sh value saved as %s, got %s", want, got) + } +} + +func TestWriteBuildScript(t *testing.T) { + // temporary directory to store file + dir, _ := ioutil.TempDir("", "drone-test-") + defer os.RemoveAll(dir) + + b := Builder{} + b.Build = &script.Build{ + Hosts: []string{"127.0.0.1"}} + b.Repo = &repo.Repo{ + Path: "git://github.com/drone/drone.git", + Branch: "master", + Commit: "e7e046b35", + PR: "123", + Dir: "/var/cache/drone/github.com/drone/drone"} + b.writeBuildScript(dir) + + // persist a dummy build script to disk + script, err := ioutil.ReadFile(filepath.Join(dir, "drone")) + if err != nil { + t.Errorf("Expected id_rsa file saved to disk") + } + + f := buildfile.New() + f.WriteEnv("CI", "true") + f.WriteEnv("DRONE", "true") + f.WriteEnv("DRONE_BRANCH", "master") + f.WriteEnv("DRONE_COMMIT", "e7e046b35") + f.WriteEnv("DRONE_PR", "123") + f.WriteEnv("DRONE_BUILD_DIR", "/var/cache/drone/github.com/drone/drone") + f.WriteHost("127.0.0.1") + f.WriteCmd("git clone --depth=0 --recursive --branch=master git://github.com/drone/drone.git /var/cache/drone/github.com/drone/drone") + f.WriteCmd("git fetch origin +refs/pull/123/head:refs/remotes/origin/pr/123") + f.WriteCmd("git checkout -qf -b pr/123 origin/pr/123") + + if string(script) != f.String() { + t.Errorf("Expected build script value saved as %s, got %s", f.String(), script) + } +} diff --git a/pkg/handler/testing/github_test.go b/pkg/handler/testing/github_test.go index abcf84a15..c2fb65182 100644 --- a/pkg/handler/testing/github_test.go +++ b/pkg/handler/testing/github_test.go @@ -210,50 +210,6 @@ func SetupFixtures() { settings.Domain = "localhost" database.SaveSettings(&settings) - /* - mux.HandleFunc("/repos/octocat/", func(w http.ResponseWriter, r *http.Request) { - - switch { - case r.URL.Path == "/repos/octocat/notfound": - http.NotFound(w, r) - - case r.URL.Path == "/repos/ocotcat/hookfail": - fmt.Fprint(w, `{"id":1296269,"name":"hookfail","full_name":"octocat/hookfail","owner":{"login":"octocat","id":583231,"avatar_url":"https://gravatar.com/avatar/7194e8d48fa1d2b689f99443b767316c?d=https%3A%2F%2Fidenticons.github.com%2Fb295bf8043975e176de44c2617751f8b.png&r=x","gravatar_id":"7194e8d48fa1d2b689f99443b767316c","url":"https://api.github.com/users/octocat","html_url":"https://github.com/octocat","followers_url":"https://api.github.com/users/octocat/followers","following_url":"https://api.github.com/users/octocat/following{/other_user}","gists_url":"https://api.github.com/users/octocat/gists{/gist_id}","starred_url":"https://api.github.com/users/octocat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/octocat/subscriptions","organizations_url":"https://api.github.com/users/octocat/orgs","repos_url":"https://api.github.com/users/octocat/repos","events_url":"https://api.github.com/users/octocat/events{/privacy}","received_events_url":"https://api.github.com/users/octocat/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/octocat/hookfail","description":"This your first repo!","fork":false,"url":"https://api.github.com/repos/octocat/hookfail","forks_url":"https://api.github.com/repos/octocat/hookfail/forks","keys_url":"https://api.github.com/repos/octocat/hookfail/keys{/key_id}","collaborators_url":"https://api.github.com/repos/octocat/hookfail/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/octocat/hookfail/teams","hooks_url":"https://api.github.com/repos/octocat/hookfail/hooks","issue_events_url":"https://api.github.com/repos/octocat/hookfail/issues/events{/number}","events_url":"https://api.github.com/repos/octocat/hookfail/events","assignees_url":"https://api.github.com/repos/octocat/hookfail/assignees{/user}","branches_url":"https://api.github.com/repos/octocat/hookfail/branches{/branch}","tags_url":"https://api.github.com/repos/octocat/hookfail/tags","blobs_url":"https://api.github.com/repos/octocat/hookfail/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/octocat/hookfail/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/octocat/hookfail/git/refs{/sha}","trees_url":"https://api.github.com/repos/octocat/hookfail/git/trees{/sha}","statuses_url":"https://api.github.com/repos/octocat/hookfail/statuses/{sha}","languages_url":"https://api.github.com/repos/octocat/hookfail/languages","stargazers_url":"https://api.github.com/repos/octocat/hookfail/stargazers","contributors_url":"https://api.github.com/repos/octocat/hookfail/contributors","subscribers_url":"https://api.github.com/repos/octocat/hookfail/subscribers","subscription_url":"https://api.github.com/repos/octocat/hookfail/subscription","commits_url":"https://api.github.com/repos/octocat/hookfail/commits{/sha}","git_commits_url":"https://api.github.com/repos/octocat/hookfail/git/commits{/sha}","comments_url":"https://api.github.com/repos/octocat/hookfail/comments{/number}","issue_comment_url":"https://api.github.com/repos/octocat/hookfail/issues/comments/{number}","contents_url":"https://api.github.com/repos/octocat/hookfail/contents/{+path}","compare_url":"https://api.github.com/repos/octocat/hookfail/compare/{base}...{head}","merges_url":"https://api.github.com/repos/octocat/hookfail/merges","archive_url":"https://api.github.com/repos/octocat/hookfail/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/octocat/hookfail/downloads","issues_url":"https://api.github.com/repos/octocat/hookfail/issues{/number}","pulls_url":"https://api.github.com/repos/octocat/hookfail/pulls{/number}","milestones_url":"https://api.github.com/repos/octocat/hookfail/milestones{/number}","notifications_url":"https://api.github.com/repos/octocat/hookfail/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/octocat/hookfail/labels{/name}","releases_url":"https://api.github.com/repos/octocat/hookfail/releases{/id}","created_at":"2011-01-26T19:01:12Z","updated_at":"2014-01-14T20:21:44Z","pushed_at":"2012-03-06T23:06:51Z","git_url":"git://github.com/octocat/hookfail.git","ssh_url":"git@github.com:octocat/hookfail.git","clone_url":"https://github.com/octocat/hookfail.git","svn_url":"https://github.com/octocat/hookfail","homepage":"","size":263,"stargazers_count":1365,"watchers_count":1365,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"forks_count":867,"mirror_url":null,"open_issues_count":87,"forks":867,"open_issues":87,"watchers":1365,"default_branch":"master","master_branch":"master","network_count":867,"subscribers_count":1396}`) - - // ------------------------------------------------------------------------ - // Repository that should fail to add an SSH key - case r.URL.Path == "/repos/ocotcat/keyfail": - fmt.Fprint(w, `{"id":1296269,"name":"keyfail","full_name":"octocat/keyfail","owner":{"login":"octocat","id":583231,"avatar_url":"https://gravatar.com/avatar/7194e8d48fa1d2b689f99443b767316c?d=https%3A%2F%2Fidenticons.github.com%2Fb295bf8043975e176de44c2617751f8b.png&r=x","gravatar_id":"7194e8d48fa1d2b689f99443b767316c","url":"https://api.github.com/users/octocat","html_url":"https://github.com/octocat","followers_url":"https://api.github.com/users/octocat/followers","following_url":"https://api.github.com/users/octocat/following{/other_user}","gists_url":"https://api.github.com/users/octocat/gists{/gist_id}","starred_url":"https://api.github.com/users/octocat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/octocat/subscriptions","organizations_url":"https://api.github.com/users/octocat/orgs","repos_url":"https://api.github.com/users/octocat/repos","events_url":"https://api.github.com/users/octocat/events{/privacy}","received_events_url":"https://api.github.com/users/octocat/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/octocat/keyfail","description":"This your first repo!","fork":false,"url":"https://api.github.com/repos/octocat/keyfail","forks_url":"https://api.github.com/repos/octocat/keyfail/forks","keys_url":"https://api.github.com/repos/octocat/keyfail/keys{/key_id}","collaborators_url":"https://api.github.com/repos/octocat/keyfail/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/octocat/keyfail/teams","hooks_url":"https://api.github.com/repos/octocat/keyfail/hooks","issue_events_url":"https://api.github.com/repos/octocat/keyfail/issues/events{/number}","events_url":"https://api.github.com/repos/octocat/keyfail/events","assignees_url":"https://api.github.com/repos/octocat/keyfail/assignees{/user}","branches_url":"https://api.github.com/repos/octocat/keyfail/branches{/branch}","tags_url":"https://api.github.com/repos/octocat/keyfail/tags","blobs_url":"https://api.github.com/repos/octocat/keyfail/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/octocat/keyfail/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/octocat/keyfail/git/refs{/sha}","trees_url":"https://api.github.com/repos/octocat/keyfail/git/trees{/sha}","statuses_url":"https://api.github.com/repos/octocat/keyfail/statuses/{sha}","languages_url":"https://api.github.com/repos/octocat/keyfail/languages","stargazers_url":"https://api.github.com/repos/octocat/keyfail/stargazers","contributors_url":"https://api.github.com/repos/octocat/keyfail/contributors","subscribers_url":"https://api.github.com/repos/octocat/keyfail/subscribers","subscription_url":"https://api.github.com/repos/octocat/keyfail/subscription","commits_url":"https://api.github.com/repos/octocat/keyfail/commits{/sha}","git_commits_url":"https://api.github.com/repos/octocat/keyfail/git/commits{/sha}","comments_url":"https://api.github.com/repos/octocat/keyfail/comments{/number}","issue_comment_url":"https://api.github.com/repos/octocat/keyfail/issues/comments/{number}","contents_url":"https://api.github.com/repos/octocat/keyfail/contents/{+path}","compare_url":"https://api.github.com/repos/octocat/keyfail/compare/{base}...{head}","merges_url":"https://api.github.com/repos/octocat/keyfail/merges","archive_url":"https://api.github.com/repos/octocat/keyfail/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/octocat/keyfail/downloads","issues_url":"https://api.github.com/repos/octocat/keyfail/issues{/number}","pulls_url":"https://api.github.com/repos/octocat/keyfail/pulls{/number}","milestones_url":"https://api.github.com/repos/octocat/keyfail/milestones{/number}","notifications_url":"https://api.github.com/repos/octocat/keyfail/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/octocat/keyfail/labels{/name}","releases_url":"https://api.github.com/repos/octocat/keyfail/releases{/id}","created_at":"2011-01-26T19:01:12Z","updated_at":"2014-01-14T20:21:44Z","pushed_at":"2012-03-06T23:06:51Z","git_url":"git://github.com/octocat/keyfail.git","ssh_url":"git@github.com:octocat/keyfail.git","clone_url":"https://github.com/octocat/keyfail.git","svn_url":"https://github.com/octocat/keyfail","homepage":"","size":263,"stargazers_count":1365,"watchers_count":1365,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"forks_count":867,"mirror_url":null,"open_issues_count":87,"forks":867,"open_issues":87,"watchers":1365,"default_branch":"master","master_branch":"master","network_count":867,"subscribers_count":1396}`) - - case r.URL.Path == "/repos/octocat/hookfail/hooks": - http.Error(w, "Forbidden", http.StatusForbidden) - - case r.URL.Path == "/repos/octocat/hookfail/hooks": - http.Error(w, "Forbidden", http.StatusForbidden) - } - - }) - - // github repository not found - mux.HandleFunc("/repos/octocat/notfound", func(w http.ResponseWriter, r *http.Request) { - http.NotFound(w, r) - }) - - // user does not have permissions to create the hook - mux.HandleFunc("/repos/octocat/hookfail/hooks", func(w http.ResponseWriter, r *http.Request) { - http.Error(w, "Forbidden", http.StatusForbidden) - }) - - // github repository found, but forbidden to create hooks - mux.HandleFunc("/repos/octocat/hookfail", func(w http.ResponseWriter, r *http.Request) { - fmt.Fprint(w, `{"id":1296269,"name":"hookfail","full_name":"octocat/hookfail","owner":{"login":"octocat","id":583231,"avatar_url":"https://gravatar.com/avatar/7194e8d48fa1d2b689f99443b767316c?d=https%3A%2F%2Fidenticons.github.com%2Fb295bf8043975e176de44c2617751f8b.png&r=x","gravatar_id":"7194e8d48fa1d2b689f99443b767316c","url":"https://api.github.com/users/octocat","html_url":"https://github.com/octocat","followers_url":"https://api.github.com/users/octocat/followers","following_url":"https://api.github.com/users/octocat/following{/other_user}","gists_url":"https://api.github.com/users/octocat/gists{/gist_id}","starred_url":"https://api.github.com/users/octocat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/octocat/subscriptions","organizations_url":"https://api.github.com/users/octocat/orgs","repos_url":"https://api.github.com/users/octocat/repos","events_url":"https://api.github.com/users/octocat/events{/privacy}","received_events_url":"https://api.github.com/users/octocat/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/octocat/hookfail","description":"This your first repo!","fork":false,"url":"https://api.github.com/repos/octocat/hookfail","forks_url":"https://api.github.com/repos/octocat/hookfail/forks","keys_url":"https://api.github.com/repos/octocat/hookfail/keys{/key_id}","collaborators_url":"https://api.github.com/repos/octocat/hookfail/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/octocat/hookfail/teams","hooks_url":"https://api.github.com/repos/octocat/hookfail/hooks","issue_events_url":"https://api.github.com/repos/octocat/hookfail/issues/events{/number}","events_url":"https://api.github.com/repos/octocat/hookfail/events","assignees_url":"https://api.github.com/repos/octocat/hookfail/assignees{/user}","branches_url":"https://api.github.com/repos/octocat/hookfail/branches{/branch}","tags_url":"https://api.github.com/repos/octocat/hookfail/tags","blobs_url":"https://api.github.com/repos/octocat/hookfail/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/octocat/hookfail/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/octocat/hookfail/git/refs{/sha}","trees_url":"https://api.github.com/repos/octocat/hookfail/git/trees{/sha}","statuses_url":"https://api.github.com/repos/octocat/hookfail/statuses/{sha}","languages_url":"https://api.github.com/repos/octocat/hookfail/languages","stargazers_url":"https://api.github.com/repos/octocat/hookfail/stargazers","contributors_url":"https://api.github.com/repos/octocat/hookfail/contributors","subscribers_url":"https://api.github.com/repos/octocat/hookfail/subscribers","subscription_url":"https://api.github.com/repos/octocat/hookfail/subscription","commits_url":"https://api.github.com/repos/octocat/hookfail/commits{/sha}","git_commits_url":"https://api.github.com/repos/octocat/hookfail/git/commits{/sha}","comments_url":"https://api.github.com/repos/octocat/hookfail/comments{/number}","issue_comment_url":"https://api.github.com/repos/octocat/hookfail/issues/comments/{number}","contents_url":"https://api.github.com/repos/octocat/hookfail/contents/{+path}","compare_url":"https://api.github.com/repos/octocat/hookfail/compare/{base}...{head}","merges_url":"https://api.github.com/repos/octocat/hookfail/merges","archive_url":"https://api.github.com/repos/octocat/hookfail/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/octocat/hookfail/downloads","issues_url":"https://api.github.com/repos/octocat/hookfail/issues{/number}","pulls_url":"https://api.github.com/repos/octocat/hookfail/pulls{/number}","milestones_url":"https://api.github.com/repos/octocat/hookfail/milestones{/number}","notifications_url":"https://api.github.com/repos/octocat/hookfail/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/octocat/hookfail/labels{/name}","releases_url":"https://api.github.com/repos/octocat/hookfail/releases{/id}","created_at":"2011-01-26T19:01:12Z","updated_at":"2014-01-14T20:21:44Z","pushed_at":"2012-03-06T23:06:51Z","git_url":"git://github.com/octocat/hookfail.git","ssh_url":"git@github.com:octocat/hookfail.git","clone_url":"https://github.com/octocat/hookfail.git","svn_url":"https://github.com/octocat/hookfail","homepage":"","size":263,"stargazers_count":1365,"watchers_count":1365,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"forks_count":867,"mirror_url":null,"open_issues_count":87,"forks":867,"open_issues":87,"watchers":1365,"default_branch":"master","master_branch":"master","network_count":867,"subscribers_count":1396}`) - }) - - // github repository found, but forbidden to create hooks - mux.HandleFunc("/repos/octocat/hookfail", func(w http.ResponseWriter, r *http.Request) { - fmt.Fprint(w, `{"id":1296269,"name":"hookfail","full_name":"octocat/hookfail","owner":{"login":"octocat","id":583231,"avatar_url":"https://gravatar.com/avatar/7194e8d48fa1d2b689f99443b767316c?d=https%3A%2F%2Fidenticons.github.com%2Fb295bf8043975e176de44c2617751f8b.png&r=x","gravatar_id":"7194e8d48fa1d2b689f99443b767316c","url":"https://api.github.com/users/octocat","html_url":"https://github.com/octocat","followers_url":"https://api.github.com/users/octocat/followers","following_url":"https://api.github.com/users/octocat/following{/other_user}","gists_url":"https://api.github.com/users/octocat/gists{/gist_id}","starred_url":"https://api.github.com/users/octocat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/octocat/subscriptions","organizations_url":"https://api.github.com/users/octocat/orgs","repos_url":"https://api.github.com/users/octocat/repos","events_url":"https://api.github.com/users/octocat/events{/privacy}","received_events_url":"https://api.github.com/users/octocat/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/octocat/hookfail","description":"This your first repo!","fork":false,"url":"https://api.github.com/repos/octocat/hookfail","forks_url":"https://api.github.com/repos/octocat/hookfail/forks","keys_url":"https://api.github.com/repos/octocat/hookfail/keys{/key_id}","collaborators_url":"https://api.github.com/repos/octocat/hookfail/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/octocat/hookfail/teams","hooks_url":"https://api.github.com/repos/octocat/hookfail/hooks","issue_events_url":"https://api.github.com/repos/octocat/hookfail/issues/events{/number}","events_url":"https://api.github.com/repos/octocat/hookfail/events","assignees_url":"https://api.github.com/repos/octocat/hookfail/assignees{/user}","branches_url":"https://api.github.com/repos/octocat/hookfail/branches{/branch}","tags_url":"https://api.github.com/repos/octocat/hookfail/tags","blobs_url":"https://api.github.com/repos/octocat/hookfail/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/octocat/hookfail/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/octocat/hookfail/git/refs{/sha}","trees_url":"https://api.github.com/repos/octocat/hookfail/git/trees{/sha}","statuses_url":"https://api.github.com/repos/octocat/hookfail/statuses/{sha}","languages_url":"https://api.github.com/repos/octocat/hookfail/languages","stargazers_url":"https://api.github.com/repos/octocat/hookfail/stargazers","contributors_url":"https://api.github.com/repos/octocat/hookfail/contributors","subscribers_url":"https://api.github.com/repos/octocat/hookfail/subscribers","subscription_url":"https://api.github.com/repos/octocat/hookfail/subscription","commits_url":"https://api.github.com/repos/octocat/hookfail/commits{/sha}","git_commits_url":"https://api.github.com/repos/octocat/hookfail/git/commits{/sha}","comments_url":"https://api.github.com/repos/octocat/hookfail/comments{/number}","issue_comment_url":"https://api.github.com/repos/octocat/hookfail/issues/comments/{number}","contents_url":"https://api.github.com/repos/octocat/hookfail/contents/{+path}","compare_url":"https://api.github.com/repos/octocat/hookfail/compare/{base}...{head}","merges_url":"https://api.github.com/repos/octocat/hookfail/merges","archive_url":"https://api.github.com/repos/octocat/hookfail/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/octocat/hookfail/downloads","issues_url":"https://api.github.com/repos/octocat/hookfail/issues{/number}","pulls_url":"https://api.github.com/repos/octocat/hookfail/pulls{/number}","milestones_url":"https://api.github.com/repos/octocat/hookfail/milestones{/number}","notifications_url":"https://api.github.com/repos/octocat/hookfail/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/octocat/hookfail/labels{/name}","releases_url":"https://api.github.com/repos/octocat/hookfail/releases{/id}","created_at":"2011-01-26T19:01:12Z","updated_at":"2014-01-14T20:21:44Z","pushed_at":"2012-03-06T23:06:51Z","git_url":"git://github.com/octocat/hookfail.git","ssh_url":"git@github.com:octocat/hookfail.git","clone_url":"https://github.com/octocat/hookfail.git","svn_url":"https://github.com/octocat/hookfail","homepage":"","size":263,"stargazers_count":1365,"watchers_count":1365,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"forks_count":867,"mirror_url":null,"open_issues_count":87,"forks":867,"open_issues":87,"watchers":1365,"default_branch":"master","master_branch":"master","network_count":867,"subscribers_count":1396}`) - }) - */ // ----------------------------------------------------------------------------------- // fixture to return a public repository and successfully // create a commit hook. From 96c256b17dd5409322bd3137151ed3c09df9c694 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Mon, 17 Mar 2014 12:04:48 -0700 Subject: [PATCH 7/7] added some comments to describe the different test cases --- pkg/build/build_test.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/pkg/build/build_test.go b/pkg/build/build_test.go index 0457a4f21..34f2f3c50 100644 --- a/pkg/build/build_test.go +++ b/pkg/build/build_test.go @@ -95,8 +95,8 @@ func TestSetup(t *testing.T) { } } -// Expected behavior is that a build script with no docker image -// specified will thrown an error. +// TestSetupEmptyImage will test our ability to handle a nil or +// blank Docker build image. We expect this to return an error. func TestSetupEmptyImage(t *testing.T) { b := Builder{Build: &script.Build{}} var got, want = b.setup(), "Error: missing Docker image" @@ -106,8 +106,8 @@ func TestSetupEmptyImage(t *testing.T) { } } -// Expected behavior is that a build script with an unknown -// service (ie mysql) +// TestSetupUnknownService will test our ability to handle an +// unknown or unsupported service (i.e. mysql). func TestSetupUnknownService(t *testing.T) { b := Builder{} b.Repo = &repo.Repo{} @@ -122,6 +122,8 @@ func TestSetupUnknownService(t *testing.T) { } } +// TestSetupErrorRunDaemonPorts will test our ability to handle a +// failure when starting a service (i.e. mysql) as a daemon. func TestSetupErrorRunDaemonPorts(t *testing.T) { setup() defer teardown() @@ -144,6 +146,9 @@ func TestSetupErrorRunDaemonPorts(t *testing.T) { } } +// TestSetupErrorServiceInspect will test our ability to handle a +// failure when a service (i.e. mysql) is started successfully, +// but cannot be queried post-start with the Docker remote API. func TestSetupErrorServiceInspect(t *testing.T) { setup() defer teardown() @@ -175,6 +180,8 @@ func TestSetupErrorServiceInspect(t *testing.T) { } } +// TestSetupErrorImagePull will test our ability to handle a +// failure when a the build image cannot be pulled from the index. func TestSetupErrorImagePull(t *testing.T) { setup() defer teardown() @@ -201,6 +208,9 @@ func TestSetupErrorImagePull(t *testing.T) { } } +// TestSetupErrorBuild will test our ability to handle a failure +// when creating a Docker image with the injected build script, +// ssh keys, etc. func TestSetupErrorBuild(t *testing.T) { setup() defer teardown() @@ -227,6 +237,10 @@ func TestSetupErrorBuild(t *testing.T) { } } +// TestSetupErrorBuildInspect will test our ability to handle a failure +// when we successfully create a Docker image with the injected build script, +// ssh keys, etc, however, we cannot inspect it post-creation using +// the Docker remote API. func TestSetupErrorBuildInspect(t *testing.T) { setup() defer teardown() @@ -258,10 +272,16 @@ func TestSetupErrorBuildInspect(t *testing.T) { } } +// TestTeardown will test our ability to sucessfully teardown a +// Docker-based build environment. func TestTeardown(t *testing.T) {} +// TestTeardownContainerFail will test our ability to handle a +// failure to stop and remove the build container. func TestTeardownContainerFail(t *testing.T) {} +// TestTeardownImageFail will test our ability to handle a +// failure to stop and remove the build image. func TestTeardownImageFail(t *testing.T) {} func TestWriteIdentifyFile(t *testing.T) {