github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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 testAccDockerImageDestroy(s *terraform.State) error {
    93  	for _, rs := range s.RootModule().Resources {
    94  		if rs.Type != "docker_image" {
    95  			continue
    96  		}
    97  
    98  		client := testAccProvider.Meta().(*dc.Client)
    99  		_, err := client.InspectImage(rs.Primary.Attributes["latest"])
   100  		if err == nil {
   101  			return fmt.Errorf("Image still exists")
   102  		} else if err != dc.ErrNoSuchImage {
   103  			return err
   104  		}
   105  	}
   106  	return nil
   107  }
   108  
   109  const testAccDockerImageConfig = `
   110  resource "docker_image" "foo" {
   111  	name = "alpine:3.1"
   112  }
   113  `
   114  
   115  const testAddDockerPrivateImageConfig = `
   116  resource "docker_image" "foobar" {
   117  	name = "gcr.io:443/google_containers/pause:0.8.0"
   118  }
   119  `
   120  
   121  const testAccDockerImageKeepLocallyConfig = `
   122  resource "docker_image" "foobarzoo" {
   123  	name = "crux:3.1"
   124  	keep_locally = true
   125  }
   126  `
   127  
   128  const testAccDockerImageFromDataConfig = `
   129  data "docker_registry_image" "foobarbaz" {
   130  	name = "alpine:3.1"
   131  }
   132  resource "docker_image" "foobarbaz" {
   133  	name = "${data.docker_registry_image.foobarbaz.name}"
   134  	pull_trigger = "${data.docker_registry_image.foobarbaz.sha256_digest}"
   135  }
   136  `