github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/kubernetes/resource_kubernetes_persistent_volume_claim_test.go (about)

     1  package kubernetes
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	api "k8s.io/kubernetes/pkg/api/v1"
    11  	kubernetes "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
    12  )
    13  
    14  func TestAccKubernetesPersistentVolumeClaim_basic(t *testing.T) {
    15  	var conf api.PersistentVolumeClaim
    16  	name := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:      func() { testAccPreCheck(t) },
    20  		IDRefreshName: "kubernetes_persistent_volume_claim.test",
    21  		Providers:     testAccProviders,
    22  		CheckDestroy:  testAccCheckKubernetesPersistentVolumeClaimDestroy,
    23  		Steps: []resource.TestStep{
    24  			{
    25  				Config: testAccKubernetesPersistentVolumeClaimConfig_basic(name),
    26  				Check: resource.ComposeAggregateTestCheckFunc(
    27  					testAccCheckKubernetesPersistentVolumeClaimExists("kubernetes_persistent_volume_claim.test", &conf),
    28  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.annotations.%", "1"),
    29  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.annotations.TestAnnotationOne", "one"),
    30  					testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{"TestAnnotationOne": "one"}),
    31  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.labels.%", "3"),
    32  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.labels.TestLabelOne", "one"),
    33  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.labels.TestLabelThree", "three"),
    34  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.labels.TestLabelFour", "four"),
    35  					testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{"TestLabelOne": "one", "TestLabelThree": "three", "TestLabelFour": "four"}),
    36  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.name", name),
    37  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.generation"),
    38  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.resource_version"),
    39  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.self_link"),
    40  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.uid"),
    41  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.access_modes.#", "1"),
    42  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.access_modes.1254135962", "ReadWriteMany"),
    43  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.#", "1"),
    44  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.0.requests.%", "1"),
    45  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.0.requests.storage", "5Gi"),
    46  				),
    47  			},
    48  			{
    49  				Config: testAccKubernetesPersistentVolumeClaimConfig_metaModified(name),
    50  				Check: resource.ComposeAggregateTestCheckFunc(
    51  					testAccCheckKubernetesPersistentVolumeClaimExists("kubernetes_persistent_volume_claim.test", &conf),
    52  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.annotations.%", "2"),
    53  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.annotations.TestAnnotationOne", "one"),
    54  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.annotations.TestAnnotationTwo", "two"),
    55  					testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{"TestAnnotationOne": "one", "TestAnnotationTwo": "two"}),
    56  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.labels.%", "3"),
    57  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.labels.TestLabelOne", "one"),
    58  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.labels.TestLabelTwo", "two"),
    59  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.labels.TestLabelThree", "three"),
    60  					testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{"TestLabelOne": "one", "TestLabelTwo": "two", "TestLabelThree": "three"}),
    61  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.name", name),
    62  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.generation"),
    63  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.resource_version"),
    64  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.self_link"),
    65  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.uid"),
    66  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.access_modes.#", "1"),
    67  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.access_modes.1254135962", "ReadWriteMany"),
    68  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.#", "1"),
    69  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.0.requests.%", "1"),
    70  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.0.requests.storage", "5Gi"),
    71  				),
    72  			},
    73  		},
    74  	})
    75  }
    76  
    77  func TestAccKubernetesPersistentVolumeClaim_importBasic(t *testing.T) {
    78  	resourceName := "kubernetes_persistent_volume_claim.test"
    79  	volumeName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))
    80  	claimName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))
    81  
    82  	resource.Test(t, resource.TestCase{
    83  		PreCheck:     func() { testAccPreCheck(t) },
    84  		Providers:    testAccProviders,
    85  		CheckDestroy: testAccCheckKubernetesPersistentVolumeClaimDestroy,
    86  		Steps: []resource.TestStep{
    87  			{
    88  				Config: testAccKubernetesPersistentVolumeClaimConfig_import(volumeName, claimName),
    89  			},
    90  			{
    91  				ResourceName:      resourceName,
    92  				ImportState:       true,
    93  				ImportStateVerify: true,
    94  			},
    95  		},
    96  	})
    97  }
    98  
    99  func TestAccKubernetesPersistentVolumeClaim_volumeMatch(t *testing.T) {
   100  	var pvcConf api.PersistentVolumeClaim
   101  	var pvConf api.PersistentVolume
   102  
   103  	claimName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))
   104  	volumeName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))
   105  	volumeNameModified := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))
   106  
   107  	resource.Test(t, resource.TestCase{
   108  		PreCheck:      func() { testAccPreCheck(t) },
   109  		IDRefreshName: "kubernetes_persistent_volume_claim.test",
   110  		Providers:     testAccProviders,
   111  		CheckDestroy:  testAccCheckKubernetesPersistentVolumeClaimDestroy,
   112  		Steps: []resource.TestStep{
   113  			{
   114  				Config: testAccKubernetesPersistentVolumeClaimConfig_volumeMatch(volumeName, claimName),
   115  				Check: resource.ComposeAggregateTestCheckFunc(
   116  					testAccCheckKubernetesPersistentVolumeClaimExists("kubernetes_persistent_volume_claim.test", &pvcConf),
   117  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.annotations.%", "0"),
   118  					testAccCheckMetaAnnotations(&pvcConf.ObjectMeta, map[string]string{"pv.kubernetes.io/bind-completed": "yes"}),
   119  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.labels.%", "0"),
   120  					testAccCheckMetaLabels(&pvcConf.ObjectMeta, map[string]string{}),
   121  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.name", claimName),
   122  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.generation"),
   123  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.resource_version"),
   124  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.self_link"),
   125  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.uid"),
   126  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.access_modes.#", "1"),
   127  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.access_modes.1254135962", "ReadWriteMany"),
   128  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.#", "1"),
   129  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.0.requests.%", "1"),
   130  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.0.requests.storage", "5Gi"),
   131  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.volume_name", volumeName),
   132  					testAccCheckKubernetesPersistentVolumeExists("kubernetes_persistent_volume.test", &pvConf),
   133  					testAccCheckMetaAnnotations(&pvConf.ObjectMeta, map[string]string{"pv.kubernetes.io/bound-by-controller": "yes"}),
   134  				),
   135  			},
   136  			{
   137  				Config: testAccKubernetesPersistentVolumeClaimConfig_volumeMatch_modified(volumeNameModified, claimName),
   138  				Check: resource.ComposeAggregateTestCheckFunc(
   139  					testAccCheckKubernetesPersistentVolumeClaimExists("kubernetes_persistent_volume_claim.test", &pvcConf),
   140  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.annotations.%", "0"),
   141  					testAccCheckMetaAnnotations(&pvcConf.ObjectMeta, map[string]string{"pv.kubernetes.io/bind-completed": "yes"}),
   142  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.labels.%", "0"),
   143  					testAccCheckMetaLabels(&pvcConf.ObjectMeta, map[string]string{}),
   144  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.name", claimName),
   145  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.generation"),
   146  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.resource_version"),
   147  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.self_link"),
   148  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.uid"),
   149  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.access_modes.#", "1"),
   150  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.access_modes.1254135962", "ReadWriteMany"),
   151  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.#", "1"),
   152  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.0.requests.%", "1"),
   153  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.0.requests.storage", "5Gi"),
   154  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.volume_name", volumeNameModified),
   155  					testAccCheckKubernetesPersistentVolumeExists("kubernetes_persistent_volume.test2", &pvConf),
   156  					testAccCheckMetaAnnotations(&pvConf.ObjectMeta, map[string]string{"pv.kubernetes.io/bound-by-controller": "yes"}),
   157  				),
   158  			},
   159  		},
   160  	})
   161  }
   162  
   163  func TestAccKubernetesPersistentVolumeClaim_labelsMatch(t *testing.T) {
   164  	var conf api.PersistentVolumeClaim
   165  	claimName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))
   166  	volumeName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))
   167  
   168  	resource.Test(t, resource.TestCase{
   169  		PreCheck:      func() { testAccPreCheck(t) },
   170  		IDRefreshName: "kubernetes_persistent_volume_claim.test",
   171  		Providers:     testAccProviders,
   172  		CheckDestroy:  testAccCheckKubernetesPersistentVolumeClaimDestroy,
   173  		Steps: []resource.TestStep{
   174  			{
   175  				Config: testAccKubernetesPersistentVolumeClaimConfig_labelsMatch(volumeName, claimName),
   176  				Check: resource.ComposeAggregateTestCheckFunc(
   177  					testAccCheckKubernetesPersistentVolumeClaimExists("kubernetes_persistent_volume_claim.test", &conf),
   178  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.annotations.%", "0"),
   179  					testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{"pv.kubernetes.io/bind-completed": "yes", "pv.kubernetes.io/bound-by-controller": "yes"}),
   180  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.labels.%", "0"),
   181  					testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{}),
   182  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.name", claimName),
   183  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.generation"),
   184  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.resource_version"),
   185  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.self_link"),
   186  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.uid"),
   187  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.access_modes.#", "1"),
   188  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.access_modes.1254135962", "ReadWriteMany"),
   189  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.#", "1"),
   190  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.0.requests.%", "1"),
   191  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.0.requests.storage", "5Gi"),
   192  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.selector.#", "1"),
   193  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.selector.0.match_labels.%", "1"),
   194  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.selector.0.match_labels.TfAccTestEnvironment", "blablah"),
   195  				),
   196  			},
   197  		},
   198  	})
   199  }
   200  
   201  func TestAccKubernetesPersistentVolumeClaim_labelsMatchExpression(t *testing.T) {
   202  	var conf api.PersistentVolumeClaim
   203  	claimName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))
   204  	volumeName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))
   205  
   206  	resource.Test(t, resource.TestCase{
   207  		PreCheck:      func() { testAccPreCheck(t) },
   208  		IDRefreshName: "kubernetes_persistent_volume_claim.test",
   209  		Providers:     testAccProviders,
   210  		CheckDestroy:  testAccCheckKubernetesPersistentVolumeClaimDestroy,
   211  		Steps: []resource.TestStep{
   212  			{
   213  				Config: testAccKubernetesPersistentVolumeClaimConfig_labelsMatchExpression(volumeName, claimName),
   214  				Check: resource.ComposeAggregateTestCheckFunc(
   215  					testAccCheckKubernetesPersistentVolumeClaimExists("kubernetes_persistent_volume_claim.test", &conf),
   216  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.annotations.%", "0"),
   217  					testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{"pv.kubernetes.io/bind-completed": "yes", "pv.kubernetes.io/bound-by-controller": "yes"}),
   218  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.labels.%", "0"),
   219  					testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{}),
   220  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.name", claimName),
   221  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.generation"),
   222  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.resource_version"),
   223  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.self_link"),
   224  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.uid"),
   225  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.access_modes.#", "1"),
   226  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.access_modes.1254135962", "ReadWriteMany"),
   227  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.#", "1"),
   228  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.0.requests.%", "1"),
   229  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.0.requests.storage", "5Gi"),
   230  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.selector.#", "1"),
   231  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.selector.0.match_expressions.#", "1"),
   232  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.selector.0.match_expressions.0.key", "TfAccTestEnvironment"),
   233  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.selector.0.match_expressions.0.operator", "In"),
   234  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.selector.0.match_expressions.0.values.#", "3"),
   235  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.selector.0.match_expressions.0.values.1187371253", "three"),
   236  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.selector.0.match_expressions.0.values.2053932785", "one"),
   237  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.selector.0.match_expressions.0.values.298486374", "two"),
   238  				),
   239  			},
   240  		},
   241  	})
   242  }
   243  
   244  func TestAccKubernetesPersistentVolumeClaim_volumeUpdate(t *testing.T) {
   245  	var pvcConf api.PersistentVolumeClaim
   246  	var pvConf api.PersistentVolume
   247  
   248  	claimName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))
   249  	volumeName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(10))
   250  
   251  	resource.Test(t, resource.TestCase{
   252  		PreCheck:      func() { testAccPreCheck(t) },
   253  		IDRefreshName: "kubernetes_persistent_volume_claim.test",
   254  		Providers:     testAccProviders,
   255  		CheckDestroy:  testAccCheckKubernetesPersistentVolumeClaimDestroy,
   256  		Steps: []resource.TestStep{
   257  			{
   258  				Config: testAccKubernetesPersistentVolumeClaimConfig_volumeUpdate(volumeName, claimName, "5Gi"),
   259  				Check: resource.ComposeAggregateTestCheckFunc(
   260  					testAccCheckKubernetesPersistentVolumeClaimExists("kubernetes_persistent_volume_claim.test", &pvcConf),
   261  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.annotations.%", "0"),
   262  					testAccCheckMetaAnnotations(&pvcConf.ObjectMeta, map[string]string{"pv.kubernetes.io/bind-completed": "yes"}),
   263  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.labels.%", "0"),
   264  					testAccCheckMetaLabels(&pvcConf.ObjectMeta, map[string]string{}),
   265  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.name", claimName),
   266  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.generation"),
   267  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.resource_version"),
   268  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.self_link"),
   269  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.uid"),
   270  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.access_modes.#", "1"),
   271  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.access_modes.1254135962", "ReadWriteMany"),
   272  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.#", "1"),
   273  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.0.requests.%", "1"),
   274  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.0.requests.storage", "5Gi"),
   275  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.volume_name", volumeName),
   276  					testAccCheckKubernetesPersistentVolumeExists("kubernetes_persistent_volume.test", &pvConf),
   277  					testAccCheckMetaAnnotations(&pvConf.ObjectMeta, map[string]string{"pv.kubernetes.io/bound-by-controller": "yes"}),
   278  					testAccCheckClaimRef(&pvConf, &ObjectRefStatic{Namespace: "default", Name: claimName}),
   279  				),
   280  			},
   281  			{
   282  				Config: testAccKubernetesPersistentVolumeClaimConfig_volumeUpdate(volumeName, claimName, "10Gi"),
   283  				Check: resource.ComposeAggregateTestCheckFunc(
   284  					testAccCheckKubernetesPersistentVolumeClaimExists("kubernetes_persistent_volume_claim.test", &pvcConf),
   285  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.annotations.%", "0"),
   286  					testAccCheckMetaAnnotations(&pvcConf.ObjectMeta, map[string]string{"pv.kubernetes.io/bind-completed": "yes"}),
   287  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.labels.%", "0"),
   288  					testAccCheckMetaLabels(&pvcConf.ObjectMeta, map[string]string{}),
   289  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "metadata.0.name", claimName),
   290  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.generation"),
   291  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.resource_version"),
   292  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.self_link"),
   293  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume_claim.test", "metadata.0.uid"),
   294  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.access_modes.#", "1"),
   295  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.access_modes.1254135962", "ReadWriteMany"),
   296  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.#", "1"),
   297  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.0.requests.%", "1"),
   298  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.resources.0.requests.storage", "5Gi"),
   299  					resource.TestCheckResourceAttr("kubernetes_persistent_volume_claim.test", "spec.0.volume_name", volumeName),
   300  					testAccCheckKubernetesPersistentVolumeExists("kubernetes_persistent_volume.test", &pvConf),
   301  					testAccCheckMetaAnnotations(&pvConf.ObjectMeta, map[string]string{"pv.kubernetes.io/bound-by-controller": "yes"}),
   302  					testAccCheckClaimRef(&pvConf, &ObjectRefStatic{Namespace: "default", Name: claimName}),
   303  				),
   304  			},
   305  		},
   306  	})
   307  }
   308  
   309  func testAccCheckKubernetesPersistentVolumeClaimDestroy(s *terraform.State) error {
   310  	conn := testAccProvider.Meta().(*kubernetes.Clientset)
   311  
   312  	for _, rs := range s.RootModule().Resources {
   313  		if rs.Type != "kubernetes_persistent_volume_claim" {
   314  			continue
   315  		}
   316  		namespace, name := idParts(rs.Primary.ID)
   317  		resp, err := conn.CoreV1().PersistentVolumeClaims(namespace).Get(name)
   318  		if err == nil {
   319  			if resp.Namespace == namespace && resp.Name == name {
   320  				return fmt.Errorf("Persistent Volume still exists: %s", rs.Primary.ID)
   321  			}
   322  		}
   323  	}
   324  
   325  	return nil
   326  }
   327  
   328  func testAccCheckKubernetesPersistentVolumeClaimExists(n string, obj *api.PersistentVolumeClaim) resource.TestCheckFunc {
   329  	return func(s *terraform.State) error {
   330  		rs, ok := s.RootModule().Resources[n]
   331  		if !ok {
   332  			return fmt.Errorf("Not found: %s", n)
   333  		}
   334  
   335  		conn := testAccProvider.Meta().(*kubernetes.Clientset)
   336  		namespace, name := idParts(rs.Primary.ID)
   337  		out, err := conn.CoreV1().PersistentVolumeClaims(namespace).Get(name)
   338  		if err != nil {
   339  			return err
   340  		}
   341  
   342  		*obj = *out
   343  		return nil
   344  	}
   345  }
   346  
   347  func testAccCheckClaimRef(pv *api.PersistentVolume, expected *ObjectRefStatic) resource.TestCheckFunc {
   348  	return func(s *terraform.State) error {
   349  		or := pv.Spec.ClaimRef
   350  		if or == nil {
   351  			return fmt.Errorf("Expected ClaimRef to be not-nil, specifically %#v", *expected)
   352  		}
   353  		if or.Namespace != expected.Namespace {
   354  			return fmt.Errorf("Expected object reference %q, given: %q", expected.Namespace, or.Namespace)
   355  		}
   356  		if or.Name != expected.Name {
   357  			return fmt.Errorf("Expected object reference %q, given: %q", expected.Name, or.Name)
   358  		}
   359  		return nil
   360  	}
   361  }
   362  
   363  type ObjectRefStatic struct {
   364  	Namespace string
   365  	Name      string
   366  }
   367  
   368  func testAccKubernetesPersistentVolumeClaimConfig_basic(name string) string {
   369  	return fmt.Sprintf(`
   370  resource "kubernetes_persistent_volume_claim" "test" {
   371  	metadata {
   372  		annotations {
   373  			TestAnnotationOne = "one"
   374  		}
   375  		labels {
   376  			TestLabelOne = "one"
   377  			TestLabelThree = "three"
   378  			TestLabelFour = "four"
   379  		}
   380  		name = "%s"
   381  	}
   382  	spec {
   383  		access_modes = ["ReadWriteMany"]
   384  		resources {
   385  			requests {
   386  				storage = "5Gi"
   387  			}
   388  		}
   389  		selector {
   390  			match_expressions {
   391  				key = "environment"
   392  				operator = "In"
   393  				values = ["non-exists-12345"]
   394  			}
   395  		}
   396  	}
   397  	wait_until_bound = false
   398  }
   399  `, name)
   400  }
   401  
   402  func testAccKubernetesPersistentVolumeClaimConfig_metaModified(name string) string {
   403  	return fmt.Sprintf(`
   404  resource "kubernetes_persistent_volume_claim" "test" {
   405  	metadata {
   406  		annotations {
   407  			TestAnnotationOne = "one"
   408  			TestAnnotationTwo = "two"
   409  		}
   410  		labels {
   411  			TestLabelOne = "one"
   412  			TestLabelTwo = "two"
   413  			TestLabelThree = "three"
   414  		}
   415  		name = "%s"
   416  	}
   417  	spec {
   418  		access_modes = ["ReadWriteMany"]
   419  		resources {
   420  			requests {
   421  				storage = "5Gi"
   422  			}
   423  		}
   424  		selector {
   425  			match_expressions {
   426  				key = "environment"
   427  				operator = "In"
   428  				values = ["non-exists-12345"]
   429  			}
   430  		}
   431  	}
   432  	wait_until_bound = false
   433  }
   434  `, name)
   435  }
   436  
   437  func testAccKubernetesPersistentVolumeClaimConfig_import(volumeName, claimName string) string {
   438  	return fmt.Sprintf(`
   439  resource "kubernetes_persistent_volume" "test" {
   440  	metadata {
   441  		name = "%s"
   442  	}
   443  	spec {
   444  		capacity {
   445  			storage = "10Gi"
   446  		}
   447  		access_modes = ["ReadWriteMany"]
   448  		persistent_volume_source {
   449  			gce_persistent_disk {
   450  				pd_name = "test123"
   451  			}
   452  		}
   453  	}
   454  }
   455  
   456  resource "kubernetes_persistent_volume_claim" "test" {
   457  	metadata {
   458  		name = "%s"
   459  	}
   460  	spec {
   461  		access_modes = ["ReadWriteMany"]
   462  		resources {
   463  			requests {
   464  				storage = "5Gi"
   465  			}
   466  		}
   467  		volume_name = "${kubernetes_persistent_volume.test.metadata.0.name}"
   468  	}
   469  }
   470  `, volumeName, claimName)
   471  }
   472  
   473  func testAccKubernetesPersistentVolumeClaimConfig_volumeMatch(volumeName, claimName string) string {
   474  	return fmt.Sprintf(`
   475  resource "kubernetes_persistent_volume" "test" {
   476  	metadata {
   477  		name = "%s"
   478  	}
   479  	spec {
   480  		capacity {
   481  			storage = "10Gi"
   482  		}
   483  		access_modes = ["ReadWriteMany"]
   484  		persistent_volume_source {
   485  			gce_persistent_disk {
   486  				pd_name = "test123"
   487  			}
   488  		}
   489  	}
   490  }
   491  
   492  resource "kubernetes_persistent_volume_claim" "test" {
   493  	metadata {
   494  		name = "%s"
   495  	}
   496  	spec {
   497  		access_modes = ["ReadWriteMany"]
   498  		resources {
   499  			requests {
   500  				storage = "5Gi"
   501  			}
   502  		}
   503  		volume_name = "${kubernetes_persistent_volume.test.metadata.0.name}"
   504  	}
   505  }
   506  `, volumeName, claimName)
   507  }
   508  
   509  func testAccKubernetesPersistentVolumeClaimConfig_volumeMatch_modified(volumeName, claimName string) string {
   510  	return fmt.Sprintf(`
   511  resource "kubernetes_persistent_volume" "test2" {
   512  	metadata {
   513  		name = "%s"
   514  	}
   515  	spec {
   516  		capacity {
   517  			storage = "10Gi"
   518  		}
   519  		access_modes = ["ReadWriteMany"]
   520  		persistent_volume_source {
   521  			gce_persistent_disk {
   522  				pd_name = "test123"
   523  			}
   524  		}
   525  	}
   526  }
   527  
   528  resource "kubernetes_persistent_volume_claim" "test" {
   529  	metadata {
   530  		name = "%s"
   531  	}
   532  	spec {
   533  		access_modes = ["ReadWriteMany"]
   534  		resources {
   535  			requests {
   536  				storage = "5Gi"
   537  			}
   538  		}
   539  		volume_name = "${kubernetes_persistent_volume.test2.metadata.0.name}"
   540  	}
   541  }
   542  `, volumeName, claimName)
   543  }
   544  
   545  func testAccKubernetesPersistentVolumeClaimConfig_labelsMatch(volumeName, claimName string) string {
   546  	return fmt.Sprintf(`
   547  resource "kubernetes_persistent_volume" "test" {
   548  	metadata {
   549  		labels {
   550  			TfAccTestEnvironment = "blablah"
   551  		}
   552  		name = "%s"
   553  	}
   554  	spec {
   555  		capacity {
   556  			storage = "10Gi"
   557  		}
   558  		access_modes = ["ReadWriteMany"]
   559  		persistent_volume_source {
   560  			gce_persistent_disk {
   561  				pd_name = "test123"
   562  			}
   563  		}
   564  	}
   565  }
   566  
   567  resource "kubernetes_persistent_volume_claim" "test" {
   568  	metadata {
   569  		name = "%s"
   570  	}
   571  	spec {
   572  		access_modes = ["ReadWriteMany"]
   573  		resources {
   574  			requests {
   575  				storage = "5Gi"
   576  			}
   577  		}
   578  		selector {
   579  			match_labels {
   580  				TfAccTestEnvironment = "blablah"
   581  			}
   582  		}
   583  	}
   584  }
   585  `, volumeName, claimName)
   586  }
   587  
   588  func testAccKubernetesPersistentVolumeClaimConfig_labelsMatchExpression(volumeName, claimName string) string {
   589  	return fmt.Sprintf(`
   590  resource "kubernetes_persistent_volume" "test" {
   591  	metadata {
   592  		labels {
   593  			TfAccTestEnvironment = "two"
   594  		}
   595  		name = "%s"
   596  	}
   597  	spec {
   598  		capacity {
   599  			storage = "10Gi"
   600  		}
   601  		access_modes = ["ReadWriteMany"]
   602  		persistent_volume_source {
   603  			gce_persistent_disk {
   604  				pd_name = "test123"
   605  			}
   606  		}
   607  	}
   608  }
   609  
   610  resource "kubernetes_persistent_volume_claim" "test" {
   611  	metadata {
   612  		name = "%s"
   613  	}
   614  	spec {
   615  		access_modes = ["ReadWriteMany"]
   616  		resources {
   617  			requests {
   618  				storage = "5Gi"
   619  			}
   620  		}
   621  		selector {
   622  			match_expressions {
   623  				key = "TfAccTestEnvironment"
   624  				operator = "In"
   625  				values = ["one", "three", "two"]
   626  			}
   627  		}
   628  	}
   629  }
   630  `, volumeName, claimName)
   631  }
   632  
   633  func testAccKubernetesPersistentVolumeClaimConfig_volumeUpdate(volumeName, claimName, storage string) string {
   634  	return fmt.Sprintf(`
   635  resource "kubernetes_persistent_volume" "test" {
   636  	metadata {
   637  		name = "%s"
   638  	}
   639  	spec {
   640  		capacity {
   641  			storage = "%s"
   642  		}
   643  		access_modes = ["ReadWriteMany"]
   644  		persistent_volume_source {
   645  			gce_persistent_disk {
   646  				pd_name = "test123"
   647  			}
   648  		}
   649  	}
   650  }
   651  
   652  resource "kubernetes_persistent_volume_claim" "test" {
   653  	metadata {
   654  		name = "%s"
   655  	}
   656  	spec {
   657  		access_modes = ["ReadWriteMany"]
   658  		resources {
   659  			requests {
   660  				storage = "5Gi"
   661  			}
   662  		}
   663  		volume_name = "${kubernetes_persistent_volume.test.metadata.0.name}"
   664  	}
   665  }
   666  `, volumeName, storage, claimName)
   667  }