Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(602)

Side by Side Diff: go/buildbot/buildbot_test.go

Issue 813443002: Overhaul database package (Closed) Base URL: https://skia.googlesource.com/buildbot@master
Patch Set: Move README Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 package buildbot 1 package buildbot
2 2
3 import ( 3 import (
4 "bytes" 4 "bytes"
5 "encoding/json" 5 "encoding/json"
6 "fmt" 6 "fmt"
7 "io" 7 "io"
8 "net/http" 8 "net/http"
9 "path/filepath" 9 "path/filepath"
10 "reflect" 10 "reflect"
11 "testing" 11 "testing"
12 12
13 "github.com/golang/glog" 13 "github.com/golang/glog"
14 14
15 "skia.googlesource.com/buildbot.git/go/database"
16 "skia.googlesource.com/buildbot.git/go/gitinfo" 15 "skia.googlesource.com/buildbot.git/go/gitinfo"
17 "skia.googlesource.com/buildbot.git/go/testutils" 16 "skia.googlesource.com/buildbot.git/go/testutils"
18 "skia.googlesource.com/buildbot.git/go/util" 17 "skia.googlesource.com/buildbot.git/go/util"
19 ) 18 )
20 19
21 var ( 20 var (
22 // testJsonInput is raw JSON data as returned from the build master. 21 // testJsonInput is raw JSON data as returned from the build master.
23 testJsonInput = testutils.MustReadFile("default_build.json") 22 testJsonInput = testutils.MustReadFile("default_build.json")
24 23
25 // testIncompleteBuild is JSON data for a not-yet-finished build. 24 // testIncompleteBuild is JSON data for a not-yet-finished build.
(...skipping 22 matching lines...) Expand all
48 "http://build.chromium.org/p/client.skia/json/builders/Test-Ubun tu12-ShuttleA-GTX550Ti-x86_64-Release-Valgrind/builds/152": []byte(testIncomplet eBuild), 47 "http://build.chromium.org/p/client.skia/json/builders/Test-Ubun tu12-ShuttleA-GTX550Ti-x86_64-Release-Valgrind/builds/152": []byte(testIncomplet eBuild),
49 "http://build.chromium.org/p/client.skia.android/json/builders/P erf-Android-Venue8-PowerVR-x86-Release/builds/464": []byte(venue464), 48 "http://build.chromium.org/p/client.skia.android/json/builders/P erf-Android-Venue8-PowerVR-x86-Release/builds/464": []byte(venue464),
50 "http://build.chromium.org/p/client.skia.android/json/builders/P erf-Android-Venue8-PowerVR-x86-Release/builds/465": []byte(venue465), 49 "http://build.chromium.org/p/client.skia.android/json/builders/P erf-Android-Venue8-PowerVR-x86-Release/builds/465": []byte(venue465),
51 "http://build.chromium.org/p/client.skia.android/json/builders/P erf-Android-Venue8-PowerVR-x86-Release/builds/466": []byte(venue466), 50 "http://build.chromium.org/p/client.skia.android/json/builders/P erf-Android-Venue8-PowerVR-x86-Release/builds/466": []byte(venue466),
52 "http://build.chromium.org/p/client.skia.fyi/json/builders/House keeper-PerCommit/builds/1035": []byte(housekeeper10 35), 51 "http://build.chromium.org/p/client.skia.fyi/json/builders/House keeper-PerCommit/builds/1035": []byte(housekeeper10 35),
53 } 52 }
54 ) 53 )
55 54
56 // clearDB initializes the database, upgrading it if needed, and removes all 55 // clearDB initializes the database, upgrading it if needed, and removes all
57 // data to ensure that the test begins with a clean slate. 56 // data to ensure that the test begins with a clean slate.
58 func clearDB(t *testing.T, conf *database.DatabaseConfig) error { 57 func clearDB(t *testing.T) error {
59 failMsg := "Database initialization failed. Do you have the test databas e set up properly? Details: %v" 58 failMsg := "Database initialization failed. Do you have the test databas e set up properly? Details: %v"
60 » if err := InitDB(conf); err != nil { 59 » if err := InitDB(true); err != nil {
61 t.Fatalf(failMsg, err) 60 t.Fatalf(failMsg, err)
62 } 61 }
63 tables := []string{ 62 tables := []string{
64 TABLE_BUILD_REVISIONS, 63 TABLE_BUILD_REVISIONS,
65 TABLE_BUILD_STEPS, 64 TABLE_BUILD_STEPS,
66 TABLE_BUILDS, 65 TABLE_BUILDS,
67 } 66 }
68 // Delete the data. 67 // Delete the data.
69 for _, table := range tables { 68 for _, table := range tables {
70 _, err := DB.Exec(fmt.Sprintf("DELETE FROM %s;", table)) 69 _, err := DB.Exec(fmt.Sprintf("DELETE FROM %s;", table))
(...skipping 29 matching lines...) Expand all
100 // testGetBuild is a helper function which pretends to load JSON data from a 99 // testGetBuild is a helper function which pretends to load JSON data from a
101 // build master and decodes it into a Build object. 100 // build master and decodes it into a Build object.
102 func testGetBuildFromMaster(repo *gitinfo.GitInfo) (*Build, error) { 101 func testGetBuildFromMaster(repo *gitinfo.GitInfo) (*Build, error) {
103 httpGet = testGet 102 httpGet = testGet
104 return getBuildFromMaster("client.skia", "Test-Ubuntu12-ShuttleA-GTX660- x86-Release", 721, repo) 103 return getBuildFromMaster("client.skia", "Test-Ubuntu12-ShuttleA-GTX660- x86-Release", 721, repo)
105 } 104 }
106 105
107 // TestGetBuildFromMaster verifies that we can load JSON data from the build mas ter and 106 // TestGetBuildFromMaster verifies that we can load JSON data from the build mas ter and
108 // decode it into a Build object. 107 // decode it into a Build object.
109 func TestGetBuildFromMaster(t *testing.T) { 108 func TestGetBuildFromMaster(t *testing.T) {
110 » clearDB(t, ProdDatabaseConfig(true)) 109 » clearDB(t)
111 110
112 // Load the test repo. 111 // Load the test repo.
113 tr := util.NewTempRepo() 112 tr := util.NewTempRepo()
114 defer tr.Cleanup() 113 defer tr.Cleanup()
115 repo, err := gitinfo.NewGitInfo(filepath.Join(tr.Dir, "testrepo"), false , true) 114 repo, err := gitinfo.NewGitInfo(filepath.Join(tr.Dir, "testrepo"), false , true)
116 if err != nil { 115 if err != nil {
117 t.Fatal(err) 116 t.Fatal(err)
118 } 117 }
119 118
120 // Default, complete build. 119 // Default, complete build.
121 if _, err := testGetBuildFromMaster(repo); err != nil { 120 if _, err := testGetBuildFromMaster(repo); err != nil {
122 t.Fatal(err) 121 t.Fatal(err)
123 } 122 }
124 // Incomplete build. 123 // Incomplete build.
125 _, err = getBuildFromMaster("client.skia", "Test-Ubuntu12-ShuttleA-GTX55 0Ti-x86_64-Release-Valgrind", 152, repo) 124 _, err = getBuildFromMaster("client.skia", "Test-Ubuntu12-ShuttleA-GTX55 0Ti-x86_64-Release-Valgrind", 152, repo)
126 if err != nil { 125 if err != nil {
127 t.Fatal(err) 126 t.Fatal(err)
128 } 127 }
129 } 128 }
130 129
131 // TestBuildJsonSerialization verifies that we can serialize a build to JSON 130 // TestBuildJsonSerialization verifies that we can serialize a build to JSON
132 // and back without losing or corrupting the data. 131 // and back without losing or corrupting the data.
133 func TestBuildJsonSerialization(t *testing.T) { 132 func TestBuildJsonSerialization(t *testing.T) {
134 » if err := clearDB(t, ProdDatabaseConfig(true)); err != nil { 133 » clearDB(t)
135 » » t.Fatal(err)
136 » }
137 134
138 // Load the test repo. 135 // Load the test repo.
139 tr := util.NewTempRepo() 136 tr := util.NewTempRepo()
140 defer tr.Cleanup() 137 defer tr.Cleanup()
141 repo, err := gitinfo.NewGitInfo(filepath.Join(tr.Dir, "testrepo"), false , true) 138 repo, err := gitinfo.NewGitInfo(filepath.Join(tr.Dir, "testrepo"), false , true)
142 if err != nil { 139 if err != nil {
143 t.Fatal(err) 140 t.Fatal(err)
144 } 141 }
145 142
146 b1, err := testGetBuildFromMaster(repo) 143 b1, err := testGetBuildFromMaster(repo)
147 if err != nil { 144 if err != nil {
148 t.Fatal(err) 145 t.Fatal(err)
149 } 146 }
150 bytes, err := json.Marshal(b1) 147 bytes, err := json.Marshal(b1)
151 if err != nil { 148 if err != nil {
152 t.Fatal(err) 149 t.Fatal(err)
153 } 150 }
154 b2 := &Build{} 151 b2 := &Build{}
155 if err := json.Unmarshal(bytes, b2); err != nil { 152 if err := json.Unmarshal(bytes, b2); err != nil {
156 t.Fatal(err) 153 t.Fatal(err)
157 } 154 }
158 if !reflect.DeepEqual(b1, b2) { 155 if !reflect.DeepEqual(b1, b2) {
159 t.Fatalf("Serialization diff:\nIn: %v\nOut: %v", b1, b2) 156 t.Fatalf("Serialization diff:\nIn: %v\nOut: %v", b1, b2)
160 } 157 }
161 } 158 }
162 159
163 // TestFindCommitsForBuild verifies that findCommitsForBuild correctly obtains 160 // TestFindCommitsForBuild verifies that findCommitsForBuild correctly obtains
164 // the list of commits which were newly built in a given build. 161 // the list of commits which were newly built in a given build.
165 func TestFindCommitsForBuild(t *testing.T) { 162 func TestFindCommitsForBuild(t *testing.T) {
166 » if err := clearDB(t, ProdDatabaseConfig(true)); err != nil { 163 » clearDB(t)
167 » » t.Fatal(err)
168 » }
169 164
170 // Load the test repo. 165 // Load the test repo.
171 tr := util.NewTempRepo() 166 tr := util.NewTempRepo()
172 defer tr.Cleanup() 167 defer tr.Cleanup()
173 repo, err := gitinfo.NewGitInfo(filepath.Join(tr.Dir, "testrepo"), false , true) 168 repo, err := gitinfo.NewGitInfo(filepath.Join(tr.Dir, "testrepo"), false , true)
174 if err != nil { 169 if err != nil {
175 t.Fatal(err) 170 t.Fatal(err)
176 } 171 }
177 172
178 // The test repo is laid out like this: 173 // The test repo is laid out like this:
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 glog.Errorf("Not equal:\n %+v\n %+v\n", s, b2.St eps[i]) 271 glog.Errorf("Not equal:\n %+v\n %+v\n", s, b2.St eps[i])
277 } 272 }
278 } 273 }
279 return fmt.Errorf("Builds are not equal! Builds:\nExpected: %+v\ nActual: %+v", b1, b2) 274 return fmt.Errorf("Builds are not equal! Builds:\nExpected: %+v\ nActual: %+v", b1, b2)
280 } 275 }
281 return nil 276 return nil
282 } 277 }
283 278
284 // testBuildDbSerialization verifies that we can write a build to the DB and 279 // testBuildDbSerialization verifies that we can write a build to the DB and
285 // pull it back out without losing or corrupting the data. 280 // pull it back out without losing or corrupting the data.
286 func testBuildDbSerialization(t *testing.T, conf *database.DatabaseConfig) { 281 func testBuildDbSerialization(t *testing.T) {
287 » clearDB(t, conf) 282 » clearDB(t)
288 // Load the test repo. 283 // Load the test repo.
289 tr := util.NewTempRepo() 284 tr := util.NewTempRepo()
290 defer tr.Cleanup() 285 defer tr.Cleanup()
291 repo, err := gitinfo.NewGitInfo(filepath.Join(tr.Dir, "testrepo"), false , true) 286 repo, err := gitinfo.NewGitInfo(filepath.Join(tr.Dir, "testrepo"), false , true)
292 if err != nil { 287 if err != nil {
293 t.Fatal(err) 288 t.Fatal(err)
294 } 289 }
295 290
296 // Test case: an empty build. Tests null and empty values. 291 // Test case: an empty build. Tests null and empty values.
297 emptyTime := 0.0 292 emptyTime := 0.0
(...skipping 13 matching lines...) Expand all
311 for _, b := range testCases { 306 for _, b := range testCases {
312 if err = dbSerializeAndCompare(b); err != nil { 307 if err = dbSerializeAndCompare(b); err != nil {
313 t.Fatal(err) 308 t.Fatal(err)
314 } 309 }
315 } 310 }
316 } 311 }
317 312
318 // testUnfinishedBuild verifies that we can write a build which is not yet 313 // testUnfinishedBuild verifies that we can write a build which is not yet
319 // finished, load the build back from the database, and update it when it 314 // finished, load the build back from the database, and update it when it
320 // finishes. 315 // finishes.
321 func testUnfinishedBuild(t *testing.T, conf *database.DatabaseConfig) { 316 func testUnfinishedBuild(t *testing.T) {
322 » clearDB(t, conf) 317 » clearDB(t)
323 // Load the test repo. 318 // Load the test repo.
324 tr := util.NewTempRepo() 319 tr := util.NewTempRepo()
325 defer tr.Cleanup() 320 defer tr.Cleanup()
326 repo, err := gitinfo.NewGitInfo(filepath.Join(tr.Dir, "testrepo"), false , true) 321 repo, err := gitinfo.NewGitInfo(filepath.Join(tr.Dir, "testrepo"), false , true)
327 if err != nil { 322 if err != nil {
328 t.Fatal(err) 323 t.Fatal(err)
329 } 324 }
330 325
331 // Obtain and insert an unfinished build. 326 // Obtain and insert an unfinished build.
332 httpGet = testGet 327 httpGet = testGet
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 break 389 break
395 } 390 }
396 } 391 }
397 if found { 392 if found {
398 t.Fatal(fmt.Errorf("Finished build was found by getUnfinishedBui lds!")) 393 t.Fatal(fmt.Errorf("Finished build was found by getUnfinishedBui lds!"))
399 } 394 }
400 } 395 }
401 396
402 // testLastProcessedBuilds verifies that getLastProcessedBuilds gives us 397 // testLastProcessedBuilds verifies that getLastProcessedBuilds gives us
403 // the expected result. 398 // the expected result.
404 func testLastProcessedBuilds(t *testing.T, conf *database.DatabaseConfig) { 399 func testLastProcessedBuilds(t *testing.T) {
405 » clearDB(t, conf) 400 » clearDB(t)
406 // Load the test repo. 401 // Load the test repo.
407 tr := util.NewTempRepo() 402 tr := util.NewTempRepo()
408 defer tr.Cleanup() 403 defer tr.Cleanup()
409 repo, err := gitinfo.NewGitInfo(filepath.Join(tr.Dir, "testrepo"), false , true) 404 repo, err := gitinfo.NewGitInfo(filepath.Join(tr.Dir, "testrepo"), false , true)
410 if err != nil { 405 if err != nil {
411 t.Fatal(err) 406 t.Fatal(err)
412 } 407 }
413 408
414 build, err := testGetBuildFromMaster(repo) 409 build, err := testGetBuildFromMaster(repo)
415 if err != nil { 410 if err != nil {
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 actual, err := getLatestBuilds() 510 actual, err := getLatestBuilds()
516 if err != nil { 511 if err != nil {
517 t.Fatal(err) 512 t.Fatal(err)
518 } 513 }
519 if !reflect.DeepEqual(expected, actual) { 514 if !reflect.DeepEqual(expected, actual) {
520 t.Fatal(fmt.Errorf("getLatestBuilds returned incorrect results: %v", actual)) 515 t.Fatal(fmt.Errorf("getLatestBuilds returned incorrect results: %v", actual))
521 } 516 }
522 } 517 }
523 518
524 // testGetUningestedBuilds verifies that getUningestedBuilds works as expected. 519 // testGetUningestedBuilds verifies that getUningestedBuilds works as expected.
525 func testGetUningestedBuilds(t *testing.T, conf *database.DatabaseConfig) { 520 func testGetUningestedBuilds(t *testing.T) {
526 // First, insert some builds into the database as a starting point. 521 // First, insert some builds into the database as a starting point.
527 » clearDB(t, conf) 522 » clearDB(t)
528 523
529 // Load the test repo. 524 // Load the test repo.
530 tr := util.NewTempRepo() 525 tr := util.NewTempRepo()
531 defer tr.Cleanup() 526 defer tr.Cleanup()
532 repo, err := gitinfo.NewGitInfo(filepath.Join(tr.Dir, "testrepo"), false , true) 527 repo, err := gitinfo.NewGitInfo(filepath.Join(tr.Dir, "testrepo"), false , true)
533 if err != nil { 528 if err != nil {
534 t.Fatal(err) 529 t.Fatal(err)
535 } 530 }
536 531
537 // This builder is no longer found on the master. 532 // This builder is no longer found on the master.
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
606 t.Fatal(err) 601 t.Fatal(err)
607 } 602 }
608 if !reflect.DeepEqual(expected, actual) { 603 if !reflect.DeepEqual(expected, actual) {
609 t.Fatal(fmt.Errorf("getUningestedBuilds returned incorrect resul ts: %v", actual)) 604 t.Fatal(fmt.Errorf("getUningestedBuilds returned incorrect resul ts: %v", actual))
610 } 605 }
611 } 606 }
612 607
613 // testIngestNewBuilds verifies that we can successfully query the masters and 608 // testIngestNewBuilds verifies that we can successfully query the masters and
614 // the database for new and unfinished builds, respectively, and ingest them 609 // the database for new and unfinished builds, respectively, and ingest them
615 // into the database. 610 // into the database.
616 func testIngestNewBuilds(t *testing.T, conf *database.DatabaseConfig) { 611 func testIngestNewBuilds(t *testing.T) {
617 // First, insert some builds into the database as a starting point. 612 // First, insert some builds into the database as a starting point.
618 » clearDB(t, conf) 613 » clearDB(t)
619 614
620 // Load the test repo. 615 // Load the test repo.
621 tr := util.NewTempRepo() 616 tr := util.NewTempRepo()
622 defer tr.Cleanup() 617 defer tr.Cleanup()
623 repo, err := gitinfo.NewGitInfo(filepath.Join(tr.Dir, "testrepo"), false , true) 618 repo, err := gitinfo.NewGitInfo(filepath.Join(tr.Dir, "testrepo"), false , true)
624 if err != nil { 619 if err != nil {
625 t.Fatal(err) 620 t.Fatal(err)
626 } 621 }
627 622
628 // This builder needs to load a few builds. 623 // This builder needs to load a few builds.
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
711 } 706 }
712 if !(a.MasterName == e.MasterName && a.BuilderName == e.BuilderN ame && a.Number == e.Number) { 707 if !(a.MasterName == e.MasterName && a.BuilderName == e.BuilderN ame && a.Number == e.Number) {
713 t.Fatalf("Incorrect build was inserted! %v", a) 708 t.Fatalf("Incorrect build was inserted! %v", a)
714 } 709 }
715 if !a.IsFinished() { 710 if !a.IsFinished() {
716 t.Fatalf("Failed to update build properly; it should be finished: %v", a) 711 t.Fatalf("Failed to update build properly; it should be finished: %v", a)
717 } 712 }
718 } 713 }
719 } 714 }
720 715
721 func TestSQLiteBuildDbSerialization(t *testing.T) {
722 testBuildDbSerialization(t, ProdDatabaseConfig(true))
723 }
724
725 func TestSQLiteUnfinishedBuild(t *testing.T) {
726 testUnfinishedBuild(t, ProdDatabaseConfig(true))
727 }
728
729 func TestSQLiteLastProcessedBuilds(t *testing.T) {
730 testLastProcessedBuilds(t, ProdDatabaseConfig(true))
731 }
732
733 func TestSQLiteGetUningestedBuilds(t *testing.T) {
734 testGetUningestedBuilds(t, ProdDatabaseConfig(true))
735 }
736
737 func TestSQLiteIngestNewBuilds(t *testing.T) {
738 testIngestNewBuilds(t, ProdDatabaseConfig(true))
739 }
740
741 // The below MySQL tests require:
742 // shell> mysql -u root
743 // mysql> CREATE DATABASE sk_testing;
744 // mysql> CREATE USER 'test_user'@'localhost';
745 // mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON sk_testing.* TO 'test _user'@'localhost';
746 //
747 // They are skipped when using the -short flag.
748 func TestMySQLBuildDbSerialization(t *testing.T) { 716 func TestMySQLBuildDbSerialization(t *testing.T) {
749 if testing.Short() { 717 if testing.Short() {
750 t.Skip("Skipping MySQL tests with -short.") 718 t.Skip("Skipping MySQL tests with -short.")
751 } 719 }
752 » testBuildDbSerialization(t, localMySQLTestDatabaseConfig("test_user", "" )) 720 » testBuildDbSerialization(t)
753 } 721 }
754 722
755 func TestMySQLUnfinishedBuild(t *testing.T) { 723 func TestMySQLUnfinishedBuild(t *testing.T) {
756 if testing.Short() { 724 if testing.Short() {
757 t.Skip("Skipping MySQL tests with -short.") 725 t.Skip("Skipping MySQL tests with -short.")
758 } 726 }
759 » testUnfinishedBuild(t, localMySQLTestDatabaseConfig("test_user", "")) 727 » testUnfinishedBuild(t)
760 } 728 }
761 729
762 func TestMySQLLastProcessedBuilds(t *testing.T) { 730 func TestMySQLLastProcessedBuilds(t *testing.T) {
763 if testing.Short() { 731 if testing.Short() {
764 t.Skip("Skipping MySQL tests with -short.") 732 t.Skip("Skipping MySQL tests with -short.")
765 } 733 }
766 » testLastProcessedBuilds(t, localMySQLTestDatabaseConfig("test_user", "") ) 734 » testLastProcessedBuilds(t)
767 } 735 }
768 736
769 func TestMySQLGetUningestedBuilds(t *testing.T) { 737 func TestMySQLGetUningestedBuilds(t *testing.T) {
770 if testing.Short() { 738 if testing.Short() {
771 t.Skip("Skipping MySQL tests with -short.") 739 t.Skip("Skipping MySQL tests with -short.")
772 } 740 }
773 » testGetUningestedBuilds(t, localMySQLTestDatabaseConfig("test_user", "") ) 741 » testGetUningestedBuilds(t)
774 } 742 }
775 743
776 func TestMySQLIngestNewBuilds(t *testing.T) { 744 func TestMySQLIngestNewBuilds(t *testing.T) {
777 if testing.Short() { 745 if testing.Short() {
778 t.Skip("Skipping MySQL tests with -short.") 746 t.Skip("Skipping MySQL tests with -short.")
779 } 747 }
780 » testIngestNewBuilds(t, localMySQLTestDatabaseConfig("test_user", "")) 748 » testIngestNewBuilds(t)
781 } 749 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698