| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The LUCI Authors. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 | |
| 15 package frontend | |
| 16 | |
| 17 import ( | |
| 18 "flag" | |
| 19 "fmt" | |
| 20 "io/ioutil" | |
| 21 "net/http" | |
| 22 "net/url" | |
| 23 "path/filepath" | |
| 24 "regexp" | |
| 25 "strings" | |
| 26 "testing" | |
| 27 "time" | |
| 28 | |
| 29 "golang.org/x/net/context" | |
| 30 | |
| 31 "github.com/luci/gae/impl/memory" | |
| 32 "github.com/luci/luci-go/common/clock/testclock" | |
| 33 "github.com/luci/luci-go/milo/buildsource/swarming" | |
| 34 "github.com/luci/luci-go/milo/common" | |
| 35 "github.com/luci/luci-go/server/auth" | |
| 36 "github.com/luci/luci-go/server/auth/authtest" | |
| 37 "github.com/luci/luci-go/server/auth/identity" | |
| 38 "github.com/luci/luci-go/server/settings" | |
| 39 "github.com/luci/luci-go/server/templates" | |
| 40 | |
| 41 . "github.com/smartystreets/goconvey/convey" | |
| 42 ) | |
| 43 | |
| 44 type testPackage struct { | |
| 45 Data func() []common.TestBundle | |
| 46 DisplayName string | |
| 47 TemplateName string | |
| 48 } | |
| 49 | |
| 50 var ( | |
| 51 allPackages = []testPackage{ | |
| 52 {buildbotBuildTestData, "buildbot.build", "build.html"}, | |
| 53 {buildbotBuilderTestData, "buildbot.builder", "builder.html"}, | |
| 54 {func() []common.TestBundle { | |
| 55 return swarming.BuildTestData("../buildsource/swarming") | |
| 56 }, "swarming.build", "build.html"}, | |
| 57 {swarming.LogTestData, "swarming.log", "log.html"}, | |
| 58 {frontpageTestData, "frontpage", "frontpage.html"}, | |
| 59 } | |
| 60 ) | |
| 61 | |
| 62 var generate = flag.Bool( | |
| 63 "test.generate", false, "Generate expectations instead of running tests.
") | |
| 64 | |
| 65 func expectFileName(name string) string { | |
| 66 name = strings.Replace(name, " ", "_", -1) | |
| 67 name = strings.Replace(name, "/", "_", -1) | |
| 68 name = strings.Replace(name, ":", "-", -1) | |
| 69 return filepath.Join("expectations", name) | |
| 70 } | |
| 71 | |
| 72 func load(name string) ([]byte, error) { | |
| 73 filename := expectFileName(name) | |
| 74 return ioutil.ReadFile(filename) | |
| 75 } | |
| 76 | |
| 77 // mustWrite Writes a buffer into an expectation file. Should always work or | |
| 78 // panic. This is fine because this only runs when -generate is passed in, | |
| 79 // not during tests. | |
| 80 func mustWrite(name string, buf []byte) { | |
| 81 filename := expectFileName(name) | |
| 82 err := ioutil.WriteFile(filename, buf, 0644) | |
| 83 if err != nil { | |
| 84 panic(err) | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 type analyticsSettings struct { | |
| 89 AnalyticsID string `json:"analytics_id"` | |
| 90 } | |
| 91 | |
| 92 func TestPages(t *testing.T) { | |
| 93 fixZeroDurationRE := regexp.MustCompile(`(Running for:|waiting) 0s?`) | |
| 94 fixZeroDuration := func(text string) string { | |
| 95 return fixZeroDurationRE.ReplaceAllLiteralString(text, "[ZERO DU
RATION]") | |
| 96 } | |
| 97 | |
| 98 Convey("Testing basic rendering.", t, func() { | |
| 99 c := context.Background() | |
| 100 c = memory.Use(c) | |
| 101 c = common.WithRequest(c, &http.Request{URL: &url.URL{Path: "/fo
obar"}}) | |
| 102 c, _ = testclock.UseTime(c, testclock.TestTimeUTC) | |
| 103 c = auth.WithState(c, &authtest.FakeState{Identity: identity.Ano
nymousIdentity}) | |
| 104 c = settings.Use(c, settings.New(&settings.MemoryStorage{Expirat
ion: time.Second})) | |
| 105 err := settings.Set(c, "analytics", &analyticsSettings{"UA-12345
-01"}, "", "") | |
| 106 So(err, ShouldBeNil) | |
| 107 c = templates.Use(c, common.GetTemplateBundle("appengine/templat
es")) | |
| 108 for _, p := range allPackages { | |
| 109 Convey(fmt.Sprintf("Testing handler %q", p.DisplayName),
func() { | |
| 110 for _, b := range p.Data() { | |
| 111 Convey(fmt.Sprintf("Testing: %q", b.Desc
ription), func() { | |
| 112 args := b.Data | |
| 113 // This is not a path, but a fil
e key, should always be "/". | |
| 114 tmplName := "pages/" + p.Templat
eName | |
| 115 buf, err := templates.Render(c,
tmplName, args) | |
| 116 So(err, ShouldBeNil) | |
| 117 fname := fmt.Sprintf( | |
| 118 "%s-%s.html", p.DisplayN
ame, b.Description) | |
| 119 if *generate { | |
| 120 mustWrite(fname, buf) | |
| 121 } else { | |
| 122 localBuf, err := load(fn
ame) | |
| 123 So(err, ShouldBeNil) | |
| 124 So(fixZeroDuration(strin
g(buf)), ShouldEqual, fixZeroDuration(string(localBuf))) | |
| 125 } | |
| 126 }) | |
| 127 } | |
| 128 }) | |
| 129 } | |
| 130 }) | |
| 131 } | |
| OLD | NEW |