Skip to content

Commit

Permalink
Add inital support for folder HostBind Mount
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Elizarov committed Feb 1, 2021
1 parent 4c2c33a commit f8ddf06
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cmd/container-structure-test/app/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions pkg/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion pkg/drivers/docker_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
type DockerDriver struct {
originalImage string
currentImage string
mount string
cli docker.Client
env map[string]string
save bool
Expand All @@ -51,17 +52,36 @@ func NewDockerDriver(args DriverConfig) (Driver, error) {
return &DockerDriver{
originalImage: args.Image,
currentImage: args.Image,
mount: args.Mount,
cli: *newCli,
env: nil,
save: args.Save,
runtime: args.Runtime,
}, 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
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions pkg/drivers/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f8ddf06

Please sign in to comment.