Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 1 | // Copyright 2019 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 | |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 5 | // +build ignore |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 6 | |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 7 | package main |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 8 | |
| 9 | import ( |
| 10 | "archive/tar" |
| 11 | "bytes" |
| 12 | "compress/gzip" |
| 13 | "flag" |
| 14 | "fmt" |
| 15 | "io" |
| 16 | "io/ioutil" |
| 17 | "net/http" |
| 18 | "os" |
| 19 | "os/exec" |
| 20 | "path/filepath" |
| 21 | "regexp" |
| 22 | "runtime" |
| 23 | "strings" |
| 24 | "testing" |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 25 | "time" |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | var ( |
| 29 | regenerate = flag.Bool("regenerate", false, "regenerate files") |
| 30 | |
Joe Tsai | 9d19e5c | 2019-04-03 15:44:11 -0700 | [diff] [blame^] | 31 | protobufVersion = "3.7.1" |
| 32 | golangVersions = []string{"1.9.7", "1.10.8", "1.11.6", "1.12.1"} |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 33 | golangLatest = golangVersions[len(golangVersions)-1] |
| 34 | |
Joe Tsai | 9e88bc0 | 2019-03-12 01:30:40 -0700 | [diff] [blame] | 35 | // purgeTimeout determines the maximum age of unused sub-directories. |
| 36 | purgeTimeout = 30 * 24 * time.Hour // 1 month |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 37 | |
| 38 | // Variables initialized by mustInitDeps. |
| 39 | goPath string |
| 40 | modulePath string |
| 41 | ) |
| 42 | |
| 43 | func Test(t *testing.T) { |
| 44 | mustInitDeps(t) |
| 45 | |
| 46 | if *regenerate { |
| 47 | t.Run("Generate", func(t *testing.T) { |
| 48 | fmt.Print(mustRunCommand(t, ".", "go", "run", "./internal/cmd/generate-types", "-execute")) |
| 49 | fmt.Print(mustRunCommand(t, ".", "go", "run", "./internal/cmd/generate-protos", "-execute")) |
| 50 | files := strings.Split(strings.TrimSpace(mustRunCommand(t, ".", "git", "ls-files", "*.go")), "\n") |
| 51 | mustRunCommand(t, ".", append([]string{"gofmt", "-w"}, files...)...) |
| 52 | }) |
| 53 | t.SkipNow() |
| 54 | } |
| 55 | |
| 56 | for _, v := range golangVersions { |
| 57 | t.Run("Go"+v, func(t *testing.T) { |
Joe Tsai | d56458e | 2019-03-01 18:11:42 -0800 | [diff] [blame] | 58 | runGo := func(label, workDir string, args ...string) { |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 59 | args[0] += v |
| 60 | t.Run(label, func(t *testing.T) { |
| 61 | t.Parallel() |
Joe Tsai | d56458e | 2019-03-01 18:11:42 -0800 | [diff] [blame] | 62 | mustRunCommand(t, workDir, args...) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 63 | }) |
| 64 | } |
Joe Tsai | d56458e | 2019-03-01 18:11:42 -0800 | [diff] [blame] | 65 | workDir := filepath.Join(goPath, "src", modulePath) |
| 66 | runGo("Build", workDir, "go", "build", "./...") |
| 67 | runGo("TestNormal", workDir, "go", "test", "-race", "./...") |
| 68 | runGo("TestPureGo", workDir, "go", "test", "-race", "-tags", "purego", "./...") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 69 | if v == golangLatest { |
Joe Tsai | d56458e | 2019-03-01 18:11:42 -0800 | [diff] [blame] | 70 | runGo("TestProto1Legacy", workDir, "go", "test", "-race", "-tags", "proto1_legacy", "./...") |
| 71 | runGo("TestProtocGenGo", "cmd/protoc-gen-go/testdata", "go", "test") |
| 72 | runGo("TestProtocGenGoGRPC", "cmd/protoc-gen-go-grpc/testdata", "go", "test") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 73 | } |
| 74 | }) |
| 75 | } |
| 76 | |
| 77 | t.Run("GeneratedGoFiles", func(t *testing.T) { |
| 78 | diff := mustRunCommand(t, ".", "go", "run", "./internal/cmd/generate-types") |
| 79 | if strings.TrimSpace(diff) != "" { |
| 80 | t.Fatalf("stale generated files:\n%v", diff) |
| 81 | } |
| 82 | diff = mustRunCommand(t, ".", "go", "run", "./internal/cmd/generate-protos") |
| 83 | if strings.TrimSpace(diff) != "" { |
| 84 | t.Fatalf("stale generated files:\n%v", diff) |
| 85 | } |
| 86 | }) |
| 87 | t.Run("FormattedGoFiles", func(t *testing.T) { |
| 88 | files := strings.Split(strings.TrimSpace(mustRunCommand(t, ".", "git", "ls-files", "*.go")), "\n") |
| 89 | diff := mustRunCommand(t, ".", append([]string{"gofmt", "-d"}, files...)...) |
| 90 | if strings.TrimSpace(diff) != "" { |
| 91 | t.Fatalf("unformatted source files:\n%v", diff) |
| 92 | } |
| 93 | }) |
| 94 | t.Run("CommittedGitChanges", func(t *testing.T) { |
| 95 | diff := mustRunCommand(t, ".", "git", "diff", "--no-prefix", "HEAD") |
| 96 | if strings.TrimSpace(diff) != "" { |
| 97 | t.Fatalf("uncommitted changes:\n%v", diff) |
| 98 | } |
| 99 | }) |
| 100 | t.Run("TrackedGitFiles", func(t *testing.T) { |
| 101 | diff := mustRunCommand(t, ".", "git", "ls-files", "--others", "--exclude-standard") |
| 102 | if strings.TrimSpace(diff) != "" { |
| 103 | t.Fatalf("untracked files:\n%v", diff) |
| 104 | } |
| 105 | }) |
| 106 | } |
| 107 | |
| 108 | func mustInitDeps(t *testing.T) { |
| 109 | check := func(err error) { |
| 110 | t.Helper() |
| 111 | if err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Determine the directory to place the test directory. |
| 117 | repoRoot, err := os.Getwd() |
| 118 | check(err) |
| 119 | testDir := filepath.Join(repoRoot, ".cache") |
| 120 | check(os.MkdirAll(testDir, 0775)) |
| 121 | |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 122 | // Travis-CI has a hard-coded timeout where it kills the test after |
| 123 | // 10 minutes of a lack of activity on stdout. |
| 124 | // We work around this restriction by periodically printing the timestamp. |
| 125 | ticker := time.NewTicker(5 * time.Minute) |
| 126 | done := make(chan struct{}) |
| 127 | go func() { |
| 128 | now := time.Now() |
| 129 | for { |
| 130 | select { |
| 131 | case t := <-ticker.C: |
| 132 | fmt.Printf("\tt=%0.fmin\n", t.Sub(now).Minutes()) |
| 133 | case <-done: |
| 134 | return |
| 135 | } |
| 136 | } |
| 137 | }() |
| 138 | defer close(done) |
| 139 | defer ticker.Stop() |
| 140 | |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 141 | // Delete the current directory if non-empty, |
| 142 | // which only occurs if a dependency failed to initialize properly. |
| 143 | var workingDir string |
| 144 | defer func() { |
| 145 | if workingDir != "" { |
| 146 | os.RemoveAll(workingDir) // best-effort |
| 147 | } |
| 148 | }() |
| 149 | |
| 150 | // Delete other sub-directories that are no longer relevant. |
| 151 | defer func() { |
| 152 | subDirs := map[string]bool{"bin": true, "gocache": true, "gopath": true} |
| 153 | subDirs["protobuf-"+protobufVersion] = true |
| 154 | for _, v := range golangVersions { |
| 155 | subDirs["go"+v] = true |
| 156 | } |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 157 | |
Joe Tsai | 9e88bc0 | 2019-03-12 01:30:40 -0700 | [diff] [blame] | 158 | now := time.Now() |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 159 | fis, _ := ioutil.ReadDir(testDir) |
| 160 | for _, fi := range fis { |
Joe Tsai | 9e88bc0 | 2019-03-12 01:30:40 -0700 | [diff] [blame] | 161 | if subDirs[fi.Name()] { |
| 162 | os.Chtimes(filepath.Join(testDir, fi.Name()), now, now) // best-effort |
| 163 | continue |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 164 | } |
Joe Tsai | 9e88bc0 | 2019-03-12 01:30:40 -0700 | [diff] [blame] | 165 | if now.Sub(fi.ModTime()) < purgeTimeout { |
| 166 | continue |
| 167 | } |
| 168 | fmt.Printf("delete %v\n", fi.Name()) |
| 169 | os.RemoveAll(filepath.Join(testDir, fi.Name())) // best-effort |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 170 | } |
| 171 | }() |
| 172 | |
| 173 | // The bin directory contains symlinks to each tool by version. |
| 174 | // It is safe to delete this directory and run the test script from scratch. |
| 175 | binPath := filepath.Join(testDir, "bin") |
| 176 | check(os.RemoveAll(binPath)) |
| 177 | check(os.Mkdir(binPath, 0775)) |
| 178 | check(os.Setenv("PATH", binPath+":"+os.Getenv("PATH"))) |
| 179 | registerBinary := func(name, path string) { |
| 180 | check(os.Symlink(path, filepath.Join(binPath, name))) |
| 181 | } |
| 182 | |
| 183 | // Download and build the protobuf toolchain. |
| 184 | // We avoid downloading the pre-compiled binaries since they do not contain |
| 185 | // the conformance test runner. |
| 186 | workingDir = filepath.Join(testDir, "protobuf-"+protobufVersion) |
| 187 | if _, err := os.Stat(workingDir); err != nil { |
| 188 | fmt.Printf("download %v\n", filepath.Base(workingDir)) |
| 189 | url := fmt.Sprintf("https://github.com/google/protobuf/releases/download/v%v/protobuf-all-%v.tar.gz", protobufVersion, protobufVersion) |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 190 | downloadArchive(check, workingDir, url, "protobuf-"+protobufVersion) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 191 | |
| 192 | fmt.Printf("build %v\n", filepath.Base(workingDir)) |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 193 | mustRunCommand(t, workingDir, "./autogen.sh") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 194 | mustRunCommand(t, workingDir, "./configure") |
| 195 | mustRunCommand(t, workingDir, "make") |
| 196 | mustRunCommand(t, filepath.Join(workingDir, "conformance"), "make") |
| 197 | } |
| 198 | patchProtos(check, workingDir) |
| 199 | check(os.Setenv("PROTOBUF_ROOT", workingDir)) // for generate-protos |
| 200 | registerBinary("conform-test-runner", filepath.Join(workingDir, "conformance", "conformance-test-runner")) |
| 201 | registerBinary("protoc", filepath.Join(workingDir, "src", "protoc")) |
| 202 | workingDir = "" |
| 203 | |
| 204 | // Download each Go toolchain version. |
| 205 | for _, v := range golangVersions { |
| 206 | workingDir = filepath.Join(testDir, "go"+v) |
| 207 | if _, err := os.Stat(workingDir); err != nil { |
| 208 | fmt.Printf("download %v\n", filepath.Base(workingDir)) |
| 209 | url := fmt.Sprintf("https://dl.google.com/go/go%v.%v-%v.tar.gz", v, runtime.GOOS, runtime.GOARCH) |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 210 | downloadArchive(check, workingDir, url, "go") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 211 | } |
| 212 | registerBinary("go"+v, filepath.Join(workingDir, "bin", "go")) |
| 213 | } |
| 214 | registerBinary("go", filepath.Join(testDir, "go"+golangLatest, "bin", "go")) |
| 215 | registerBinary("gofmt", filepath.Join(testDir, "go"+golangLatest, "bin", "gofmt")) |
| 216 | workingDir = "" |
| 217 | |
| 218 | // Travis-CI sets GOROOT, which confuses invocations of the Go toolchain. |
| 219 | // Explicitly clear GOROOT, so each toolchain uses their default GOROOT. |
| 220 | check(os.Unsetenv("GOROOT")) |
| 221 | |
| 222 | // Set a cache directory within the test directory. |
| 223 | check(os.Setenv("GOCACHE", filepath.Join(testDir, "gocache"))) |
| 224 | |
| 225 | // Setup GOPATH for pre-module support (i.e., go1.10 and earlier). |
| 226 | goPath = filepath.Join(testDir, "gopath") |
| 227 | modulePath = strings.TrimSpace(mustRunCommand(t, testDir, "go", "list", "-m", "-f", "{{.Path}}")) |
| 228 | check(os.RemoveAll(filepath.Join(goPath, "src"))) |
| 229 | check(os.MkdirAll(filepath.Join(goPath, "src", filepath.Dir(modulePath)), 0775)) |
| 230 | check(os.Symlink(repoRoot, filepath.Join(goPath, "src", modulePath))) |
| 231 | mustRunCommand(t, repoRoot, "go", "mod", "tidy") |
| 232 | mustRunCommand(t, repoRoot, "go", "mod", "vendor") |
| 233 | check(os.Setenv("GOPATH", goPath)) |
| 234 | } |
| 235 | |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 236 | func downloadArchive(check func(error), dstPath, srcURL, skipPrefix string) { |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 237 | check(os.RemoveAll(dstPath)) |
| 238 | |
| 239 | resp, err := http.Get(srcURL) |
| 240 | check(err) |
| 241 | defer resp.Body.Close() |
| 242 | |
| 243 | zr, err := gzip.NewReader(resp.Body) |
| 244 | check(err) |
| 245 | |
| 246 | tr := tar.NewReader(zr) |
| 247 | for { |
| 248 | h, err := tr.Next() |
| 249 | if err == io.EOF { |
| 250 | return |
| 251 | } |
| 252 | check(err) |
| 253 | |
Joe Tsai | f398784 | 2019-03-02 13:35:17 -0800 | [diff] [blame] | 254 | // Skip directories or files outside the prefix directory. |
| 255 | if len(skipPrefix) > 0 { |
| 256 | if !strings.HasPrefix(h.Name, skipPrefix) { |
| 257 | continue |
| 258 | } |
| 259 | if len(h.Name) > len(skipPrefix) && h.Name[len(skipPrefix)] != '/' { |
| 260 | continue |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | path := strings.TrimPrefix(strings.TrimPrefix(h.Name, skipPrefix), "/") |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 265 | path = filepath.Join(dstPath, filepath.FromSlash(path)) |
| 266 | mode := os.FileMode(h.Mode & 0777) |
| 267 | switch h.Typeflag { |
| 268 | case tar.TypeReg: |
| 269 | b, err := ioutil.ReadAll(tr) |
| 270 | check(err) |
| 271 | check(ioutil.WriteFile(path, b, mode)) |
| 272 | case tar.TypeDir: |
| 273 | check(os.Mkdir(path, mode)) |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // patchProtos patches proto files with v2 locations of Go packages. |
| 279 | // TODO: Commit these changes upstream. |
| 280 | func patchProtos(check func(error), repoRoot string) { |
| 281 | javaPackageRx := regexp.MustCompile(`^option\s+java_package\s*=\s*".*"\s*;\s*$`) |
| 282 | goPackageRx := regexp.MustCompile(`^option\s+go_package\s*=\s*".*"\s*;\s*$`) |
| 283 | files := map[string]string{ |
| 284 | "src/google/protobuf/any.proto": "github.com/golang/protobuf/v2/types/known;known_proto", |
| 285 | "src/google/protobuf/api.proto": "github.com/golang/protobuf/v2/types/known;known_proto", |
| 286 | "src/google/protobuf/duration.proto": "github.com/golang/protobuf/v2/types/known;known_proto", |
| 287 | "src/google/protobuf/empty.proto": "github.com/golang/protobuf/v2/types/known;known_proto", |
| 288 | "src/google/protobuf/field_mask.proto": "github.com/golang/protobuf/v2/types/known;known_proto", |
| 289 | "src/google/protobuf/source_context.proto": "github.com/golang/protobuf/v2/types/known;known_proto", |
| 290 | "src/google/protobuf/struct.proto": "github.com/golang/protobuf/v2/types/known;known_proto", |
| 291 | "src/google/protobuf/timestamp.proto": "github.com/golang/protobuf/v2/types/known;known_proto", |
| 292 | "src/google/protobuf/type.proto": "github.com/golang/protobuf/v2/types/known;known_proto", |
| 293 | "src/google/protobuf/wrappers.proto": "github.com/golang/protobuf/v2/types/known;known_proto", |
| 294 | "src/google/protobuf/descriptor.proto": "github.com/golang/protobuf/v2/types/descriptor;descriptor_proto", |
| 295 | "src/google/protobuf/compiler/plugin.proto": "github.com/golang/protobuf/v2/types/plugin;plugin_proto", |
| 296 | "conformance/conformance.proto": "github.com/golang/protobuf/v2/internal/testprotos/conformance;conformance_proto", |
| 297 | } |
| 298 | for pbpath, gopath := range files { |
| 299 | b, err := ioutil.ReadFile(filepath.Join(repoRoot, pbpath)) |
| 300 | check(err) |
| 301 | ss := strings.Split(string(b), "\n") |
| 302 | |
| 303 | // Locate java_package and (possible) go_package options. |
| 304 | javaPackageIdx, goPackageIdx := -1, -1 |
| 305 | for i, s := range ss { |
| 306 | if javaPackageIdx < 0 && javaPackageRx.MatchString(s) { |
| 307 | javaPackageIdx = i |
| 308 | } |
| 309 | if goPackageIdx < 0 && goPackageRx.MatchString(s) { |
| 310 | goPackageIdx = i |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // Ensure the proto file has the correct go_package option. |
| 315 | opt := `option go_package = "` + gopath + `";` |
| 316 | if goPackageIdx >= 0 { |
| 317 | if ss[goPackageIdx] == opt { |
| 318 | continue // no changes needed |
| 319 | } |
| 320 | ss[goPackageIdx] = opt |
| 321 | } else { |
| 322 | // Insert go_package option before java_package option. |
| 323 | ss = append(ss[:javaPackageIdx], append([]string{opt}, ss[javaPackageIdx:]...)...) |
| 324 | } |
| 325 | |
| 326 | fmt.Println("patch " + pbpath) |
| 327 | b = []byte(strings.Join(ss, "\n")) |
| 328 | check(ioutil.WriteFile(filepath.Join(repoRoot, pbpath), b, 0664)) |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | func mustRunCommand(t *testing.T, dir string, args ...string) string { |
| 333 | t.Helper() |
| 334 | stdout := new(bytes.Buffer) |
Herbie Ong | 4630b3d | 2019-03-19 16:42:01 -0700 | [diff] [blame] | 335 | stderr := new(bytes.Buffer) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 336 | cmd := exec.Command(args[0], args[1:]...) |
| 337 | cmd.Dir = dir |
| 338 | cmd.Env = append(os.Environ(), "PWD="+dir) |
Herbie Ong | 4630b3d | 2019-03-19 16:42:01 -0700 | [diff] [blame] | 339 | cmd.Stdout = stdout |
| 340 | cmd.Stderr = stderr |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 341 | if err := cmd.Run(); err != nil { |
Herbie Ong | 4630b3d | 2019-03-19 16:42:01 -0700 | [diff] [blame] | 342 | t.Fatalf("executing (%v): %v\n%s%s", strings.Join(args, " "), err, stdout.String(), stderr.String()) |
Joe Tsai | 707894e | 2019-03-01 12:50:52 -0800 | [diff] [blame] | 343 | } |
| 344 | return stdout.String() |
| 345 | } |