Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dtest] Run tests on an existing cluster launched with docker compose #3067

Merged
merged 5 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions scripts/dtest/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: "3.5"
services:
dbnode01:
networks:
- dtest
image: m3dbnode:dev
container_name: dbnode01
ports:
- "0.0.0.0:2379:2379"
- "0.0.0.0:9000:9000"
volumes:
- "./m3dbnode.yml:/etc/m3dbnode/m3dbnode.yml"
coord01:
networks:
- dtest
image: m3coordinator:dev
container_name: coord01
ports:
- "0.0.0.0:7201:7201"
- "0.0.0.0:7204:7204"
volumes:
- "./m3coordinator.yml:/etc/m3coordinator/m3coordinator.yml"
networks:
dtest:
16 changes: 16 additions & 0 deletions scripts/dtest/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

set -ex
set -o pipefail

COMPOSE_FILE=./scripts/dtest/docker-compose.yml

function defer {
docker-compose -f "${COMPOSE_FILE}" down
}

trap defer EXIT

docker-compose -f "${COMPOSE_FILE}" up --detach

go test -v -tags=dtest ./src/cmd/tools/dtest/docker/harness
6 changes: 3 additions & 3 deletions src/cmd/tools/dtest/docker/harness/harness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ var singleDBNodeDockerResources resources.DockerResources

func TestMain(m *testing.M) {
var err error
singleDBNodeDockerResources, err = resources.SetupSingleM3DBNode()
singleDBNodeDockerResources, err = resources.SetupSingleM3DBNode(
resources.WithExistingCluster("dbnode01", "coord01"),
)

if err != nil {
fmt.Println("could not set up db docker containers", err)
os.Exit(1)
}

if l := len(singleDBNodeDockerResources.Nodes()); l != 1 {
singleDBNodeDockerResources.Cleanup() //nolint:errcheck
fmt.Println("should only have a single node, have", l)
os.Exit(1)
}

code := m.Run()
singleDBNodeDockerResources.Cleanup() //nolint:errcheck
os.Exit(code)
}
11 changes: 0 additions & 11 deletions src/cmd/tools/dtest/docker/harness/resources/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"

"github.com/m3db/m3/src/x/instrument"

Expand All @@ -52,7 +51,6 @@ type dockerResourceOptions struct {
source string
containerName string
image dockerImage
dockerFile string
portList []int
mounts []string
iOpts instrument.Options
Expand All @@ -77,10 +75,6 @@ func (o dockerResourceOptions) withDefaults(
o.image = defaultOpts.image
}

if len(o.dockerFile) == 0 {
o.dockerFile = defaultOpts.dockerFile
}

if len(o.portList) == 0 {
o.portList = defaultOpts.portList
}
Expand Down Expand Up @@ -176,11 +170,6 @@ func exposePorts(
return opts
}

func getDockerfile(file string) string {
src, _ := os.Getwd()
return fmt.Sprintf("%s/%s", src, file)
}

func toResponse(
resp *http.Response,
response proto.Message,
Expand Down

This file was deleted.

This file was deleted.

6 changes: 2 additions & 4 deletions src/cmd/tools/dtest/docker/harness/resources/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ import (
)

const (
defaultCoordinatorSource = "coordinator"
defaultCoordinatorName = "coord01"
defaultCoordinatorDockerfile = "resources/config/m3coordinator.Dockerfile"
defaultCoordinatorSource = "coordinator"
defaultCoordinatorName = "coord01"
)

var (
Expand All @@ -53,7 +52,6 @@ var (
defaultCoordinatorOptions = dockerResourceOptions{
source: defaultCoordinatorSource,
containerName: defaultCoordinatorName,
dockerFile: defaultCoordinatorDockerfile,
portList: defaultCoordinatorList,
}
)
Expand Down
2 changes: 0 additions & 2 deletions src/cmd/tools/dtest/docker/harness/resources/dbnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
const (
defaultDBNodeSource = "dbnode"
defaultDBNodeContainerName = "dbnode01"
defaultDBNodeDockerfile = "resources/config/m3dbnode.Dockerfile"
)

var (
Expand All @@ -45,7 +44,6 @@ var (
defaultDBNodeOptions = dockerResourceOptions{
source: defaultDBNodeSource,
containerName: defaultDBNodeContainerName,
dockerFile: getDockerfile(defaultDBNodeDockerfile),
portList: defaultDBNodePortList,
}
)
Expand Down
16 changes: 7 additions & 9 deletions src/cmd/tools/dtest/docker/harness/resources/docker_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func newDockerResource(
source = resourceOpts.source
image = resourceOpts.image
containerName = resourceOpts.containerName
dockerFile = resourceOpts.dockerFile
iOpts = resourceOpts.iOpts
portList = resourceOpts.portList

Expand All @@ -60,11 +59,6 @@ func newDockerResource(
)
)

if err := pool.RemoveContainerByName(containerName); err != nil {
logger.Error("could not remove container from pool", zap.Error(err))
return nil, err
}

opts := exposePorts(newOptions(containerName), portList)

hostConfigOpts := func(c *dc.HostConfig) {
Expand All @@ -83,9 +77,13 @@ func newDockerResource(
var resource *dockertest.Resource
var err error
if image.name == "" {
logger.Info("building and running container with options",
zap.String("dockerFile", dockerFile), zap.Any("options", opts))
resource, err = pool.BuildAndRunWithOptions(dockerFile, opts, hostConfigOpts)
logger.Info("connecting to existing container", zap.String("container", containerName))
var ok bool
resource, ok = pool.ContainerByName(containerName)
if !ok {
logger.Error("could not find container", zap.Error(err))
return nil, fmt.Errorf("could not find container %v", containerName)
}
} else {
opts = useImage(opts, image)
imageWithTag := fmt.Sprintf("%v:%v", image.name, image.tag)
Expand Down
25 changes: 14 additions & 11 deletions src/cmd/tools/dtest/docker/harness/resources/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,22 @@ func SetupSingleM3DBNode(opts ...SetupOptions) (DockerResources, error) { // nol
}

pool.MaxWait = timeout
err = setupNetwork(pool)
if err != nil {
return nil, err
}

err = setupVolume(pool)
if err != nil {
return nil, err
if !options.existingCluster {
if err := setupNetwork(pool); err != nil {
return nil, err
}

if err := setupVolume(pool); err != nil {
return nil, err
}
}

iOpts := instrument.NewOptions()
dbNode, err := newDockerHTTPNode(pool, dockerResourceOptions{
image: options.dbNodeImage,
iOpts: iOpts,
image: options.dbNodeImage,
containerName: options.dbNodeContainerName,
iOpts: iOpts,
})

success := false
Expand All @@ -112,8 +114,9 @@ func SetupSingleM3DBNode(opts ...SetupOptions) (DockerResources, error) { // nol
}

coordinator, err := newDockerHTTPCoordinator(pool, dockerResourceOptions{
image: options.coordinatorImage,
iOpts: iOpts,
image: options.coordinatorImage,
containerName: options.coordinatorContainerName,
iOpts: iOpts,
})

defer func() {
Expand Down
14 changes: 14 additions & 0 deletions src/cmd/tools/dtest/docker/harness/resources/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ type dockerImage struct {
type setupOptions struct {
dbNodeImage dockerImage
coordinatorImage dockerImage

existingCluster bool
dbNodeContainerName string
coordinatorContainerName string
}

// SetupOptions is a setup option.
Expand All @@ -46,3 +50,13 @@ func WithCoordinatorImage(name, tag string) SetupOptions {
o.coordinatorImage = dockerImage{name: name, tag: tag}
}
}

// WithExistingCluster sets the names of already running containers dbnode and coordinator
// containers that should be used for tests.
func WithExistingCluster(dbNodeContainerName, coordinatorContainerName string) SetupOptions {
return func(o *setupOptions) {
o.existingCluster = true
o.dbNodeContainerName = dbNodeContainerName
o.coordinatorContainerName = coordinatorContainerName
}
}