| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package swarming | 5 package swarming |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "encoding/json" | 8 "encoding/json" |
| 9 "flag" | 9 "flag" |
| 10 "fmt" | 10 "fmt" |
| 11 "io/ioutil" | 11 "io/ioutil" |
| 12 "path/filepath" | 12 "path/filepath" |
| 13 "testing" | 13 "testing" |
| 14 "time" | 14 "time" |
| 15 | 15 |
| 16 "github.com/luci/luci-go/common/clock/testclock" | 16 "github.com/luci/luci-go/common/clock/testclock" |
| 17 | 17 |
| 18 "github.com/luci/gae/impl/memory" |
| 19 |
| 18 "golang.org/x/net/context" | 20 "golang.org/x/net/context" |
| 19 | 21 |
| 20 . "github.com/smartystreets/goconvey/convey" | 22 . "github.com/smartystreets/goconvey/convey" |
| 21 ) | 23 ) |
| 22 | 24 |
| 23 var generate = flag.Bool("test.generate", false, "Generate expectations instead
of running tests.") | 25 var generate = flag.Bool("test.generate", false, "Generate expectations instead
of running tests.") |
| 24 | 26 |
| 25 func load(name string) ([]byte, error) { | 27 func load(name string) ([]byte, error) { |
| 26 filename := filepath.Join("expectations", name) | 28 filename := filepath.Join("expectations", name) |
| 27 return ioutil.ReadFile(filename) | 29 return ioutil.ReadFile(filename) |
| 28 } | 30 } |
| 29 | 31 |
| 30 func shouldMatchExpectationsFor(actualContents interface{}, expectedFilename ...
interface{}) string { | 32 func shouldMatchExpectationsFor(actualContents interface{}, expectedFilename ...
interface{}) string { |
| 31 refBuild, err := load(expectedFilename[0].(string)) | 33 refBuild, err := load(expectedFilename[0].(string)) |
| 32 if err != nil { | 34 if err != nil { |
| 33 return fmt.Sprintf("Could not load %s: %s", expectedFilename[0],
err.Error()) | 35 return fmt.Sprintf("Could not load %s: %s", expectedFilename[0],
err.Error()) |
| 34 } | 36 } |
| 35 actualBuild, err := json.MarshalIndent(actualContents, "", " ") | 37 actualBuild, err := json.MarshalIndent(actualContents, "", " ") |
| 36 return ShouldEqual(string(actualBuild), string(refBuild)) | 38 return ShouldEqual(string(actualBuild), string(refBuild)) |
| 37 | 39 |
| 38 } | 40 } |
| 39 | 41 |
| 40 func TestBuild(t *testing.T) { | 42 func TestBuild(t *testing.T) { |
| 41 » c := context.Background() | 43 » t.Parallel() |
| 42 » c, _ = testclock.UseTime(c, time.Date(2016, time.March, 14, 11, 0, 0, 0,
time.UTC)) | |
| 43 | 44 |
| 44 var svc debugSwarmingService | |
| 45 if *generate { | 45 if *generate { |
| 46 c := context.Background() |
| 47 c, _ = testclock.UseTime(c, time.Date(2016, time.March, 14, 11,
0, 0, 0, time.UTC)) |
| 48 c = memory.Use(c) |
| 49 |
| 46 for _, tc := range getTestCases() { | 50 for _, tc := range getTestCases() { |
| 51 bl := buildLoader{ |
| 52 logDogClientFunc: logDogClientFunc(tc), |
| 53 } |
| 54 svc := debugSwarmingService{tc} |
| 55 |
| 47 fmt.Printf("Generating expectations for %s\n", tc) | 56 fmt.Printf("Generating expectations for %s\n", tc) |
| 48 | 57 |
| 49 » » » build, err := swarmingBuildImpl(c, svc, "foo", tc) | 58 » » » build, err := bl.swarmingBuildImpl(c, svc, "foo", tc.nam
e) |
| 50 if err != nil { | 59 if err != nil { |
| 51 panic(fmt.Errorf("Could not run swarmingBuildImp
l for %s: %s", tc, err)) | 60 panic(fmt.Errorf("Could not run swarmingBuildImp
l for %s: %s", tc, err)) |
| 52 } | 61 } |
| 53 buildJSON, err := json.MarshalIndent(build, "", " ") | 62 buildJSON, err := json.MarshalIndent(build, "", " ") |
| 54 if err != nil { | 63 if err != nil { |
| 55 » » » » panic(fmt.Errorf("Could not JSON marshal %s: %s"
, tc, err)) | 64 » » » » panic(fmt.Errorf("Could not JSON marshal %s: %s"
, tc.name, err)) |
| 56 } | 65 } |
| 57 » » » filename := filepath.Join("expectations", tc+".json") | 66 » » » filename := filepath.Join("expectations", tc.name+".json
") |
| 58 err = ioutil.WriteFile(filename, []byte(buildJSON), 0644
) | 67 err = ioutil.WriteFile(filename, []byte(buildJSON), 0644
) |
| 59 if err != nil { | 68 if err != nil { |
| 60 panic(fmt.Errorf("Encountered error while trying
to write to %s: %s", filename, err)) | 69 panic(fmt.Errorf("Encountered error while trying
to write to %s: %s", filename, err)) |
| 61 } | 70 } |
| 62 } | 71 } |
| 63 return | 72 return |
| 64 } | 73 } |
| 65 | 74 |
| 66 Convey(`A test Environment`, t, func() { | 75 Convey(`A test Environment`, t, func() { |
| 76 c := context.Background() |
| 77 c, _ = testclock.UseTime(c, time.Date(2016, time.March, 14, 11,
0, 0, 0, time.UTC)) |
| 78 c = memory.Use(c) |
| 79 |
| 67 for _, tc := range getTestCases() { | 80 for _, tc := range getTestCases() { |
| 68 » » » Convey(fmt.Sprintf("Test Case: %s", tc), func() { | 81 » » » Convey(fmt.Sprintf("Test Case: %s", tc.name), func() { |
| 69 » » » » build, err := swarmingBuildImpl(c, svc, "foo", t
c) | 82 » » » » bl := buildLoader{ |
| 83 » » » » » logDogClientFunc: logDogClientFunc(tc), |
| 84 » » » » } |
| 85 » » » » svc := debugSwarmingService{tc} |
| 86 |
| 87 » » » » build, err := bl.swarmingBuildImpl(c, svc, "foo"
, tc.name) |
| 70 So(err, ShouldBeNil) | 88 So(err, ShouldBeNil) |
| 71 » » » » So(build, shouldMatchExpectationsFor, tc+".json"
) | 89 » » » » So(build, shouldMatchExpectationsFor, tc.name+".
json") |
| 72 }) | 90 }) |
| 73 } | 91 } |
| 74 }) | 92 }) |
| 75 } | 93 } |
| OLD | NEW |