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 d7cf37d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 8 deletions.
44 changes: 44 additions & 0 deletions WIPclaim-unit-test-with-gojsonschema
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
func TestClaimSchema(t *testing.T) {
claim, err := New("my_claim")
assert.NoError(t, err)

claim.Bundle = &exampleBundle
claim.Result = Result{
Action: ActionInstall,
Message: "result message",
Status: StatusUnderway,
}

// override dynamic fields for testing
claim.Revision = staticRevision
claim.Created = staticDate
claim.Modified = staticDate

// add non-required fields
claim.Parameters = map[string]interface{}{
"myparam": "myparamvalue",
}
claim.Outputs = map[string]interface{}{
"myoutput": "myoutputvalue",
}
claim.Custom = []string{
"anything goes",
}

claimLoader := gojsonschema.NewGoLoader(claim)

// Load the claim schema
// TODO: gojsonschema.NewReferenceLoader doesn't support https/tls... PR adding functionality or at least a GHLoader?
pwd, _ := os.Getwd()
sl := gojsonschema.NewSchemaLoader()
claimSchema := gojsonschema.NewReferenceLoader(filepath.Join("file://", pwd, "testdata/claim.schema.json"))
bundleSchema := gojsonschema.NewReferenceLoader(filepath.Join("file://", pwd, "testdata/bundle.schema.json"))
sl.AddSchemas(claimSchema, bundleSchema)

schema, err := sl.Compile(claimSchema)
assert.NoError(t, err)

// Validate the claim against the schema
_, err = schema.Validate(claimLoader)
assert.NoError(t, err)
}
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 d7cf37d

Please sign in to comment.