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