integrationtest

package
v1.4.24 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package integrationtest makes it easier to run integration tests against compiled binary.

Index

Examples

Constants

View Source
const (
	BinDir                  = "target/testbin/"
	IntegrationTestCoverDir = "./target/tests/cover/int/"
)

Default file locations.

Variables

This section is empty.

Functions

func ListPackages

func ListPackages(base, target string) ([]string, error)

Types

type BinHandler

type BinHandler struct {
	// contains filtered or unexported fields
}

func NewBinHandler added in v1.4.13

func NewBinHandler(opts ...BinOpt) *BinHandler

func (*BinHandler) Build

func (bh *BinHandler) Build() error

func (*BinHandler) Init added in v1.4.13

func (bh *BinHandler) Init() error

Init applies all options to bin handler.

func (*BinHandler) Run added in v1.4.23

func (bh *BinHandler) Run() error

func (*BinHandler) Start

func (bh *BinHandler) Start() error

func (*BinHandler) Stop

func (bh *BinHandler) Stop() error

type BinOpt added in v1.4.13

type BinOpt func(*BinHandler) error

BinOpt is option type for BinHandler.

func BinOptBase added in v1.4.13

func BinOptBase(base string) BinOpt

BinOptBase sets execution base path BinHandler. BinOptBase should be usually the first option when passing options to NewIntegrationTestRunner.

func BinOptBuildArgs added in v1.4.13

func BinOptBuildArgs(args ...string) BinOpt

BinOptBuildArgs adds args to build arguments for test binary.

func BinOptBuildEnv added in v1.4.13

func BinOptBuildEnv(env ...string) BinOpt

BinOptBuildEnv adds env to test binary's build env.

func BinOptCoverDir added in v1.4.13

func BinOptCoverDir(coverDir string) BinOpt

BinOptCoverDir sets coverage directory for test binary.

func BinOptOutput added in v1.4.13

func BinOptOutput(output string) BinOpt

BinOptOutput sets output for compilation target.

func BinOptRunArgs added in v1.4.13

func BinOptRunArgs(args ...string) BinOpt

BinOptRunArgs adds args to run arguments for test binary.

func BinOptRunEnv added in v1.4.13

func BinOptRunEnv(env ...string) BinOpt

BinOptRunEnv adds env to test binary's run env.

func BinOptTarget added in v1.4.13

func BinOptTarget(target string) BinOpt

BinOptTarget sets path to compilation target.

type Compose

type Compose struct {
	Composer
}

type ComposeOpt

type ComposeOpt func(*composeHandler)

ComposeOpt is option type for OptCompose.

func ComposeComposer

func ComposeComposer(compose *Compose) ComposeOpt

ComposeComposer allows getting access to compose instance which is usable after Init() is called.

Example
package main

import (
	"context"

	it "github.com/elisasre/go-common/integrationtest"
)

func main() {
	c := &it.Compose{}

	itr := it.NewIntegrationTestRunner(
		it.OptCompose("docker-compose.yaml", it.ComposeComposer(c)),
	)

	if err := itr.Init(); err != nil {
		return
	}

	dbContainer, err := c.ServiceContainer(context.Background(), "postgres")
	if err != nil {
		return
	}

	_, _, err = dbContainer.Exec(
		context.Background(),
		[]string{"psql", "-U", "demo", "-d", "demo", "-c", "'SELECT * FROM demo_table'"},
	)
	if err != nil {
		return
	}
}
Output:

func ComposeDownOptions

func ComposeDownOptions(opts ...tc.StackDownOption) ComposeOpt

ComposeDownOptions set options for compose.Down().

func ComposeEnv

func ComposeEnv(env map[string]string) ComposeOpt

ComposeEnv set environment variables for compose.

func ComposeOsEnv

func ComposeOsEnv() ComposeOpt

ComposeOsEnv passes environment from OS to compose.

func ComposeUpOptions

func ComposeUpOptions(opts ...tc.StackUpOption) ComposeOpt

ComposeUpOptions set options for compose.Up().

func ComposeWaitForService

func ComposeWaitForService(service string, strategy wait.Strategy) ComposeOpt

ComposeWaitForService makes compose up wait for specific service with given strategy.

type Composer

type Composer interface {
	ServiceContainer(ctx context.Context, svcName string) (*testcontainers.DockerContainer, error)
	Services() []string
	Down(ctx context.Context, opts ...tc.StackDownOption) error
	Up(ctx context.Context, opts ...tc.StackUpOption) (err error)
	WaitForService(s string, strategy wait.Strategy) tc.ComposeStack
	WithEnv(m map[string]string) tc.ComposeStack
	WithOsEnv() tc.ComposeStack
}

type IntegrationTestRunner

type IntegrationTestRunner struct {
	// contains filtered or unexported fields
}

func NewIntegrationTestRunner

func NewIntegrationTestRunner(opts ...Opt) *IntegrationTestRunner

NewIntegrationTestRunner creates new IntegrationTestRunner with given options.

func (*IntegrationTestRunner) Init

func (itr *IntegrationTestRunner) Init() error

Init applies all options to test runner.

func (*IntegrationTestRunner) InitAndRun

func (itr *IntegrationTestRunner) InitAndRun() error

InitAndRun combines Init() and Run() calls for convenience.

func (*IntegrationTestRunner) Run

func (itr *IntegrationTestRunner) Run() error

Run starts test workflow.

Workflow steps: 1. Run all preSetup steps 2. Build test binary 3. Start test binary in background 4. Execute test function 5. Cleanup resources

The contents of above steps will depend on given options.

type Opt

type Opt func(*IntegrationTestRunner) error

Opt is option type for IntegrationTestRunner.

func OptBase

func OptBase(base string) Opt

OptBase sets execution base path IntegrationTestRunner. OptBase should be usually the first option when passing options to NewIntegrationTestRunner.

func OptBuildArgs

func OptBuildArgs(args ...string) Opt

OptBuildArgs adds args to build arguments for test binary.

func OptBuildEnv

func OptBuildEnv(env ...string) Opt

OptBuildEnv adds env to test binary's build env.

func OptCompose

func OptCompose(composeFile string, opts ...ComposeOpt) Opt

OptCompose adds docker compose stack as pre condition for tests to run.

func OptCoverDir

func OptCoverDir(coverDir string) Opt

OptCoverDir sets coverage directory for test binary.

func OptFuncRunner added in v1.4.3

func OptFuncRunner(fn func() error) Opt

OptFuncRunner allows wrapping any function as testRunner function. This can be useful injecting code between ready and test code. Example TestMain:

func TestMain(m *testing.M) {
	itr := it.NewIntegrationTestRunner(
		it.OptBase("../"),
		it.OptTarget("./cmd/app"),
		it.OptCompose("docker-compose.yaml"),
		it.OptWaitHTTPReady("http://127.0.0.1:8080", time.Second*10),
		it.OptFuncRunner(func() error {
			if err := initStuff(); err != nil {
				return err
			}
			if code := m.Run(); code != 0 {
				return errors.New("tests have failed")
			}
			return nil
		}),
	)
	if err := itr.InitAndRun(); err != nil {
		log.Fatal(err)
	}
}

Before using this pattern be sure to read how TestMain should be used!

func OptOutput

func OptOutput(output string) Opt

OptOutput sets output for compilation target.

func OptPreHandler added in v1.4.2

func OptPreHandler(h PreHandler) Opt

OptPreHandler adds handler as pre condition for tests to run.

func OptPreHandlerFn added in v1.4.2

func OptPreHandlerFn(run, stop func() error) Opt

OptPreHandlerFn wraps functions as PreHandler and set's it as a pre condition for tests to run.

func OptRunArgs

func OptRunArgs(args ...string) Opt

OptRunArgs adds args to run arguments for test binary.

func OptRunEnv

func OptRunEnv(env ...string) Opt

OptRunEnv adds env to test binary's run env.

func OptTarget

func OptTarget(target string) Opt

OptTarget sets path to compilation target.

func OptTestFunc

func OptTestFunc(t *testing.T, fn func(*testing.T)) Opt

OptTestFunc allows wrapping testing.T into IntegrationTestRunner. Example TestApp:

func TestApp(t *testing.T) {
	itr := it.NewIntegrationTestRunner(
		it.OptBase("../"),
		it.OptTarget("./cmd/app"),
		it.OptCompose("docker-compose.yaml"),
		it.OptWaitHTTPReady("http://127.0.0.1:8080", time.Second*10),
		it.OptTestFunc(t, testApp),
	)
	if err := itr.InitAndRun(); err != nil {
		t.Fatal(err)
	}
}

func testApp(t *testing.T) {
	// run tests here
}

This pattern allows setting the env for each test separately.

func OptTestMain

func OptTestMain(m *testing.M) Opt

OptTestMain allows wrapping testing.M into IntegrationTestRunner. Example TestMain:

func TestMain(m *testing.M) {
	itr := it.NewIntegrationTestRunner(
		it.OptBase("../"),
		it.OptTarget("./cmd/app"),
		it.OptCompose("docker-compose.yaml"),
		it.OptWaitHTTPReady("http://127.0.0.1:8080", time.Second*10),
		it.OptTestMain(m),
	)
	if err := itr.InitAndRun(); err != nil {
		log.Fatal(err)
	}
}

Before using this pattern be sure to read how TestMain should be used!

func OptWaitHTTPReady

func OptWaitHTTPReady(url string, timeout time.Duration) Opt

OptWaitHTTPReady expects 200 OK from given url before tests can be started.

type PreHandler

type PreHandler interface {
	Run() error
	Stop() error
}

PreHandler is used for setting up pre conditions for test. Run() allows to perform setup before tests and Stop() the cleanup after the tests.

type PreHandlerFn added in v1.4.2

type PreHandlerFn struct {
	RunFn, StopFn func() error
}

func (*PreHandlerFn) Run added in v1.4.2

func (h *PreHandlerFn) Run() error

func (*PreHandlerFn) Stop added in v1.4.2

func (h *PreHandlerFn) Stop() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL