golang.org/x/build@v0.0.0-20240506185731-218518f32b70/cmd/coordinator/internal/legacydash/ui_test.go (about) 1 // Copyright 2019 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 legacydash 8 9 import ( 10 "context" 11 "testing" 12 "time" 13 14 "github.com/google/go-cmp/cmp" 15 "github.com/google/go-cmp/cmp/cmpopts" 16 "golang.org/x/build/cmd/coordinator/internal/lucipoll" 17 "golang.org/x/build/dashboard" 18 "golang.org/x/build/maintner/maintnerd/apipb" 19 "golang.org/x/build/types" 20 ) 21 22 func TestUITemplateDataBuilder(t *testing.T) { 23 // Thin the list of builders to make this test's data lighter 24 // and require less maintenance keeping it in sync. 25 origBuilders := dashboard.Builders 26 defer func() { dashboard.Builders = origBuilders }() 27 dashboard.Builders = map[string]*dashboard.BuildConfig{ 28 "linux-amd64": origBuilders["linux-amd64"], 29 "linux-386": origBuilders["linux-386"], 30 } 31 32 tests := []struct { 33 name string // test subname 34 view dashboardView // one of htmlView{}, jsonView{}, or failuresView{} 35 req *apipb.DashboardRequest // what we pretend we sent to maintner 36 res *apipb.DashboardResponse // what we pretend we got back from maintner 37 testCommitData map[string]*Commit // what we pretend we loaded from datastore 38 activeBuilds []types.ActivePostSubmitBuild 39 want *uiTemplateData // what we're hoping we generated for the view/template 40 }{ 41 // Basic test. 42 { 43 name: "html,zero_value_req,no_commits", 44 view: htmlView{}, 45 req: &apipb.DashboardRequest{}, 46 res: &apipb.DashboardResponse{ 47 Branches: []string{"release.foo", "release.bar", "dev.blah"}, 48 }, 49 want: &uiTemplateData{ 50 Dashboard: goDash, 51 Repo: "go", 52 Branch: "master", 53 Package: &Package{Name: "Go", Path: ""}, 54 Branches: []string{"release.foo", "release.bar", "dev.blah"}, 55 Builders: []string{"linux-386", "linux-amd64"}, 56 Pagination: &Pagination{}, 57 }, 58 }, 59 60 // Basic test + two commits: one that's in datastore and one that's not. 61 { 62 name: "html,zero_value_req,has_commit", 63 view: htmlView{}, 64 req: &apipb.DashboardRequest{}, 65 // Have only one commit load from the datastore: 66 testCommitData: map[string]*Commit{ 67 "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac": { 68 PackagePath: "", 69 Hash: "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac", 70 ResultData: []string{ 71 "openbsd-amd64|true||", // pretend openbsd-amd64 passed (and thus exists) 72 }, 73 }, 74 }, 75 activeBuilds: []types.ActivePostSubmitBuild{ 76 {Builder: "linux-amd64", Commit: "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac", StatusURL: "http://fake-status"}, 77 }, 78 res: &apipb.DashboardResponse{ 79 Branches: []string{"release.foo", "release.bar", "dev.blah"}, 80 Commits: []*apipb.DashCommit{ 81 // This is the maintner commit response that is in the datastore: 82 { 83 Commit: "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac", 84 AuthorName: "Foo Bar", 85 AuthorEmail: "foo@example.com", 86 CommitTimeSec: 1257894001, 87 Title: "runtime: fix all the bugs", 88 Branch: "master", 89 }, 90 // And another commit that's not in the datastore: 91 { 92 Commit: "ffffffffffffffffffffffffffffffffffffffff", 93 AuthorName: "Fancy Fred", 94 AuthorEmail: "f@eff.tld", 95 CommitTimeSec: 1257894000, 96 Title: "all: add effs", 97 Branch: "master", 98 }, 99 }, 100 CommitsTruncated: true, // pretend there's a page 2 101 }, 102 want: &uiTemplateData{ 103 Dashboard: goDash, 104 Repo: "go", 105 Branch: "master", 106 Package: &Package{Name: "Go", Path: ""}, 107 Branches: []string{"release.foo", "release.bar", "dev.blah"}, 108 Builders: []string{"linux-386", "linux-amd64", "openbsd-amd64"}, 109 Pagination: &Pagination{ 110 Next: 1, 111 }, 112 Commits: []*CommitInfo{ 113 { 114 Hash: "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac", 115 User: "Foo Bar <foo@example.com>", 116 Desc: "runtime: fix all the bugs", 117 Time: time.Unix(1257894001, 0), 118 ResultData: []string{ 119 "openbsd-amd64|true||", 120 }, 121 Branch: "master", 122 BuildingURLs: map[builderAndGoHash]string{{builder: "linux-amd64"}: "http://fake-status"}, 123 }, 124 { 125 Hash: "ffffffffffffffffffffffffffffffffffffffff", 126 User: "Fancy Fred <f@eff.tld>", 127 Desc: "all: add effs", 128 Time: time.Unix(1257894000, 0), 129 Branch: "master", 130 }, 131 }, 132 }, 133 }, 134 135 // Test that we generate the TagState (sections at 136 // bottom with the x/foo repo state). 137 { 138 name: "html,zero_value_req,has_xrepos", 139 view: htmlView{}, 140 req: &apipb.DashboardRequest{}, 141 testCommitData: map[string]*Commit{}, 142 res: &apipb.DashboardResponse{ 143 Branches: []string{"release.foo", "release.bar", "dev.blah"}, 144 Commits: []*apipb.DashCommit{ 145 { 146 Commit: "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac", 147 AuthorName: "Foo Bar", 148 AuthorEmail: "foo@example.com", 149 CommitTimeSec: 1257894001, 150 Title: "runtime: fix all the bugs", 151 Branch: "master", 152 }, 153 }, 154 Releases: []*apipb.GoRelease{ 155 { 156 BranchName: "master", 157 BranchCommit: "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac", 158 }, 159 { 160 BranchName: "release-branch.go1.99", 161 BranchCommit: "ffffffffffffffffffffffffffffffffffffffff", 162 }, 163 }, 164 RepoHeads: []*apipb.DashRepoHead{ 165 { 166 GerritProject: "go", 167 Commit: &apipb.DashCommit{ 168 Commit: "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac", 169 AuthorName: "Foo Bar", 170 AuthorEmail: "foo@example.com", 171 CommitTimeSec: 1257894001, 172 Title: "runtime: fix all the bugs", 173 Branch: "master", 174 }, 175 }, 176 { 177 GerritProject: "net", 178 Commit: &apipb.DashCommit{ 179 Commit: "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", 180 AuthorName: "Ee Yore", 181 AuthorEmail: "e@e.net", 182 CommitTimeSec: 1257894001, 183 Title: "all: fix networking", 184 Branch: "master", 185 }, 186 }, 187 { 188 GerritProject: "sys", 189 Commit: &apipb.DashCommit{ 190 Commit: "dddddddddddddddddddddddddddddddddddddddd", 191 AuthorName: "Sys Tem", 192 AuthorEmail: "sys@s.net", 193 CommitTimeSec: 1257894001, 194 Title: "sys: support more systems", 195 Branch: "master", 196 }, 197 }, 198 }, 199 }, 200 want: &uiTemplateData{ 201 Dashboard: goDash, 202 Repo: "go", 203 Branch: "master", 204 Package: &Package{Name: "Go", Path: ""}, 205 Branches: []string{"release.foo", "release.bar", "dev.blah"}, 206 Builders: []string{"linux-386", "linux-amd64"}, 207 Pagination: &Pagination{}, 208 Commits: []*CommitInfo{ 209 { 210 Hash: "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac", 211 User: "Foo Bar <foo@example.com>", 212 Desc: "runtime: fix all the bugs", 213 Time: time.Unix(1257894001, 0), 214 Branch: "master", 215 }, 216 }, 217 TagState: []*TagState{ 218 { 219 Name: "master", 220 Tag: &CommitInfo{Hash: "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac"}, 221 Builders: []string{"linux-386", "linux-amd64"}, 222 Packages: []*PackageState{ 223 { 224 Package: &Package{Name: "net", Path: "golang.org/x/net"}, 225 Commit: &CommitInfo{ 226 Hash: "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", 227 PackagePath: "golang.org/x/net", 228 User: "Ee Yore <e@e.net>", 229 Desc: "all: fix networking", 230 Time: time.Unix(1257894001, 0), 231 Branch: "master", 232 }, 233 }, 234 { 235 Package: &Package{Name: "sys", Path: "golang.org/x/sys"}, 236 Commit: &CommitInfo{ 237 Hash: "dddddddddddddddddddddddddddddddddddddddd", 238 PackagePath: "golang.org/x/sys", 239 User: "Sys Tem <sys@s.net>", 240 Desc: "sys: support more systems", 241 Time: time.Unix(1257894001, 0), 242 Branch: "master", 243 }, 244 }, 245 }, 246 }, 247 { 248 Name: "release-branch.go1.99", 249 Tag: &CommitInfo{Hash: "ffffffffffffffffffffffffffffffffffffffff"}, 250 Builders: []string{"linux-386", "linux-amd64"}, 251 Packages: []*PackageState{ 252 { 253 Package: &Package{Name: "net", Path: "golang.org/x/net"}, 254 Commit: &CommitInfo{ 255 Hash: "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", 256 PackagePath: "golang.org/x/net", 257 User: "Ee Yore <e@e.net>", 258 Desc: "all: fix networking", 259 Time: time.Unix(1257894001, 0), 260 Branch: "master", 261 }, 262 }, 263 { 264 Package: &Package{Name: "sys", Path: "golang.org/x/sys"}, 265 Commit: &CommitInfo{ 266 Hash: "dddddddddddddddddddddddddddddddddddddddd", 267 PackagePath: "golang.org/x/sys", 268 User: "Sys Tem <sys@s.net>", 269 Desc: "sys: support more systems", 270 Time: time.Unix(1257894001, 0), 271 Branch: "master", 272 }, 273 }, 274 }, 275 }, 276 }, 277 }, 278 }, 279 280 // Test viewing a non-go repo. 281 { 282 name: "html,other_repo", 283 view: htmlView{}, 284 req: &apipb.DashboardRequest{Repo: "golang.org/x/net"}, 285 testCommitData: map[string]*Commit{}, 286 res: &apipb.DashboardResponse{ 287 Branches: []string{"master", "dev.blah"}, 288 Commits: []*apipb.DashCommit{ 289 { 290 Commit: "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac", 291 AuthorName: "Foo Bar", 292 AuthorEmail: "foo@example.com", 293 CommitTimeSec: 1257894001, 294 Title: "net: fix all the bugs", 295 Branch: "master", 296 GoCommitAtTime: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 297 GoCommitLatest: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 298 }, 299 }, 300 }, 301 want: &uiTemplateData{ 302 Dashboard: goDash, 303 Repo: "net", 304 Branch: "master", 305 Package: &Package{Name: "net", Path: "golang.org/x/net"}, 306 Branches: []string{"master", "dev.blah"}, 307 Builders: []string{"linux-386", "linux-amd64"}, 308 Pagination: &Pagination{}, 309 Commits: []*CommitInfo{ 310 { 311 PackagePath: "golang.org/x/net", 312 Hash: "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac", 313 User: "Foo Bar <foo@example.com>", 314 Desc: "net: fix all the bugs", 315 Time: time.Unix(1257894001, 0), 316 Branch: "master", 317 ResultData: []string{ 318 "|false||aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 319 "|false||bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 320 }, 321 }, 322 }, 323 }, 324 }, 325 } 326 for _, tt := range tests { 327 t.Run(tt.name, func(t *testing.T) { 328 tb := &uiTemplateDataBuilder{ 329 view: tt.view, 330 req: tt.req, 331 res: tt.res, 332 activeBuilds: tt.activeBuilds, 333 testCommitData: tt.testCommitData, 334 } 335 data, err := tb.buildTemplateData(context.Background(), nil, lucipoll.Snapshot{}) 336 if err != nil { 337 t.Fatal(err) 338 } 339 diff := cmp.Diff(tt.want, data, cmpopts.IgnoreUnexported(CommitInfo{})) 340 if diff != "" { 341 t.Errorf("mismatch want->got:\n%s", diff) 342 } 343 }) 344 } 345 } 346 347 func TestToBuildStatus(t *testing.T) { 348 tests := []struct { 349 name string 350 data *uiTemplateData 351 want types.BuildStatus 352 }{ 353 { 354 name: "go repo", 355 data: &uiTemplateData{ 356 Dashboard: goDash, 357 Repo: "go", 358 Branch: "master", 359 Package: &Package{Name: "Go", Path: ""}, 360 Branches: []string{"release.foo", "release.bar", "dev.blah"}, 361 Builders: []string{"linux-386", "linux-amd64"}, 362 Pagination: &Pagination{}, 363 Commits: []*CommitInfo{ 364 { 365 Hash: "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac", 366 User: "Foo Bar <foo@example.com>", 367 Desc: "runtime: fix all the bugs", 368 Time: time.Unix(1257894001, 0).UTC(), 369 Branch: "master", 370 }, 371 }, 372 TagState: []*TagState{ 373 { 374 Name: "master", 375 Tag: &CommitInfo{Hash: "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac"}, 376 Builders: []string{"linux-386", "linux-amd64"}, 377 Packages: []*PackageState{ 378 { 379 Package: &Package{Name: "net", Path: "golang.org/x/net"}, 380 Commit: &CommitInfo{ 381 Hash: "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", 382 PackagePath: "golang.org/x/net", 383 User: "Ee Yore <e@e.net>", 384 Desc: "all: fix networking", 385 Time: time.Unix(1257894001, 0).UTC(), 386 Branch: "master", 387 }, 388 }, 389 { 390 Package: &Package{Name: "sys", Path: "golang.org/x/sys"}, 391 Commit: &CommitInfo{ 392 Hash: "dddddddddddddddddddddddddddddddddddddddd", 393 PackagePath: "golang.org/x/sys", 394 User: "Sys Tem <sys@s.net>", 395 Desc: "sys: support more systems", 396 Time: time.Unix(1257894001, 0).UTC(), 397 Branch: "master", 398 }, 399 }, 400 }, 401 }, 402 { 403 Name: "release-branch.go1.99", 404 Tag: &CommitInfo{Hash: "ffffffffffffffffffffffffffffffffffffffff"}, 405 Builders: []string{"linux-386", "linux-amd64"}, 406 Packages: []*PackageState{ 407 { 408 Package: &Package{Name: "net", Path: "golang.org/x/net"}, 409 Commit: &CommitInfo{ 410 Hash: "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", 411 PackagePath: "golang.org/x/net", 412 User: "Ee Yore <e@e.net>", 413 Desc: "all: fix networking", 414 Time: time.Unix(1257894001, 0).UTC(), 415 Branch: "master", 416 }, 417 }, 418 { 419 Package: &Package{Name: "sys", Path: "golang.org/x/sys"}, 420 Commit: &CommitInfo{ 421 Hash: "dddddddddddddddddddddddddddddddddddddddd", 422 PackagePath: "golang.org/x/sys", 423 User: "Sys Tem <sys@s.net>", 424 Desc: "sys: support more systems", 425 Time: time.Unix(1257894001, 0).UTC(), 426 Branch: "master", 427 }, 428 }, 429 }, 430 }, 431 }, 432 }, 433 want: types.BuildStatus{ 434 Builders: []string{"linux-386", "linux-amd64"}, 435 Revisions: []types.BuildRevision{ 436 { 437 Repo: "go", 438 Revision: "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac", 439 Date: "2009-11-10T23:00:01Z", 440 Branch: "master", 441 Author: "Foo Bar <foo@example.com>", 442 Desc: "runtime: fix all the bugs", 443 Results: []string{"", ""}, 444 }, 445 { 446 Repo: "net", 447 Revision: "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", 448 GoRevision: "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac", 449 Date: "2009-11-10T23:00:01Z", 450 Branch: "master", 451 GoBranch: "master", 452 Author: "Ee Yore <e@e.net>", 453 Desc: "all: fix networking", 454 Results: []string{"", ""}, 455 }, 456 { 457 Repo: "sys", 458 Revision: "dddddddddddddddddddddddddddddddddddddddd", 459 GoRevision: "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac", 460 Date: "2009-11-10T23:00:01Z", 461 Branch: "master", 462 GoBranch: "master", 463 Author: "Sys Tem <sys@s.net>", 464 Desc: "sys: support more systems", 465 Results: []string{"", ""}, 466 }, 467 { 468 Repo: "net", 469 Revision: "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", 470 GoRevision: "ffffffffffffffffffffffffffffffffffffffff", 471 Date: "2009-11-10T23:00:01Z", 472 Branch: "master", 473 GoBranch: "release-branch.go1.99", 474 Author: "Ee Yore <e@e.net>", 475 Desc: "all: fix networking", 476 Results: []string{"", ""}, 477 }, 478 { 479 Repo: "sys", 480 Revision: "dddddddddddddddddddddddddddddddddddddddd", 481 GoRevision: "ffffffffffffffffffffffffffffffffffffffff", 482 Date: "2009-11-10T23:00:01Z", 483 Branch: "master", 484 GoBranch: "release-branch.go1.99", 485 Author: "Sys Tem <sys@s.net>", 486 Desc: "sys: support more systems", 487 Results: []string{"", ""}, 488 }, 489 }, 490 }, 491 }, 492 { 493 name: "other repo", 494 data: &uiTemplateData{ 495 Dashboard: goDash, 496 Repo: "tools", 497 Branch: "master", 498 Builders: []string{"linux", "windows"}, 499 Commits: []*CommitInfo{ 500 { 501 PackagePath: "golang.org/x/tools", 502 Hash: "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac", 503 User: "Foo Bar <foo@example.com>", 504 Desc: "tools: fix all the bugs", 505 Time: time.Unix(1257894001, 0).UTC(), 506 Branch: "master", 507 ResultData: []string{ 508 "linux|false|123|aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 509 "windows|false|456|bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 510 }, 511 }, 512 }, 513 }, 514 want: types.BuildStatus{ 515 Builders: []string{"linux", "windows"}, 516 Revisions: []types.BuildRevision{ 517 { 518 Repo: "tools", 519 Revision: "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac", 520 GoRevision: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 521 Date: "2009-11-10T23:00:01Z", 522 Branch: "master", 523 Author: "Foo Bar <foo@example.com>", 524 Desc: "tools: fix all the bugs", 525 Results: []string{"", "https://build.golang.org/log/456"}, 526 }, 527 { 528 Repo: "tools", 529 Revision: "26957168c4c0cdcc7ca4f0b19d0eb19474d224ac", 530 GoRevision: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 531 Date: "2009-11-10T23:00:01Z", 532 Branch: "master", 533 Author: "Foo Bar <foo@example.com>", 534 Desc: "tools: fix all the bugs", 535 Results: []string{"https://build.golang.org/log/123", ""}, 536 }, 537 }, 538 }, 539 }, 540 } 541 542 for _, tt := range tests { 543 t.Run(tt.name, func(t *testing.T) { 544 got := toBuildStatus("build.golang.org", tt.data) 545 if diff := cmp.Diff(tt.want, got); diff != "" { 546 t.Errorf("buildStatus(...) mismatch (-want +got):\n%s", diff) 547 } 548 }) 549 } 550 }