github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/kubernetes/resource_kubernetes_persistent_volume_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 TestAccKubernetesPersistentVolume_basic(t *testing.T) {
    15  	var conf api.PersistentVolume
    16  	randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
    17  	name := fmt.Sprintf("tf-acc-test-%s", randString)
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:      func() { testAccPreCheck(t) },
    21  		IDRefreshName: "kubernetes_persistent_volume.test",
    22  		Providers:     testAccProviders,
    23  		CheckDestroy:  testAccCheckKubernetesPersistentVolumeDestroy,
    24  		Steps: []resource.TestStep{
    25  			{
    26  				Config: testAccKubernetesPersistentVolumeConfig_basic(name),
    27  				Check: resource.ComposeAggregateTestCheckFunc(
    28  					testAccCheckKubernetesPersistentVolumeExists("kubernetes_persistent_volume.test", &conf),
    29  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.annotations.%", "2"),
    30  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.annotations.TestAnnotationOne", "one"),
    31  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.annotations.TestAnnotationTwo", "two"),
    32  					testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{"TestAnnotationOne": "one", "TestAnnotationTwo": "two"}),
    33  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.labels.%", "3"),
    34  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.labels.TestLabelOne", "one"),
    35  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.labels.TestLabelTwo", "two"),
    36  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.labels.TestLabelThree", "three"),
    37  					testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{"TestLabelOne": "one", "TestLabelTwo": "two", "TestLabelThree": "three"}),
    38  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.name", name),
    39  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.generation"),
    40  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.resource_version"),
    41  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.self_link"),
    42  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.uid"),
    43  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.capacity.%", "1"),
    44  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.capacity.storage", "123Gi"),
    45  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.access_modes.#", "1"),
    46  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.access_modes.1254135962", "ReadWriteMany"),
    47  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.gce_persistent_disk.#", "1"),
    48  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.gce_persistent_disk.0.pd_name", "test123"),
    49  				),
    50  			},
    51  			{
    52  				Config: testAccKubernetesPersistentVolumeConfig_modified(name),
    53  				Check: resource.ComposeAggregateTestCheckFunc(
    54  					testAccCheckKubernetesPersistentVolumeExists("kubernetes_persistent_volume.test", &conf),
    55  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.annotations.%", "2"),
    56  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.annotations.TestAnnotationOne", "one"),
    57  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.annotations.TestAnnotationTwo", "two"),
    58  					testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{"TestAnnotationOne": "one", "TestAnnotationTwo": "two"}),
    59  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.labels.%", "3"),
    60  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.labels.TestLabelOne", "one"),
    61  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.labels.TestLabelTwo", "two"),
    62  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.labels.TestLabelThree", "three"),
    63  					testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{"TestLabelOne": "one", "TestLabelTwo": "two", "TestLabelThree": "three"}),
    64  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.name", name),
    65  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.generation"),
    66  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.resource_version"),
    67  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.self_link"),
    68  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.uid"),
    69  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.capacity.%", "1"),
    70  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.capacity.storage", "42Mi"),
    71  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.access_modes.#", "2"),
    72  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.access_modes.1245328686", "ReadWriteOnce"),
    73  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.access_modes.1254135962", "ReadWriteMany"),
    74  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.gce_persistent_disk.#", "1"),
    75  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.gce_persistent_disk.0.fs_type", "ntfs"),
    76  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.gce_persistent_disk.0.pd_name", "test123"),
    77  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.gce_persistent_disk.0.read_only", "true"),
    78  				),
    79  			},
    80  		},
    81  	})
    82  }
    83  
    84  func TestAccKubernetesPersistentVolume_importBasic(t *testing.T) {
    85  	resourceName := "kubernetes_persistent_volume.test"
    86  	randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
    87  	name := fmt.Sprintf("tf-acc-test-import-%s", randString)
    88  
    89  	resource.Test(t, resource.TestCase{
    90  		PreCheck:     func() { testAccPreCheck(t) },
    91  		Providers:    testAccProviders,
    92  		CheckDestroy: testAccCheckKubernetesPersistentVolumeDestroy,
    93  		Steps: []resource.TestStep{
    94  			{
    95  				Config: testAccKubernetesPersistentVolumeConfig_basic(name),
    96  			},
    97  			{
    98  				ResourceName:      resourceName,
    99  				ImportState:       true,
   100  				ImportStateVerify: true,
   101  			},
   102  		},
   103  	})
   104  }
   105  
   106  func TestAccKubernetesPersistentVolume_volumeSource(t *testing.T) {
   107  	var conf api.PersistentVolume
   108  	randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
   109  	name := fmt.Sprintf("tf-acc-test-%s", randString)
   110  
   111  	resource.Test(t, resource.TestCase{
   112  		PreCheck:      func() { testAccPreCheck(t) },
   113  		IDRefreshName: "kubernetes_persistent_volume.test",
   114  		Providers:     testAccProviders,
   115  		CheckDestroy:  testAccCheckKubernetesPersistentVolumeDestroy,
   116  		Steps: []resource.TestStep{
   117  			{
   118  				Config: testAccKubernetesPersistentVolumeConfig_volumeSource(name),
   119  				Check: resource.ComposeAggregateTestCheckFunc(
   120  					testAccCheckKubernetesPersistentVolumeExists("kubernetes_persistent_volume.test", &conf),
   121  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.annotations.%", "0"),
   122  					testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{}),
   123  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.labels.%", "0"),
   124  					testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{}),
   125  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.name", name),
   126  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.generation"),
   127  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.resource_version"),
   128  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.self_link"),
   129  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.uid"),
   130  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.capacity.%", "1"),
   131  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.capacity.storage", "123Gi"),
   132  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.access_modes.#", "1"),
   133  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.access_modes.1254135962", "ReadWriteMany"),
   134  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.aws_elastic_block_store.#", "1"),
   135  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.aws_elastic_block_store.0.volume_id", "vol-12345678"),
   136  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.aws_elastic_block_store.0.fs_type", "ntfs"),
   137  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.aws_elastic_block_store.0.partition", "1"),
   138  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.aws_elastic_block_store.0.read_only", "true"),
   139  				),
   140  			},
   141  			{
   142  				Config: testAccKubernetesPersistentVolumeConfig_volumeSource_modified(name),
   143  				Check: resource.ComposeAggregateTestCheckFunc(
   144  					testAccCheckKubernetesPersistentVolumeExists("kubernetes_persistent_volume.test", &conf),
   145  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.annotations.%", "0"),
   146  					testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{}),
   147  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.labels.%", "0"),
   148  					testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{}),
   149  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.name", name),
   150  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.generation"),
   151  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.resource_version"),
   152  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.self_link"),
   153  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.uid"),
   154  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.capacity.%", "1"),
   155  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.capacity.storage", "123Gi"),
   156  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.access_modes.#", "1"),
   157  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.access_modes.1254135962", "ReadWriteMany"),
   158  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.host_path.#", "1"),
   159  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.host_path.0.path", "/custom/testing/path"),
   160  				),
   161  			},
   162  		},
   163  	})
   164  }
   165  
   166  func TestAccKubernetesPersistentVolume_cephFsSecretRef(t *testing.T) {
   167  	var conf api.PersistentVolume
   168  	randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
   169  	name := fmt.Sprintf("tf-acc-test-%s", randString)
   170  
   171  	resource.Test(t, resource.TestCase{
   172  		PreCheck:      func() { testAccPreCheck(t) },
   173  		IDRefreshName: "kubernetes_persistent_volume.test",
   174  		Providers:     testAccProviders,
   175  		CheckDestroy:  testAccCheckKubernetesPersistentVolumeDestroy,
   176  		Steps: []resource.TestStep{
   177  			{
   178  				Config: testAccKubernetesPersistentVolumeConfig_cephFsSecretRef(name),
   179  				Check: resource.ComposeAggregateTestCheckFunc(
   180  					testAccCheckKubernetesPersistentVolumeExists("kubernetes_persistent_volume.test", &conf),
   181  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.annotations.%", "0"),
   182  					testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{}),
   183  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.labels.%", "0"),
   184  					testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{}),
   185  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.name", name),
   186  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.generation"),
   187  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.resource_version"),
   188  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.self_link"),
   189  					resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.uid"),
   190  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.capacity.%", "1"),
   191  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.capacity.storage", "2Gi"),
   192  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.access_modes.#", "1"),
   193  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.access_modes.1254135962", "ReadWriteMany"),
   194  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.ceph_fs.#", "1"),
   195  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.ceph_fs.0.monitors.#", "2"),
   196  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.ceph_fs.0.monitors.2848821021", "10.16.154.78:6789"),
   197  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.ceph_fs.0.monitors.4263435410", "10.16.154.82:6789"),
   198  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.ceph_fs.0.secret_ref.#", "1"),
   199  					resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "spec.0.persistent_volume_source.0.ceph_fs.0.secret_ref.0.name", "ceph-secret"),
   200  				),
   201  			},
   202  		},
   203  	})
   204  }
   205  
   206  func testAccCheckKubernetesPersistentVolumeDestroy(s *terraform.State) error {
   207  	conn := testAccProvider.Meta().(*kubernetes.Clientset)
   208  
   209  	for _, rs := range s.RootModule().Resources {
   210  		if rs.Type != "kubernetes_persistent_volume" {
   211  			continue
   212  		}
   213  		name := rs.Primary.ID
   214  		resp, err := conn.CoreV1().PersistentVolumes().Get(name)
   215  		if err == nil {
   216  			if resp.Name == rs.Primary.ID {
   217  				return fmt.Errorf("Persistent Volume still exists: %s", rs.Primary.ID)
   218  			}
   219  		}
   220  	}
   221  
   222  	return nil
   223  }
   224  
   225  func testAccCheckKubernetesPersistentVolumeExists(n string, obj *api.PersistentVolume) resource.TestCheckFunc {
   226  	return func(s *terraform.State) error {
   227  		rs, ok := s.RootModule().Resources[n]
   228  		if !ok {
   229  			return fmt.Errorf("Not found: %s", n)
   230  		}
   231  
   232  		conn := testAccProvider.Meta().(*kubernetes.Clientset)
   233  		name := rs.Primary.ID
   234  		out, err := conn.CoreV1().PersistentVolumes().Get(name)
   235  		if err != nil {
   236  			return err
   237  		}
   238  
   239  		*obj = *out
   240  		return nil
   241  	}
   242  }
   243  
   244  func testAccKubernetesPersistentVolumeConfig_basic(name string) string {
   245  	return fmt.Sprintf(`
   246  resource "kubernetes_persistent_volume" "test" {
   247  	metadata {
   248  		annotations {
   249  			TestAnnotationOne = "one"
   250  			TestAnnotationTwo = "two"
   251  		}
   252  		labels {
   253  			TestLabelOne = "one"
   254  			TestLabelTwo = "two"
   255  			TestLabelThree = "three"
   256  		}
   257  		name = "%s"
   258  	}
   259  	spec {
   260  		capacity {
   261  			storage = "123Gi"
   262  		}
   263  		access_modes = ["ReadWriteMany"]
   264  		persistent_volume_source {
   265  			gce_persistent_disk {
   266  				pd_name = "test123"
   267  			}
   268  		}
   269  	}
   270  }`, name)
   271  }
   272  
   273  func testAccKubernetesPersistentVolumeConfig_modified(name string) string {
   274  	return fmt.Sprintf(`
   275  resource "kubernetes_persistent_volume" "test" {
   276  	metadata {
   277  		annotations {
   278  			TestAnnotationOne = "one"
   279  			TestAnnotationTwo = "two"
   280  		}
   281  		labels {
   282  			TestLabelOne = "one"
   283  			TestLabelTwo = "two"
   284  			TestLabelThree = "three"
   285  		}
   286  		name = "%s"
   287  	}
   288  	spec {
   289  		capacity {
   290  			storage = "42Mi"
   291  		}
   292  		access_modes = ["ReadWriteMany", "ReadWriteOnce"]
   293  		persistent_volume_source {
   294  			gce_persistent_disk {
   295  				fs_type = "ntfs"
   296  				pd_name = "test123"
   297  				read_only = true
   298  			}
   299  		}
   300  	}
   301  }`, name)
   302  }
   303  
   304  func testAccKubernetesPersistentVolumeConfig_volumeSource(name string) string {
   305  	return fmt.Sprintf(`
   306  resource "kubernetes_persistent_volume" "test" {
   307  	metadata {
   308  		name = "%s"
   309  	}
   310  	spec {
   311  		capacity {
   312  			storage = "123Gi"
   313  		}
   314  		access_modes = ["ReadWriteMany"]
   315  		persistent_volume_source {
   316  			aws_elastic_block_store {
   317  				volume_id = "vol-12345678"
   318  				fs_type = "ntfs"
   319  				partition = 1
   320  				read_only = true
   321  			}
   322  		}
   323  	}
   324  }`, name)
   325  }
   326  
   327  func testAccKubernetesPersistentVolumeConfig_volumeSource_modified(name string) string {
   328  	return fmt.Sprintf(`
   329  resource "kubernetes_persistent_volume" "test" {
   330  	metadata {
   331  		name = "%s"
   332  	}
   333  	spec {
   334  		capacity {
   335  			storage = "123Gi"
   336  		}
   337  		access_modes = ["ReadWriteMany"]
   338  		persistent_volume_source {
   339  			host_path {
   340  				path = "/custom/testing/path"
   341  			}
   342  		}
   343  	}
   344  }`, name)
   345  }
   346  
   347  func testAccKubernetesPersistentVolumeConfig_cephFsSecretRef(name string) string {
   348  	return fmt.Sprintf(`
   349  resource "kubernetes_persistent_volume" "test" {
   350  	metadata {
   351  		name = "%s"
   352  	}
   353  	spec {
   354  		capacity {
   355  			storage = "2Gi"
   356  		}
   357  		access_modes = ["ReadWriteMany"]
   358  		persistent_volume_source {
   359  			ceph_fs {
   360  				monitors = ["10.16.154.78:6789", "10.16.154.82:6789"]
   361  				secret_ref {
   362  					name = "ceph-secret"
   363  				}
   364  			}
   365  		}
   366  	}
   367  }`, name)
   368  }