ginkgo: flag provided but not defined: -test.timeout (Go 1.13)

When trying to run the apt-buildpack integration tests on a computer running Go 1.13, we were unable to and got the following error:


	 -------------------------------------------------------------------
	|                                                                   |
	|  Ginkgo timed out waiting for all parallel nodes to report back!  |
	|                                                                   |
	 -------------------------------------------------------------------

 integration timed out. path: .
[1] flag provided but not defined: -test.timeout
[1] Usage of /tmp/ginkgo538951234/integration.test:
...

Our integration test script:

#!/usr/bin/env bash
set -euo pipefail

cd "$( dirname "${BASH_SOURCE[0]}" )/.."
source .envrc
./scripts/install_tools.sh

GINKGO_NODES=${GINKGO_NODES:-3}
GINKGO_ATTEMPTS=${GINKGO_ATTEMPTS:-2}
export CF_STACK=${CF_STACK:-cflinuxfs3}

UNCACHED_BUILDPACK_FILE=${UNCACHED_BUILDPACK_FILE:-""}

cd src/*/integration

echo "Run Uncached Buildpack"
BUILDPACK_FILE="$UNCACHED_BUILDPACK_FILE" \
  ginkgo -r -mod vendor --flakeAttempts=$GINKGO_ATTEMPTS -nodes $GINKGO_NODES --slowSpecThreshold=60 -- --cached=false

Env:

root@b349d15677b6:/app# go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/app/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build280745408=/tmp/go-build -gno-record-gcc-switches"

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 18 (1 by maintainers)

Commits related to this issue

Most upvoted comments

I can confirm that replacing

func init() {
	flag.Parse()
}

TestXYZ(t *testing.T) {}

with

func TestMain(m *testing.M) {
	flag.Parse()
	os.Exit(m.Run())
}

TestXYZ(t *testing.T) {}

solves the issue.

As @dfreilich correctly pointed out, the flag provided but not defined error occurs because of https://golang.org/doc/go1.13#testing:

packages that call flag.Parse during package initialization may cause tests to fail

It occurs when flag.Parse is called within the init function. The solution is to move flag.Parse from the init function to somewhere else (e.g. to main or BeforeSuite) For the example above this line must be moved.

An example how to reproduce this error without using ginkgo can be found here. (I can reproduce it with the latest go version go1.13.4 darwin/amd64.)

I agree with @zhubuntu that this is not a Ginkgo issue. I therefore suggest to close this issue.

Looks like it’s a breaking change in Go 1.13. The solution is for users not to call flag.Parse() in an init() function. Please let us know if that does not work for you.

In some cases explicit call for testing.Init() before init() functions in suites may help:


var _ = func() bool {
 	testing.Init()
	return true
}()

func TestApp(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "App / Suite")
}

any update? are you planning to fix that?

Suspect this is the cause https://golang.org/doc/go1.13#testing

Testing flags are now registered in the new Init function, which is invoked by the generated main function for the test. As a result, testing flags are now only registered when running a test binary, and packages that call flag.Parse during package initialization may cause tests to fail.

I am hitting this error flag provided but not defined: -test.timeout without having to use the flag.Parse anywhere in my codebase. Nor there is any init func My env:

$ go version
go version go1.13.3 darwin/amd64
$ ginkgo version
Ginkgo Version 1.12.3

I think this is the test case problem, not the ginkgoproblem. As kubernetes/kubernetes#82742