From 877a21c39fad79e72273cc4a18bfd8ad118e461e Mon Sep 17 00:00:00 2001 From: Vilius Pranckaitis Date: Thu, 7 Jan 2021 11:28:08 +0200 Subject: [PATCH 1/3] [dtest] Run test on an existing cluster launched with docker compose --- scripts/dtest/docker-compose.yml | 24 +++++++++++++++++ .../dtest}/m3coordinator.yml | 0 .../config => scripts/dtest}/m3dbnode.yml | 0 scripts/dtest/run.sh | 16 +++++++++++ .../dtest/docker/harness/harness_test.go | 6 ++--- .../dtest/docker/harness/resources/common.go | 12 +-------- .../resources/config/m3coordinator.Dockerfile | 12 --------- .../resources/config/m3dbnode.Dockerfile | 12 --------- .../docker/harness/resources/coordinator.go | 6 ++--- .../dtest/docker/harness/resources/dbnode.go | 2 -- .../harness/resources/docker_resource.go | 16 +++++------ .../dtest/docker/harness/resources/harness.go | 27 +++++++++++-------- .../dtest/docker/harness/resources/options.go | 14 ++++++++++ 13 files changed, 83 insertions(+), 64 deletions(-) create mode 100755 scripts/dtest/docker-compose.yml rename {src/cmd/tools/dtest/docker/harness/resources/config => scripts/dtest}/m3coordinator.yml (100%) rename {src/cmd/tools/dtest/docker/harness/resources/config => scripts/dtest}/m3dbnode.yml (100%) create mode 100755 scripts/dtest/run.sh delete mode 100644 src/cmd/tools/dtest/docker/harness/resources/config/m3coordinator.Dockerfile delete mode 100644 src/cmd/tools/dtest/docker/harness/resources/config/m3dbnode.Dockerfile diff --git a/scripts/dtest/docker-compose.yml b/scripts/dtest/docker-compose.yml new file mode 100755 index 0000000000..ba8502c42e --- /dev/null +++ b/scripts/dtest/docker-compose.yml @@ -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: diff --git a/src/cmd/tools/dtest/docker/harness/resources/config/m3coordinator.yml b/scripts/dtest/m3coordinator.yml similarity index 100% rename from src/cmd/tools/dtest/docker/harness/resources/config/m3coordinator.yml rename to scripts/dtest/m3coordinator.yml diff --git a/src/cmd/tools/dtest/docker/harness/resources/config/m3dbnode.yml b/scripts/dtest/m3dbnode.yml similarity index 100% rename from src/cmd/tools/dtest/docker/harness/resources/config/m3dbnode.yml rename to scripts/dtest/m3dbnode.yml diff --git a/scripts/dtest/run.sh b/scripts/dtest/run.sh new file mode 100755 index 0000000000..d1be90f6f9 --- /dev/null +++ b/scripts/dtest/run.sh @@ -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 diff --git a/src/cmd/tools/dtest/docker/harness/harness_test.go b/src/cmd/tools/dtest/docker/harness/harness_test.go index 138a997959..109bcfa22d 100644 --- a/src/cmd/tools/dtest/docker/harness/harness_test.go +++ b/src/cmd/tools/dtest/docker/harness/harness_test.go @@ -34,7 +34,9 @@ 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) @@ -42,12 +44,10 @@ func TestMain(m *testing.M) { } 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) } diff --git a/src/cmd/tools/dtest/docker/harness/resources/common.go b/src/cmd/tools/dtest/docker/harness/resources/common.go index e6b2e4a286..ce74d509f2 100644 --- a/src/cmd/tools/dtest/docker/harness/resources/common.go +++ b/src/cmd/tools/dtest/docker/harness/resources/common.go @@ -26,7 +26,6 @@ import ( "fmt" "io/ioutil" "net/http" - "os" "github.com/m3db/m3/src/x/instrument" @@ -52,7 +51,6 @@ type dockerResourceOptions struct { source string containerName string image dockerImage - dockerFile string portList []int mounts []string iOpts instrument.Options @@ -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 } @@ -117,6 +111,7 @@ func setupNetwork(pool *dockertest.Pool) error { for _, n := range networks { if n.Name == networkName { + return nil if err := pool.Client.RemoveNetwork(networkName); err != nil { return err } @@ -176,11 +171,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, diff --git a/src/cmd/tools/dtest/docker/harness/resources/config/m3coordinator.Dockerfile b/src/cmd/tools/dtest/docker/harness/resources/config/m3coordinator.Dockerfile deleted file mode 100644 index 994ed23c8b..0000000000 --- a/src/cmd/tools/dtest/docker/harness/resources/config/m3coordinator.Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM alpine:latest AS builder -LABEL maintainer="The M3DB Authors " - -RUN mkdir -p /bin -RUN mkdir -p /etc/m3coordinator -ADD ./m3coordinator /bin/ -ADD ./m3coordinator.yml /etc/m3coordinator.yml - -EXPOSE 7201/tcp 7203/tcp 7204/tcp - -ENTRYPOINT [ "/bin/m3coordinator" ] -CMD [ "-f", "/etc/m3coordinator.yml" ] diff --git a/src/cmd/tools/dtest/docker/harness/resources/config/m3dbnode.Dockerfile b/src/cmd/tools/dtest/docker/harness/resources/config/m3dbnode.Dockerfile deleted file mode 100644 index b5bf62d1a4..0000000000 --- a/src/cmd/tools/dtest/docker/harness/resources/config/m3dbnode.Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM alpine:latest AS builder -LABEL maintainer="The M3DB Authors " - -RUN mkdir -p /bin -RUN mkdir -p /etc/m3dbnode -ADD ./m3dbnode /bin/ -ADD ./m3dbnode.yml /etc/m3dbnode/m3dbnode.yml - -EXPOSE 2379/tcp 2380/tcp 7201/tcp 7203/tcp 9000-9004/tcp - -ENTRYPOINT [ "/bin/m3dbnode" ] -CMD [ "-f", "/etc/m3dbnode/m3dbnode.yml" ] diff --git a/src/cmd/tools/dtest/docker/harness/resources/coordinator.go b/src/cmd/tools/dtest/docker/harness/resources/coordinator.go index 2c7f2cba1c..768ceed7fd 100644 --- a/src/cmd/tools/dtest/docker/harness/resources/coordinator.go +++ b/src/cmd/tools/dtest/docker/harness/resources/coordinator.go @@ -42,9 +42,8 @@ import ( ) const ( - defaultCoordinatorSource = "coordinator" - defaultCoordinatorName = "coord01" - defaultCoordinatorDockerfile = "resources/config/m3coordinator.Dockerfile" + defaultCoordinatorSource = "coordinator" + defaultCoordinatorName = "coord01" ) var ( @@ -53,7 +52,6 @@ var ( defaultCoordinatorOptions = dockerResourceOptions{ source: defaultCoordinatorSource, containerName: defaultCoordinatorName, - dockerFile: defaultCoordinatorDockerfile, portList: defaultCoordinatorList, } ) diff --git a/src/cmd/tools/dtest/docker/harness/resources/dbnode.go b/src/cmd/tools/dtest/docker/harness/resources/dbnode.go index ef6adf2774..d7eca22f4b 100644 --- a/src/cmd/tools/dtest/docker/harness/resources/dbnode.go +++ b/src/cmd/tools/dtest/docker/harness/resources/dbnode.go @@ -36,7 +36,6 @@ import ( const ( defaultDBNodeSource = "dbnode" defaultDBNodeContainerName = "dbnode01" - defaultDBNodeDockerfile = "resources/config/m3dbnode.Dockerfile" ) var ( @@ -45,7 +44,6 @@ var ( defaultDBNodeOptions = dockerResourceOptions{ source: defaultDBNodeSource, containerName: defaultDBNodeContainerName, - dockerFile: getDockerfile(defaultDBNodeDockerfile), portList: defaultDBNodePortList, } ) diff --git a/src/cmd/tools/dtest/docker/harness/resources/docker_resource.go b/src/cmd/tools/dtest/docker/harness/resources/docker_resource.go index a8b9f8ae79..7cfa5357d3 100644 --- a/src/cmd/tools/dtest/docker/harness/resources/docker_resource.go +++ b/src/cmd/tools/dtest/docker/harness/resources/docker_resource.go @@ -50,7 +50,6 @@ func newDockerResource( source = resourceOpts.source image = resourceOpts.image containerName = resourceOpts.containerName - dockerFile = resourceOpts.dockerFile iOpts = resourceOpts.iOpts portList = resourceOpts.portList @@ -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) { @@ -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) diff --git a/src/cmd/tools/dtest/docker/harness/resources/harness.go b/src/cmd/tools/dtest/docker/harness/resources/harness.go index 6ad245027d..8678ac5652 100644 --- a/src/cmd/tools/dtest/docker/harness/resources/harness.go +++ b/src/cmd/tools/dtest/docker/harness/resources/harness.go @@ -77,20 +77,24 @@ 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 { + err = setupNetwork(pool) + if err != nil { + return nil, err + } + + err = setupVolume(pool) + if 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 @@ -112,8 +116,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() { diff --git a/src/cmd/tools/dtest/docker/harness/resources/options.go b/src/cmd/tools/dtest/docker/harness/resources/options.go index 7f1f970333..886aebb746 100644 --- a/src/cmd/tools/dtest/docker/harness/resources/options.go +++ b/src/cmd/tools/dtest/docker/harness/resources/options.go @@ -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. @@ -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 + } +} From 1a75b8fa88aa67bf8bdefc9a57adf11706c81906 Mon Sep 17 00:00:00 2001 From: Vilius Pranckaitis Date: Fri, 8 Jan 2021 20:13:56 +1100 Subject: [PATCH 2/3] refactor a few ifs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Linas Medžiūnas --- src/cmd/tools/dtest/docker/harness/resources/harness.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/cmd/tools/dtest/docker/harness/resources/harness.go b/src/cmd/tools/dtest/docker/harness/resources/harness.go index 8678ac5652..74ec12257d 100644 --- a/src/cmd/tools/dtest/docker/harness/resources/harness.go +++ b/src/cmd/tools/dtest/docker/harness/resources/harness.go @@ -79,13 +79,11 @@ func SetupSingleM3DBNode(opts ...SetupOptions) (DockerResources, error) { // nol pool.MaxWait = timeout if !options.existingCluster { - err = setupNetwork(pool) - if err != nil { + if err := setupNetwork(pool); err != nil { return nil, err } - err = setupVolume(pool) - if err != nil { + if err := setupVolume(pool); err != nil { return nil, err } } From 8543d3a4b1a11116b2cdec2e36be0e6167a0909a Mon Sep 17 00:00:00 2001 From: Vilius Pranckaitis Date: Tue, 12 Jan 2021 17:38:50 +0200 Subject: [PATCH 3/3] remove a leftover line --- src/cmd/tools/dtest/docker/harness/resources/common.go | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cmd/tools/dtest/docker/harness/resources/common.go b/src/cmd/tools/dtest/docker/harness/resources/common.go index ce74d509f2..99f00b780d 100644 --- a/src/cmd/tools/dtest/docker/harness/resources/common.go +++ b/src/cmd/tools/dtest/docker/harness/resources/common.go @@ -111,7 +111,6 @@ func setupNetwork(pool *dockertest.Pool) error { for _, n := range networks { if n.Name == networkName { - return nil if err := pool.Client.RemoveNetwork(networkName); err != nil { return err }