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,
},
"hosts": &schema.Schema{
"host": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
ForceNew: true,

View File

@ -68,7 +68,7 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err
}
extraHosts := []string{}
if v, ok := d.GetOk("extra_hosts"); ok {
if v, ok := d.GetOk("host"); ok {
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"])
}
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
}
@ -132,6 +144,7 @@ resource "docker_container" "foo" {
image = "${docker_image.foo.latest}"
}
`
const testAccDockerContainerCustomizedConfig = `
resource "docker_image" "foo" {
name = "nginx:latest"
@ -140,21 +153,31 @@ resource "docker_image" "foo" {
resource "docker_container" "foo" {
name = "tf-test"
image = "${docker_image.foo.latest}"
entrypoint = ["/bin/bash", "-c", "ping localhost"]
restart = "on-failure"
max_retry_count = 5
memory = 512
memory_swap = 2048
cpu_shares = 32
labels {
env = "prod"
role = "test"
}
log_driver = "json-file"
log_opts = {
max-size = "10m"
max-file = 20
entrypoint = ["/bin/bash", "-c", "ping localhost"]
restart = "on-failure"
max_retry_count = 5
memory = 512
memory_swap = 2048
cpu_shares = 32
labels {
env = "prod"
role = "test"
}
log_driver = "json-file"
log_opts = {
max-size = "10m"
max-file = 20
}
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
assumes it is successful.
* `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.
* `publish_all_ports` - (Optional, bool) Publish all ports of the container.
* `volumes` - (Optional) See [Volumes](#volumes) below for details.
@ -88,13 +88,16 @@ the following:
<a id="extra_hosts"></a>
## Extra Hosts
`extra_hosts` is a block within the configuration that can be repeated to specify
the extra host mappings for the container. Each `extra_hosts` block supports
`host_entry` is a block within the configuration that can be repeated to specify
the extra host mappings for the container. Each `host_entry` block supports
the following:
* `host` - (Required, int) Hostname to add.
* `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>
## Volumes