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 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 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 ¶ 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) 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
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
BinOptBuildArgs adds args to build arguments for test binary.
func BinOptBuildEnv ¶ added in v1.4.13
BinOptBuildEnv adds env to test binary's build env.
func BinOptCoverDir ¶ added in v1.4.13
BinOptCoverDir sets coverage directory for test binary.
func BinOptOutput ¶ added in v1.4.13
BinOptOutput sets output for compilation target.
func BinOptRunArgs ¶ added in v1.4.13
BinOptRunArgs adds args to run arguments for test binary.
func BinOptRunEnv ¶ added in v1.4.13
BinOptRunEnv adds env to test binary's run env.
func BinOptTarget ¶ added in v1.4.13
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/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 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 ¶ added in v1.4.3
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 ¶ 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
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:
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!
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 ¶ 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