branch: add branch_api_test.go
BUG=chromium:1069862
TEST=yes
Change-Id: I0a0bc6b25c511d2259241c47accbb3a91b26f97a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/infra/go/+/2274925
Reviewed-by: George Engelbrecht <engeg@google.com>
Commit-Queue: Sean Abraham <seanabraham@chromium.org>
Tested-by: Sean Abraham <seanabraham@chromium.org>
diff --git a/internal/branch/branch_api_test.go b/internal/branch/branch_api_test.go
new file mode 100644
index 0000000..6b82831
--- /dev/null
+++ b/internal/branch/branch_api_test.go
@@ -0,0 +1,161 @@
+package branch
+
+import (
+ "encoding/json"
+ "fmt"
+ "github.com/andygrunwald/go-gerrit"
+ "io/ioutil"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+)
+
+var (
+ httpClient = &http.Client{}
+ testMux *http.ServeMux
+ testClient *gerrit.Client
+ testServer *httptest.Server
+ largeQps = 1e9
+)
+
+func setUp() {
+ testMux = http.NewServeMux()
+ testServer = httptest.NewServer(testMux)
+ testClient, _ = gerrit.NewClient(testServer.URL, nil)
+}
+
+func tearDown() {
+ testServer.Close()
+}
+
+func testMethod(t *testing.T, r *http.Request, want string) {
+ if got := r.Method; got != want {
+ t.Errorf("Request method: %v, want %v", got, want)
+ }
+}
+
+func TestCreateRemoteBranchesApi_success(t *testing.T) {
+ setUp()
+ defer tearDown()
+
+ branchesToCreate := []GerritProjectBranch{
+ {GerritURL: testServer.URL, Project: "my-project", SrcRef: "my-source", Branch: "my-branch-1"},
+ {GerritURL: testServer.URL, Project: "my-project", SrcRef: "my-source", Branch: "my-branch-2"},
+ }
+
+ branchesCreated := make(chan string, len(branchesToCreate))
+
+ testMux.HandleFunc("/projects/my-project/branches/", func(w http.ResponseWriter, r *http.Request) {
+ testMethod(t, r, "PUT")
+ branchName := r.URL.Path[len("/projects/my-project/branches/"):]
+ branchesCreated <- branchName
+
+ defer r.Body.Close()
+ b, err := ioutil.ReadAll(r.Body)
+ if err != nil {
+ http.Error(w, branchName, http.StatusBadRequest)
+ }
+
+ bi := &gerrit.BranchInput{}
+ if err := json.Unmarshal(b, bi); err != nil {
+ http.Error(w, branchName, http.StatusBadRequest)
+ }
+ info := &gerrit.BranchInfo{
+ Ref: bi.Ref,
+ }
+ branchInfoRaw, err := json.Marshal(&info)
+ if err != nil {
+ http.Error(w, branchName, http.StatusBadRequest)
+ }
+
+ fmt.Fprint(w, `)]}'`+"\n"+string(branchInfoRaw))
+ })
+
+ if err := CreateRemoteBranchesApi(httpClient,
+ branchesToCreate, false, false, largeQps); err != nil {
+ t.Error(err)
+ }
+ close(branchesCreated)
+ branchMap := make(map[string]bool)
+ for bc := range branchesCreated {
+ branchMap[bc] = true
+ }
+ if len(branchesToCreate) != len(branchMap) {
+ t.Errorf("expected %v branches created, instead %v", len(branchesToCreate), len(branchMap))
+ }
+ for _, btc := range branchesToCreate {
+ if _, ok := branchMap[btc.Branch]; !ok {
+ t.Errorf("no branch creation call made for %v", btc.Branch)
+ }
+ }
+}
+
+func TestCreateRemoteBranchesApi_apiError(t *testing.T) {
+ setUp()
+ defer tearDown()
+
+ branchesToCreate := []GerritProjectBranch{
+ {GerritURL: testServer.URL, Project: "my-project", SrcRef: "my-source", Branch: "my-branch-1"},
+ }
+
+ testMux.HandleFunc("/projects/my-project/branches/", func(w http.ResponseWriter, r *http.Request) {
+ testMethod(t, r, "PUT")
+ branchName := r.URL.Path[len("/projects/my-project/branches/"):]
+ http.Error(w, branchName, http.StatusBadRequest)
+ })
+
+ if err := CreateRemoteBranchesApi(httpClient,
+ branchesToCreate, false, false, largeQps); err != nil {
+ } else {
+ t.Errorf("expected an error, instead nil")
+ }
+}
+
+func TestCheckSelfGroupMembership_success(t *testing.T) {
+ setUp()
+ defer tearDown()
+
+ testMux.HandleFunc("/accounts/self/groups", func(w http.ResponseWriter, r *http.Request) {
+ testMethod(t, r, "GET")
+ info := []*gerrit.GroupInfo{
+ {Name: "in this group"},
+ }
+ groupInfoRaw, err := json.Marshal(&info)
+ if err != nil {
+ http.Error(w, "groups", http.StatusBadRequest)
+ }
+
+ fmt.Fprint(w, `)]}'`+"\n"+string(groupInfoRaw))
+ })
+
+ inGroup, err := CheckSelfGroupMembership(httpClient, testServer.URL, "in this group")
+ if err != nil {
+ t.Error(err)
+ }
+ if !inGroup {
+ t.Errorf("expected to be in group, but wasn't")
+ }
+
+ inGroup, err = CheckSelfGroupMembership(httpClient, testServer.URL, "not in this group")
+ if err != nil {
+ t.Error(err)
+ }
+ if inGroup {
+ t.Errorf("expected not to be in group, but was")
+ }
+}
+
+func TestCheckSelfGroupMembership_apiError(t *testing.T) {
+ setUp()
+ defer tearDown()
+
+ testMux.HandleFunc("/accounts/self/groups", func(w http.ResponseWriter, r *http.Request) {
+ testMethod(t, r, "GET")
+ http.Error(w, "groups", http.StatusBadRequest)
+ })
+
+ _, err := CheckSelfGroupMembership(httpClient, testServer.URL, "some group")
+ if err == nil {
+ t.Errorf("expected an error, but was none")
+ }
+}