Skip to content

Commit

Permalink
In "go test" 1.13 and higher, you cannot call parse.Flags in init()
Browse files Browse the repository at this point in the history
In 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.

See:
  https://groups.google.com/d/msg/golang-nuts/rkCdS1EQwSM/4gbwNh0QAwAJ
  golang/go#21051
  https://go-review.googlesource.com/c/go/+/173722/
  https://www.bountysource.com/issues/79987300-flag-provided-but-not-defined-test-timeout-go-1-13
  https://golang.org/pkg/testing/#hdr-Main
  • Loading branch information
Craig Rodrigues committed Feb 5, 2020
1 parent 491ea43 commit d550ee3
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 9 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ TAGS := daemon
endif

ifndef PKGS
# shell does not honor export command above, so we need to explicitly pass GOFLAGS here
PKGS := $(shell GOFLAGS=-mod=vendor go list ./... 2>&1 | grep -v 'github.com/portworx/torpedo/tests')
endif

Expand Down
5 changes: 4 additions & 1 deletion tests/asg/asg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests
import (
"fmt"
"math/rand"
"os"
"testing"
"time"

Expand Down Expand Up @@ -203,8 +204,10 @@ var _ = AfterSuite(func() {
ValidateCleanup()
})

func init() {
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
ParseFlags()
os.Exit(m.Run())
}

func Scale(count int64) {
Expand Down
5 changes: 4 additions & 1 deletion tests/autopilot/autopilot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tests

import (
"fmt"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -525,6 +526,8 @@ var _ = AfterSuite(func() {
ValidateCleanup()
})

func init() {
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
ParseFlags()
os.Exit(m.Run())
}
5 changes: 4 additions & 1 deletion tests/basic/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests
import (
"fmt"
"math/rand"
"os"
"testing"
"time"

Expand Down Expand Up @@ -444,6 +445,8 @@ var _ = AfterSuite(func() {
ValidateCleanup()
})

func init() {
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
ParseFlags()
os.Exit(m.Run())
}
5 changes: 4 additions & 1 deletion tests/drive_failure/drive_failure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tests

import (
"fmt"
"os"
"testing"
"time"

Expand Down Expand Up @@ -122,6 +123,8 @@ var _ = AfterSuite(func() {
ValidateCleanup()
})

func init() {
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
ParseFlags()
os.Exit(m.Run())
}
5 changes: 4 additions & 1 deletion tests/node_decommission/node_decommission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests
import (
"fmt"
"math/rand"
"os"
"testing"
"time"

Expand Down Expand Up @@ -121,6 +122,8 @@ var _ = AfterSuite(func() {
ValidateCleanup()
})

func init() {
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
ParseFlags()
os.Exit(m.Run())
}
5 changes: 4 additions & 1 deletion tests/reboot/reboot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tests

import (
"fmt"
"os"
"testing"
"time"

Expand Down Expand Up @@ -160,6 +161,8 @@ var _ = AfterSuite(func() {
ValidateCleanup()
})

func init() {
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
ParseFlags()
os.Exit(m.Run())
}
5 changes: 4 additions & 1 deletion tests/sched/sched_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tests

import (
"fmt"
"os"
"testing"
"time"

Expand Down Expand Up @@ -83,6 +84,8 @@ var _ = AfterSuite(func() {
ValidateCleanup()
})

func init() {
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
ParseFlags()
os.Exit(m.Run())
}
5 changes: 4 additions & 1 deletion tests/upgrade/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tests

import (
"fmt"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -116,6 +117,8 @@ var _ = AfterSuite(func() {
ValidateCleanup()
})

func init() {
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
ParseFlags()
os.Exit(m.Run())
}
5 changes: 4 additions & 1 deletion tests/volume_ops/volume_ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests
import (
"fmt"
"math"
"os"
"testing"

. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -167,6 +168,8 @@ var _ = AfterSuite(func() {
ValidateCleanup()
})

func init() {
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
ParseFlags()
os.Exit(m.Run())
}

0 comments on commit d550ee3

Please sign in to comment.