2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-18 02:55:18 +00:00
restic/run_integration_tests.go

610 lines
14 KiB
Go
Raw Normal View History

// +build ignore
package main
import (
"bufio"
2015-12-20 17:09:35 +00:00
"bytes"
"encoding/base64"
"errors"
2016-01-17 17:33:03 +00:00
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
)
// ForbiddenImports are the packages from the stdlib that should not be used in
// our code.
var ForbiddenImports = map[string]bool{
"errors": true,
}
// Use a specific version of gofmt (the latest stable, usually) to guarantee
// deterministic formatting. This is used with the GoVersion.AtLeast()
2019-11-20 20:13:56 +00:00
// function (so that we don't forget to update it). This is also used to run
2020-03-01 10:30:12 +00:00
// `go mod tidy`.
var GofmtVersion = ParseGoVersion("go1.14")
// GoVersion is the version of Go used to compile the project.
type GoVersion struct {
Major int
Minor int
Patch int
}
// ParseGoVersion parses the Go version s. If s cannot be parsed, the returned GoVersion is null.
func ParseGoVersion(s string) (v GoVersion) {
if !strings.HasPrefix(s, "go") {
return
}
s = s[2:]
data := strings.Split(s, ".")
if len(data) < 2 || len(data) > 3 {
// invalid version
return GoVersion{}
}
var err error
v.Major, err = strconv.Atoi(data[0])
if err != nil {
return GoVersion{}
}
// try to parse the minor version while removing an eventual suffix (like
// "rc2" or so)
for s := data[1]; s != ""; s = s[:len(s)-1] {
v.Minor, err = strconv.Atoi(s)
if err == nil {
break
}
}
if v.Minor == 0 {
// no minor version found
return GoVersion{}
}
if len(data) >= 3 {
v.Patch, err = strconv.Atoi(data[2])
if err != nil {
return GoVersion{}
}
}
return
}
// AtLeast returns true if v is at least as new as other. If v is empty, true is returned.
func (v GoVersion) AtLeast(other GoVersion) bool {
var empty GoVersion
// the empty version satisfies all versions
if v == empty {
return true
}
if v.Major < other.Major {
return false
}
if v.Minor < other.Minor {
return false
}
if v.Patch < other.Patch {
return false
}
return true
}
func (v GoVersion) String() string {
return fmt.Sprintf("Go %d.%d.%d", v.Major, v.Minor, v.Patch)
}
// CloudBackends contains a map of backend tests for cloud services to one
// of the essential environment variables which must be present in order to
// test it.
var CloudBackends = map[string]string{
"restic/backend/s3.TestBackendS3": "RESTIC_TEST_S3_REPOSITORY",
"restic/backend/swift.TestBackendSwift": "RESTIC_TEST_SWIFT",
"restic/backend/b2.TestBackendB2": "RESTIC_TEST_B2_REPOSITORY",
"restic/backend/gs.TestBackendGS": "RESTIC_TEST_GS_REPOSITORY",
"restic/backend/azure.TestBackendAzure": "RESTIC_TEST_AZURE_REPOSITORY",
}
2016-01-17 17:33:03 +00:00
var runCrossCompile = flag.Bool("cross-compile", true, "run cross compilation tests")
2016-04-17 15:49:36 +00:00
2016-01-17 17:33:03 +00:00
func init() {
flag.Parse()
}
2016-04-18 18:33:45 +00:00
// CIEnvironment is implemented by environments where tests can be run.
type CIEnvironment interface {
2016-04-17 16:22:26 +00:00
Prepare() error
RunTests() error
Teardown() error
}
2016-04-18 18:33:45 +00:00
// TravisEnvironment is the environment in which Travis tests run.
type TravisEnvironment struct {
goxOSArch []string
env map[string]string
gcsCredentialsFile string
}
2016-04-17 16:22:26 +00:00
func (env *TravisEnvironment) getMinio() error {
tempfile, err := os.Create(filepath.Join(os.Getenv("GOPATH"), "bin", "minio"))
if err != nil {
return fmt.Errorf("create tempfile for minio download failed: %v", err)
}
url := fmt.Sprintf("https://dl.minio.io/server/minio/release/%s-%s/minio",
runtime.GOOS, runtime.GOARCH)
msg("downloading %v\n", url)
res, err := http.Get(url)
if err != nil {
return fmt.Errorf("error downloading minio server: %v", err)
}
_, err = io.Copy(tempfile, res.Body)
if err != nil {
return fmt.Errorf("error saving minio server to file: %v", err)
}
err = res.Body.Close()
if err != nil {
return fmt.Errorf("error closing HTTP download: %v", err)
}
err = tempfile.Close()
if err != nil {
msg("closing tempfile failed: %v\n", err)
return fmt.Errorf("error closing minio server file: %v", err)
}
err = os.Chmod(tempfile.Name(), 0755)
if err != nil {
2016-04-17 16:22:26 +00:00
return fmt.Errorf("chmod(minio-server) failed: %v", err)
}
msg("downloaded minio server to %v\n", tempfile.Name())
return nil
}
2016-04-18 18:33:45 +00:00
// Prepare installs dependencies and starts services in order to run the tests.
2016-04-17 16:22:26 +00:00
func (env *TravisEnvironment) Prepare() error {
2016-04-17 15:49:36 +00:00
env.env = make(map[string]string)
2015-08-19 18:29:06 +00:00
msg("preparing environment for Travis CI\n")
pkgs := []string{
"github.com/NebulousLabs/glyphcheck",
2017-06-27 19:19:48 +00:00
"github.com/restic/rest-server/cmd/rest-server",
2017-12-27 20:10:11 +00:00
"github.com/restic/calens",
2019-11-06 03:59:48 +00:00
"github.com/rclone/rclone",
}
for _, pkg := range pkgs {
2016-04-17 16:39:19 +00:00
err := run("go", "get", pkg)
if err != nil {
return err
}
}
2019-11-22 12:17:52 +00:00
// reset changes made to go.mod/go.sum by "go get"
if err := run("git", "checkout", "go.mod", "go.sum"); err != nil {
return err
}
2016-04-17 16:39:19 +00:00
if err := env.getMinio(); err != nil {
return err
}
if *runCrossCompile {
2016-01-17 17:33:03 +00:00
// only test cross compilation on linux with Travis
2016-04-17 16:39:19 +00:00
if err := run("go", "get", "github.com/mitchellh/gox"); err != nil {
return err
}
2019-11-22 12:17:52 +00:00
// reset changes made to go.mod/go.sum by "go get"
if err := run("git", "checkout", "go.mod", "go.sum"); err != nil {
return err
}
2016-01-17 17:33:03 +00:00
if runtime.GOOS == "linux" {
env.goxOSArch = []string{
"linux/386", "linux/amd64",
"windows/386", "windows/amd64",
"darwin/amd64",
"freebsd/386", "freebsd/amd64",
2017-01-12 18:50:41 +00:00
"openbsd/386", "openbsd/amd64",
"netbsd/386", "netbsd/amd64",
"linux/arm", "freebsd/arm",
"linux/ppc64le", "solaris/amd64",
2016-01-17 17:33:03 +00:00
}
} else {
env.goxOSArch = []string{runtime.GOOS + "/" + runtime.GOARCH}
2016-01-17 17:33:03 +00:00
}
msg("gox: OS/ARCH %v\n", env.goxOSArch)
}
2016-04-17 16:22:26 +00:00
// do not run cloud tests on darwin
if os.Getenv("RESTIC_TEST_CLOUD_BACKENDS") == "0" {
msg("skipping cloud backend tests\n")
for _, name := range CloudBackends {
err := os.Unsetenv(name)
if err != nil {
msg(" error unsetting %v: %v\n", name, err)
}
}
}
// extract credentials file for GCS tests
if b64data := os.Getenv("RESTIC_TEST_GS_APPLICATION_CREDENTIALS_B64"); b64data != "" {
buf, err := base64.StdEncoding.DecodeString(b64data)
if err != nil {
return err
}
f, err := ioutil.TempFile("", "gcs-credentials-")
if err != nil {
return err
}
msg("saving GCS credentials to %v\n", f.Name())
_, err = f.Write(buf)
if err != nil {
f.Close()
return err
}
env.gcsCredentialsFile = f.Name()
if err = f.Close(); err != nil {
return err
}
}
2016-04-17 16:22:26 +00:00
return nil
}
2016-04-18 18:33:45 +00:00
// Teardown stops backend services and cleans the environment again.
2016-04-17 16:22:26 +00:00
func (env *TravisEnvironment) Teardown() error {
2016-04-17 15:49:36 +00:00
msg("run travis teardown\n")
if env.gcsCredentialsFile != "" {
msg("remove gcs credentials file %v\n", env.gcsCredentialsFile)
return os.Remove(env.gcsCredentialsFile)
}
2016-04-17 16:22:26 +00:00
return nil
2016-04-17 15:49:36 +00:00
}
2016-04-18 18:33:45 +00:00
// RunTests starts the tests for Travis.
2016-04-17 16:22:26 +00:00
func (env *TravisEnvironment) RunTests() error {
2017-07-21 20:02:50 +00:00
env.env["GOPATH"] = os.Getenv("GOPATH")
if env.gcsCredentialsFile != "" {
env.env["GOOGLE_APPLICATION_CREDENTIALS"] = env.gcsCredentialsFile
}
// ensure that the following tests cannot be silently skipped on Travis
ensureTests := []string{
"restic/backend/rest.TestBackendREST",
"restic/backend/sftp.TestBackendSFTP",
"restic/backend/s3.TestBackendMinio",
"restic/backend/rclone.TestBackendRclone",
}
// make sure that cloud backends for which we have credentials are not
// silently skipped.
for pkg, env := range CloudBackends {
if _, ok := os.LookupEnv(env); ok {
ensureTests = append(ensureTests, pkg)
} else {
msg("credentials for %v are not available, skipping\n", pkg)
}
}
env.env["RESTIC_TEST_DISALLOW_SKIP"] = strings.Join(ensureTests, ",")
if *runCrossCompile {
2016-01-17 17:33:03 +00:00
// compile for all target architectures with tags
2018-10-07 18:40:18 +00:00
for _, tags := range []string{"", "debug"} {
2016-08-21 12:21:19 +00:00
err := runWithEnv(env.env, "gox", "-verbose",
"-osarch", strings.Join(env.goxOSArch, " "),
2016-01-17 17:33:03 +00:00
"-tags", tags,
"-output", "/tmp/{{.Dir}}_{{.OS}}_{{.Arch}}",
2017-07-21 20:02:50 +00:00
"./cmd/restic")
2016-08-21 12:21:19 +00:00
if err != nil {
return err
}
2016-01-17 17:33:03 +00:00
}
}
v := ParseGoVersion(runtime.Version())
msg("Detected Go version %v\n", v)
2020-03-01 10:30:12 +00:00
args := []string{"go", "run", "build.go"}
// run the build script
2018-08-31 18:56:25 +00:00
err := run(args[0], args[1:]...)
if err != nil {
2016-04-17 16:39:19 +00:00
return err
}
2020-03-01 10:30:12 +00:00
// run the tests and gather coverage information
err = runWithEnv(env.env, "go", "test", "-count", "1", "-coverprofile", "all.cov", "./...")
2016-04-17 16:39:19 +00:00
if err != nil {
return err
}
// only run gofmt on a specific version of Go.
if v.AtLeast(GofmtVersion) {
if err = runGofmt(); err != nil {
return err
}
2018-09-09 08:36:27 +00:00
msg("run go mod tidy\n")
if err := runGoModTidy(); err != nil {
return err
}
} else {
2020-07-22 21:35:10 +00:00
msg("Skipping gofmt and mod tidy check for %v\n", v)
}
if err = runGlyphcheck(); err != nil {
return err
}
2017-08-05 09:02:00 +00:00
// check for forbidden imports
2017-08-05 10:00:13 +00:00
deps, err := env.findImports()
if err != nil {
return err
}
foundForbiddenImports := false
for name, imports := range deps {
for _, pkg := range imports {
if _, ok := ForbiddenImports[pkg]; ok {
fmt.Fprintf(os.Stderr, "========== package %v imports forbidden package %v\n", name, pkg)
foundForbiddenImports = true
}
}
}
if foundForbiddenImports {
return errors.New("CI: forbidden imports found")
}
2017-12-27 20:10:11 +00:00
// check that the entries in changelog/ are valid
if err := run("calens"); err != nil {
2018-03-21 19:53:01 +00:00
return errors.New("calens failed, files in changelog/ are not valid")
2017-12-27 20:10:11 +00:00
}
return nil
}
2016-04-18 18:33:45 +00:00
// AppveyorEnvironment is the environment on Windows.
2015-08-19 18:45:54 +00:00
type AppveyorEnvironment struct{}
2016-04-18 18:33:45 +00:00
// Prepare installs dependencies and starts services in order to run the tests.
2016-04-17 16:22:26 +00:00
func (env *AppveyorEnvironment) Prepare() error {
return nil
2015-08-19 18:45:54 +00:00
}
2016-04-18 18:33:45 +00:00
// RunTests start the tests.
2016-04-17 16:22:26 +00:00
func (env *AppveyorEnvironment) RunTests() error {
2020-03-01 10:30:12 +00:00
return runWithEnv(nil, "go", "run", "build.go", "-v", "-T")
2015-08-19 18:45:54 +00:00
}
2016-04-18 18:33:45 +00:00
// Teardown is a noop.
2016-04-17 16:22:26 +00:00
func (env *AppveyorEnvironment) Teardown() error {
return nil
}
2016-04-17 15:49:36 +00:00
2015-08-19 18:29:06 +00:00
func msg(format string, args ...interface{}) {
fmt.Printf("CI: "+format, args...)
}
func updateEnv(env []string, override map[string]string) []string {
var newEnv []string
for _, s := range env {
d := strings.SplitN(s, "=", 2)
key := d[0]
if _, ok := override[key]; ok {
continue
}
newEnv = append(newEnv, s)
}
for k, v := range override {
newEnv = append(newEnv, k+"="+v)
}
return newEnv
}
2017-08-05 10:00:13 +00:00
func (env *TravisEnvironment) findImports() (map[string][]string, error) {
res := make(map[string][]string)
2019-11-23 09:31:40 +00:00
msg("checking for forbidden imports\n")
2017-08-05 10:00:13 +00:00
cmd := exec.Command("go", "list", "-f", `{{.ImportPath}} {{join .Imports " "}}`, "./internal/...", "./cmd/...")
cmd.Stderr = os.Stderr
output, err := cmd.Output()
if err != nil {
return nil, err
}
sc := bufio.NewScanner(bytes.NewReader(output))
for sc.Scan() {
wordScanner := bufio.NewScanner(strings.NewReader(sc.Text()))
wordScanner.Split(bufio.ScanWords)
if !wordScanner.Scan() {
return nil, fmt.Errorf("package name not found in line: %s", output)
}
name := wordScanner.Text()
var deps []string
for wordScanner.Scan() {
deps = append(deps, wordScanner.Text())
}
res[name] = deps
}
return res, nil
}
2016-04-17 16:22:26 +00:00
func runGofmt() error {
2020-03-01 10:30:12 +00:00
cmd := exec.Command("gofmt", "-l", ".")
cmd.Stderr = os.Stderr
buf, err := cmd.Output()
if err != nil {
return fmt.Errorf("error running gofmt: %v\noutput: %s", err, buf)
}
if len(buf) > 0 {
return fmt.Errorf("not formatted with `gofmt`:\n%s", buf)
}
2016-04-17 16:22:26 +00:00
return nil
}
2018-09-09 08:36:27 +00:00
// run "go mod tidy" so that go.sum and go.mod are updated to reflect all
// dependencies for all OS/Arch combinations, see
// https://github.com/golang/go/wiki/Modules#why-does-go-mod-tidy-put-so-many-indirect-dependencies-in-my-gomod
func runGoModTidy() error {
cmd := exec.Command("go", "mod", "tidy")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
2020-03-01 10:30:12 +00:00
cmd.Env = updateEnv(os.Environ(), nil)
2018-09-09 08:36:27 +00:00
err := cmd.Run()
if err != nil {
2020-03-01 10:30:12 +00:00
return fmt.Errorf("error running 'go mod tidy': %v", err)
2018-09-09 08:36:27 +00:00
}
// check that "git diff" does not return any output
cmd = exec.Command("git", "diff", "go.sum", "go.mod")
cmd.Stderr = os.Stderr
buf, err := cmd.Output()
if err != nil {
2020-03-01 10:30:12 +00:00
return fmt.Errorf("error running 'git diff': %v\noutput: %s", err, buf)
2018-09-09 08:36:27 +00:00
}
if len(buf) > 0 {
2020-03-01 10:30:12 +00:00
return fmt.Errorf("`go.mod` or `go.sum` not up to date (forgot to run `go mod tidy`?):\n%s", buf)
2018-09-09 08:36:27 +00:00
}
return nil
}
func runGlyphcheck() error {
2017-07-23 13:51:39 +00:00
cmd := exec.Command("glyphcheck", "./cmd/...", "./internal/...")
cmd.Stderr = os.Stderr
buf, err := cmd.Output()
if err != nil {
return fmt.Errorf("error running glyphcheck: %v\noutput: %s", err, buf)
}
return nil
}
2016-04-17 16:39:19 +00:00
func run(command string, args ...string) error {
2015-08-19 18:29:06 +00:00
msg("run %v %v\n", command, strings.Join(args, " "))
2016-04-17 16:39:19 +00:00
return runWithEnv(nil, command, args...)
}
// runWithEnv calls a command with the current environment, except the entries
// of the env map are set additionally.
2016-04-17 16:22:26 +00:00
func runWithEnv(env map[string]string, command string, args ...string) error {
msg("runWithEnv %v %v\n", command, strings.Join(args, " "))
cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if env != nil {
cmd.Env = updateEnv(os.Environ(), env)
}
err := cmd.Run()
if err != nil {
2016-04-17 16:22:26 +00:00
return fmt.Errorf("error running %v %v: %v",
command, strings.Join(args, " "), err)
}
2016-04-17 16:22:26 +00:00
return nil
}
func isTravis() bool {
return os.Getenv("TRAVIS_BUILD_DIR") != ""
}
2015-08-19 18:45:54 +00:00
func isAppveyor() bool {
return runtime.GOOS == "windows"
}
func main() {
2020-03-01 10:30:12 +00:00
// make sure we run in Module mode
err := os.Setenv("GO111MODULE", "on")
if err != nil {
msg("setenv(GO111MODULE=on) return error: %v\n", err)
os.Exit(1)
}
2020-03-01 18:47:23 +00:00
// enable the Go Module Proxy
err = os.Setenv("GOPROXY", "https://proxy.golang.org")
if err != nil {
msg("setenv(GOPROXY) return error: %v\n", err)
os.Exit(1)
}
var env CIEnvironment
switch {
case isTravis():
env = &TravisEnvironment{}
2015-08-19 18:45:54 +00:00
case isAppveyor():
env = &AppveyorEnvironment{}
default:
fmt.Fprintln(os.Stderr, "unknown CI environment")
os.Exit(1)
}
2020-03-01 10:30:12 +00:00
err = env.Prepare()
if err != nil {
fmt.Fprintf(os.Stderr, "error preparing: %v\n", err)
os.Exit(1)
}
2016-04-17 16:47:00 +00:00
err = env.RunTests()
if err != nil {
fmt.Fprintf(os.Stderr, "error running tests: %v\n", err)
os.Exit(2)
}
err = env.Teardown()
if err != nil {
fmt.Fprintf(os.Stderr, "error during teardown: %v\n", err)
os.Exit(3)
2016-04-17 16:47:00 +00:00
}
}