diff --git a/docker/resource_docker_container_test.go b/docker/resource_docker_container_test.go index 3d5a425c..86b5b99b 100644 --- a/docker/resource_docker_container_test.go +++ b/docker/resource_docker_container_test.go @@ -613,6 +613,23 @@ func TestAccDockerContainer_multiple_ports(t *testing.T) { }) } +func TestAccDockerContainer_nostart(t *testing.T) { + var c types.ContainerJSON + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccDockerContainerNoStartConfig, + Check: resource.ComposeTestCheckFunc( + testAccContainerNotRunning("docker_container.foo", &c), + ), + }, + }, + }) +} + func testAccContainerRunning(n string, container *types.ContainerJSON) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -645,6 +662,33 @@ func testAccContainerRunning(n string, container *types.ContainerJSON) resource. } } +func testAccContainerNotRunning(n string, container *types.ContainerJSON) 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().(*ProviderConfig).DockerClient + containers, err := client.ContainerList(context.Background(), types.ContainerListOptions{}) + if err != nil { + return err + } + + for _, c := range containers { + if c.ID == rs.Primary.ID { + return fmt.Errorf("Container found: %s", rs.Primary.ID) + } + } + + return nil + } +} + func testValueHigherEqualThan(name, key string, value int) resource.TestCheckFunc { return func(s *terraform.State) error { ms := s.RootModule() @@ -885,3 +929,16 @@ resource "docker_container" "foo" { ] } ` +const testAccDockerContainerNoStartConfig = ` +resource "docker_image" "foo" { + name = "nginx:latest" + keep_locally = true +} + +resource "docker_container" "foo" { + name = "tf-test" + image = "nginx:latest" + start = false + must_run = false +} +`