github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/docker/resource_docker_image_test.go (about) 1 package docker 2 3 import ( 4 "fmt" 5 "regexp" 6 "testing" 7 8 dc "github.com/fsouza/go-dockerclient" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 var contentDigestRegexp = regexp.MustCompile(`\A[A-Za-z0-9_\+\.-]+:[A-Fa-f0-9]+\z`) 14 15 func TestAccDockerImage_basic(t *testing.T) { 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccDockerImageDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccDockerImageConfig, 23 Check: resource.ComposeTestCheckFunc( 24 resource.TestMatchResourceAttr("docker_image.foo", "latest", contentDigestRegexp), 25 ), 26 }, 27 }, 28 }) 29 } 30 31 func TestAccDockerImage_private(t *testing.T) { 32 resource.Test(t, resource.TestCase{ 33 PreCheck: func() { testAccPreCheck(t) }, 34 Providers: testAccProviders, 35 CheckDestroy: testAccDockerImageDestroy, 36 Steps: []resource.TestStep{ 37 resource.TestStep{ 38 Config: testAddDockerPrivateImageConfig, 39 Check: resource.ComposeTestCheckFunc( 40 resource.TestMatchResourceAttr("docker_image.foobar", "latest", contentDigestRegexp), 41 ), 42 }, 43 }, 44 }) 45 } 46 47 func TestAccDockerImage_destroy(t *testing.T) { 48 resource.Test(t, resource.TestCase{ 49 PreCheck: func() { testAccPreCheck(t) }, 50 Providers: testAccProviders, 51 CheckDestroy: func(s *terraform.State) error { 52 for _, rs := range s.RootModule().Resources { 53 if rs.Type != "docker_image" { 54 continue 55 } 56 57 client := testAccProvider.Meta().(*dc.Client) 58 _, err := client.InspectImage(rs.Primary.Attributes["latest"]) 59 if err != nil { 60 return err 61 } 62 } 63 return nil 64 }, 65 Steps: []resource.TestStep{ 66 resource.TestStep{ 67 Config: testAccDockerImageKeepLocallyConfig, 68 Check: resource.ComposeTestCheckFunc( 69 resource.TestMatchResourceAttr("docker_image.foobarzoo", "latest", contentDigestRegexp), 70 ), 71 }, 72 }, 73 }) 74 } 75 76 func TestAccDockerImage_data(t *testing.T) { 77 resource.Test(t, resource.TestCase{ 78 PreCheck: func() { testAccPreCheck(t) }, 79 Providers: testAccProviders, 80 PreventPostDestroyRefresh: true, 81 Steps: []resource.TestStep{ 82 resource.TestStep{ 83 Config: testAccDockerImageFromDataConfig, 84 Check: resource.ComposeTestCheckFunc( 85 resource.TestMatchResourceAttr("docker_image.foobarbaz", "latest", contentDigestRegexp), 86 ), 87 }, 88 }, 89 }) 90 } 91 92 func TestAccDockerImage_data_pull_trigger(t *testing.T) { 93 resource.Test(t, resource.TestCase{ 94 PreCheck: func() { testAccPreCheck(t) }, 95 Providers: testAccProviders, 96 PreventPostDestroyRefresh: true, 97 Steps: []resource.TestStep{ 98 resource.TestStep{ 99 Config: testAccDockerImageFromDataConfigWithPullTrigger, 100 Check: resource.ComposeTestCheckFunc( 101 resource.TestMatchResourceAttr("docker_image.foobarbazoo", "latest", contentDigestRegexp), 102 ), 103 }, 104 }, 105 }) 106 } 107 108 func testAccDockerImageDestroy(s *terraform.State) error { 109 for _, rs := range s.RootModule().Resources { 110 if rs.Type != "docker_image" { 111 continue 112 } 113 114 client := testAccProvider.Meta().(*dc.Client) 115 _, err := client.InspectImage(rs.Primary.Attributes["latest"]) 116 if err == nil { 117 return fmt.Errorf("Image still exists") 118 } else if err != dc.ErrNoSuchImage { 119 return err 120 } 121 } 122 return nil 123 } 124 125 const testAccDockerImageConfig = ` 126 resource "docker_image" "foo" { 127 name = "alpine:3.1" 128 } 129 ` 130 131 const testAddDockerPrivateImageConfig = ` 132 resource "docker_image" "foobar" { 133 name = "gcr.io:443/google_containers/pause:0.8.0" 134 } 135 ` 136 137 const testAccDockerImageKeepLocallyConfig = ` 138 resource "docker_image" "foobarzoo" { 139 name = "crux:3.1" 140 keep_locally = true 141 } 142 ` 143 144 const testAccDockerImageFromDataConfig = ` 145 data "docker_registry_image" "foobarbaz" { 146 name = "alpine:3.1" 147 } 148 resource "docker_image" "foobarbaz" { 149 name = "${data.docker_registry_image.foobarbaz.name}" 150 pull_triggers = ["${data.docker_registry_image.foobarbaz.sha256_digest}"] 151 } 152 ` 153 154 const testAccDockerImageFromDataConfigWithPullTrigger = ` 155 data "docker_registry_image" "foobarbazoo" { 156 name = "alpine:3.1" 157 } 158 resource "docker_image" "foobarbazoo" { 159 name = "${data.docker_registry_image.foobarbazoo.name}" 160 pull_trigger = "${data.docker_registry_image.foobarbazoo.sha256_digest}" 161 } 162 `