Correctly stop the minio server after the tests

This commit is contained in:
Alexander Neumann 2015-12-20 20:42:17 +01:00
parent 0b12ceabe9
commit 43cf95e3c6

View file

@ -5,7 +5,6 @@ package main
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
@ -13,6 +12,7 @@ import (
"regexp" "regexp"
"runtime" "runtime"
"strings" "strings"
"sync"
) )
type CIEnvironment interface { type CIEnvironment interface {
@ -82,6 +82,12 @@ func goVersionAtLeast151() bool {
return true return true
} }
type MinioServer struct {
cmd *exec.Cmd
done bool
m sync.Mutex
}
func (env *TravisEnvironment) RunTests() { func (env *TravisEnvironment) RunTests() {
// run fuse tests on darwin // run fuse tests on darwin
if runtime.GOOS != "darwin" { if runtime.GOOS != "darwin" {
@ -104,12 +110,12 @@ func (env *TravisEnvironment) RunTests() {
var ( var (
testEnv map[string]string testEnv map[string]string
minioCmd *exec.Cmd srv *MinioServer
err error err error
) )
if goVersionAtLeast151() { if goVersionAtLeast151() {
minioCmd, err = runMinio() srv, err = NewMinioServer()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "error running minio server: %v", err) fmt.Fprintf(os.Stderr, "error running minio server: %v", err)
os.Exit(8) os.Exit(8)
@ -121,15 +127,9 @@ func (env *TravisEnvironment) RunTests() {
// run the tests and gather coverage information // run the tests and gather coverage information
runWithEnv(testEnv, "gotestcover", "-coverprofile", "all.cov", "./...") runWithEnv(testEnv, "gotestcover", "-coverprofile", "all.cov", "./...")
if minioCmd != nil {
err := minioCmd.Process.Kill()
if err != nil {
fmt.Fprintf(os.Stderr, "error stopping minio server: %v", err)
os.Exit(8)
}
}
runGofmt() runGofmt()
srv.Stop()
} }
type AppveyorEnvironment struct{} type AppveyorEnvironment struct{}
@ -261,9 +261,9 @@ var minioEnv = map[string]string{
"AWS_SECRET_ACCESS_KEY": "bVX1KhipSBPopEfmhc7rGz8ooxx27xdJ7Gkh1mVe", "AWS_SECRET_ACCESS_KEY": "bVX1KhipSBPopEfmhc7rGz8ooxx27xdJ7Gkh1mVe",
} }
// runMinio prepares and runs a minio server for the s3 backend tests in a // NewMinioServer prepares and runs a minio server for the s3 backend tests in
// temporary directory. // a temporary directory.
func runMinio() (*exec.Cmd, error) { func NewMinioServer() (*MinioServer, error) {
msg("running minio server\n") msg("running minio server\n")
cfgdir, err := ioutil.TempDir("", "minio-config-") cfgdir, err := ioutil.TempDir("", "minio-config-")
if err != nil { if err != nil {
@ -303,16 +303,40 @@ func runMinio() (*exec.Cmd, error) {
return nil, err return nil, err
} }
go func() { srv := &MinioServer{cmd: cmd}
err := cmd.Wait() go srv.Wait()
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 { if err != nil {
fmt.Fprintf(os.Stderr, "error running minio server: %v, output:\n", err) fmt.Fprintf(os.Stderr, "error stopping minio server: %v", err)
io.Copy(os.Stderr, out) 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) os.Exit(12)
} }
}()
return cmd, nil
} }
func isTravis() bool { func isTravis() bool {