Skip to content

Commit

Permalink
testutil: add SkipIfNotIntegration and RequireTimeout
Browse files Browse the repository at this point in the history
This commit adds the beginnings of a dedicated integration test suite to help
segment intensive and long running tests from shorter ones. This comes in the
form of the `testutil.SkipIfNotIntegration` helper.
  • Loading branch information
chrisseto authored and RafalKorepta committed Dec 16, 2024
1 parent bb41e7c commit 7d7ea08
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/testutil/integration_off.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2024 Redpanda Data, Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.md
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0

//go:build !integration

package testutil

const skipIntegrationTests = true
14 changes: 14 additions & 0 deletions pkg/testutil/integration_on.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2024 Redpanda Data, Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.md
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0

//go:build integration

package testutil

const skipIntegrationTests = false
46 changes: 46 additions & 0 deletions pkg/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,52 @@ func Context(t *testing.T) context.Context {
return ctx
}

// SkipIfNotIntegration skips t if the integration build tag has not be
// specified or -short has been specified. It additionally asserts that callers
// are appropriately prefixed with `TestIntegration` and that an appropriate
// `-timeout` value has been specified. To run integration tests, invoke go
// test as:
// `go test ./... --tags integration -run '^TestIntegration' -timeout 10m`
// Usage:
//
// func TestIntegrationSomeIntegrationTest(t *testing.T) {
// SkipIfNotIntegration(t, time.Hour)
// }
func SkipIfNotIntegration(t *testing.T) {
const prefix = "TestIntegration"

if !strings.HasPrefix(t.Name(), prefix) {
t.Fatalf("tests calling SkipIfNotIntegration must be prefixed with %q; got: %s", prefix, t.Name())
}

if skipIntegrationTests {
t.Skipf("integration build flag not set; skipping integration test")
} else if testing.Short() {
t.Skipf("-short specified; skipping integration test")
} else {
RequireTimeout(t, 20*time.Minute)
}
}

// RequireTimeout asserts that the `-timeout` flag is at least `minimum`.
// Usage:
//
// func TestLogThing(t *testing.T) {
// RequireTimeout(t, time.Hour)
// }
func RequireTimeout(t *testing.T, minimum time.Duration) {
deadline, ok := t.Deadline()
if !ok {
return
}

timeout := time.Until(deadline).Round(time.Minute)

if timeout < minimum {
t.Fatalf("-timeout is too low. needed at least %s; got: %s", minimum, timeout)
}
}

// Writer wraps a [testing.T] to implement [io.Writer] by utilizing
// [testing.T.Log].
type Writer struct {
Expand Down
16 changes: 16 additions & 0 deletions pkg/testutil/testutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2024 Redpanda Data, Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.md
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0

package testutil

import "testing"

func TestIntegrationSkipIfNotIntegration(t *testing.T) {
SkipIfNotIntegration(t)
}

0 comments on commit 7d7ea08

Please sign in to comment.