From 3e6abf78b7af8c3bd61f9d512374768d3128e6cb Mon Sep 17 00:00:00 2001 From: Francisco Souza Date: Wed, 11 Feb 2015 15:33:34 -0200 Subject: [PATCH] gandalftest: implement getDiff in the testing server --- gandalftest/server.go | 44 ++++++++++++++++++++++++++++++++------ gandalftest/server_test.go | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/gandalftest/server.go b/gandalftest/server.go index a56d6c5..7cf5012 100644 --- a/gandalftest/server.go +++ b/gandalftest/server.go @@ -18,12 +18,13 @@ import ( ) type Repository struct { - Name string `json:"name"` - Users []string `json:"users"` - ReadOnlyUsers []string `json:",omit"` - ReadOnlyURL string `json:"git_url"` - ReadWriteURL string `json:"ssh_url"` - IsPublic bool `json:"ispublic"` + Name string `json:"name"` + Users []string `json:"users"` + ReadOnlyUsers []string `json:"readonlyusers"` + ReadOnlyURL string `json:"git_url"` + ReadWriteURL string `json:"ssh_url"` + IsPublic bool `json:"ispublic"` + Diffs chan string `json:"-"` } type user struct { @@ -169,6 +170,17 @@ func (s *GandalfServer) Keys(user string) (map[string]string, error) { return keyMap, nil } +// PrepareDiff prepares a diff for the given repository and writes it to the +// next getDiff call in the server. +func (s *GandalfServer) PrepareDiff(repository, content string) { + if repo, index := s.findRepository(repository); index > -1 { + repo.Diffs <- content + s.repoLock.Lock() + s.repos[index] = repo + s.repoLock.Unlock() + } +} + // Reset resets all internal information of the server, like keys, repositories, users and prepared failures. func (s *GandalfServer) Reset() { s.usersLock.Lock() @@ -197,6 +209,7 @@ func (s *GandalfServer) buildMuxer() { s.muxer.Post("/repository/grant", http.HandlerFunc(s.grantAccess)) s.muxer.Delete("/repository/revoke", http.HandlerFunc(s.revokeAccess)) s.muxer.Post("/repository", http.HandlerFunc(s.createRepository)) + s.muxer.Get("/repository/{name}/diff/commits", http.HandlerFunc(s.getDiff)) s.muxer.Delete("/repository/{name}", http.HandlerFunc(s.removeRepository)) s.muxer.Get("/repository/{name}", http.HandlerFunc(s.getRepository)) s.muxer.Get("/healthcheck", http.HandlerFunc(s.healthcheck)) @@ -247,6 +260,7 @@ func (s *GandalfServer) createRepository(w http.ResponseWriter, r *http.Request) http.Error(w, err.Error(), http.StatusBadRequest) return } + repo.Diffs = make(chan string, 1) users := append(repo.Users, repo.ReadOnlyUsers...) for _, userName := range users { _, index := s.findUser(userName) @@ -295,6 +309,24 @@ func (s *GandalfServer) getRepository(w http.ResponseWriter, r *http.Request) { } } +func (s *GandalfServer) getDiff(w http.ResponseWriter, r *http.Request) { + name := r.URL.Query().Get(":name") + repo, index := s.findRepository(name) + if index < 0 { + http.Error(w, "repository not found", http.StatusNotFound) + return + } + var content string + select { + case content = <-repo.Diffs: + default: + } + fmt.Fprint(w, content) + s.repoLock.Lock() + s.repos[index] = repo + s.repoLock.Unlock() +} + func (s *GandalfServer) grantAccess(w http.ResponseWriter, r *http.Request) { readOnly := r.URL.Query().Get("readonly") == "yes" repositories, users, err := s.validateAccessRequest(r) diff --git a/gandalftest/server_test.go b/gandalftest/server_test.go index 9da7fd8..d833efe 100644 --- a/gandalftest/server_test.go +++ b/gandalftest/server_test.go @@ -299,6 +299,44 @@ func (s *S) TestGetRepositoryNotFound(c *check.C) { c.Assert(recorder.Body.String(), check.Equals, "repository not found\n") } +func (s *S) TestGetDiff(c *check.C) { + repo := Repository{Name: "somerepo", Diffs: make(chan string, 1)} + server, err := NewServer("127.0.0.1:0") + c.Assert(err, check.IsNil) + defer server.Stop() + server.repos = []Repository{repo} + server.PrepareDiff(repo.Name, "some diff") + recorder := httptest.NewRecorder() + request, _ := http.NewRequest("GET", "/repository/somerepo/diff/commits?previous_commit=10&last_commit=11", nil) + server.ServeHTTP(recorder, request) + c.Assert(recorder.Code, check.Equals, http.StatusOK) + c.Assert(recorder.Body.String(), check.Equals, "some diff") +} + +func (s *S) TestGetDiffUnprepared(c *check.C) { + repo := Repository{Name: "somerepo", Diffs: make(chan string, 1)} + server, err := NewServer("127.0.0.1:0") + c.Assert(err, check.IsNil) + defer server.Stop() + server.repos = []Repository{repo} + recorder := httptest.NewRecorder() + request, _ := http.NewRequest("GET", "/repository/somerepo/diff/commits?previous_commit=10&last_commit=11", nil) + server.ServeHTTP(recorder, request) + c.Assert(recorder.Code, check.Equals, http.StatusOK) + c.Assert(recorder.Body.String(), check.Equals, "") +} + +func (s *S) TestGetDiffNotFound(c *check.C) { + server, err := NewServer("127.0.0.1:0") + c.Assert(err, check.IsNil) + defer server.Stop() + recorder := httptest.NewRecorder() + request, _ := http.NewRequest("GET", "/repository/somerepo/diff/commits?previous_commit=10&last_commit=11", nil) + server.ServeHTTP(recorder, request) + c.Assert(recorder.Code, check.Equals, http.StatusNotFound) + c.Assert(recorder.Body.String(), check.Equals, "repository not found\n") +} + func (s *S) TestAddKeys(c *check.C) { server, err := NewServer("127.0.0.1:0") c.Assert(err, check.IsNil)