| 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 "fmt" | 9 "fmt" |
| 9 "io/ioutil" | 10 "io/ioutil" |
| 11 "os" |
| 10 "path/filepath" | 12 "path/filepath" |
| 11 "strings" | 13 "strings" |
| 12 "time" | 14 "time" |
| 13 | 15 |
| 14 "golang.org/x/net/context" | 16 "golang.org/x/net/context" |
| 15 | 17 |
| 18 swarming "github.com/luci/luci-go/common/api/swarming/swarming/v1" |
| 16 "github.com/luci/luci-go/common/clock/testclock" | 19 "github.com/luci/luci-go/common/clock/testclock" |
| 17 "github.com/luci/luci-go/milo/api/resp" | 20 "github.com/luci/luci-go/milo/api/resp" |
| 18 "github.com/luci/luci-go/milo/appengine/settings" | 21 "github.com/luci/luci-go/milo/appengine/settings" |
| 19 "github.com/luci/luci-go/server/templates" | 22 "github.com/luci/luci-go/server/templates" |
| 20 ) | 23 ) |
| 21 | 24 |
| 22 var testCases = []struct { | 25 var testCases = []struct { |
| 23 input string | 26 input string |
| 24 expectations string | 27 expectations string |
| 25 }{ | 28 }{ |
| 26 {"build-canceled", "build-canceled.json"}, | 29 {"build-canceled", "build-canceled.json"}, |
| 27 {"build-exception", "build-exception.json"}, | 30 {"build-exception", "build-exception.json"}, |
| 28 {"build-expired", "build-expired.json"}, | 31 {"build-expired", "build-expired.json"}, |
| 29 {"build-link", "build-link.json"}, | 32 {"build-link", "build-link.json"}, |
| 30 {"build-patch-failure", "build-patch-failure.json"}, | 33 {"build-patch-failure", "build-patch-failure.json"}, |
| 31 {"build-pending", "build-pending.json"}, | 34 {"build-pending", "build-pending.json"}, |
| 32 {"build-running", "build-running.json"}, | 35 {"build-running", "build-running.json"}, |
| 33 {"build-timeout", "build-timeout.json"}, | 36 {"build-timeout", "build-timeout.json"}, |
| 34 {"build-unicode", "build-unicode.json"}, | 37 {"build-unicode", "build-unicode.json"}, |
| 35 {"build-nested", "build-nested.json"}, | 38 {"build-nested", "build-nested.json"}, |
| 36 } | 39 } |
| 37 | 40 |
| 38 func getTestCases() []string { | 41 func getTestCases() []string { |
| 42 // References "milo/appengine/swarming/testdata". |
| 39 testdata := filepath.Join("..", "swarming", "testdata") | 43 testdata := filepath.Join("..", "swarming", "testdata") |
| 40 results := []string{} | 44 results := []string{} |
| 41 f, err := ioutil.ReadDir(testdata) | 45 f, err := ioutil.ReadDir(testdata) |
| 42 if err != nil { | 46 if err != nil { |
| 43 panic(err) | 47 panic(err) |
| 44 } | 48 } |
| 45 for _, fi := range f { | 49 for _, fi := range f { |
| 46 if strings.HasSuffix(fi.Name(), ".swarm") { | 50 if strings.HasSuffix(fi.Name(), ".swarm") { |
| 47 results = append(results, fi.Name()[0:len(fi.Name())-6]) | 51 results = append(results, fi.Name()[0:len(fi.Name())-6]) |
| 48 } | 52 } |
| 49 } | 53 } |
| 50 return results | 54 return results |
| 51 } | 55 } |
| 52 | 56 |
| 57 type debugSwarmingService struct{} |
| 58 |
| 59 func (svc debugSwarmingService) getHost() string { return "example.com" } |
| 60 |
| 61 func (svc debugSwarmingService) getContent(taskID, suffix string) ([]byte, error
) { |
| 62 // ../swarming below assumes that |
| 63 // - this code is not executed by tests outside of this dir |
| 64 // - this dir is a sibling of frontend dir |
| 65 logFilename := filepath.Join("..", "swarming", "testdata", taskID) |
| 66 if suffix != "" { |
| 67 logFilename += suffix |
| 68 } |
| 69 return ioutil.ReadFile(logFilename) |
| 70 } |
| 71 |
| 72 func (svc debugSwarmingService) getSwarmingJSON(taskID, suffix string, dst inter
face{}) error { |
| 73 content, err := svc.getContent(taskID, suffix) |
| 74 if err != nil { |
| 75 return err |
| 76 } |
| 77 return json.Unmarshal(content, dst) |
| 78 } |
| 79 |
| 80 func (svc debugSwarmingService) getSwarmingResult(c context.Context, taskID stri
ng) ( |
| 81 *swarming.SwarmingRpcsTaskResult, error) { |
| 82 |
| 83 var sr swarming.SwarmingRpcsTaskResult |
| 84 if err := svc.getSwarmingJSON(taskID, ".swarm", &sr); err != nil { |
| 85 return nil, err |
| 86 } |
| 87 return &sr, nil |
| 88 } |
| 89 |
| 90 func (svc debugSwarmingService) getTaskOutput(c context.Context, taskID string)
(string, error) { |
| 91 content, err := svc.getContent(taskID, "") |
| 92 if err != nil { |
| 93 if os.IsNotExist(err) { |
| 94 err = nil |
| 95 } |
| 96 return "", err |
| 97 } |
| 98 return string(content), nil |
| 99 } |
| 100 |
| 53 // TestableLog is a subclass of Log that interfaces with TestableHandler and | 101 // TestableLog is a subclass of Log that interfaces with TestableHandler and |
| 54 // includes sample test data. | 102 // includes sample test data. |
| 55 type TestableLog struct{ Log } | 103 type TestableLog struct{ Log } |
| 56 | 104 |
| 57 // TestableBuild is a subclass of Build that interfaces with TestableHandler and | 105 // TestableBuild is a subclass of Build that interfaces with TestableHandler and |
| 58 // includes sample test data. | 106 // includes sample test data. |
| 59 type TestableBuild struct{ Build } | 107 type TestableBuild struct{ Build } |
| 60 | 108 |
| 61 // TestData returns sample test data. | 109 // TestData returns sample test data. |
| 62 func (l TestableLog) TestData() []settings.TestBundle { | 110 func (l TestableLog) TestData() []settings.TestBundle { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 83 }, | 131 }, |
| 84 } | 132 } |
| 85 results := []settings.TestBundle{ | 133 results := []settings.TestBundle{ |
| 86 { | 134 { |
| 87 Description: "Basic successful build", | 135 Description: "Basic successful build", |
| 88 Data: templates.Args{"Build": basic}, | 136 Data: templates.Args{"Build": basic}, |
| 89 }, | 137 }, |
| 90 } | 138 } |
| 91 c := context.Background() | 139 c := context.Background() |
| 92 c, _ = testclock.UseTime(c, time.Date(2016, time.March, 14, 11, 0, 0, 0,
time.UTC)) | 140 c, _ = testclock.UseTime(c, time.Date(2016, time.March, 14, 11, 0, 0, 0,
time.UTC)) |
| 141 |
| 142 var svc debugSwarmingService |
| 93 for _, tc := range getTestCases() { | 143 for _, tc := range getTestCases() { |
| 94 » » build, err := swarmingBuildImpl(c, "foo", "debug", tc) | 144 » » build, err := swarmingBuildImpl(c, svc, "foo", tc) |
| 95 if err != nil { | 145 if err != nil { |
| 96 panic(fmt.Errorf("Error while processing %s: %s", tc, er
r)) | 146 panic(fmt.Errorf("Error while processing %s: %s", tc, er
r)) |
| 97 } | 147 } |
| 98 results = append(results, settings.TestBundle{ | 148 results = append(results, settings.TestBundle{ |
| 99 Description: tc, | 149 Description: tc, |
| 100 Data: templates.Args{"Build": build}, | 150 Data: templates.Args{"Build": build}, |
| 101 }) | 151 }) |
| 102 } | 152 } |
| 103 return results | 153 return results |
| 104 } | 154 } |
| OLD | NEW |