Documentation
¶
Overview ¶
Example (Container) ¶
This example demonstrates using the low-level Container API: creating a container from a custom image, configuring environment variables and port bindings, waiting for a log line, and making gRPC calls.
package main
import (
"context"
"fmt"
"time"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/teran/echo-grpc-server/presenter/proto"
docker "github.com/teran/go-docker-testsuite"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel()
c, err := docker.NewContainer(
"echo-server",
"ghcr.io/teran/echo-grpc-server:latest",
nil,
docker.NewEnvironment().
StringVar("ADDR", ":5555").
LogLevelVar("LOG_LEVEL", logrus.TraceLevel),
docker.NewPortBindings().
PortDNAT(docker.ProtoTCP, 5555),
)
if err != nil {
fmt.Printf("error creating container: %v\n", err)
return
}
defer func() { _ = c.Close(ctx) }()
if err := c.Run(ctx); err != nil {
fmt.Printf("error running container: %v\n", err)
return
}
if err := c.AwaitOutput(ctx, docker.NewSubstringMatcher("running GRPC echo server")); err != nil {
fmt.Printf("error waiting for server: %v\n", err)
return
}
fmt.Println("server is ready")
hp, err := c.URL(docker.ProtoTCP, 5555)
if err != nil {
fmt.Printf("error getting URL: %v\n", err)
return
}
conn, err := grpc.NewClient(hp.String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
fmt.Printf("error dialing: %v\n", err)
return
}
defer func() { _ = conn.Close() }()
cli := proto.NewEchoServiceClient(conn)
resp, err := cli.Echo(ctx, &proto.EchoRequest{Message: "Hello!"})
if err != nil {
fmt.Printf("error calling Echo: %v\n", err)
return
}
fmt.Printf("echo response: %s\n", resp.GetMessage())
}
Output:
Example (Group) ¶
This example demonstrates the Group API: running two containers on the same internal Docker network so they can reach each other by container name.
package main
import (
"context"
"fmt"
"time"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/teran/echo-grpc-server/presenter/proto"
docker "github.com/teran/go-docker-testsuite"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
awaitRunFn := func(ctx context.Context, ht docker.HookType, c docker.Container) error {
if ht == docker.HookTypeAfterRun {
return c.AwaitOutput(ctx, docker.NewSubstringMatcher("running GRPC echo server"))
}
return nil
}
svr, err := docker.NewContainer(
"my-server",
"ghcr.io/teran/echo-grpc-server:latest",
nil,
docker.NewEnvironment().
StringVar("ADDR", ":5555").
LogLevelVar("LOG_LEVEL", logrus.TraceLevel),
docker.NewPortBindings().
PortDNAT(docker.ProtoTCP, 5555),
)
if err != nil {
fmt.Printf("error creating server container: %v\n", err)
return
}
client, err := docker.NewContainer(
"my-client",
"ghcr.io/teran/echo-grpc-server:latest",
nil,
docker.NewEnvironment().
StringVar("ADDR", ":5555").
LogLevelVar("LOG_LEVEL", logrus.TraceLevel),
docker.NewPortBindings().
PortDNAT(docker.ProtoTCP, 5555),
)
if err != nil {
fmt.Printf("error creating client container: %v\n", err)
return
}
g, err := docker.NewGroup("my-group",
docker.NewApplication(svr, awaitRunFn),
docker.NewApplication(client, awaitRunFn),
)
if err != nil {
fmt.Printf("error creating group: %v\n", err)
return
}
defer func() { _ = g.Close(ctx) }()
if err := g.Run(ctx); err != nil {
fmt.Printf("error running group: %v\n", err)
return
}
fmt.Println("group started")
// Connect to client and call server by its DNS name.
hp, err := client.URL(docker.ProtoTCP, 5555)
if err != nil {
fmt.Printf("error getting client URL: %v\n", err)
return
}
conn, err := grpc.NewClient(hp.String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
fmt.Printf("error dialing: %v\n", err)
return
}
defer func() { _ = conn.Close() }()
cli := proto.NewRemoteEchoServiceClient(conn)
resp, err := cli.RemoteEcho(ctx, &proto.RemoteEchoRequest{
Remote: "my-server:5555",
Message: "Hello across containers!",
})
if err != nil {
fmt.Printf("error calling RemoteEcho: %v\n", err)
return
}
fmt.Printf("remote echo response: %s\n", resp.GetMessage())
}
Output:
Index ¶
- Variables
- func DockerIP() (string, error)
- func NewHostConfig(pb *PortBindings, opts ...ContainerOption) (*dockerContainer.HostConfig, error)
- func OneToOneRandomPort(proto Protocol, srcPort uint16) (string, uint16, []string, error)
- func RandomPort(proto Protocol, dstPort uint16) (string, uint16, []string, error)
- type Application
- type Binding
- type Container
- type ContainerID
- type ContainerInfo
- type ContainerOption
- type Environment
- func (e Environment) BoolVar(name string, value bool) Environment
- func (e Environment) Eval(c ContainerInfo) (es []string)
- func (e Environment) Int8Var(name string, value int8) Environment
- func (e Environment) Int16Var(name string, value int16) Environment
- func (e Environment) Int32Var(name string, value int32) Environment
- func (e Environment) Int64Var(name string, value int64) Environment
- func (e Environment) IntVar(name string, value int) Environment
- func (e Environment) LogLevelVar(name string, l log.Level) Environment
- func (e Environment) StringVar(name, value string) Environment
- func (e Environment) Uint8Var(name string, value uint8) Environment
- func (e Environment) Uint16Var(name string, value uint16) Environment
- func (e Environment) Uint32Var(name string, value uint32) Environment
- func (e Environment) Uint64Var(name string, value uint64) Environment
- func (e Environment) UintVar(name string, value uint) Environment
- func (e Environment) Var(name string, vfn func(c ContainerInfo) string) Environment
- type Group
- type Hook
- type HookType
- type HostConfigSpec
- type HostPort
- type Matcher
- type NetworkID
- type PortAllocator
- type PortBindings
- type Protocol
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrPortNotMapped = errors.New("port not mapped") ErrDockerHostIPIsNotResolved = errors.New("docker host IP address cannot be resolved") )
Functions ¶
func NewHostConfig ¶
func NewHostConfig(pb *PortBindings, opts ...ContainerOption) (*dockerContainer.HostConfig, error)
NewHostConfig creates new HostConfig instance
func OneToOneRandomPort ¶ added in v1.1.0
Types ¶
type Application ¶
type Application struct {
// contains filtered or unexported fields
}
func NewApplication ¶
func NewApplication(c Container, hooks ...Hook) *Application
type Container ¶
type Container interface {
AwaitOutput(ctx context.Context, m Matcher) error
GetOutput(ctx context.Context, m ...Matcher) ([]string, error)
Close(ctx context.Context) error
ID() ContainerID
Name() string
NetworkAttach(networkID string) error
Ping(ctx context.Context) error
Run(ctx context.Context) error
URL(proto Protocol, port uint16) (*HostPort, error)
}
Container exposes interface to control the container runtime
func NewContainer ¶
func NewContainer(name, image string, cmd []string, environment Environment, ports *PortBindings, opts ...ContainerOption) (Container, error)
New creates new container instance from remote docker image
func NewContainerWithClient ¶
func NewContainerWithClient(cli *client.Client, name, image string, cmd []string, env Environment, ports *PortBindings, opts ...ContainerOption) (Container, error)
NewContainerWithClient creates new container from remote docker image and allows to pass custom docker.Client instance
type ContainerID ¶
type ContainerID = string
type ContainerInfo ¶ added in v1.1.0
type ContainerOption ¶ added in v1.3.0
type ContainerOption func(*dockerContainer.HostConfig)
ContainerOption modifies the docker HostConfig before container creation.
func WithBinds ¶ added in v1.3.0
func WithBinds(binds ...string) ContainerOption
WithBinds adds volume bind mounts (host:container[:mode]).
func WithPrivileged ¶ added in v1.3.0
func WithPrivileged() ContainerOption
WithPrivileged grants the container elevated privileges.
func WithTmpfs ¶ added in v1.3.0
func WithTmpfs(m map[string]string) ContainerOption
WithTmpfs mounts tmpfs filesystems at the given paths.
type Environment ¶
type Environment map[string]func(c ContainerInfo) string
Environment represents the container environment passed into runtime
func NewEnvironment ¶
func NewEnvironment() Environment
NewEnvironment creates new Environment instance
func (Environment) BoolVar ¶
func (e Environment) BoolVar(name string, value bool) Environment
BoolVar sets bool var to the environment
func (Environment) Eval ¶ added in v1.1.0
func (e Environment) Eval(c ContainerInfo) (es []string)
func (Environment) Int8Var ¶
func (e Environment) Int8Var(name string, value int8) Environment
Int8Var sets int8 var to the environment
func (Environment) Int16Var ¶
func (e Environment) Int16Var(name string, value int16) Environment
Int16Var sets int16 var to the environment
func (Environment) Int32Var ¶
func (e Environment) Int32Var(name string, value int32) Environment
Int32Var sets int32 var to the environment
func (Environment) Int64Var ¶
func (e Environment) Int64Var(name string, value int64) Environment
Int64Var sets int64 var to the environment
func (Environment) IntVar ¶
func (e Environment) IntVar(name string, value int) Environment
IntVar sets int var to the environment
func (Environment) LogLevelVar ¶
func (e Environment) LogLevelVar(name string, l log.Level) Environment
LogLevelVar sets logrus.Level var to the environment
func (Environment) StringVar ¶
func (e Environment) StringVar(name, value string) Environment
StringVar sets string var to the environment
func (Environment) Uint8Var ¶
func (e Environment) Uint8Var(name string, value uint8) Environment
Uint8Var sets uint8 var to the environment
func (Environment) Uint16Var ¶
func (e Environment) Uint16Var(name string, value uint16) Environment
Uint16Var sets uint16 var to the environment
func (Environment) Uint32Var ¶
func (e Environment) Uint32Var(name string, value uint32) Environment
Uint32Var sets uint32 var to the environment
func (Environment) Uint64Var ¶
func (e Environment) Uint64Var(name string, value uint64) Environment
Uint64Var sets uint64 var to the environment
func (Environment) UintVar ¶
func (e Environment) UintVar(name string, value uint) Environment
UintVar sets uint var to the environment
func (Environment) Var ¶ added in v1.1.0
func (e Environment) Var(name string, vfn func(c ContainerInfo) string) Environment
Var allows to set custom function to generate environment variable
type Group ¶
func NewGroupWithClient ¶
type HostConfigSpec ¶
HostConfigSpec is just a wrapper structure to pass host configuration to the container
type Matcher ¶
Matcher allows to create any kind of matcher for container outputs
func NewExactMatcher ¶
NewExactMatcher represents exact matcher i.e. the output should be exactly matched (except space chars around the word)
func NewRegexpMatcher ¶
NewRegexpMatcher returns a matcher that succeeds when the line matches the compiled regular expression.
func NewSubstringMatcher ¶
NewSubstringMatcher represents partial matcher
type PortAllocator ¶ added in v1.1.0
type PortBindings ¶
type PortBindings struct {
// contains filtered or unexported fields
}
PortBindings is a full mapping of internal & external docker container ports
func NewDirectPortBinding ¶ added in v1.1.0
func NewDirectPortBinding() *PortBindings
func NewPortBindings ¶
func NewPortBindings() *PortBindings
NewPortBindings creates new PortBindings instance
func NewPortBindingsWithPortAllocator ¶ added in v1.1.0
func NewPortBindingsWithPortAllocator(allocator PortAllocator) *PortBindings
NewPortBindingsWithTCPPortAllocator creates new PortBinding instance and allows to pass custom port allocation function
func (*PortBindings) PortDNAT ¶
func (pb *PortBindings) PortDNAT(proto Protocol, port uint16) *PortBindings
PortDNAT adds new port to be exposed from the container
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
applications
|
|
|
k3s
Package k3s provides a K3s container for integration testing.
|
Package k3s provides a K3s container for integration testing. |
|
k3s/versions
Package versions provides a shared test suite for versioned K3s images.
|
Package versions provides a shared test suite for versioned K3s images. |
|
internal
|
|
|
tools
|
|
|
cmd/split_test_groups
command
split_test_groups discovers Go test packages and groups them into balanced CI matrix groups by application type.
|
split_test_groups discovers Go test packages and groups them into balanced CI matrix groups by application type. |