github.com/jsoriano/terraform@v0.6.7-0.20151026070445-8b70867fdd95/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  	resource.Test(t, resource.TestCase{
    14  		PreCheck:  func() { testAccPreCheck(t) },
    15  		Providers: testAccProviders,
    16  		Steps: []resource.TestStep{
    17  			resource.TestStep{
    18  				Config: testAccDockerContainerConfig,
    19  				Check: resource.ComposeTestCheckFunc(
    20  					testAccContainerRunning("docker_container.foo"),
    21  				),
    22  			},
    23  		},
    24  	})
    25  }
    26  
    27  func testAccContainerRunning(n string) resource.TestCheckFunc {
    28  	return func(s *terraform.State) error {
    29  		rs, ok := s.RootModule().Resources[n]
    30  		if !ok {
    31  			return fmt.Errorf("Not found: %s", n)
    32  		}
    33  
    34  		if rs.Primary.ID == "" {
    35  			return fmt.Errorf("No ID is set")
    36  		}
    37  
    38  		client := testAccProvider.Meta().(*dc.Client)
    39  		containers, err := client.ListContainers(dc.ListContainersOptions{})
    40  		if err != nil {
    41  			return err
    42  		}
    43  
    44  		for _, c := range containers {
    45  			if c.ID == rs.Primary.ID {
    46  				return nil
    47  			}
    48  		}
    49  
    50  		return fmt.Errorf("Container not found: %s", rs.Primary.ID)
    51  	}
    52  }
    53  
    54  const testAccDockerContainerConfig = `
    55  resource "docker_image" "foo" {
    56  	name = "nginx:latest"
    57  }
    58  
    59  resource "docker_container" "foo" {
    60  	name = "tf-test"
    61  	image = "${docker_image.foo.latest}"
    62  }
    63  `