| 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" |
| 13 "strings" | 13 "strings" |
| 14 "testing" | 14 "testing" |
| 15 "time" |
| 15 | 16 |
| 16 "github.com/luci/luci-go/common/clock/testclock" | 17 "github.com/luci/luci-go/common/clock/testclock" |
| 17 . "github.com/smartystreets/goconvey/convey" | 18 . "github.com/smartystreets/goconvey/convey" |
| 18 "golang.org/x/net/context" | 19 "golang.org/x/net/context" |
| 19 ) | 20 ) |
| 20 | 21 |
| 21 var generate = flag.Bool("test.generate", false, "Generate expectations instead
of running tests.") | 22 var generate = flag.Bool("test.generate", false, "Generate expectations instead
of running tests.") |
| 22 | 23 |
| 23 func load(name string) ([]byte, error) { | 24 func load(name string) ([]byte, error) { |
| 24 filename := strings.Join([]string{"expectations", name}, "/") | 25 filename := strings.Join([]string{"expectations", name}, "/") |
| (...skipping 11 matching lines...) Expand all Loading... |
| 36 } | 37 } |
| 37 | 38 |
| 38 func TestBuild(t *testing.T) { | 39 func TestBuild(t *testing.T) { |
| 39 testCases := []struct { | 40 testCases := []struct { |
| 40 input string | 41 input string |
| 41 expectations string | 42 expectations string |
| 42 }{ | 43 }{ |
| 43 {"debug:build-exception", "build-exception.json"}, | 44 {"debug:build-exception", "build-exception.json"}, |
| 44 {"debug:build-patch-failure", "build-patch-failure.json"}, | 45 {"debug:build-patch-failure", "build-patch-failure.json"}, |
| 45 {"debug:build-pending", "build-pending.json"}, | 46 {"debug:build-pending", "build-pending.json"}, |
| 47 {"debug:build-running", "build-running.json"}, |
| 46 {"debug:build-timeout", "build-timeout.json"}, | 48 {"debug:build-timeout", "build-timeout.json"}, |
| 47 } | 49 } |
| 48 | 50 |
| 51 c := context.Background() |
| 52 c, _ = testclock.UseTime(c, time.Date(2016, time.March, 14, 11, 0, 0, 0,
time.UTC)) |
| 53 |
| 49 if *generate { | 54 if *generate { |
| 50 for _, tc := range testCases { | 55 for _, tc := range testCases { |
| 51 c := context.Background() | |
| 52 c, _ = testclock.UseTime(c, testclock.TestTimeUTC) | |
| 53 fmt.Printf("Generating expectations for %s\n", tc.input) | 56 fmt.Printf("Generating expectations for %s\n", tc.input) |
| 54 build, err := swarmingBuildImpl(c, "foo", "default", tc.
input) | 57 build, err := swarmingBuildImpl(c, "foo", "default", tc.
input) |
| 55 if err != nil { | 58 if err != nil { |
| 56 panic(fmt.Errorf("Could not run swarmingBuildImp
l for %s: %s", tc.input, err)) | 59 panic(fmt.Errorf("Could not run swarmingBuildImp
l for %s: %s", tc.input, err)) |
| 57 } | 60 } |
| 58 buildJSON, err := json.MarshalIndent(build, "", " ") | 61 buildJSON, err := json.MarshalIndent(build, "", " ") |
| 59 if err != nil { | 62 if err != nil { |
| 60 panic(fmt.Errorf("Could not JSON marshal %s: %s"
, tc.input, err)) | 63 panic(fmt.Errorf("Could not JSON marshal %s: %s"
, tc.input, err)) |
| 61 } | 64 } |
| 62 filename := path.Join("expectations", tc.expectations) | 65 filename := path.Join("expectations", tc.expectations) |
| 63 err = ioutil.WriteFile(filename, []byte(buildJSON), 0644
) | 66 err = ioutil.WriteFile(filename, []byte(buildJSON), 0644
) |
| 64 if err != nil { | 67 if err != nil { |
| 65 panic(fmt.Errorf("Encountered error while trying
to write to %s: %s", filename, err)) | 68 panic(fmt.Errorf("Encountered error while trying
to write to %s: %s", filename, err)) |
| 66 } | 69 } |
| 67 } | 70 } |
| 68 return | 71 return |
| 69 } | 72 } |
| 70 | 73 |
| 71 Convey(`A test Environment`, t, func() { | 74 Convey(`A test Environment`, t, func() { |
| 72 c := context.Background() | |
| 73 c, _ = testclock.UseTime(c, testclock.TestTimeUTC) | |
| 74 | |
| 75 for _, tc := range testCases { | 75 for _, tc := range testCases { |
| 76 Convey(fmt.Sprintf("Test Case: %s", tc.input), func() { | 76 Convey(fmt.Sprintf("Test Case: %s", tc.input), func() { |
| 77 build, err := swarmingBuildImpl(c, "foo", "defau
lt", tc.input) | 77 build, err := swarmingBuildImpl(c, "foo", "defau
lt", tc.input) |
| 78 So(err, ShouldBeNil) | 78 So(err, ShouldBeNil) |
| 79 So(build, shouldMatchExpectationsFor, tc.expecta
tions) | 79 So(build, shouldMatchExpectationsFor, tc.expecta
tions) |
| 80 }) | 80 }) |
| 81 } | 81 } |
| 82 }) | 82 }) |
| 83 } | 83 } |
| OLD | NEW |