Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/uptest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package main

import (
"context"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -107,5 +108,6 @@ func e2eTests() {
SetUseLibraryMode(*useLibraryMode).
Build()

kingpin.FatalIfError(pkg.RunTest(automatedTest), "cannot run e2e tests successfully")
ctx := context.Background()
kingpin.FatalIfError(pkg.RunTestContext(ctx, automatedTest), "cannot run e2e tests successfully")
}
18 changes: 9 additions & 9 deletions internal/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type Tester struct {
}

// ExecuteTests execute tests via chainsaw.
func (t *Tester) ExecuteTests() error {
func (t *Tester) ExecuteTests(ctx context.Context) error {
if err := writeTestFile(t.manifests, t.options.Directory); err != nil {
return errors.Wrap(err, "cannot write test manifest files")
}
Expand All @@ -87,21 +87,21 @@ func (t *Tester) ExecuteTests() error {
log.Println("Skipping test " + tf)
continue
}
if err := executeSingleTestFile(t, tf, timeout-time.Since(startTime), resources); err != nil {
if err := executeSingleTestFile(ctx, t, tf, timeout-time.Since(startTime), resources); err != nil {
return errors.Wrap(err, "cannot execute test "+tf)
}
}
return nil
}

func executeSingleTestFile(t *Tester, tf string, timeout time.Duration, resources []config.Resource) error {
func executeSingleTestFile(ctx context.Context, t *Tester, tf string, timeout time.Duration, resources []config.Resource) error {
if t.options.UseLibraryMode {
return executeSingleTestFileLibraryMode(t, tf, timeout, resources)
return executeSingleTestFileLibraryMode(ctx, t, tf, timeout, resources)
}
return executeSingleTestFileCLIMode(t, tf, timeout, resources)
return executeSingleTestFileCLIMode(ctx, t, tf, timeout, resources)
}

func executeSingleTestFileLibraryMode(t *Tester, tf string, timeout time.Duration, resources []config.Resource) error {
func executeSingleTestFileLibraryMode(ctx context.Context, t *Tester, tf string, timeout time.Duration, resources []config.Resource) error {
// Explicitly Set Controller Logger
// because of log.SetLogger(...) was never called;
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
Expand Down Expand Up @@ -170,7 +170,7 @@ func executeSingleTestFileLibraryMode(t *Tester, tf string, timeout time.Duratio

go logCollectorLibraryMode(done, ticker, &mutex, resources)

ctx, cancel := context.WithTimeout(context.Background(), timeout)
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

if err := runnerflags.SetupFlags(configuration.Spec); err != nil {
Expand All @@ -193,12 +193,12 @@ func executeSingleTestFileLibraryMode(t *Tester, tf string, timeout time.Duratio
return nil
}

func executeSingleTestFileCLIMode(t *Tester, tf string, timeout time.Duration, resources []config.Resource) error {
func executeSingleTestFileCLIMode(ctx context.Context, t *Tester, tf string, timeout time.Duration, resources []config.Resource) error {
chainsawCommand := fmt.Sprintf(`"${CHAINSAW}" test --test-dir %s --test-file %s --skip-delete --parallel 1 2>&1`,
filepath.Clean(filepath.Join(t.options.Directory, caseDirectory)),
filepath.Clean(tf))

ctx, cancel := context.WithTimeout(context.Background(), timeout)
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

cmd := exec.CommandContext(ctx, "bash", "-c", chainsawCommand) // #nosec G204
Expand Down
13 changes: 11 additions & 2 deletions pkg/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package pkg

import (
"context"
"log"
"os"

Expand All @@ -15,8 +16,16 @@ import (
"github.com/crossplane/uptest/v2/internal/config"
)

// RunTest runs the specified automated test
// RunTest runs the specified automated test.
//
// Deprecated: Use RunTestContext.
func RunTest(o *config.AutomatedTest) error {
return RunTestContext(context.Background(), o)
}

// RunTestContext runs the specified automated test, respecting context
// cancellation.
func RunTestContext(ctx context.Context, o *config.AutomatedTest) error {
if !o.RenderOnly {
defer func() {
if err := os.RemoveAll(o.Directory); err != nil {
Expand All @@ -32,7 +41,7 @@ func RunTest(o *config.AutomatedTest) error {
}

// Prepare assert environment and run tests
if err := internal.NewTester(manifests, o).ExecuteTests(); err != nil {
if err := internal.NewTester(manifests, o).ExecuteTests(ctx); err != nil {
return errors.Wrap(err, "cannot execute tests")
}

Expand Down
Loading