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}
GOBUILD=go build -ldflags "-X main.Version=${VERSION}"
PKG=gitlab.com/gitlab-org/gitlab-workhorse
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/$@
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/$@
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}
install: gitlab-workhorse gitlab-zip-cat gitlab-zip-metadata
mkdir -p $(DESTDIR)${PREFIX}/bin/
cd ${BUILD_DIR} && install gitlab-workhorse gitlab-zip-cat gitlab-zip-metadata ${DESTDIR}${PREFIX}/bin/
${BUILD_DIR}/_build:
mkdir -p $@/src/${PKG}
tar -cf - --exclude _build --exclude .git . | (cd $@/src/${PKG} && tar -xf -)
${BUILD_DIR}/_build/.sync:
mkdir -p ${BUILD_DIR}/_build/src/${PKG}
tar -cf - --exclude _build --exclude .git . | (cd ${BUILD_DIR}/_build/src/${PKG} && tar -xf -)
touch $@
.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 } }'
_support/detect-context.sh
cd ${GOPATH}/src/${PKG} && govendor sync
cp $(EXE_ALL) ${GOPATH}/src/${PKG}
go test ${PKG_ALL}
@echo SUCCESS
......@@ -61,3 +63,9 @@ release:
.PHONY: clean-build
clean-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 (
)
func TestMain(m *testing.M) {
cleanup, err := testhelper.BuildExecutables()
if err != nil {
log.WithError(err).Print("Test setup: failed to build executables")
os.Exit(1)
if err := testhelper.BuildExecutables(); err != nil {
log.WithError(err).Fatal()
}
os.Exit(func() int {
defer cleanup()
return m.Run()
}())
os.Exit(m.Run())
}
......@@ -5,11 +5,9 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path"
"regexp"
"runtime"
......@@ -126,35 +124,24 @@ func TestServerWithHandler(url *regexp.Regexp, handler http.HandlerFunc) *httpte
}))
}
func BuildExecutables() (func(), error) {
rootDir := RootDir()
var workhorseExecutables = []string{"gitlab-workhorse", "gitlab-zip-cat", "gitlab-zip-metadata"}
// This method will be invoked more than once due to Go test
// parallelization. We must use a unique temp directory for each
// 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")
}
func BuildExecutables() error {
rootDir := RootDir()
makeCmd := exec.Command("make", "BUILD_DIR="+testDir)
makeCmd.Dir = rootDir
makeCmd.Stderr = os.Stderr
makeCmd.Stdout = os.Stdout
if err := makeCmd.Run(); err != nil {
return nil, fmt.Errorf("failed to run %v in %v", makeCmd, rootDir)
for _, exe := range workhorseExecutables {
if _, err := os.Stat(path.Join(rootDir, exe)); os.IsNotExist(err) {
return fmt.Errorf("cannot find executable %s. Please run 'make prepare-tests'", exe)
}
}
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 {
return nil, fmt.Errorf("failed to set PATH to %v", testPath)
return fmt.Errorf("failed to set PATH to %v", testPath)
}
return func() {
os.Setenv("PATH", oldPath)
os.RemoveAll(testDir)
}, nil
return nil
}
func RootDir() string {
......
......@@ -45,31 +45,17 @@ var cacheDir = path.Join(scratchDir, "cache")
func TestMain(m *testing.M) {
git.Testing = true
source := "https://gitlab.com/gitlab-org/gitlab-test.git"
clonePath := path.Join(testRepoRoot, testRepo)
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)
}
if _, err := os.Stat(path.Join(testRepoRoot, testRepo)); os.IsNotExist(err) {
log.Fatal("cannot find test repository. Please run 'make prepare-tests'")
}
cleanup, err := testhelper.BuildExecutables()
if err != nil {
log.WithError(err).Print("Test setup: failed to build executables")
os.Exit(1)
if err := testhelper.BuildExecutables(); err != nil {
log.WithError(err).Fatal()
}
defer gitaly.CloseConnections()
os.Exit(func() int {
defer cleanup()
return m.Run()
}())
os.Exit(m.Run())
}
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