golang.org/x/build@v0.0.0-20240506185731-218518f32b70/cmd/coordinator/internal/dashboard/handler_test.go (about) 1 // Copyright 2020 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build linux || darwin 6 7 package dashboard 8 9 import ( 10 "context" 11 "io" 12 "net/http" 13 "net/http/httptest" 14 "strings" 15 "testing" 16 "time" 17 18 "github.com/google/go-cmp/cmp" 19 "github.com/google/go-cmp/cmp/cmpopts" 20 "golang.org/x/build/cmd/coordinator/internal/lucipoll" 21 "golang.org/x/build/dashboard" 22 "golang.org/x/build/maintner/maintnerd/apipb" 23 "golang.org/x/build/types" 24 "google.golang.org/grpc" 25 ) 26 27 type fakeMaintner struct { 28 resp *apipb.DashboardResponse 29 } 30 31 func (f *fakeMaintner) GetDashboard(ctx context.Context, in *apipb.DashboardRequest, opts ...grpc.CallOption) (*apipb.DashboardResponse, error) { 32 return f.resp, nil 33 } 34 35 func TestHandlerServeHTTP(t *testing.T) { 36 fm := &fakeMaintner{ 37 resp: &apipb.DashboardResponse{Commits: []*apipb.DashCommit{ 38 { 39 Title: "x/build/cmd/coordinator: implement dashboard", 40 Commit: "752029e171d535b0dd4ff7bbad5ad0275a3969a8", 41 CommitTimeSec: 1257894000, 42 AuthorName: "Gopherbot", 43 AuthorEmail: "gopherbot@example.com", 44 }, 45 }}, 46 } 47 dh := &Handler{ 48 Maintner: fm, 49 memoryResults: map[string][]string{ 50 "752029e171d535b0dd4ff7bbad5ad0275a3969a8": {"linux-amd64-longtest|true|SomeLog|752029e171d535b0dd4ff7bbad5ad0275a3969a8"}, 51 }, 52 } 53 req := httptest.NewRequest("GET", "/dashboard", nil) 54 w := httptest.NewRecorder() 55 56 dh.ServeHTTP(w, req) 57 resp := w.Result() 58 defer resp.Body.Close() 59 io.ReadAll(resp.Body) 60 61 if resp.StatusCode != http.StatusOK { 62 t.Errorf("resp.StatusCode = %d, wanted %d", resp.StatusCode, http.StatusOK) 63 } 64 } 65 66 func TestHandlerCommits(t *testing.T) { 67 fm := &fakeMaintner{ 68 resp: &apipb.DashboardResponse{Commits: []*apipb.DashCommit{ 69 { 70 Title: "x/build/cmd/coordinator: implement dashboard", 71 Commit: "752029e171d535b0dd4ff7bbad5ad0275a3969a8", 72 CommitTimeSec: 1257894000, 73 AuthorName: "Gopherbot", 74 AuthorEmail: "gopherbot@example.com", 75 }, 76 }}, 77 } 78 dh := &Handler{ 79 Maintner: fm, 80 memoryResults: map[string][]string{ 81 "752029e171d535b0dd4ff7bbad5ad0275a3969a8": {"test-builder|true|SomeLog|752029e171d535b0dd4ff7bbad5ad0275a3969a8"}, 82 }, 83 } 84 want := []*commit{ 85 { 86 Desc: "x/build/cmd/coordinator: implement dashboard", 87 Hash: "752029e171d535b0dd4ff7bbad5ad0275a3969a8", 88 Time: time.Unix(1257894000, 0).Format("02 Jan 15:04"), 89 User: "Gopherbot <gopherbot@example.com>", 90 ResultData: []string{"test-builder|true|SomeLog|752029e171d535b0dd4ff7bbad5ad0275a3969a8"}, 91 }, 92 } 93 got := dh.commits(context.Background(), lucipoll.Snapshot{}) 94 95 if diff := cmp.Diff(want, got); diff != "" { 96 t.Errorf("dh.Commits() mismatch (-want +got):\n%s", diff) 97 } 98 } 99 100 func TestHandlerGetBuilders(t *testing.T) { 101 dh := &Handler{} 102 builders := map[string]*dashboard.BuildConfig{ 103 "linux-amd64-testfile": { 104 Name: "linux-amd64-testfile", 105 HostType: "this-is-a-test-file", 106 Notes: "", 107 MinimumGoVersion: types.MajorMinor{}, 108 }, 109 "linux-386-testfile": { 110 Name: "linux-386-testfile", 111 HostType: "this-is-a-test-file", 112 Notes: "", 113 MinimumGoVersion: types.MajorMinor{}, 114 }, 115 "darwin-amd64-testfile": { 116 Name: "darwin-amd64-testfile", 117 HostType: "this-is-a-test-file", 118 Notes: "", 119 MinimumGoVersion: types.MajorMinor{}, 120 }, 121 "android-386-testfile": { 122 Name: "android-386-testfile", 123 HostType: "this-is-a-test-file", 124 Notes: "", 125 MinimumGoVersion: types.MajorMinor{}, 126 }, 127 } 128 want := []*builder{ 129 { 130 OS: "darwin", 131 Archs: []*arch{ 132 { 133 os: "darwin", Arch: "amd64", 134 Name: "darwin-amd64-testfile", 135 Tag: "testfile", 136 }, 137 }, 138 }, 139 { 140 OS: "linux", 141 Archs: []*arch{ 142 { 143 os: "linux", Arch: "386", 144 Name: "linux-386-testfile", 145 Tag: "testfile", 146 }, 147 { 148 os: "linux", Arch: "amd64", 149 Name: "linux-amd64-testfile", 150 Tag: "testfile", 151 }, 152 }, 153 }, 154 { 155 OS: "android", 156 Archs: []*arch{ 157 { 158 os: "android", Arch: "386", 159 Name: "android-386-testfile", 160 Tag: "testfile", 161 }, 162 }, 163 }, 164 } 165 got := dh.getBuilders(builders, lucipoll.Snapshot{}) 166 167 if diff := cmp.Diff(want, got, cmpopts.EquateComparable(arch{})); diff != "" { 168 t.Errorf("dh.getBuilders() mismatch (-want +got):\n%s", diff) 169 } 170 } 171 172 func TestArchFirstClass(t *testing.T) { 173 cases := []struct { 174 name string 175 want bool 176 }{ 177 { 178 name: "linux-amd64-longtest", 179 want: true, 180 }, 181 { 182 name: "linux-buzz-longtest", 183 want: false, 184 }, 185 { 186 name: "linux-amd64", 187 want: true, 188 }, 189 { 190 name: "linux", 191 want: false, 192 }, 193 } 194 for _, c := range cases { 195 segs := strings.SplitN(c.name, "-", 3) 196 if len(segs) == 1 { 197 segs = append(segs, "") 198 } 199 a := &arch{os: segs[0], Arch: segs[1], Name: c.name} 200 if a.FirstClass() != c.want { 201 t.Errorf("%+v.FirstClass() = %v, wanted %v", a, a.FirstClass(), c.want) 202 } 203 } 204 } 205 206 func TestCommitResultForBuilder(t *testing.T) { 207 c := &commit{ 208 Desc: "x/build/cmd/coordinator: implement dashboard", 209 Hash: "752029e171d535b0dd4ff7bbad5ad0275a3969a8", 210 Time: "10 Nov 18:00", 211 User: "Gopherbot <gopherbot@example.com>", 212 ResultData: []string{"test-builder|true|SomeLog|752029e171d535b0dd4ff7bbad5ad0275a3969a8"}, 213 } 214 want := &result{ 215 OK: true, 216 LogHash: "SomeLog", 217 } 218 got := c.ResultForBuilder("test-builder") 219 220 if diff := cmp.Diff(want, got); diff != "" { 221 t.Errorf("c.ResultForBuilder(%q) mismatch (-want +got):\n%s", "test-builder", diff) 222 } 223 }