From 75e1fee5fa433c48c52c8a0f6e830543565608d3 Mon Sep 17 00:00:00 2001 From: James Nugent Date: Fri, 15 Jan 2016 02:59:07 +0000 Subject: [PATCH] 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. --- resource_docker_container.go | 2 +- resource_docker_container_funcs.go | 2 +- resource_docker_container_test.go | 51 ++++++++++++++++++++++-------- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/resource_docker_container.go b/resource_docker_container.go index 3ee87cf7..c8f363e3 100644 --- a/resource_docker_container.go +++ b/resource_docker_container.go @@ -184,7 +184,7 @@ func resourceDockerContainer() *schema.Resource { Set: resourceDockerPortsHash, }, - "hosts": &schema.Schema{ + "host": &schema.Schema{ Type: schema.TypeSet, Optional: true, ForceNew: true, diff --git a/resource_docker_container_funcs.go b/resource_docker_container_funcs.go index 1c3f3d1d..47878f93 100644 --- a/resource_docker_container_funcs.go +++ b/resource_docker_container_funcs.go @@ -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)) } diff --git a/resource_docker_container_test.go b/resource_docker_container_test.go index a5c36a5c..aa1eee96 100644 --- a/resource_docker_container_test.go +++ b/resource_docker_container_test.go @@ -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" + } } `