| 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" | 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 |
| 18 "golang.org/x/net/context" |
| 19 |
| 17 . "github.com/smartystreets/goconvey/convey" | 20 . "github.com/smartystreets/goconvey/convey" |
| 18 "golang.org/x/net/context" | |
| 19 ) | 21 ) |
| 20 | 22 |
| 21 var generate = flag.Bool("test.generate", false, "Generate expectations instead
of running tests.") | 23 var generate = flag.Bool("test.generate", false, "Generate expectations instead
of running tests.") |
| 22 | 24 |
| 23 func load(name string) ([]byte, error) { | 25 func load(name string) ([]byte, error) { |
| 24 » filename := path.Join("expectations", name) | 26 » filename := filepath.Join("expectations", name) |
| 25 return ioutil.ReadFile(filename) | 27 return ioutil.ReadFile(filename) |
| 26 } | 28 } |
| 27 | 29 |
| 28 func shouldMatchExpectationsFor(actualContents interface{}, expectedFilename ...
interface{}) string { | 30 func shouldMatchExpectationsFor(actualContents interface{}, expectedFilename ...
interface{}) string { |
| 29 refBuild, err := load(expectedFilename[0].(string)) | 31 refBuild, err := load(expectedFilename[0].(string)) |
| 30 if err != nil { | 32 if err != nil { |
| 31 return fmt.Sprintf("Could not load %s: %s", expectedFilename[0],
err.Error()) | 33 return fmt.Sprintf("Could not load %s: %s", expectedFilename[0],
err.Error()) |
| 32 } | 34 } |
| 33 actualBuild, err := json.MarshalIndent(actualContents, "", " ") | 35 actualBuild, err := json.MarshalIndent(actualContents, "", " ") |
| 34 return ShouldEqual(string(actualBuild), string(refBuild)) | 36 return ShouldEqual(string(actualBuild), string(refBuild)) |
| 35 | 37 |
| 36 } | 38 } |
| 37 | 39 |
| 38 func TestBuild(t *testing.T) { | 40 func TestBuild(t *testing.T) { |
| 39 c := context.Background() | 41 c := context.Background() |
| 40 c, _ = testclock.UseTime(c, time.Date(2016, time.March, 14, 11, 0, 0, 0,
time.UTC)) | 42 c, _ = testclock.UseTime(c, time.Date(2016, time.March, 14, 11, 0, 0, 0,
time.UTC)) |
| 41 | 43 |
| 44 var svc debugSwarmingService |
| 42 if *generate { | 45 if *generate { |
| 43 for _, tc := range getTestCases() { | 46 for _, tc := range getTestCases() { |
| 44 fmt.Printf("Generating expectations for %s\n", tc) | 47 fmt.Printf("Generating expectations for %s\n", tc) |
| 45 » » » build, err := swarmingBuildImpl(c, "foo", "debug", tc) | 48 |
| 49 » » » build, err := swarmingBuildImpl(c, svc, "foo", tc) |
| 46 if err != nil { | 50 if err != nil { |
| 47 panic(fmt.Errorf("Could not run swarmingBuildImp
l for %s: %s", tc, err)) | 51 panic(fmt.Errorf("Could not run swarmingBuildImp
l for %s: %s", tc, err)) |
| 48 } | 52 } |
| 49 buildJSON, err := json.MarshalIndent(build, "", " ") | 53 buildJSON, err := json.MarshalIndent(build, "", " ") |
| 50 if err != nil { | 54 if err != nil { |
| 51 panic(fmt.Errorf("Could not JSON marshal %s: %s"
, tc, err)) | 55 panic(fmt.Errorf("Could not JSON marshal %s: %s"
, tc, err)) |
| 52 } | 56 } |
| 53 » » » filename := path.Join("expectations", tc+".json") | 57 » » » filename := filepath.Join("expectations", tc+".json") |
| 54 err = ioutil.WriteFile(filename, []byte(buildJSON), 0644
) | 58 err = ioutil.WriteFile(filename, []byte(buildJSON), 0644
) |
| 55 if err != nil { | 59 if err != nil { |
| 56 panic(fmt.Errorf("Encountered error while trying
to write to %s: %s", filename, err)) | 60 panic(fmt.Errorf("Encountered error while trying
to write to %s: %s", filename, err)) |
| 57 } | 61 } |
| 58 } | 62 } |
| 59 return | 63 return |
| 60 } | 64 } |
| 61 | 65 |
| 62 Convey(`A test Environment`, t, func() { | 66 Convey(`A test Environment`, t, func() { |
| 63 for _, tc := range getTestCases() { | 67 for _, tc := range getTestCases() { |
| 64 Convey(fmt.Sprintf("Test Case: %s", tc), func() { | 68 Convey(fmt.Sprintf("Test Case: %s", tc), func() { |
| 65 » » » » build, err := swarmingBuildImpl(c, "foo", "debug
", tc) | 69 » » » » build, err := swarmingBuildImpl(c, svc, "foo", t
c) |
| 66 So(err, ShouldBeNil) | 70 So(err, ShouldBeNil) |
| 67 So(build, shouldMatchExpectationsFor, tc+".json"
) | 71 So(build, shouldMatchExpectationsFor, tc+".json"
) |
| 68 }) | 72 }) |
| 69 } | 73 } |
| 70 }) | 74 }) |
| 71 } | 75 } |
| OLD | NEW |