Skip to content

Commit

Permalink
Allow Windows Docker containers to map volumes (hashicorp#13584)
Browse files Browse the repository at this point in the history
* fix regex for windows path

* Fix escaping

* move validate function out and create test
  • Loading branch information
derBroBro authored and stack72 committed May 15, 2017
1 parent 0e0c61b commit 994284f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
25 changes: 14 additions & 11 deletions builtin/providers/docker/resource_docker_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,10 @@ func resourceDockerContainer() *schema.Resource {
},

"host_path": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
value := v.(string)
if !regexp.MustCompile(`^/`).MatchString(value) {
es = append(es, fmt.Errorf(
"%q must be an absolute path", k))
}
return
},
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateDockerContainerPath,
},

"volume_name": &schema.Schema{
Expand Down Expand Up @@ -515,3 +508,13 @@ func resourceDockerUploadHash(v interface{}) int {

return hashcode.String(buf.String())
}

func validateDockerContainerPath(v interface{}, k string) (ws []string, errors []error) {

value := v.(string)
if !regexp.MustCompile(`^[a-zA-Z]:\\|^/`).MatchString(value) {
errors = append(errors, fmt.Errorf("%q must be an absolute path", k))
}

return
}
23 changes: 23 additions & 0 deletions builtin/providers/docker/resource_docker_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@ func TestAccDockerContainer_basic(t *testing.T) {
})
}

func TestAccDockerContainerPath_validation(t *testing.T) {
cases := []struct {
Value string
ErrCount int
}{
{Value: "/var/log", ErrCount: 0},
{Value: "/tmp", ErrCount: 0},
{Value: "C:\\Windows\\System32", ErrCount: 0},
{Value: "C:\\Program Files\\MSBuild", ErrCount: 0},
{Value: "test", ErrCount: 1},
{Value: "C:Test", ErrCount: 1},
{Value: "", ErrCount: 1},
}

for _, tc := range cases {
_, errors := validateDockerContainerPath(tc.Value, "docker_container")

if len(errors) != tc.ErrCount {
t.Fatalf("Expected the Docker Container Path to trigger a validation error")
}
}
}

func TestAccDockerContainer_volume(t *testing.T) {
var c dc.Container

Expand Down

0 comments on commit 994284f

Please sign in to comment.