2015-03-27 23:33:17 +01:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2015-03-29 03:45:36 +02:00
|
|
|
"fmt"
|
2015-03-27 23:33:17 +01:00
|
|
|
"testing"
|
|
|
|
|
2015-03-29 03:45:36 +02:00
|
|
|
dc "github.com/fsouza/go-dockerclient"
|
2015-03-27 23:33:17 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2015-03-29 03:45:36 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-03-27 23:33:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccDockerContainer_basic(t *testing.T) {
|
2015-10-26 22:24:48 +01:00
|
|
|
var c dc.Container
|
2015-03-27 23:33:17 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccDockerContainerConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2015-10-26 22:24:48 +01:00
|
|
|
testAccContainerRunning("docker_container.foo", &c),
|
2015-03-27 23:33:17 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-10-27 17:08:57 +01:00
|
|
|
func TestAccDockerContainer_customized(t *testing.T) {
|
2015-10-26 22:24:48 +01:00
|
|
|
var c dc.Container
|
|
|
|
|
|
|
|
testCheck := func(*terraform.State) error {
|
|
|
|
if len(c.Config.Entrypoint) < 3 ||
|
|
|
|
(c.Config.Entrypoint[0] != "/bin/bash" &&
|
|
|
|
c.Config.Entrypoint[1] != "-c" &&
|
|
|
|
c.Config.Entrypoint[2] != "ping localhost") {
|
|
|
|
return fmt.Errorf("Container wrong entrypoint: %s", c.Config.Entrypoint)
|
|
|
|
}
|
2015-10-27 17:08:57 +01:00
|
|
|
|
|
|
|
if c.HostConfig.RestartPolicy.Name == "on-failure" {
|
|
|
|
if c.HostConfig.RestartPolicy.MaximumRetryCount != 5 {
|
|
|
|
return fmt.Errorf("Container has wrong restart policy max retry count: %d", c.HostConfig.RestartPolicy.MaximumRetryCount)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Container has wrong restart policy: %s", c.HostConfig.RestartPolicy.Name)
|
|
|
|
}
|
|
|
|
|
2015-10-28 00:53:49 +01:00
|
|
|
if c.HostConfig.Memory != (128 * 1024 * 1024) {
|
|
|
|
return fmt.Errorf("Container has wrong memory setting: %d", c.HostConfig.Memory)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HostConfig.MemorySwap != (128 * 1024 * 1024) {
|
|
|
|
return fmt.Errorf("Container has wrong memory swap setting: %d", c.HostConfig.Memory)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HostConfig.CPUShares != 512 {
|
|
|
|
return fmt.Errorf("Container has wrong cpu shares setting: %d", c.HostConfig.CPUShares)
|
|
|
|
}
|
2015-10-26 22:24:48 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2015-10-27 17:08:57 +01:00
|
|
|
Config: testAccDockerContainerCustomizedConfig,
|
2015-10-26 22:24:48 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccContainerRunning("docker_container.foo", &c),
|
|
|
|
testCheck,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccContainerRunning(n string, container *dc.Container) resource.TestCheckFunc {
|
2015-03-29 03:45:36 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
client := testAccProvider.Meta().(*dc.Client)
|
|
|
|
containers, err := client.ListContainers(dc.ListContainersOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range containers {
|
|
|
|
if c.ID == rs.Primary.ID {
|
2015-10-26 22:24:48 +01:00
|
|
|
inspected, err := client.InspectContainer(c.ID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Container could not be inspected: %s", err)
|
|
|
|
}
|
|
|
|
*container = *inspected
|
2015-03-29 03:45:36 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Container not found: %s", rs.Primary.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 23:33:17 +01:00
|
|
|
const testAccDockerContainerConfig = `
|
|
|
|
resource "docker_image" "foo" {
|
2015-06-29 23:09:05 +02:00
|
|
|
name = "nginx:latest"
|
2015-03-27 23:33:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "docker_container" "foo" {
|
|
|
|
name = "tf-test"
|
|
|
|
image = "${docker_image.foo.latest}"
|
|
|
|
}
|
|
|
|
`
|
2015-10-27 17:08:57 +01:00
|
|
|
const testAccDockerContainerCustomizedConfig = `
|
2015-10-26 22:24:48 +01:00
|
|
|
resource "docker_image" "foo" {
|
|
|
|
name = "nginx:latest"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "docker_container" "foo" {
|
|
|
|
name = "tf-test"
|
|
|
|
image = "${docker_image.foo.latest}"
|
|
|
|
entrypoint = ["/bin/bash", "-c", "ping localhost"]
|
2015-10-27 17:08:57 +01:00
|
|
|
restart = "on-failure"
|
|
|
|
max_retry_count = 5
|
2015-10-28 00:53:49 +01:00
|
|
|
memory = 128
|
|
|
|
memory_swap = 128
|
|
|
|
cpu_shares = 512
|
2015-10-26 22:24:48 +01:00
|
|
|
}
|
|
|
|
`
|