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