-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reuse same TestMain for cmd/ and cmd/tests/
See #2821 (comment)
- Loading branch information
Ivan Mirić
committed
Jan 17, 2023
1 parent
737d26a
commit 895a657
Showing
3 changed files
with
67 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters