github.com/leeprovoost/terraform@v0.6.10-0.20160119085442-96f3f76118e7/builtin/providers/docker/resource_docker_container_test.go (about) 1 package docker 2 3 import ( 4 "fmt" 5 "testing" 6 7 dc "github.com/fsouza/go-dockerclient" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestAccDockerContainer_basic(t *testing.T) { 13 var c dc.Container 14 resource.Test(t, resource.TestCase{ 15 PreCheck: func() { testAccPreCheck(t) }, 16 Providers: testAccProviders, 17 Steps: []resource.TestStep{ 18 resource.TestStep{ 19 Config: testAccDockerContainerConfig, 20 Check: resource.ComposeTestCheckFunc( 21 testAccContainerRunning("docker_container.foo", &c), 22 ), 23 }, 24 }, 25 }) 26 } 27 28 func TestAccDockerContainer_volume(t *testing.T) { 29 var c dc.Container 30 31 testCheck := func(*terraform.State) error { 32 if len(c.Mounts) != 2 { 33 return fmt.Errorf("Incorrect number of mounts: expected 2, got %d", len(c.Mounts)) 34 } 35 36 for _, v := range c.Mounts { 37 if v.Name != "testAccDockerContainerVolume_volume" { 38 continue 39 } 40 41 if v.Destination != "/tmp/volume" { 42 return fmt.Errorf("Bad destination on mount: expected /tmp/volume, got %q", v.Destination) 43 } 44 45 if v.Mode != "rw" { 46 return fmt.Errorf("Bad mode on mount: expected rw, got %q", v.Mode) 47 } 48 49 return nil 50 } 51 52 return fmt.Errorf("Mount for testAccDockerContainerVolume_volume not found") 53 } 54 55 resource.Test(t, resource.TestCase{ 56 PreCheck: func() { testAccPreCheck(t) }, 57 Providers: testAccProviders, 58 Steps: []resource.TestStep{ 59 resource.TestStep{ 60 Config: testAccDockerContainerVolumeConfig, 61 Check: resource.ComposeTestCheckFunc( 62 testAccContainerRunning("docker_container.foo", &c), 63 testCheck, 64 ), 65 }, 66 }, 67 }) 68 } 69 70 func TestAccDockerContainer_customized(t *testing.T) { 71 var c dc.Container 72 73 testCheck := func(*terraform.State) error { 74 if len(c.Config.Entrypoint) < 3 || 75 (c.Config.Entrypoint[0] != "/bin/bash" && 76 c.Config.Entrypoint[1] != "-c" && 77 c.Config.Entrypoint[2] != "ping localhost") { 78 return fmt.Errorf("Container wrong entrypoint: %s", c.Config.Entrypoint) 79 } 80 81 if c.HostConfig.RestartPolicy.Name == "on-failure" { 82 if c.HostConfig.RestartPolicy.MaximumRetryCount != 5 { 83 return fmt.Errorf("Container has wrong restart policy max retry count: %d", c.HostConfig.RestartPolicy.MaximumRetryCount) 84 } 85 } else { 86 return fmt.Errorf("Container has wrong restart policy: %s", c.HostConfig.RestartPolicy.Name) 87 } 88 89 if c.HostConfig.Memory != (512 * 1024 * 1024) { 90 return fmt.Errorf("Container has wrong memory setting: %d", c.HostConfig.Memory) 91 } 92 93 if c.HostConfig.MemorySwap != (2048 * 1024 * 1024) { 94 return fmt.Errorf("Container has wrong memory swap setting: %d", c.HostConfig.MemorySwap) 95 } 96 97 if c.HostConfig.CPUShares != 32 { 98 return fmt.Errorf("Container has wrong cpu shares setting: %d", c.HostConfig.CPUShares) 99 } 100 101 if c.Config.Labels["env"] != "prod" || c.Config.Labels["role"] != "test" { 102 return fmt.Errorf("Container does not have the correct labels") 103 } 104 105 if c.HostConfig.LogConfig.Type != "json-file" { 106 return fmt.Errorf("Container does not have the correct log config: %s", c.HostConfig.LogConfig.Type) 107 } 108 109 if c.HostConfig.LogConfig.Config["max-size"] != "10m" { 110 return fmt.Errorf("Container does not have the correct max-size log option: %v", c.HostConfig.LogConfig.Config["max-size"]) 111 } 112 113 if c.HostConfig.LogConfig.Config["max-file"] != "20" { 114 return fmt.Errorf("Container does not have the correct max-file log option: %v", c.HostConfig.LogConfig.Config["max-file"]) 115 } 116 117 if len(c.HostConfig.ExtraHosts) != 2 { 118 return fmt.Errorf("Container does not have correct number of extra host entries, got %d", len(c.HostConfig.ExtraHosts)) 119 } 120 121 if c.HostConfig.ExtraHosts[0] != "testhost2:10.0.2.0" { 122 return fmt.Errorf("Container has incorrect extra host string: %q", c.HostConfig.ExtraHosts[0]) 123 } 124 125 if c.HostConfig.ExtraHosts[1] != "testhost:10.0.1.0" { 126 return fmt.Errorf("Container has incorrect extra host string: %q", c.HostConfig.ExtraHosts[1]) 127 } 128 129 return nil 130 } 131 132 resource.Test(t, resource.TestCase{ 133 PreCheck: func() { testAccPreCheck(t) }, 134 Providers: testAccProviders, 135 Steps: []resource.TestStep{ 136 resource.TestStep{ 137 Config: testAccDockerContainerCustomizedConfig, 138 Check: resource.ComposeTestCheckFunc( 139 testAccContainerRunning("docker_container.foo", &c), 140 testCheck, 141 ), 142 }, 143 }, 144 }) 145 } 146 147 func testAccContainerRunning(n string, container *dc.Container) resource.TestCheckFunc { 148 return func(s *terraform.State) error { 149 rs, ok := s.RootModule().Resources[n] 150 if !ok { 151 return fmt.Errorf("Not found: %s", n) 152 } 153 154 if rs.Primary.ID == "" { 155 return fmt.Errorf("No ID is set") 156 } 157 158 client := testAccProvider.Meta().(*dc.Client) 159 containers, err := client.ListContainers(dc.ListContainersOptions{}) 160 if err != nil { 161 return err 162 } 163 164 for _, c := range containers { 165 if c.ID == rs.Primary.ID { 166 inspected, err := client.InspectContainer(c.ID) 167 if err != nil { 168 return fmt.Errorf("Container could not be inspected: %s", err) 169 } 170 *container = *inspected 171 return nil 172 } 173 } 174 175 return fmt.Errorf("Container not found: %s", rs.Primary.ID) 176 } 177 } 178 179 const testAccDockerContainerConfig = ` 180 resource "docker_image" "foo" { 181 name = "nginx:latest" 182 } 183 184 resource "docker_container" "foo" { 185 name = "tf-test" 186 image = "${docker_image.foo.latest}" 187 } 188 ` 189 190 const testAccDockerContainerVolumeConfig = ` 191 resource "docker_image" "foo" { 192 name = "nginx:latest" 193 } 194 195 resource "docker_volume" "foo" { 196 name = "testAccDockerContainerVolume_volume" 197 } 198 199 resource "docker_container" "foo" { 200 name = "tf-test" 201 image = "${docker_image.foo.latest}" 202 203 volumes { 204 volume_name = "${docker_volume.foo.name}" 205 container_path = "/tmp/volume" 206 read_only = false 207 } 208 } 209 ` 210 211 const testAccDockerContainerCustomizedConfig = ` 212 resource "docker_image" "foo" { 213 name = "nginx:latest" 214 } 215 216 resource "docker_container" "foo" { 217 name = "tf-test" 218 image = "${docker_image.foo.latest}" 219 entrypoint = ["/bin/bash", "-c", "ping localhost"] 220 restart = "on-failure" 221 max_retry_count = 5 222 memory = 512 223 memory_swap = 2048 224 cpu_shares = 32 225 labels { 226 env = "prod" 227 role = "test" 228 } 229 log_driver = "json-file" 230 log_opts = { 231 max-size = "10m" 232 max-file = 20 233 } 234 network_mode = "bridge" 235 236 host { 237 host = "testhost" 238 ip = "10.0.1.0" 239 } 240 241 host { 242 host = "testhost2" 243 ip = "10.0.2.0" 244 } 245 } 246 `