github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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_customized(t *testing.T) { 29 var c dc.Container 30 31 testCheck := func(*terraform.State) error { 32 if len(c.Config.Entrypoint) < 3 || 33 (c.Config.Entrypoint[0] != "/bin/bash" && 34 c.Config.Entrypoint[1] != "-c" && 35 c.Config.Entrypoint[2] != "ping localhost") { 36 return fmt.Errorf("Container wrong entrypoint: %s", c.Config.Entrypoint) 37 } 38 39 if c.HostConfig.RestartPolicy.Name == "on-failure" { 40 if c.HostConfig.RestartPolicy.MaximumRetryCount != 5 { 41 return fmt.Errorf("Container has wrong restart policy max retry count: %d", c.HostConfig.RestartPolicy.MaximumRetryCount) 42 } 43 } else { 44 return fmt.Errorf("Container has wrong restart policy: %s", c.HostConfig.RestartPolicy.Name) 45 } 46 47 if c.HostConfig.Memory != (512 * 1024 * 1024) { 48 return fmt.Errorf("Container has wrong memory setting: %d", c.HostConfig.Memory) 49 } 50 51 if c.HostConfig.MemorySwap != (2048 * 1024 * 1024) { 52 return fmt.Errorf("Container has wrong memory swap setting: %d", c.HostConfig.MemorySwap) 53 } 54 55 if c.HostConfig.CPUShares != 32 { 56 return fmt.Errorf("Container has wrong cpu shares setting: %d", c.HostConfig.CPUShares) 57 } 58 59 if c.Config.Labels["env"] != "prod" || c.Config.Labels["role"] != "test" { 60 return fmt.Errorf("Container does not have the correct labels") 61 } 62 63 if c.HostConfig.LogConfig.Type != "json-file" { 64 return fmt.Errorf("Container does not have the correct log config: %s", c.HostConfig.LogConfig.Type) 65 } 66 67 if c.HostConfig.LogConfig.Config["max-size"] != "10m" { 68 return fmt.Errorf("Container does not have the correct max-size log option: %v", c.HostConfig.LogConfig.Config["max-size"]) 69 } 70 71 if c.HostConfig.LogConfig.Config["max-file"] != "20" { 72 return fmt.Errorf("Container does not have the correct max-file log option: %v", c.HostConfig.LogConfig.Config["max-file"]) 73 } 74 75 return nil 76 } 77 78 resource.Test(t, resource.TestCase{ 79 PreCheck: func() { testAccPreCheck(t) }, 80 Providers: testAccProviders, 81 Steps: []resource.TestStep{ 82 resource.TestStep{ 83 Config: testAccDockerContainerCustomizedConfig, 84 Check: resource.ComposeTestCheckFunc( 85 testAccContainerRunning("docker_container.foo", &c), 86 testCheck, 87 ), 88 }, 89 }, 90 }) 91 } 92 93 func testAccContainerRunning(n string, container *dc.Container) resource.TestCheckFunc { 94 return func(s *terraform.State) error { 95 rs, ok := s.RootModule().Resources[n] 96 if !ok { 97 return fmt.Errorf("Not found: %s", n) 98 } 99 100 if rs.Primary.ID == "" { 101 return fmt.Errorf("No ID is set") 102 } 103 104 client := testAccProvider.Meta().(*dc.Client) 105 containers, err := client.ListContainers(dc.ListContainersOptions{}) 106 if err != nil { 107 return err 108 } 109 110 for _, c := range containers { 111 if c.ID == rs.Primary.ID { 112 inspected, err := client.InspectContainer(c.ID) 113 if err != nil { 114 return fmt.Errorf("Container could not be inspected: %s", err) 115 } 116 *container = *inspected 117 return nil 118 } 119 } 120 121 return fmt.Errorf("Container not found: %s", rs.Primary.ID) 122 } 123 } 124 125 const testAccDockerContainerConfig = ` 126 resource "docker_image" "foo" { 127 name = "nginx:latest" 128 } 129 130 resource "docker_container" "foo" { 131 name = "tf-test" 132 image = "${docker_image.foo.latest}" 133 } 134 ` 135 const testAccDockerContainerCustomizedConfig = ` 136 resource "docker_image" "foo" { 137 name = "nginx:latest" 138 } 139 140 resource "docker_container" "foo" { 141 name = "tf-test" 142 image = "${docker_image.foo.latest}" 143 entrypoint = ["/bin/bash", "-c", "ping localhost"] 144 restart = "on-failure" 145 max_retry_count = 5 146 memory = 512 147 memory_swap = 2048 148 cpu_shares = 32 149 labels { 150 env = "prod" 151 role = "test" 152 } 153 log_driver = "json-file" 154 log_opts = { 155 max-size = "10m" 156 max-file = 20 157 } 158 } 159 `