github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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) != 1 { 33 return fmt.Errorf("Incorrect number of mounts: expected 1, 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.Config.User != "root:root" { 82 return fmt.Errorf("Container wrong user: %s", c.Config.User) 83 } 84 85 if c.HostConfig.RestartPolicy.Name == "on-failure" { 86 if c.HostConfig.RestartPolicy.MaximumRetryCount != 5 { 87 return fmt.Errorf("Container has wrong restart policy max retry count: %d", c.HostConfig.RestartPolicy.MaximumRetryCount) 88 } 89 } else { 90 return fmt.Errorf("Container has wrong restart policy: %s", c.HostConfig.RestartPolicy.Name) 91 } 92 93 if c.HostConfig.Memory != (512 * 1024 * 1024) { 94 return fmt.Errorf("Container has wrong memory setting: %d", c.HostConfig.Memory) 95 } 96 97 if c.HostConfig.MemorySwap != (2048 * 1024 * 1024) { 98 return fmt.Errorf("Container has wrong memory swap setting: %d\n\r\tPlease check that you machine supports memory swap (you can do that by running 'docker info' command).", c.HostConfig.MemorySwap) 99 } 100 101 if c.HostConfig.CPUShares != 32 { 102 return fmt.Errorf("Container has wrong cpu shares setting: %d", c.HostConfig.CPUShares) 103 } 104 105 if len(c.HostConfig.DNS) != 1 { 106 return fmt.Errorf("Container does not have the correct number of dns entries: %d", len(c.HostConfig.DNS)) 107 } 108 109 if c.HostConfig.DNS[0] != "8.8.8.8" { 110 return fmt.Errorf("Container has wrong dns setting: %v", c.HostConfig.DNS[0]) 111 } 112 113 if len(c.HostConfig.DNSOptions) != 1 { 114 return fmt.Errorf("Container does not have the correct number of dns option entries: %d", len(c.HostConfig.DNS)) 115 } 116 117 if c.HostConfig.DNSOptions[0] != "rotate" { 118 return fmt.Errorf("Container has wrong dns option setting: %v", c.HostConfig.DNS[0]) 119 } 120 121 if len(c.HostConfig.DNSSearch) != 1 { 122 return fmt.Errorf("Container does not have the correct number of dns search entries: %d", len(c.HostConfig.DNS)) 123 } 124 125 if c.HostConfig.DNSSearch[0] != "example.com" { 126 return fmt.Errorf("Container has wrong dns search setting: %v", c.HostConfig.DNS[0]) 127 } 128 129 if c.HostConfig.CPUShares != 32 { 130 return fmt.Errorf("Container has wrong cpu shares setting: %d", c.HostConfig.CPUShares) 131 } 132 133 if c.HostConfig.CPUShares != 32 { 134 return fmt.Errorf("Container has wrong cpu shares setting: %d", c.HostConfig.CPUShares) 135 } 136 137 if c.Config.Labels["env"] != "prod" || c.Config.Labels["role"] != "test" { 138 return fmt.Errorf("Container does not have the correct labels") 139 } 140 141 if c.HostConfig.LogConfig.Type != "json-file" { 142 return fmt.Errorf("Container does not have the correct log config: %s", c.HostConfig.LogConfig.Type) 143 } 144 145 if c.HostConfig.LogConfig.Config["max-size"] != "10m" { 146 return fmt.Errorf("Container does not have the correct max-size log option: %v", c.HostConfig.LogConfig.Config["max-size"]) 147 } 148 149 if c.HostConfig.LogConfig.Config["max-file"] != "20" { 150 return fmt.Errorf("Container does not have the correct max-file log option: %v", c.HostConfig.LogConfig.Config["max-file"]) 151 } 152 153 if len(c.HostConfig.ExtraHosts) != 2 { 154 return fmt.Errorf("Container does not have correct number of extra host entries, got %d", len(c.HostConfig.ExtraHosts)) 155 } 156 157 if c.HostConfig.ExtraHosts[0] != "testhost2:10.0.2.0" { 158 return fmt.Errorf("Container has incorrect extra host string: %q", c.HostConfig.ExtraHosts[0]) 159 } 160 161 if c.HostConfig.ExtraHosts[1] != "testhost:10.0.1.0" { 162 return fmt.Errorf("Container has incorrect extra host string: %q", c.HostConfig.ExtraHosts[1]) 163 } 164 165 return nil 166 } 167 168 resource.Test(t, resource.TestCase{ 169 PreCheck: func() { testAccPreCheck(t) }, 170 Providers: testAccProviders, 171 Steps: []resource.TestStep{ 172 resource.TestStep{ 173 Config: testAccDockerContainerCustomizedConfig, 174 Check: resource.ComposeTestCheckFunc( 175 testAccContainerRunning("docker_container.foo", &c), 176 testCheck, 177 ), 178 }, 179 }, 180 }) 181 } 182 183 func testAccContainerRunning(n string, container *dc.Container) resource.TestCheckFunc { 184 return func(s *terraform.State) error { 185 rs, ok := s.RootModule().Resources[n] 186 if !ok { 187 return fmt.Errorf("Not found: %s", n) 188 } 189 190 if rs.Primary.ID == "" { 191 return fmt.Errorf("No ID is set") 192 } 193 194 client := testAccProvider.Meta().(*dc.Client) 195 containers, err := client.ListContainers(dc.ListContainersOptions{}) 196 if err != nil { 197 return err 198 } 199 200 for _, c := range containers { 201 if c.ID == rs.Primary.ID { 202 inspected, err := client.InspectContainer(c.ID) 203 if err != nil { 204 return fmt.Errorf("Container could not be inspected: %s", err) 205 } 206 *container = *inspected 207 return nil 208 } 209 } 210 211 return fmt.Errorf("Container not found: %s", rs.Primary.ID) 212 } 213 } 214 215 const testAccDockerContainerConfig = ` 216 resource "docker_image" "foo" { 217 name = "nginx:latest" 218 } 219 220 resource "docker_container" "foo" { 221 name = "tf-test" 222 image = "${docker_image.foo.latest}" 223 } 224 ` 225 226 const testAccDockerContainerVolumeConfig = ` 227 resource "docker_image" "foo" { 228 name = "nginx:latest" 229 } 230 231 resource "docker_volume" "foo" { 232 name = "testAccDockerContainerVolume_volume" 233 } 234 235 resource "docker_container" "foo" { 236 name = "tf-test" 237 image = "${docker_image.foo.latest}" 238 239 volumes { 240 volume_name = "${docker_volume.foo.name}" 241 container_path = "/tmp/volume" 242 read_only = false 243 } 244 } 245 ` 246 247 const testAccDockerContainerCustomizedConfig = ` 248 resource "docker_image" "foo" { 249 name = "nginx:latest" 250 } 251 252 resource "docker_container" "foo" { 253 name = "tf-test" 254 image = "${docker_image.foo.latest}" 255 entrypoint = ["/bin/bash", "-c", "ping localhost"] 256 user = "root:root" 257 restart = "on-failure" 258 destroy_grace_seconds = 10 259 max_retry_count = 5 260 memory = 512 261 memory_swap = 2048 262 cpu_shares = 32 263 dns = ["8.8.8.8"] 264 dns_opts = ["rotate"] 265 dns_search = ["example.com"] 266 labels { 267 env = "prod" 268 role = "test" 269 } 270 log_driver = "json-file" 271 log_opts = { 272 max-size = "10m" 273 max-file = 20 274 } 275 network_mode = "bridge" 276 277 host { 278 host = "testhost" 279 ip = "10.0.1.0" 280 } 281 282 host { 283 host = "testhost2" 284 ip = "10.0.2.0" 285 } 286 } 287 `