terraform/builtin/providers/docker/resource_docker_image_test.go

79 lines
1.9 KiB
Go
Raw Normal View History

package docker
import (
2016-03-23 05:56:51 +01:00
"fmt"
"regexp"
"testing"
2016-03-23 05:56:51 +01:00
dc "github.com/fsouza/go-dockerclient"
"github.com/hashicorp/terraform/helper/resource"
2016-03-23 05:56:51 +01:00
"github.com/hashicorp/terraform/terraform"
)
var contentDigestRegexp = regexp.MustCompile(`\A[A-Za-z0-9_\+\.-]+:[A-Fa-f0-9]+\z`)
func TestAccDockerImage_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
2016-03-23 05:56:51 +01:00
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccDockerImageDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDockerImageConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("docker_image.foo", "latest", contentDigestRegexp),
),
},
},
})
}
func TestAccDockerImage_private(t *testing.T) {
resource.Test(t, resource.TestCase{
2016-03-23 05:56:51 +01:00
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccDockerImageDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAddDockerPrivateImageConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("docker_image.foobar", "latest", contentDigestRegexp),
),
},
},
})
}
2016-03-23 05:56:51 +01:00
func testAccDockerImageDestroy(s *terraform.State) error {
//client := testAccProvider.Meta().(*dc.Client)
for _, rs := range s.RootModule().Resources {
if rs.Type != "docker_image" {
continue
}
client := testAccProvider.Meta().(*dc.Client)
_, err := client.InspectImage(rs.Primary.Attributes["latest"])
if err == nil {
return fmt.Errorf("Image still exists")
} else if err != dc.ErrNoSuchImage {
return err
}
}
return nil
}
const testAccDockerImageConfig = `
resource "docker_image" "foo" {
name = "alpine:3.1"
keep_updated = false
}
`
const testAddDockerPrivateImageConfig = `
resource "docker_image" "foobar" {
name = "gcr.io:443/google_containers/pause:0.8.0"
keep_updated = true
}
`