Skip to content

Commit

Permalink
[dtest] Allow skipping docker image build by providing existing image (
Browse files Browse the repository at this point in the history
  • Loading branch information
vpranckaitis authored Nov 27, 2020
1 parent 725cdb4 commit 182519f
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 18 deletions.
11 changes: 11 additions & 0 deletions src/cmd/tools/dtest/docker/harness/resources/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type dockerResourceOptions struct {
overrideDefaults bool
source string
containerName string
image dockerImage
dockerFile string
portList []int
mounts []string
Expand All @@ -72,6 +73,10 @@ func (o dockerResourceOptions) withDefaults(
o.containerName = defaultOpts.containerName
}

if o.image == (dockerImage{}) {
o.image = defaultOpts.image
}

if len(o.dockerFile) == 0 {
o.dockerFile = defaultOpts.dockerFile
}
Expand All @@ -98,6 +103,12 @@ func newOptions(name string) *dockertest.RunOptions {
}
}

func useImage(opts *dockertest.RunOptions, image dockerImage) *dockertest.RunOptions {
opts.Repository = image.name
opts.Tag = image.tag
return opts
}

func setupNetwork(pool *dockertest.Pool) error {
networks, err := pool.Client.ListNetworks()
if err != nil {
Expand Down
47 changes: 30 additions & 17 deletions src/cmd/tools/dtest/docker/harness/resources/docker_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"strconv"
"strings"

dockertest "github.com/ory/dockertest"
"github.com/ory/dockertest"
"github.com/ory/dockertest/docker"
dc "github.com/ory/dockertest/docker"
"github.com/ory/dockertest/docker/types/mount"
Expand All @@ -49,6 +49,7 @@ func newDockerResource(
) (*dockerResource, error) {
var (
source = resourceOpts.source
image = resourceOpts.image
containerName = resourceOpts.containerName
dockerFile = resourceOpts.dockerFile
iOpts = resourceOpts.iOpts
Expand All @@ -66,24 +67,36 @@ func newDockerResource(
}

opts := exposePorts(newOptions(containerName), portList)
logger.Info("building container with options",
zap.String("dockerFile", dockerFile), zap.Any("options", opts))
resource, err := pool.BuildAndRunWithOptions(dockerFile, opts,
func(c *dc.HostConfig) {
c.NetworkMode = networkName
mounts := make([]dc.HostMount, 0, len(resourceOpts.mounts))
for _, m := range resourceOpts.mounts {
mounts = append(mounts, dc.HostMount{
Target: m,
Type: string(mount.TypeTmpfs),
})
}

c.Mounts = mounts
})

hostConfigOpts := func(c *dc.HostConfig) {
c.NetworkMode = networkName
mounts := make([]dc.HostMount, 0, len(resourceOpts.mounts))
for _, m := range resourceOpts.mounts {
mounts = append(mounts, dc.HostMount{
Target: m,
Type: string(mount.TypeTmpfs),
})
}

c.Mounts = mounts
}

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)
} else {
opts = useImage(opts, image)
imageWithTag := fmt.Sprintf("%v:%v", image.name, image.tag)
logger.Info("running container with options",
zap.String("image", imageWithTag), zap.Any("options", opts))
resource, err = pool.RunWithOptions(opts, hostConfigOpts)
}

if err != nil {
logger.Error("could not build and run container", zap.Error(err))
logger.Error("could not run container", zap.Error(err))
return nil, err
}

Expand Down
10 changes: 9 additions & 1 deletion src/cmd/tools/dtest/docker/harness/resources/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// Package resources contains resources needed to setup docker containers for M3 tests.
package resources

import (
Expand Down Expand Up @@ -64,7 +65,12 @@ type dockerResources struct {

// SetupSingleM3DBNode creates docker resources representing a setup with a
// single DB node.
func SetupSingleM3DBNode() (DockerResources, error) {
func SetupSingleM3DBNode(opts ...SetupOptions) (DockerResources, error) { // nolint: gocyclo
options := setupOptions{}
for _, f := range opts {
f(&options)
}

pool, err := dockertest.NewPool("")
if err != nil {
return nil, err
Expand All @@ -83,6 +89,7 @@ func SetupSingleM3DBNode() (DockerResources, error) {

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

Expand All @@ -105,6 +112,7 @@ func SetupSingleM3DBNode() (DockerResources, error) {
}

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

Expand Down
48 changes: 48 additions & 0 deletions src/cmd/tools/dtest/docker/harness/resources/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package resources

type dockerImage struct {
name string
tag string
}

type setupOptions struct {
dbNodeImage dockerImage
coordinatorImage dockerImage
}

// SetupOptions is a setup option.
type SetupOptions func(*setupOptions)

// WithDBNodeImage sets an option to use an image name and tag for the DB node.
func WithDBNodeImage(name, tag string) SetupOptions {
return func(o *setupOptions) {
o.dbNodeImage = dockerImage{name: name, tag: tag}
}
}

// WithCoordinatorImage sets an option to use an image name and tag for the coordinator.
func WithCoordinatorImage(name, tag string) SetupOptions {
return func(o *setupOptions) {
o.coordinatorImage = dockerImage{name: name, tag: tag}
}
}

0 comments on commit 182519f

Please sign in to comment.