Documentation
¶
Overview ¶
Package integrationtest makes it easier to run integration tests against compiled binary.
Index ¶
- Constants
- func ListPackages(base, target string) ([]string, error)
- type BinHandler
- type BinOpt
- func BinOptBase(base string) BinOpt
- func BinOptBuildArgs(args ...string) BinOpt
- func BinOptBuildEnv(env ...string) BinOpt
- func BinOptCoverDir(coverDir string) BinOpt
- func BinOptOutput(output string) BinOpt
- func BinOptRunArgs(args ...string) BinOpt
- func BinOptRunEnv(env ...string) BinOpt
- func BinOptSetRunArgs(args ...string) BinOpt
- func BinOptSetRunEnv(env ...string) BinOpt
- func BinOptTarget(target string) BinOpt
- type Compose
- type ComposeOpt
- func ComposeComposer(compose *Compose) ComposeOpt
- func ComposeDownOptions(opts ...tc.StackDownOption) ComposeOpt
- func ComposeEnv(env map[string]string) ComposeOpt
- func ComposeOsEnv() ComposeOpt
- func ComposeUpOptions(opts ...tc.StackUpOption) ComposeOpt
- func ComposeWaitForService(service string, strategy wait.Strategy) ComposeOpt
- type Composer
- type IntegrationTestRunner
- type Opt
- func OptBase(base string) Opt
- func OptBinHandler(bh *BinHandler) Opt
- func OptBuildArgs(args ...string) Opt
- func OptBuildEnv(env ...string) Opt
- func OptCompose(composeFile string, opts ...ComposeOpt) Opt
- func OptCoverDir(coverDir string) Opt
- func OptFuncRunner(fn func() error) Opt
- func OptOutput(output string) Opt
- func OptPreHandler(h PreHandler) Opt
- func OptPreHandlerFn(run, stop func() error) Opt
- func OptRunArgs(args ...string) Opt
- func OptRunEnv(env ...string) Opt
- func OptTarget(target string) Opt
- func OptTestFunc(t *testing.T, fn func(*testing.T)) Opt
- func OptTestMain(m *testing.M) Opt
- func OptWaitHTTPReady(url string, timeout time.Duration) Opt
- type PreHandler
- type PreHandlerFn
Examples ¶
Constants ¶
const ( BinDir = "target/testbin/" IntegrationTestCoverDir = "./target/tests/cover/int/" )
Default file locations.
Variables ¶
This section is empty.
Functions ¶
func ListPackages ¶
Types ¶
type BinHandler ¶
type BinHandler struct {
// contains filtered or unexported fields
}
func NewBinHandler ¶
func NewBinHandler(opts ...BinOpt) *BinHandler
func (*BinHandler) AddOpts ¶ added in v2.1.10
func (bh *BinHandler) AddOpts(opts ...BinOpt)
AddOpts adds options to bin handler.
func (*BinHandler) Build ¶
func (bh *BinHandler) Build() error
func (BinHandler) Copy ¶ added in v2.1.10
func (bh BinHandler) Copy() *BinHandler
func (*BinHandler) Init ¶
func (bh *BinHandler) Init() error
Init applies all options to bin handler.
func (*BinHandler) Run ¶
func (bh *BinHandler) Run() error
func (*BinHandler) Start ¶
func (bh *BinHandler) Start() error
func (*BinHandler) Stop ¶
func (bh *BinHandler) Stop() error
type BinOpt ¶
type BinOpt func(*BinHandler) error
BinOpt is option type for BinHandler.
func BinOptBase ¶
BinOptBase sets execution base path BinHandler. BinOptBase should be usually the first option when passing options to NewIntegrationTestRunner.
func BinOptBuildArgs ¶
BinOptBuildArgs adds args to build arguments for test binary.
func BinOptBuildEnv ¶
BinOptBuildEnv adds env to test binary's build env.
func BinOptCoverDir ¶
BinOptCoverDir sets coverage directory for test binary.
func BinOptOutput ¶
BinOptOutput sets output for compilation target.
func BinOptRunArgs ¶
BinOptRunArgs adds args to run arguments for test binary.
func BinOptRunEnv ¶
BinOptRunEnv adds env to test binary's run env.
func BinOptSetRunArgs ¶ added in v2.1.10
BinOptSetRunArgs sets run arguments for test binary.
func BinOptSetRunEnv ¶ added in v2.1.10
BinOptSetRunEnv sets test binary's run env.
func BinOptTarget ¶
BinOptTarget sets path to compilation target.
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/v2/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 ¶
OptBase sets execution base path IntegrationTestRunner. OptBase should be usually the first option when passing options to NewIntegrationTestRunner.
func OptBinHandler ¶ added in v2.1.10
func OptBinHandler(bh *BinHandler) Opt
OptBinHandler allows setting BinHandler. This can be useful when you want to reuse BinHandler between multiple IntegrationTestRunners.
func OptBuildArgs ¶
OptBuildArgs adds args to build arguments for test binary.
func OptBuildEnv ¶
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 ¶
OptCoverDir sets coverage directory for test binary.
func OptFuncRunner ¶
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 OptPreHandler ¶
func OptPreHandler(h PreHandler) Opt
OptPreHandler adds handler as pre condition for tests to run.
func OptPreHandlerFn ¶
OptPreHandlerFn wraps functions as PreHandler and set's it as a pre condition for tests to run.
func OptRunArgs ¶
OptRunArgs adds args to run arguments for test binary.
func OptTestFunc ¶
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 ¶
OptTestMain allows wrapping testing.M into IntegrationTestRunner. Example TestMain:
import ( "io" "log" "os" "time" it "github.com/elisasre/go-common/v2/integrationtest" tc "github.com/testcontainers/testcontainers-go/modules/compose" ) func TestMain(m *testing.M) { os.Setenv("GOFLAGS", "-tags=integration") itr := it.NewIntegrationTestRunner( it.OptBase("../"), it.OptTarget("./cmd/app"), it.OptCoverDir(it.IntegrationTestCoverDir), it.OptCompose("docker-compose.yaml", it.ComposeUpOptions(tc.Wait(true))), it.OptWaitHTTPReady("http://127.0.0.1:8080/healthz", 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!
type PreHandler ¶
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 ¶
type PreHandlerFn struct {
RunFn, StopFn func() error
}
func (*PreHandlerFn) Run ¶
func (h *PreHandlerFn) Run() error
func (*PreHandlerFn) Stop ¶
func (h *PreHandlerFn) Stop() error