Skip to content

Commit

Permalink
keep deep copy logic for container config accessors
Browse files Browse the repository at this point in the history
Signed-off-by: Vaughn Dice <[email protected]>
  • Loading branch information
vdice committed Apr 24, 2020
1 parent c46b74f commit 9b6875f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
30 changes: 26 additions & 4 deletions driver/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package docker
import (
"archive/tar"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -20,6 +21,7 @@ import (
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/docker/registry"
"github.com/mitchellh/copystructure"
)

// Driver is capable of running Docker invocation images using Docker itself.
Expand Down Expand Up @@ -52,14 +54,34 @@ func (d *Driver) AddConfigurationOptions(opts ...ConfigurationOption) {

// GetContainerConfig returns a copy of the container configuration
// used by the driver during container exec
func (d *Driver) GetContainerConfig() container.Config {
return d.containerCfg
func (d *Driver) GetContainerConfig() (container.Config, error) {
cpy, err := copystructure.Copy(d.containerCfg)
if err != nil {
return container.Config{}, err
}

cfg, ok := cpy.(container.Config)
if !ok {
return container.Config{}, errors.New("unable to process container config")
}

return cfg, nil
}

// GetContainerHostConfig returns a copy of the container host configuration
// used by the driver during container exec
func (d *Driver) GetContainerHostConfig() container.HostConfig {
return d.containerHostCfg
func (d *Driver) GetContainerHostConfig() (container.HostConfig, error) {
cpy, err := copystructure.Copy(d.containerHostCfg)
if err != nil {
return container.HostConfig{}, err
}

cfg, ok := cpy.(container.HostConfig)
if !ok {
return container.HostConfig{}, errors.New("unable to process container host config")
}

return cfg, nil
}

// Config returns the Docker driver configuration options
Expand Down
12 changes: 8 additions & 4 deletions driver/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ func TestDriver_GetConfigurationOptions(t *testing.T) {
err := d.ApplyConfigurationOptions()
is.NoError(err)

cfg := d.GetContainerConfig()
cfg, err := d.GetContainerConfig()
is.NoError(err)
is.Equal(container.Config{}, cfg)

hostCfg := d.GetContainerHostConfig()
hostCfg, err := d.GetContainerHostConfig()
is.NoError(err)
is.Equal(container.HostConfig{}, hostCfg)
})

Expand All @@ -42,10 +44,12 @@ func TestDriver_GetConfigurationOptions(t *testing.T) {
Privileged: true,
}

cfg := d.GetContainerConfig()
cfg, err := d.GetContainerConfig()
is.NoError(err)
is.Equal(expectedContainerCfg, cfg)

hostCfg := d.GetContainerHostConfig()
hostCfg, err := d.GetContainerHostConfig()
is.NoError(err)
is.Equal(expectedHostCfg, hostCfg)
})
}

0 comments on commit 9b6875f

Please sign in to comment.