provider/docker: Tweak and test `host_entry`

This adds acceptance tests for specifying extra hosts on Docker
containers. It also renames the repeating block from `hosts` to `host`,
which reads more naturally in the schema when multiple instances of the
block are declared.
This commit is contained in:
James Nugent 2016-01-15 02:59:07 +00:00
parent 35c21cb808
commit 3380f08e5a
4 changed files with 45 additions and 19 deletions

View File

@ -184,7 +184,7 @@ func resourceDockerContainer() *schema.Resource {
Set: resourceDockerPortsHash, Set: resourceDockerPortsHash,
}, },
"hosts": &schema.Schema{ "host": &schema.Schema{
Type: schema.TypeSet, Type: schema.TypeSet,
Optional: true, Optional: true,
ForceNew: true, ForceNew: true,

View File

@ -68,7 +68,7 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err
} }
extraHosts := []string{} extraHosts := []string{}
if v, ok := d.GetOk("extra_hosts"); ok { if v, ok := d.GetOk("host"); ok {
extraHosts = extraHostsSetToDockerExtraHosts(v.(*schema.Set)) extraHosts = extraHostsSetToDockerExtraHosts(v.(*schema.Set))
} }

View File

@ -72,6 +72,18 @@ func TestAccDockerContainer_customized(t *testing.T) {
return fmt.Errorf("Container does not have the correct max-file log option: %v", c.HostConfig.LogConfig.Config["max-file"]) return fmt.Errorf("Container does not have the correct max-file log option: %v", c.HostConfig.LogConfig.Config["max-file"])
} }
if len(c.HostConfig.ExtraHosts) != 2 {
return fmt.Errorf("Container does not have correct number of extra host entries, got %d", len(c.HostConfig.ExtraHosts))
}
if c.HostConfig.ExtraHosts[0] != "testhost2:10.0.2.0" {
return fmt.Errorf("Container has incorrect extra host string: %q", c.HostConfig.ExtraHosts[0])
}
if c.HostConfig.ExtraHosts[1] != "testhost:10.0.1.0" {
return fmt.Errorf("Container has incorrect extra host string: %q", c.HostConfig.ExtraHosts[1])
}
return nil return nil
} }
@ -132,6 +144,7 @@ resource "docker_container" "foo" {
image = "${docker_image.foo.latest}" image = "${docker_image.foo.latest}"
} }
` `
const testAccDockerContainerCustomizedConfig = ` const testAccDockerContainerCustomizedConfig = `
resource "docker_image" "foo" { resource "docker_image" "foo" {
name = "nginx:latest" name = "nginx:latest"
@ -156,5 +169,15 @@ resource "docker_container" "foo" {
max-file = 20 max-file = 20
} }
network_mode = "bridge" network_mode = "bridge"
host {
host = "testhost"
ip = "10.0.1.0"
}
host {
host = "testhost2"
ip = "10.0.2.0"
}
} }
` `

View File

@ -57,7 +57,7 @@ The following arguments are supported:
kept running. If false, then as long as the container exists, Terraform kept running. If false, then as long as the container exists, Terraform
assumes it is successful. assumes it is successful.
* `ports` - (Optional) See [Ports](#ports) below for details. * `ports` - (Optional) See [Ports](#ports) below for details.
* `extra_hosts` - (Optional) See [Extra Hosts](#extra_hosts) below for details. * `host_entry` - (Optional) See [Extra Hosts](#extra_hosts) below for details.
* `privileged` - (Optional, bool) Run container in privileged mode. * `privileged` - (Optional, bool) Run container in privileged mode.
* `publish_all_ports` - (Optional, bool) Publish all ports of the container. * `publish_all_ports` - (Optional, bool) Publish all ports of the container.
* `volumes` - (Optional) See [Volumes](#volumes) below for details. * `volumes` - (Optional) See [Volumes](#volumes) below for details.
@ -88,13 +88,16 @@ the following:
<a id="extra_hosts"></a> <a id="extra_hosts"></a>
## Extra Hosts ## Extra Hosts
`extra_hosts` is a block within the configuration that can be repeated to specify `host_entry` is a block within the configuration that can be repeated to specify
the extra host mappings for the container. Each `extra_hosts` block supports the extra host mappings for the container. Each `host_entry` block supports
the following: the following:
* `host` - (Required, int) Hostname to add. * `host` - (Required, int) Hostname to add.
* `ip` - (Required, int) IP address this hostname should resolve to.. * `ip` - (Required, int) IP address this hostname should resolve to..
This is equivalent to using the `--add-host` option when using the `run`
command of the Docker CLI.
<a id="volumes"></a> <a id="volumes"></a>
## Volumes ## Volumes