Allow Windows Docker containers to map volumes (#13584)

* fix regex for windows path

* Fix escaping

* move validate function out and create test
This commit is contained in:
Malte Brodersen 2017-05-15 12:09:50 +02:00 committed by Paul Stack
parent 0e0c61bbd8
commit 994284f535
2 changed files with 37 additions and 11 deletions

View File

@ -184,17 +184,10 @@ func resourceDockerContainer() *schema.Resource {
}, },
"host_path": &schema.Schema{ "host_path": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
ForceNew: true, ForceNew: true,
ValidateFunc: func(v interface{}, k string) (ws []string, es []error) { ValidateFunc: validateDockerContainerPath,
value := v.(string)
if !regexp.MustCompile(`^/`).MatchString(value) {
es = append(es, fmt.Errorf(
"%q must be an absolute path", k))
}
return
},
}, },
"volume_name": &schema.Schema{ "volume_name": &schema.Schema{
@ -515,3 +508,13 @@ func resourceDockerUploadHash(v interface{}) int {
return hashcode.String(buf.String()) 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
}

View File

@ -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) { func TestAccDockerContainer_volume(t *testing.T) {
var c dc.Container var c dc.Container