2015-03-27 23:22:33 +01:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2016-03-23 05:56:51 +01:00
|
|
|
"fmt"
|
2016-01-14 10:12:05 +01:00
|
|
|
"regexp"
|
2015-07-03 11:58:05 +02:00
|
|
|
"testing"
|
2015-10-08 10:47:50 +02:00
|
|
|
|
2016-03-23 05:56:51 +01:00
|
|
|
dc "github.com/fsouza/go-dockerclient"
|
2015-10-08 10:47:50 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2016-03-23 05:56:51 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-03-27 23:22:33 +01:00
|
|
|
)
|
|
|
|
|
2016-03-06 16:22:11 +01:00
|
|
|
var contentDigestRegexp = regexp.MustCompile(`\A[A-Za-z0-9_\+\.-]+:[A-Fa-f0-9]+\z`)
|
|
|
|
|
2015-03-27 23:22:33 +01:00
|
|
|
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,
|
2015-03-27 23:22:33 +01:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccDockerImageConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-03-06 16:22:11 +01:00
|
|
|
resource.TestMatchResourceAttr("docker_image.foo", "latest", contentDigestRegexp),
|
2015-03-27 23:22:33 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-14 10:12:05 +01:00
|
|
|
func TestAccDockerImage_private(t *testing.T) {
|
2015-07-03 11:58:05 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
2016-03-23 05:56:51 +01:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccDockerImageDestroy,
|
2015-07-03 11:58:05 +02:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAddDockerPrivateImageConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-03-06 16:22:11 +01:00
|
|
|
resource.TestMatchResourceAttr("docker_image.foobar", "latest", contentDigestRegexp),
|
2015-07-03 11:58:05 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-27 18:18:02 +02:00
|
|
|
func TestAccDockerImage_destroy(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: func(s *terraform.State) error {
|
|
|
|
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 err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccDockerImageKeepLocallyConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
resource.TestMatchResourceAttr("docker_image.foobarzoo", "latest", contentDigestRegexp),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2016-03-23 05:56:51 +01:00
|
|
|
|
2016-07-26 17:18:38 +02:00
|
|
|
func TestAccDockerImage_data(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
PreventPostDestroyRefresh: true,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccDockerImageFromDataConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
resource.TestMatchResourceAttr("docker_image.foobarbaz", "latest", contentDigestRegexp),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-27 18:18:02 +02:00
|
|
|
func testAccDockerImageDestroy(s *terraform.State) error {
|
2016-03-23 05:56:51 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-03-27 23:22:33 +01:00
|
|
|
const testAccDockerImageConfig = `
|
|
|
|
resource "docker_image" "foo" {
|
2015-12-02 21:03:29 +01:00
|
|
|
name = "alpine:3.1"
|
2015-03-27 23:22:33 +01:00
|
|
|
}
|
|
|
|
`
|
2015-07-03 11:58:05 +02:00
|
|
|
|
|
|
|
const testAddDockerPrivateImageConfig = `
|
|
|
|
resource "docker_image" "foobar" {
|
|
|
|
name = "gcr.io:443/google_containers/pause:0.8.0"
|
|
|
|
}
|
|
|
|
`
|
2016-04-27 18:18:02 +02:00
|
|
|
|
|
|
|
const testAccDockerImageKeepLocallyConfig = `
|
|
|
|
resource "docker_image" "foobarzoo" {
|
|
|
|
name = "crux:3.1"
|
|
|
|
keep_locally = true
|
|
|
|
}
|
|
|
|
`
|
2016-07-26 17:18:38 +02:00
|
|
|
|
|
|
|
const testAccDockerImageFromDataConfig = `
|
|
|
|
data "docker_registry_image" "foobarbaz" {
|
|
|
|
name = "alpine:3.1"
|
|
|
|
}
|
|
|
|
resource "docker_image" "foobarbaz" {
|
|
|
|
name = "${data.docker_registry_image.foobarbaz.name}"
|
|
|
|
pull_trigger = "${data.docker_registry_image.foobarbaz.sha256_digest}"
|
|
|
|
}
|
|
|
|
`
|