Commit 88cc7a86 authored by Jacob Vosmaer (GitLab)'s avatar Jacob Vosmaer (GitLab) Committed by Nick Thomas

Stop calling 'make' from 'go test'

parent 25d866fa
...@@ -6,32 +6,34 @@ export PATH:=${GOPATH}/bin:${PATH} ...@@ -6,32 +6,34 @@ export PATH:=${GOPATH}/bin:${PATH}
GOBUILD=go build -ldflags "-X main.Version=${VERSION}" GOBUILD=go build -ldflags "-X main.Version=${VERSION}"
PKG=gitlab.com/gitlab-org/gitlab-workhorse PKG=gitlab.com/gitlab-org/gitlab-workhorse
PKG_ALL = $(shell GOPATH=${GOPATH} go list ${PKG}/... | grep -v /vendor/) PKG_ALL = $(shell GOPATH=${GOPATH} go list ${PKG}/... | grep -v /vendor/)
EXE_ALL = gitlab-zip-cat gitlab-zip-metadata gitlab-workhorse
all: clean-build gitlab-zip-cat gitlab-zip-metadata gitlab-workhorse all: clean-build $(EXE_ALL)
gitlab-zip-cat: ${BUILD_DIR}/_build $(shell find cmd/gitlab-zip-cat/ -name '*.go') gitlab-zip-cat: ${BUILD_DIR}/_build/.sync $(shell find cmd/gitlab-zip-cat/ -name '*.go')
${GOBUILD} -o ${BUILD_DIR}/$@ ${PKG}/cmd/$@ ${GOBUILD} -o ${BUILD_DIR}/$@ ${PKG}/cmd/$@
gitlab-zip-metadata: ${BUILD_DIR}/_build $(shell find cmd/gitlab-zip-metadata/ -name '*.go') gitlab-zip-metadata: ${BUILD_DIR}/_build/.sync $(shell find cmd/gitlab-zip-metadata/ -name '*.go')
${GOBUILD} -o ${BUILD_DIR}/$@ ${PKG}/cmd/$@ ${GOBUILD} -o ${BUILD_DIR}/$@ ${PKG}/cmd/$@
gitlab-workhorse: ${BUILD_DIR}/_build $(shell find . -name '*.go' | grep -v '^\./_') gitlab-workhorse: ${BUILD_DIR}/_build/.sync $(shell find . -name '*.go' | grep -v '^\./_')
${GOBUILD} -o ${BUILD_DIR}/$@ ${PKG} ${GOBUILD} -o ${BUILD_DIR}/$@ ${PKG}
install: gitlab-workhorse gitlab-zip-cat gitlab-zip-metadata install: gitlab-workhorse gitlab-zip-cat gitlab-zip-metadata
mkdir -p $(DESTDIR)${PREFIX}/bin/ mkdir -p $(DESTDIR)${PREFIX}/bin/
cd ${BUILD_DIR} && install gitlab-workhorse gitlab-zip-cat gitlab-zip-metadata ${DESTDIR}${PREFIX}/bin/ cd ${BUILD_DIR} && install gitlab-workhorse gitlab-zip-cat gitlab-zip-metadata ${DESTDIR}${PREFIX}/bin/
${BUILD_DIR}/_build: ${BUILD_DIR}/_build/.sync:
mkdir -p $@/src/${PKG} mkdir -p ${BUILD_DIR}/_build/src/${PKG}
tar -cf - --exclude _build --exclude .git . | (cd $@/src/${PKG} && tar -xf -) tar -cf - --exclude _build --exclude .git . | (cd ${BUILD_DIR}/_build/src/${PKG} && tar -xf -)
touch $@ touch $@
.PHONY: test .PHONY: test
test: clean-build clean-workhorse all govendor test: clean-build clean-workhorse govendor prepare-tests
go fmt ${PKG_ALL} | awk '{ print } END { if (NR > 0) { print "Please run go fmt"; exit 1 } }' go fmt ${PKG_ALL} | awk '{ print } END { if (NR > 0) { print "Please run go fmt"; exit 1 } }'
_support/detect-context.sh _support/detect-context.sh
cd ${GOPATH}/src/${PKG} && govendor sync cd ${GOPATH}/src/${PKG} && govendor sync
cp $(EXE_ALL) ${GOPATH}/src/${PKG}
go test ${PKG_ALL} go test ${PKG_ALL}
@echo SUCCESS @echo SUCCESS
...@@ -61,3 +63,9 @@ release: ...@@ -61,3 +63,9 @@ release:
.PHONY: clean-build .PHONY: clean-build
clean-build: clean-build:
rm -rf ${BUILD_DIR}/_build rm -rf ${BUILD_DIR}/_build
.PHONY: prepare-tests
prepare-tests: testdata/data/group/test.git $(EXE_ALL)
testdata/data/group/test.git:
git clone --quiet --bare https://gitlab.com/gitlab-org/gitlab-test.git $@
...@@ -10,15 +10,10 @@ import ( ...@@ -10,15 +10,10 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
cleanup, err := testhelper.BuildExecutables() if err := testhelper.BuildExecutables(); err != nil {
if err != nil { log.WithError(err).Fatal()
log.WithError(err).Print("Test setup: failed to build executables")
os.Exit(1)
} }
os.Exit(func() int { os.Exit(m.Run())
defer cleanup()
return m.Run()
}())
} }
...@@ -5,11 +5,9 @@ import ( ...@@ -5,11 +5,9 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
"os/exec"
"path" "path"
"regexp" "regexp"
"runtime" "runtime"
...@@ -126,35 +124,24 @@ func TestServerWithHandler(url *regexp.Regexp, handler http.HandlerFunc) *httpte ...@@ -126,35 +124,24 @@ func TestServerWithHandler(url *regexp.Regexp, handler http.HandlerFunc) *httpte
})) }))
} }
func BuildExecutables() (func(), error) { var workhorseExecutables = []string{"gitlab-workhorse", "gitlab-zip-cat", "gitlab-zip-metadata"}
rootDir := RootDir()
// This method will be invoked more than once due to Go test func BuildExecutables() error {
// parallelization. We must use a unique temp directory for each rootDir := RootDir()
// invokation so that they do not trample each other's builds.
testDir, err := ioutil.TempDir("", "gitlab-workhorse-test")
if err != nil {
return nil, errors.New("could not create temp directory")
}
makeCmd := exec.Command("make", "BUILD_DIR="+testDir) for _, exe := range workhorseExecutables {
makeCmd.Dir = rootDir if _, err := os.Stat(path.Join(rootDir, exe)); os.IsNotExist(err) {
makeCmd.Stderr = os.Stderr return fmt.Errorf("cannot find executable %s. Please run 'make prepare-tests'", exe)
makeCmd.Stdout = os.Stdout }
if err := makeCmd.Run(); err != nil {
return nil, fmt.Errorf("failed to run %v in %v", makeCmd, rootDir)
} }
oldPath := os.Getenv("PATH") oldPath := os.Getenv("PATH")
testPath := fmt.Sprintf("%s:%s", testDir, oldPath) testPath := fmt.Sprintf("%s:%s", rootDir, oldPath)
if err := os.Setenv("PATH", testPath); err != nil { if err := os.Setenv("PATH", testPath); err != nil {
return nil, fmt.Errorf("failed to set PATH to %v", testPath) return fmt.Errorf("failed to set PATH to %v", testPath)
} }
return func() { return nil
os.Setenv("PATH", oldPath)
os.RemoveAll(testDir)
}, nil
} }
func RootDir() string { func RootDir() string {
......
...@@ -45,31 +45,17 @@ var cacheDir = path.Join(scratchDir, "cache") ...@@ -45,31 +45,17 @@ var cacheDir = path.Join(scratchDir, "cache")
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
git.Testing = true git.Testing = true
source := "https://gitlab.com/gitlab-org/gitlab-test.git" if _, err := os.Stat(path.Join(testRepoRoot, testRepo)); os.IsNotExist(err) {
clonePath := path.Join(testRepoRoot, testRepo) log.Fatal("cannot find test repository. Please run 'make prepare-tests'")
if _, err := os.Stat(clonePath); err != nil {
testCmd := exec.Command("git", "clone", "--bare", source, clonePath)
testCmd.Stdout = os.Stdout
testCmd.Stderr = os.Stderr
if err := testCmd.Run(); err != nil {
log.WithField("testCmd", testCmd).Print("Test setup: failed to run")
os.Exit(-1)
}
} }
cleanup, err := testhelper.BuildExecutables() if err := testhelper.BuildExecutables(); err != nil {
if err != nil { log.WithError(err).Fatal()
log.WithError(err).Print("Test setup: failed to build executables")
os.Exit(1)
} }
defer gitaly.CloseConnections() defer gitaly.CloseConnections()
os.Exit(func() int { os.Exit(m.Run())
defer cleanup()
return m.Run()
}())
} }
func TestAllowedClone(t *testing.T) { func TestAllowedClone(t *testing.T) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment