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) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccDockerContainerConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2015-03-29 03:45:36 +02:00
|
|
|
testAccContainerRunning("docker_container.foo"),
|
2015-03-27 23:33:17 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-29 03:45:36 +02:00
|
|
|
func testAccContainerRunning(n string) resource.TestCheckFunc {
|
|
|
|
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 {
|
|
|
|
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}"
|
|
|
|
}
|
|
|
|
`
|