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] Allow skipping docker image build by providing existing image #2942

Merged
merged 14 commits into from
Nov 27, 2020
Merged
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
9 changes: 8 additions & 1 deletion src/cmd/tools/dtest/docker/harness/resources/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,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) {
options := setupOptions{}
for _, o := range opts {
o.apply(&options)
}

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

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

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

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

Expand Down
59 changes: 59 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,59 @@
// 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
}

type SetupOptions interface {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Exported symbols needs comment of form // SetupOptions .... @linasm do you mind helping us find someone to look at why the linter is not complaining about this missing comments? Most linter setups complain enforce this and I think ours is missing it. I see this in VS Code for instance:

exported function SetupOptions should have comment or be unexported go-lint

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vpranckaitis if you have some bandwidth to look into this, that would also work well, we need to close this gap so we don't have to call these out on PRs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apply(*setupOptions)
}

type dbnodeImageNameOption struct {
dockerImage
}

func (o dbnodeImageNameOption) apply(opts *setupOptions) {
opts.dbNodeImage = o.dockerImage
}

func WithDBNodeImage(name, tag string) SetupOptions {
return dbnodeImageNameOption{dockerImage{name, tag}}
}

type coordinatorImageNameOption struct {
dockerImage
}

func (o coordinatorImageNameOption) apply(opts *setupOptions) {
opts.coordinatorImage = o.dockerImage
}

func WithCoordinatorImage(name, tag string) SetupOptions {
return coordinatorImageNameOption{dockerImage{name, tag}}
}
Copy link
Collaborator

@robskillington robskillington Nov 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per similar feedback from this PR: #2928 (comment).

I would refactor these options to just use barebones functions:

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

// WithDBNodeImage sets an option to use an image name and tag for the DB node.
func WithDBNodeImage(name, tag string) SetupOption {
	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) SetupOption {
	return func(o *setupOptions) {
		o.coordinatorImage = dockerImage{name: name, tag: tag}
	}
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.