testcontainernetwork

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: May 15, 2024 License: GPL-3.0 Imports: 15 Imported by: 0

README

Test Container Network for Go

A set of helper types and methods to abstract out boilerplate code for testcontainers in Go. Currently supported are:

  • LambdaDockerContainer - a container that runs a Lambda function
  • WiremockDockerContainer - a container that runs a Wiremock server
  • SqsDockerContainer - a container that runs an SQS server

Testing

make test

Usage

package main

import "github.com/mikebharris/testcontainernetwork"
import "time"
import "fmt"

func main() {
	s.lambdaContainer = LambdaDockerContainer{
		Config: LambdaDockerContainerConfig{
			Executable:  "test-assets/lambda/main",
			Environment: map[string]string{"API_ENDPOINT": fmt.Sprintf("http://%s:8080", wiremockHostname)},
			Hostname:    "lambda",
		},
	}
	s.wiremockContainer = WiremockDockerContainer{
		Config: WiremockDockerContainerConfig{
			JsonMappings: "test-assets/wiremock/mappings",
			Hostname:     wiremockHostname,
		},
	}
	s.networkOfDockerContainers =
		NetworkOfDockerContainers{}.
			WithDockerContainer(&s.lambdaContainer).
			WithDockerContainer(&s.wiremockContainer).
			StartWithDelay(2 * time.Second)
}

Clients

There is a client for some container types that provides a simple way to interact with the container. For example, the SQS client provides methods to receive messages from the SQS server:

package main

import "github.com/mikebharris/testcontainernetwork/clients"

func main() {
	sqsClient, _ := clients.SqsClient{}.New(s.sqsContainer.MappedPort())
	if err != nil {
		log.Fatalf("creating SQS client: %v", err)
	}

	messagesOnQueue, err := sqsClient.GetMessagesFrom(sqsQueueName)
	if err != nil {
		log.Fatalf("getting messages from SQS: %v", err)
	}
}

Implementing a new container

The container should promote the values and methods of GenericDockerContainer and implement the StartableDockerContainer interface, which gets you the internal reference to the testcontainer and the port for the service, as well as the MappedPort() method. For example:

type MyDockerContainer struct {
GenericDockerContainer
Config MyDockerContainerConfig
}

func (c *MyDockerContainer) Stop(ctx context.Context) error {
....
}

func (c *LambdaDockerContainer) StartUsing(ctx context.Context, dockerNetwork *testcontainers.DockerNetwork) error {
....
}

I recommend that container-specific configuration parameters be assigned as a struct to the Config field, thus keeping the container struct itself clean and simple and the same across different container implementations.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DockerContainer added in v0.2.1

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

func (*DockerContainer) MappedPort added in v0.2.1

func (c *DockerContainer) MappedPort() int

func (*DockerContainer) Stop added in v0.2.1

func (c *DockerContainer) Stop(ctx context.Context) error

type LambdaDockerContainer

type LambdaDockerContainer struct {
	DockerContainer
	Config LambdaDockerContainerConfig
}

func (*LambdaDockerContainer) InvocationUrl added in v0.0.3

func (c *LambdaDockerContainer) InvocationUrl() string

func (*LambdaDockerContainer) Log

func (*LambdaDockerContainer) StartUsing

func (c *LambdaDockerContainer) StartUsing(ctx context.Context, dockerNetwork *testcontainers.DockerNetwork) error

type LambdaDockerContainerConfig

type LambdaDockerContainerConfig struct {
	Executable  string
	Environment map[string]string
	Hostname    string
}

type NetworkOfDockerContainers

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

func (*NetworkOfDockerContainers) StartWithDelay

func (n *NetworkOfDockerContainers) StartWithDelay(delay time.Duration) error

StartWithDelay has intentional mixed use of pointer and value receivers because this method has side effects and thus this fits better with a functional programming paradigm

func (*NetworkOfDockerContainers) Stop

func (n *NetworkOfDockerContainers) Stop() error

func (NetworkOfDockerContainers) WithDockerContainer

func (n NetworkOfDockerContainers) WithDockerContainer(dockerContainer StartableDockerContainer) NetworkOfDockerContainers

type SnsDockerContainer added in v0.3.0

type SnsDockerContainer struct {
	DockerContainer
	Config SnsDockerContainerConfig
}

func (*SnsDockerContainer) GetMessage added in v0.3.0

func (c *SnsDockerContainer) GetMessage() (string, error)

func (*SnsDockerContainer) StartUsing added in v0.3.0

func (c *SnsDockerContainer) StartUsing(ctx context.Context, dockerNetwork *testcontainers.DockerNetwork) error

type SnsDockerContainerConfig added in v0.3.0

type SnsDockerContainerConfig struct {
	Hostname   string
	ConfigFile string
}

type SqsDockerContainer added in v0.1.1

type SqsDockerContainer struct {
	DockerContainer
	Config SqsDockerContainerConfig
}

func (*SqsDockerContainer) StartUsing added in v0.1.1

func (c *SqsDockerContainer) StartUsing(ctx context.Context, dockerNetwork *testcontainers.DockerNetwork) error

type SqsDockerContainerConfig added in v0.1.1

type SqsDockerContainerConfig struct {
	Hostname       string
	ConfigFilePath string
}

type StartableDockerContainer

type StartableDockerContainer interface {
	MappedPort() int
	StartUsing(ctx context.Context, dockerNetwork *testcontainers.DockerNetwork) error
	Stop(ctx context.Context) error
}

type WiremockAdminMeta added in v0.0.5

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

type WiremockAdminRequest added in v0.0.5

type WiremockAdminRequest struct {
	Id      string `json:"id"`
	Request struct {
		Url         string `json:"url"`
		AbsoluteUrl string `json:"absoluteUrl"`
		Method      string `json:"method"`
		ClientIp    string `json:"clientIp"`
		Headers     struct {
			Connection string `json:"Connection"`
			UserAgent  string `json:"User-Agent"`
			Host       string `json:"Host"`
		} `json:"headers"`
		Cookies struct {
		} `json:"cookies"`
		BrowserProxyRequest bool      `json:"browserProxyRequest"`
		LoggedDate          int64     `json:"loggedDate"`
		BodyAsBase64        string    `json:"bodyAsBase64"`
		Body                string    `json:"body"`
		LoggedDateString    time.Time `json:"loggedDateString"`
	} `json:"request"`
	ResponseDefinition struct {
		Status int    `json:"status"`
		Body   string `json:"body"`
	} `json:"responseDefinition"`
	WasMatched bool `json:"wasMatched"`
}

type WiremockAdminStatus added in v0.0.5

type WiremockAdminStatus struct {
	Requests               []WiremockAdminRequest `json:"requests"`
	Meta                   WiremockAdminMeta      `json:"meta"`
	RequestJournalDisabled bool                   `json:"requestJournalDisabled"`
}

type WiremockDockerContainer

type WiremockDockerContainer struct {
	DockerContainer
	Config WiremockDockerContainerConfig
}

func (*WiremockDockerContainer) GetAdminStatus added in v0.0.4

func (c *WiremockDockerContainer) GetAdminStatus() WiremockAdminStatus

func (*WiremockDockerContainer) StartUsing

func (c *WiremockDockerContainer) StartUsing(ctx context.Context, dockerNetwork *testcontainers.DockerNetwork) error

type WiremockDockerContainerConfig

type WiremockDockerContainerConfig struct {
	JsonMappings string
	Hostname     string
}

Directories

Path Synopsis
test-assets

Jump to

Keyboard shortcuts

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