2015-08-19 18:12:41 +00:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-12-20 17:09:35 +00:00
|
|
|
"bytes"
|
2016-01-17 17:33:03 +00:00
|
|
|
"flag"
|
2015-08-19 18:12:41 +00:00
|
|
|
"fmt"
|
2016-01-26 22:42:45 +00:00
|
|
|
"io"
|
2015-12-19 16:21:45 +00:00
|
|
|
"io/ioutil"
|
2016-01-26 22:42:45 +00:00
|
|
|
"net/http"
|
2015-08-19 18:12:41 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2015-12-19 17:22:57 +00:00
|
|
|
"regexp"
|
2015-08-19 18:12:41 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2015-12-20 19:42:17 +00:00
|
|
|
"sync"
|
2015-08-19 18:12:41 +00:00
|
|
|
)
|
|
|
|
|
2016-01-17 17:33:03 +00:00
|
|
|
var runCrossCompile = flag.Bool("cross-compile", true, "run cross compilation tests")
|
2016-01-26 22:42:45 +00:00
|
|
|
var minioServer = flag.String("minio", "", "path to the minio server binary")
|
2016-01-17 17:33:03 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
2015-08-19 18:12:41 +00:00
|
|
|
type CIEnvironment interface {
|
|
|
|
Prepare()
|
|
|
|
RunTests()
|
|
|
|
}
|
|
|
|
|
|
|
|
type TravisEnvironment struct {
|
|
|
|
goxArch []string
|
|
|
|
goxOS []string
|
2016-01-26 22:42:45 +00:00
|
|
|
minio string
|
2015-08-19 18:12:41 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 22:42:45 +00:00
|
|
|
func (env *TravisEnvironment) getMinio() {
|
|
|
|
if *minioServer != "" {
|
|
|
|
msg("using minio server at %q\n", *minioServer)
|
|
|
|
env.minio = *minioServer
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tempfile, err := ioutil.TempFile("", "minio-server-")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "create tempfile failed: %v\n", err)
|
|
|
|
os.Exit(10)
|
|
|
|
}
|
|
|
|
|
2016-01-26 22:52:39 +00:00
|
|
|
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)
|
2016-01-26 22:42:45 +00:00
|
|
|
if err != nil {
|
|
|
|
msg("downloading minio failed: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(tempfile, res.Body)
|
|
|
|
if err != nil {
|
|
|
|
msg("downloading minio failed: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = res.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
msg("saving minio failed: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tempfile.Close()
|
|
|
|
if err != nil {
|
|
|
|
msg("closing tempfile failed: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Chmod(tempfile.Name(), 0755)
|
|
|
|
if err != nil {
|
|
|
|
msg("making minio server executable failed: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg("downloaded minio server to %v\n", tempfile.Name())
|
|
|
|
env.minio = tempfile.Name()
|
2015-12-19 16:21:45 +00:00
|
|
|
}
|
|
|
|
|
2015-08-19 18:12:41 +00:00
|
|
|
func (env *TravisEnvironment) Prepare() {
|
2015-08-19 18:29:06 +00:00
|
|
|
msg("preparing environment for Travis CI\n")
|
2015-08-19 18:12:41 +00:00
|
|
|
|
2015-09-27 15:58:13 +00:00
|
|
|
run("go", "get", "golang.org/x/tools/cmd/cover")
|
2015-08-19 18:12:41 +00:00
|
|
|
run("go", "get", "github.com/mattn/goveralls")
|
2016-01-16 12:32:23 +00:00
|
|
|
run("go", "get", "github.com/pierrre/gotestcover")
|
2016-01-26 22:42:45 +00:00
|
|
|
env.getMinio()
|
2015-08-19 18:12:41 +00:00
|
|
|
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
// install the libraries necessary for fuse
|
2015-12-19 16:38:58 +00:00
|
|
|
run("brew", "update")
|
2015-08-19 18:12:41 +00:00
|
|
|
run("brew", "install", "caskroom/cask/brew-cask")
|
2015-08-19 18:16:58 +00:00
|
|
|
run("brew", "cask", "install", "osxfuse")
|
2015-08-19 18:12:41 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 17:33:03 +00:00
|
|
|
if *runCrossCompile {
|
|
|
|
// only test cross compilation on linux with Travis
|
|
|
|
run("go", "get", "github.com/mitchellh/gox")
|
|
|
|
if runtime.GOOS == "linux" {
|
|
|
|
env.goxArch = []string{"386", "amd64"}
|
|
|
|
if !strings.HasPrefix(runtime.Version(), "go1.3") {
|
|
|
|
env.goxArch = append(env.goxArch, "arm")
|
|
|
|
}
|
2015-08-19 18:12:41 +00:00
|
|
|
|
2016-01-17 17:33:03 +00:00
|
|
|
env.goxOS = []string{"linux", "darwin", "freebsd", "openbsd", "windows"}
|
|
|
|
} else {
|
|
|
|
env.goxArch = []string{runtime.GOARCH}
|
|
|
|
env.goxOS = []string{runtime.GOOS}
|
|
|
|
}
|
2015-08-19 18:12:41 +00:00
|
|
|
|
2016-01-17 17:33:03 +00:00
|
|
|
msg("gox: OS %v, ARCH %v\n", env.goxOS, env.goxArch)
|
2015-08-20 17:42:40 +00:00
|
|
|
|
2016-01-17 17:33:03 +00:00
|
|
|
v := runtime.Version()
|
|
|
|
if !strings.HasPrefix(v, "go1.5") && !strings.HasPrefix(v, "go1.6") {
|
|
|
|
run("gox", "-build-toolchain",
|
|
|
|
"-os", strings.Join(env.goxOS, " "),
|
|
|
|
"-arch", strings.Join(env.goxArch, " "))
|
|
|
|
}
|
2015-08-20 17:42:40 +00:00
|
|
|
}
|
2015-08-19 18:12:41 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 17:22:57 +00:00
|
|
|
func goVersionAtLeast151() bool {
|
|
|
|
v := runtime.Version()
|
|
|
|
|
|
|
|
if match, _ := regexp.MatchString(`^go1\.[0-4]`, v); match {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if v == "go1.5" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-12-20 19:42:17 +00:00
|
|
|
type MinioServer struct {
|
|
|
|
cmd *exec.Cmd
|
|
|
|
done bool
|
|
|
|
m sync.Mutex
|
|
|
|
}
|
|
|
|
|
2015-08-19 18:12:41 +00:00
|
|
|
func (env *TravisEnvironment) RunTests() {
|
|
|
|
// run fuse tests on darwin
|
|
|
|
if runtime.GOOS != "darwin" {
|
2015-08-19 18:29:06 +00:00
|
|
|
msg("skip fuse integration tests on %v\n", runtime.GOOS)
|
2015-08-19 18:12:41 +00:00
|
|
|
os.Setenv("RESTIC_TEST_FUSE", "0")
|
|
|
|
}
|
|
|
|
|
2016-02-14 16:39:51 +00:00
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Getwd() returned error: %v", err)
|
|
|
|
os.Exit(9)
|
|
|
|
}
|
|
|
|
|
|
|
|
envWithGOPATH := make(map[string]string)
|
|
|
|
envWithGOPATH["GOPATH"] = cwd + ":" + filepath.Join(cwd, "vendor")
|
|
|
|
|
2016-01-17 17:33:03 +00:00
|
|
|
if *runCrossCompile {
|
|
|
|
// compile for all target architectures with tags
|
|
|
|
for _, tags := range []string{"release", "debug"} {
|
2016-02-14 16:39:51 +00:00
|
|
|
runWithEnv(envWithGOPATH, "gox", "-verbose",
|
2016-01-17 17:33:03 +00:00
|
|
|
"-os", strings.Join(env.goxOS, " "),
|
|
|
|
"-arch", strings.Join(env.goxArch, " "),
|
|
|
|
"-tags", tags,
|
|
|
|
"-output", "/tmp/{{.Dir}}_{{.OS}}_{{.Arch}}",
|
2016-02-14 16:39:51 +00:00
|
|
|
"restic/cmd/restic")
|
2016-01-17 17:33:03 +00:00
|
|
|
}
|
2015-08-19 18:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// run the build script
|
|
|
|
run("go", "run", "build.go")
|
|
|
|
|
2016-02-14 16:39:51 +00:00
|
|
|
var srv *MinioServer
|
2016-01-26 22:42:45 +00:00
|
|
|
if env.minio != "" {
|
|
|
|
srv, err = NewMinioServer(env.minio)
|
2015-12-19 17:22:57 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error running minio server: %v", err)
|
|
|
|
os.Exit(8)
|
|
|
|
}
|
|
|
|
|
2016-02-14 16:39:51 +00:00
|
|
|
for k, v := range minioEnv {
|
|
|
|
envWithGOPATH[k] = v
|
|
|
|
}
|
2015-12-19 16:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// run the tests and gather coverage information
|
2016-02-14 16:39:51 +00:00
|
|
|
runWithEnv(envWithGOPATH, "gotestcover", "-coverprofile", "all.cov", "restic/...")
|
2015-12-19 16:21:45 +00:00
|
|
|
|
2015-08-19 18:12:41 +00:00
|
|
|
runGofmt()
|
2015-12-20 19:42:17 +00:00
|
|
|
|
|
|
|
srv.Stop()
|
2015-08-19 18:12:41 +00:00
|
|
|
}
|
|
|
|
|
2015-08-19 18:45:54 +00:00
|
|
|
type AppveyorEnvironment struct{}
|
|
|
|
|
|
|
|
func (env *AppveyorEnvironment) Prepare() {
|
|
|
|
msg("preparing environment for Appveyor CI\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (env *AppveyorEnvironment) RunTests() {
|
|
|
|
run("go", "run", "build.go", "-v", "-T")
|
|
|
|
}
|
|
|
|
|
2015-08-19 18:12:41 +00:00
|
|
|
// findGoFiles returns a list of go source code file names below dir.
|
|
|
|
func findGoFiles(dir string) (list []string, err error) {
|
|
|
|
err = filepath.Walk(dir, func(name string, fi os.FileInfo, err error) error {
|
|
|
|
if filepath.Base(name) == "Godeps" {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
|
|
|
|
if filepath.Ext(name) == ".go" {
|
|
|
|
relpath, err := filepath.Rel(dir, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
list = append(list, relpath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return list, err
|
|
|
|
}
|
|
|
|
|
2015-08-19 18:29:06 +00:00
|
|
|
func msg(format string, args ...interface{}) {
|
|
|
|
fmt.Printf("CI: "+format, args...)
|
|
|
|
}
|
|
|
|
|
2015-12-19 16:21:45 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-08-19 18:12:41 +00:00
|
|
|
func runGofmt() {
|
|
|
|
dir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Getwd(): %v\n", err)
|
|
|
|
os.Exit(5)
|
|
|
|
}
|
|
|
|
|
|
|
|
files, err := findGoFiles(dir)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error finding Go files: %v\n", err)
|
|
|
|
os.Exit(4)
|
|
|
|
}
|
|
|
|
|
2015-08-19 18:29:06 +00:00
|
|
|
msg("runGofmt() with %d files\n", len(files))
|
2015-08-19 18:12:41 +00:00
|
|
|
args := append([]string{"-l"}, files...)
|
|
|
|
cmd := exec.Command("gofmt", args...)
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
buf, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error running gofmt: %v", err)
|
|
|
|
fmt.Fprintf(os.Stderr, "output:\n%s\n", buf)
|
|
|
|
os.Exit(3)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(buf) > 0 {
|
|
|
|
fmt.Fprintf(os.Stderr, "not formatted with `gofmt`:\n")
|
|
|
|
fmt.Fprintln(os.Stderr, string(buf))
|
|
|
|
os.Exit(6)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(command string, args ...string) {
|
2015-08-19 18:29:06 +00:00
|
|
|
msg("run %v %v\n", command, strings.Join(args, " "))
|
2015-12-19 16:21:45 +00:00
|
|
|
runWithEnv(nil, command, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// runWithEnv calls a command with the current environment, except the entries
|
|
|
|
// of the env map are set additionally.
|
|
|
|
func runWithEnv(env map[string]string, command string, args ...string) {
|
|
|
|
msg("runWithEnv %v %v\n", command, strings.Join(args, " "))
|
2015-08-19 18:12:41 +00:00
|
|
|
cmd := exec.Command(command, args...)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
2015-12-19 16:21:45 +00:00
|
|
|
if env != nil {
|
|
|
|
cmd.Env = updateEnv(os.Environ(), env)
|
|
|
|
}
|
2015-08-19 18:12:41 +00:00
|
|
|
err := cmd.Run()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error running %v %v: %v",
|
|
|
|
command, strings.Join(args, " "), err)
|
|
|
|
os.Exit(3)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-19 16:21:45 +00:00
|
|
|
var minioConfig = `
|
|
|
|
{
|
|
|
|
"version": "2",
|
|
|
|
"credentials": {
|
|
|
|
"accessKeyId": "KEBIYDZ87HCIH5D17YCN",
|
|
|
|
"secretAccessKey": "bVX1KhipSBPopEfmhc7rGz8ooxx27xdJ7Gkh1mVe"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var minioEnv = map[string]string{
|
2015-12-28 17:23:34 +00:00
|
|
|
"RESTIC_TEST_S3_SERVER": "http://127.0.0.1:9000",
|
2015-12-19 16:21:45 +00:00
|
|
|
"AWS_ACCESS_KEY_ID": "KEBIYDZ87HCIH5D17YCN",
|
|
|
|
"AWS_SECRET_ACCESS_KEY": "bVX1KhipSBPopEfmhc7rGz8ooxx27xdJ7Gkh1mVe",
|
|
|
|
}
|
|
|
|
|
2015-12-20 19:42:17 +00:00
|
|
|
// NewMinioServer prepares and runs a minio server for the s3 backend tests in
|
|
|
|
// a temporary directory.
|
2016-01-26 22:42:45 +00:00
|
|
|
func NewMinioServer(minio string) (*MinioServer, error) {
|
2015-12-19 16:36:48 +00:00
|
|
|
msg("running minio server\n")
|
2015-12-19 16:21:45 +00:00
|
|
|
cfgdir, err := ioutil.TempDir("", "minio-config-")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := os.Create(filepath.Join(cfgdir, "config.json"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = cfg.Write([]byte(minioConfig))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cfg.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "minio-root")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-20 17:09:35 +00:00
|
|
|
out := bytes.NewBuffer(nil)
|
2015-12-19 16:21:45 +00:00
|
|
|
|
2016-01-26 22:42:45 +00:00
|
|
|
cmd := exec.Command(minio,
|
2015-12-19 16:36:48 +00:00
|
|
|
"--config-folder", cfgdir,
|
|
|
|
"--address", "127.0.0.1:9000",
|
|
|
|
"server", dir)
|
2015-12-20 17:09:35 +00:00
|
|
|
cmd.Stdout = out
|
|
|
|
cmd.Stderr = out
|
2015-12-19 16:21:45 +00:00
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-20 19:42:17 +00:00
|
|
|
srv := &MinioServer{cmd: cmd}
|
|
|
|
go srv.Wait()
|
2015-12-20 17:09:35 +00:00
|
|
|
|
2015-12-20 19:42:17 +00:00
|
|
|
return srv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MinioServer) Stop() {
|
|
|
|
if m == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg("stopping minio server\n")
|
|
|
|
m.m.Lock()
|
|
|
|
m.done = true
|
|
|
|
m.m.Unlock()
|
|
|
|
err := m.cmd.Process.Kill()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error stopping minio server: %v", err)
|
|
|
|
os.Exit(8)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MinioServer) Wait() {
|
|
|
|
err := m.cmd.Wait()
|
|
|
|
msg("minio server exited\n")
|
|
|
|
m.m.Lock()
|
|
|
|
done := m.done
|
|
|
|
m.m.Unlock()
|
|
|
|
|
|
|
|
if err != nil && !done {
|
|
|
|
fmt.Fprintf(os.Stderr, "error running minio server: %#v, output:\n", err)
|
|
|
|
// io.Copy(os.Stderr, out)
|
|
|
|
os.Exit(12)
|
|
|
|
}
|
2015-12-19 16:21:45 +00:00
|
|
|
}
|
|
|
|
|
2015-08-19 18:12:41 +00:00
|
|
|
func isTravis() bool {
|
|
|
|
return os.Getenv("TRAVIS_BUILD_DIR") != ""
|
|
|
|
}
|
|
|
|
|
2015-08-19 18:45:54 +00:00
|
|
|
func isAppveyor() bool {
|
|
|
|
return runtime.GOOS == "windows"
|
|
|
|
}
|
|
|
|
|
2015-08-19 18:12:41 +00:00
|
|
|
func main() {
|
|
|
|
var env CIEnvironment
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case isTravis():
|
|
|
|
env = &TravisEnvironment{}
|
2015-08-19 18:45:54 +00:00
|
|
|
case isAppveyor():
|
|
|
|
env = &AppveyorEnvironment{}
|
2015-08-19 18:12:41 +00:00
|
|
|
default:
|
|
|
|
fmt.Fprintln(os.Stderr, "unknown CI environment")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range []func(){env.Prepare, env.RunTests} {
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
}
|