Skip to content

Commit f160a88

Browse files
broadydmitshur
authored andcommitted
[release-branch.go1.11] cmd/godoc: move regression tests to a go test
Run them separately from the other tests in godoc_test by requiring a regtest.host flag and by filtering on the test name. Updates golang/go#28893 Updates golang/go#27205 Change-Id: I166d2278a3f6954307f7c935567a81e73f78e7bb Reviewed-on: https://go-review.googlesource.com/c/139238 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-on: https://go-review.googlesource.com/c/150680
1 parent 934cdca commit f160a88

File tree

3 files changed

+155
-113
lines changed

3 files changed

+155
-113
lines changed

cmd/godoc/Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ GCP_PROJECT := golang-org
1616
DOCKER_TAG := gcr.io/$(GCP_PROJECT)/godoc:$(DOCKER_VERSION)
1717

1818
usage:
19-
echo "See Makefile and README.godoc-app"
20-
exit 1
19+
@echo "See Makefile and README.godoc-app"
20+
@exit 1
2121

2222
docker: Dockerfile.prod
2323
# NOTE(cbro): move up in directory to include entire tools repo.
@@ -48,4 +48,6 @@ get-latest-url:
4848
--limit 1 | cut -f1 # NOTE(cbro): gcloud prints out createTime as the second field.
4949

5050
regtest:
51-
./regtest.bash $(shell make get-latest-url)
51+
go test -v \
52+
-regtest.host=$(shell make get-latest-url) \
53+
-run=Live

cmd/godoc/regtest.bash

Lines changed: 0 additions & 110 deletions
This file was deleted.

cmd/godoc/regtest_test.go

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Regression tests to run against a production instance of godoc.
6+
7+
package main_test
8+
9+
import (
10+
"bytes"
11+
"flag"
12+
"io"
13+
"io/ioutil"
14+
"net/http"
15+
"net/url"
16+
"regexp"
17+
"strings"
18+
"testing"
19+
)
20+
21+
var host = flag.String("regtest.host", "", "host to run regression test against")
22+
23+
func init() {
24+
flag.Parse()
25+
*host = strings.TrimSuffix(*host, "/")
26+
}
27+
28+
func TestLiveServer(t *testing.T) {
29+
if *host == "" {
30+
t.Skip("regtest.host flag missing.")
31+
}
32+
substringTests := []struct {
33+
Message string
34+
Path string
35+
Substring string
36+
Regexp string
37+
NoAnalytics bool // expect the response to not contain GA.
38+
PostBody string
39+
}{
40+
{
41+
Path: "/doc/faq",
42+
Substring: "What is the purpose of the project",
43+
},
44+
{
45+
Path: "/pkg/",
46+
Substring: "Package tar",
47+
},
48+
{
49+
Path: "/pkg/os/",
50+
Substring: "func Open",
51+
},
52+
{
53+
Path: "/robots.txt",
54+
Substring: "Disallow: /search",
55+
Message: "robots not present - not deployed from Dockerfile?",
56+
NoAnalytics: true,
57+
},
58+
{
59+
Path: "/change/75944e2e3a63",
60+
Substring: "bdb10cf",
61+
Message: "no change redirect - hg to git mapping not registered?",
62+
NoAnalytics: true,
63+
},
64+
{
65+
Path: "/dl/",
66+
Substring: "go1.11.windows-amd64.msi",
67+
Message: "missing data on dl page - misconfiguration of datastore?",
68+
},
69+
{
70+
Path: "/dl/?mode=json",
71+
Substring: ".windows-amd64.msi",
72+
NoAnalytics: true,
73+
},
74+
{
75+
Message: "broken shortlinks - misconfiguration of datastore or memcache?",
76+
Path: "/s/go2design",
77+
Regexp: "proposal.*Found",
78+
NoAnalytics: true,
79+
},
80+
{
81+
Message: "incorrect search result - broken index?",
82+
Path: "/search?q=IsDir",
83+
Substring: "src/os/types.go",
84+
},
85+
{
86+
Path: "/compile",
87+
PostBody: "body=" + url.QueryEscape("package main; func main() { print(6*7); }"),
88+
Regexp: `^{"compile_errors":"","output":"42"}$`,
89+
NoAnalytics: true,
90+
},
91+
{
92+
Path: "/compile",
93+
PostBody: "body=" + url.QueryEscape("//empty"),
94+
Substring: "expected 'package', found 'EOF'",
95+
NoAnalytics: true,
96+
},
97+
{
98+
Path: "/compile",
99+
PostBody: "version=2&body=package+main%3Bimport+(%22fmt%22%3B%22time%22)%3Bfunc+main()%7Bfmt.Print(%22A%22)%3Btime.Sleep(time.Second)%3Bfmt.Print(%22B%22)%7D",
100+
Regexp: `^{"Errors":"","Events":\[{"Message":"A","Kind":"stdout","Delay":0},{"Message":"B","Kind":"stdout","Delay":1000000000}\]}$`,
101+
NoAnalytics: true,
102+
},
103+
}
104+
105+
for _, tc := range substringTests {
106+
t.Run(tc.Path, func(t *testing.T) {
107+
method := "GET"
108+
var reqBody io.Reader
109+
if tc.PostBody != "" {
110+
method = "POST"
111+
reqBody = strings.NewReader(tc.PostBody)
112+
}
113+
req, err := http.NewRequest(method, *host+tc.Path, reqBody)
114+
if err != nil {
115+
t.Fatalf("NewRequest: %v", err)
116+
}
117+
if reqBody != nil {
118+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
119+
}
120+
resp, err := http.DefaultTransport.RoundTrip(req)
121+
if err != nil {
122+
t.Fatalf("RoundTrip: %v", err)
123+
}
124+
body, err := ioutil.ReadAll(resp.Body)
125+
if err != nil {
126+
t.Fatalf("ReadAll: %v", err)
127+
}
128+
129+
const googleAnalyticsID = "UA-11222381-2" // golang.org analytics ID
130+
if !tc.NoAnalytics && !bytes.Contains(body, []byte(googleAnalyticsID)) {
131+
t.Errorf("want response to contain analytics tracking ID")
132+
}
133+
134+
if tc.Substring != "" {
135+
tc.Regexp = regexp.QuoteMeta(tc.Substring)
136+
}
137+
re := regexp.MustCompile(tc.Regexp)
138+
139+
if !re.Match(body) {
140+
t.Log("------ actual output -------")
141+
t.Log(string(body))
142+
t.Log("----------------------------")
143+
if tc.Message != "" {
144+
t.Log(tc.Message)
145+
}
146+
t.Fatalf("wanted response to match %s", tc.Regexp)
147+
}
148+
})
149+
}
150+
}

0 commit comments

Comments
 (0)