From f8ddf065dfdfe849db1293f4bea594df42a56df1 Mon Sep 17 00:00:00 2001 From: Igor Elizarov Date: Mon, 1 Feb 2021 12:26:13 +0300 Subject: [PATCH] Add inital support for folder HostBind Mount --- cmd/container-structure-test/app/cmd/test.go | 3 ++- pkg/config/options.go | 1 + pkg/drivers/docker_driver.go | 23 +++++++++++++++++++- pkg/drivers/driver.go | 1 + 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/cmd/container-structure-test/app/cmd/test.go b/cmd/container-structure-test/app/cmd/test.go index ac001896..4d90a6d8 100644 --- a/cmd/container-structure-test/app/cmd/test.go +++ b/cmd/container-structure-test/app/cmd/test.go @@ -102,6 +102,7 @@ func run(out io.Writer) error { Save: opts.Save, Metadata: opts.Metadata, Runtime: opts.Runtime, + Mount: opts.Mount, } var err error @@ -193,7 +194,7 @@ func AddTestFlags(cmd *cobra.Command) { cmd.Flags().StringVarP(&opts.Driver, "driver", "d", "docker", "driver to use when running tests") cmd.Flags().StringVar(&opts.Metadata, "metadata", "", "path to image metadata file") cmd.Flags().StringVar(&opts.Runtime, "runtime", "", "runtime to use with docker driver") - + cmd.Flags().StringVar(&opts.Mount, "mount", "", "host path to folder to mount") cmd.Flags().BoolVar(&opts.Pull, "pull", false, "force a pull of the image before running tests") cmd.Flags().BoolVar(&opts.Save, "save", false, "preserve created containers after test run") cmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "flag to suppress output") diff --git a/pkg/config/options.go b/pkg/config/options.go index 76a89f0e..2976bd20 100644 --- a/pkg/config/options.go +++ b/pkg/config/options.go @@ -19,6 +19,7 @@ import "github.com/GoogleContainerTools/container-structure-test/pkg/types/unver type StructureTestOptions struct { ImagePath string Driver string + Mount string Runtime string Metadata string TestReport string diff --git a/pkg/drivers/docker_driver.go b/pkg/drivers/docker_driver.go index 424f8c2a..178f201c 100644 --- a/pkg/drivers/docker_driver.go +++ b/pkg/drivers/docker_driver.go @@ -37,6 +37,7 @@ import ( type DockerDriver struct { originalImage string currentImage string + mount string cli docker.Client env map[string]string save bool @@ -51,6 +52,7 @@ func NewDockerDriver(args DriverConfig) (Driver, error) { return &DockerDriver{ originalImage: args.Image, currentImage: args.Image, + mount: args.Mount, cli: *newCli, env: nil, save: args.Save, @@ -58,10 +60,28 @@ func NewDockerDriver(args DriverConfig) (Driver, error) { }, nil } +func splitMount(a string) (string, string) { + s := strings.Split(a, ":") + return s[0], s[1] +} + func (d *DockerDriver) hostConfig() *docker.HostConfig { - if d.runtime != "" { + if d.runtime != "" && d.mount == "" { + return &docker.HostConfig{ + Runtime: d.runtime, + } + } + if d.runtime == "" && d.mount != "" { + sourceFolder, targetFolder := splitMount(d.mount) + return &docker.HostConfig{ + Mounts: []docker.HostMount{{Source: sourceFolder, Target: targetFolder, Type: "bind"}}, + } + } + if d.runtime != "" && d.mount != "" { + sourceFolder, targetFolder := splitMount(d.mount) return &docker.HostConfig{ Runtime: d.runtime, + Mounts: []docker.HostMount{{Source: sourceFolder, Target: targetFolder, Type: "bind"}}, } } return nil @@ -94,6 +114,7 @@ func (d *DockerDriver) SetEnv(envVars []unversioned.EnvVar) error { if err != nil { return errors.Wrap(err, "Error creating container") } + defer d.removeContainer(container.ID) image, err := d.cli.CommitContainer(docker.CommitContainerOptions{ Container: container.ID, diff --git a/pkg/drivers/driver.go b/pkg/drivers/driver.go index 0685df6b..c822eed9 100644 --- a/pkg/drivers/driver.go +++ b/pkg/drivers/driver.go @@ -33,6 +33,7 @@ type DriverConfig struct { Save bool // used by Docker/Tar drivers Metadata string // used by Host driver Runtime string // used by Docker driver + Mount string // used by Docker driver } type Driver interface {