Sean Abraham | 47cc585 | 2020-06-30 09:32:41 -0600 | [diff] [blame^] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Sean Abraham | 636ded7 | 2020-06-29 17:49:41 -0600 | [diff] [blame] | 5 | package branch |
| 6 | |
| 7 | import ( |
| 8 | "encoding/json" |
| 9 | "fmt" |
| 10 | "github.com/andygrunwald/go-gerrit" |
| 11 | "io/ioutil" |
| 12 | "net/http" |
| 13 | "net/http/httptest" |
| 14 | "testing" |
| 15 | ) |
| 16 | |
| 17 | var ( |
| 18 | httpClient = &http.Client{} |
| 19 | testMux *http.ServeMux |
| 20 | testClient *gerrit.Client |
| 21 | testServer *httptest.Server |
| 22 | largeQps = 1e9 |
| 23 | ) |
| 24 | |
| 25 | func setUp() { |
| 26 | testMux = http.NewServeMux() |
| 27 | testServer = httptest.NewServer(testMux) |
| 28 | testClient, _ = gerrit.NewClient(testServer.URL, nil) |
| 29 | } |
| 30 | |
| 31 | func tearDown() { |
| 32 | testServer.Close() |
| 33 | } |
| 34 | |
| 35 | func testMethod(t *testing.T, r *http.Request, want string) { |
| 36 | if got := r.Method; got != want { |
| 37 | t.Errorf("Request method: %v, want %v", got, want) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestCreateRemoteBranchesApi_success(t *testing.T) { |
| 42 | setUp() |
| 43 | defer tearDown() |
| 44 | |
| 45 | branchesToCreate := []GerritProjectBranch{ |
| 46 | {GerritURL: testServer.URL, Project: "my-project", SrcRef: "my-source", Branch: "my-branch-1"}, |
| 47 | {GerritURL: testServer.URL, Project: "my-project", SrcRef: "my-source", Branch: "my-branch-2"}, |
| 48 | } |
| 49 | |
| 50 | branchesCreated := make(chan string, len(branchesToCreate)) |
| 51 | |
| 52 | testMux.HandleFunc("/projects/my-project/branches/", func(w http.ResponseWriter, r *http.Request) { |
| 53 | testMethod(t, r, "PUT") |
| 54 | branchName := r.URL.Path[len("/projects/my-project/branches/"):] |
| 55 | branchesCreated <- branchName |
| 56 | |
| 57 | defer r.Body.Close() |
| 58 | b, err := ioutil.ReadAll(r.Body) |
| 59 | if err != nil { |
| 60 | http.Error(w, branchName, http.StatusBadRequest) |
| 61 | } |
| 62 | |
| 63 | bi := &gerrit.BranchInput{} |
| 64 | if err := json.Unmarshal(b, bi); err != nil { |
| 65 | http.Error(w, branchName, http.StatusBadRequest) |
| 66 | } |
| 67 | info := &gerrit.BranchInfo{ |
| 68 | Ref: bi.Ref, |
| 69 | } |
| 70 | branchInfoRaw, err := json.Marshal(&info) |
| 71 | if err != nil { |
| 72 | http.Error(w, branchName, http.StatusBadRequest) |
| 73 | } |
| 74 | |
| 75 | fmt.Fprint(w, `)]}'`+"\n"+string(branchInfoRaw)) |
| 76 | }) |
| 77 | |
| 78 | if err := CreateRemoteBranchesApi(httpClient, |
| 79 | branchesToCreate, false, false, largeQps); err != nil { |
| 80 | t.Error(err) |
| 81 | } |
| 82 | close(branchesCreated) |
| 83 | branchMap := make(map[string]bool) |
| 84 | for bc := range branchesCreated { |
| 85 | branchMap[bc] = true |
| 86 | } |
| 87 | if len(branchesToCreate) != len(branchMap) { |
| 88 | t.Errorf("expected %v branches created, instead %v", len(branchesToCreate), len(branchMap)) |
| 89 | } |
| 90 | for _, btc := range branchesToCreate { |
| 91 | if _, ok := branchMap[btc.Branch]; !ok { |
| 92 | t.Errorf("no branch creation call made for %v", btc.Branch) |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | func TestCreateRemoteBranchesApi_apiError(t *testing.T) { |
| 98 | setUp() |
| 99 | defer tearDown() |
| 100 | |
| 101 | branchesToCreate := []GerritProjectBranch{ |
| 102 | {GerritURL: testServer.URL, Project: "my-project", SrcRef: "my-source", Branch: "my-branch-1"}, |
| 103 | } |
| 104 | |
| 105 | testMux.HandleFunc("/projects/my-project/branches/", func(w http.ResponseWriter, r *http.Request) { |
| 106 | testMethod(t, r, "PUT") |
| 107 | branchName := r.URL.Path[len("/projects/my-project/branches/"):] |
| 108 | http.Error(w, branchName, http.StatusBadRequest) |
| 109 | }) |
| 110 | |
| 111 | if err := CreateRemoteBranchesApi(httpClient, |
| 112 | branchesToCreate, false, false, largeQps); err != nil { |
| 113 | } else { |
| 114 | t.Errorf("expected an error, instead nil") |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestCheckSelfGroupMembership_success(t *testing.T) { |
| 119 | setUp() |
| 120 | defer tearDown() |
| 121 | |
| 122 | testMux.HandleFunc("/accounts/self/groups", func(w http.ResponseWriter, r *http.Request) { |
| 123 | testMethod(t, r, "GET") |
| 124 | info := []*gerrit.GroupInfo{ |
| 125 | {Name: "in this group"}, |
| 126 | } |
| 127 | groupInfoRaw, err := json.Marshal(&info) |
| 128 | if err != nil { |
| 129 | http.Error(w, "groups", http.StatusBadRequest) |
| 130 | } |
| 131 | |
| 132 | fmt.Fprint(w, `)]}'`+"\n"+string(groupInfoRaw)) |
| 133 | }) |
| 134 | |
| 135 | inGroup, err := CheckSelfGroupMembership(httpClient, testServer.URL, "in this group") |
| 136 | if err != nil { |
| 137 | t.Error(err) |
| 138 | } |
| 139 | if !inGroup { |
| 140 | t.Errorf("expected to be in group, but wasn't") |
| 141 | } |
| 142 | |
| 143 | inGroup, err = CheckSelfGroupMembership(httpClient, testServer.URL, "not in this group") |
| 144 | if err != nil { |
| 145 | t.Error(err) |
| 146 | } |
| 147 | if inGroup { |
| 148 | t.Errorf("expected not to be in group, but was") |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | func TestCheckSelfGroupMembership_apiError(t *testing.T) { |
| 153 | setUp() |
| 154 | defer tearDown() |
| 155 | |
| 156 | testMux.HandleFunc("/accounts/self/groups", func(w http.ResponseWriter, r *http.Request) { |
| 157 | testMethod(t, r, "GET") |
| 158 | http.Error(w, "groups", http.StatusBadRequest) |
| 159 | }) |
| 160 | |
| 161 | _, err := CheckSelfGroupMembership(httpClient, testServer.URL, "some group") |
| 162 | if err == nil { |
| 163 | t.Errorf("expected an error, but was none") |
| 164 | } |
| 165 | } |