Skip to content

Commit

Permalink
Reuse same TestMain for cmd/ and cmd/tests/
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Mirić committed Jan 17, 2023
1 parent 737d26a commit 895a657
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 97 deletions.
54 changes: 1 addition & 53 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,16 @@ package cmd

import (
"bytes"
"fmt"
"net/http"
"os"
"sync/atomic"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"go.k6.io/k6/cmd/tests"
"go.k6.io/k6/lib/testutils"
"go.uber.org/goleak"
)

type blockingTransport struct {
fallback http.RoundTripper
forbiddenHosts map[string]bool
counter uint32
}

func (bt *blockingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
host := req.URL.Hostname()
if bt.forbiddenHosts[host] {
atomic.AddUint32(&bt.counter, 1)
panic(fmt.Errorf("trying to make forbidden request to %s during test", host))
}
return bt.fallback.RoundTrip(req)
}

func TestMain(m *testing.M) {
exitCode := 1 // error out by default
defer func() {
os.Exit(exitCode)
}()

bt := &blockingTransport{
fallback: http.DefaultTransport,
forbiddenHosts: map[string]bool{
"ingest.k6.io": true,
"cloudlogs.k6.io": true,
"app.k6.io": true,
"reports.k6.io": true,
},
}
http.DefaultTransport = bt
defer func() {
if bt.counter > 0 {
fmt.Printf("Expected blocking transport count to be 0 but was %d\n", bt.counter) //nolint:forbidigo
exitCode = 2
}
}()

defer func() {
// TODO: figure out why logrus' `Entry.WriterLevel` goroutine sticks
// around and remove this exception.
opt := goleak.IgnoreTopFunction("io.(*pipe).read")
if err := goleak.Find(opt); err != nil {
fmt.Println(err) //nolint:forbidigo
exitCode = 3
}
}()

exitCode = m.Run()
tests.Main(m)
}

func TestDeprecatedOptionWarning(t *testing.T) {
Expand Down
65 changes: 65 additions & 0 deletions cmd/tests/tests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package tests

import (
"fmt"
"net/http"
"os"
"sync/atomic"
"testing"

"go.uber.org/goleak"
)

type blockingTransport struct {
fallback http.RoundTripper
forbiddenHosts map[string]bool
counter uint32
}

func (bt *blockingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
host := req.URL.Hostname()
if bt.forbiddenHosts[host] {
atomic.AddUint32(&bt.counter, 1)
panic(fmt.Errorf("trying to make forbidden request to %s during test", host))
}
return bt.fallback.RoundTrip(req)
}

// Main is a TestMain function that can be imported by other test packages that
// want to use the blocking transport and other features useful for integration
// tests.
func Main(m *testing.M) {
exitCode := 1 // error out by default
defer func() {
os.Exit(exitCode)
}()

bt := &blockingTransport{
fallback: http.DefaultTransport,
forbiddenHosts: map[string]bool{
"ingest.k6.io": true,
"cloudlogs.k6.io": true,
"app.k6.io": true,
"reports.k6.io": true,
},
}
http.DefaultTransport = bt
defer func() {
if bt.counter > 0 {
fmt.Printf("Expected blocking transport count to be 0 but was %d\n", bt.counter) //nolint:forbidigo
exitCode = 2
}
}()

defer func() {
// TODO: figure out why logrus' `Entry.WriterLevel` goroutine sticks
// around and remove this exception.
opt := goleak.IgnoreTopFunction("io.(*pipe).read")
if err := goleak.Find(opt); err != nil {
fmt.Println(err) //nolint:forbidigo
exitCode = 3
}
}()

exitCode = m.Run()
}
45 changes: 1 addition & 44 deletions cmd/tests/tests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,9 @@
package tests

import (
"fmt"
"net/http"
"os"
"sync/atomic"
"testing"
)

type blockingTransport struct {
fallback http.RoundTripper
forbiddenHosts map[string]bool
counter uint32
}

func (bt *blockingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
host := req.URL.Hostname()
if bt.forbiddenHosts[host] {
atomic.AddUint32(&bt.counter, 1)
panic(fmt.Errorf("trying to make forbidden request to %s during test", host))
}
return bt.fallback.RoundTrip(req)
}

func TestMain(m *testing.M) {
exitCode := 1 // error out by default
defer func() {
os.Exit(exitCode)
}()

bt := &blockingTransport{
fallback: http.DefaultTransport,
forbiddenHosts: map[string]bool{
"ingest.k6.io": true,
"cloudlogs.k6.io": true,
"app.k6.io": true,
"reports.k6.io": true,
},
}
http.DefaultTransport = bt
defer func() {
if bt.counter > 0 {
fmt.Printf("Expected blocking transport count to be 0 but was %d\n", bt.counter) //nolint:forbidigo
exitCode = 2
}
}()

// TODO: add https://github.com/uber-go/goleak

exitCode = m.Run()
Main(m)
}

0 comments on commit 895a657

Please sign in to comment.