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

     1  package kubernetes
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"regexp"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  	api "k8s.io/kubernetes/pkg/api/v1"
    13  	kubernetes "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
    14  )
    15  
    16  func TestAccKubernetesConfigMap_basic(t *testing.T) {
    17  	var conf api.ConfigMap
    18  	name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:      func() { testAccPreCheck(t) },
    22  		IDRefreshName: "kubernetes_config_map.test",
    23  		Providers:     testAccProviders,
    24  		CheckDestroy:  testAccCheckKubernetesConfigMapDestroy,
    25  		Steps: []resource.TestStep{
    26  			{
    27  				Config: testAccKubernetesConfigMapConfig_basic(name),
    28  				Check: resource.ComposeAggregateTestCheckFunc(
    29  					testAccCheckKubernetesConfigMapExists("kubernetes_config_map.test", &conf),
    30  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.annotations.%", "2"),
    31  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.annotations.TestAnnotationOne", "one"),
    32  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.annotations.TestAnnotationTwo", "two"),
    33  					testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{"TestAnnotationOne": "one", "TestAnnotationTwo": "two"}),
    34  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.labels.%", "3"),
    35  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.labels.TestLabelOne", "one"),
    36  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.labels.TestLabelTwo", "two"),
    37  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.labels.TestLabelThree", "three"),
    38  					testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{"TestLabelOne": "one", "TestLabelTwo": "two", "TestLabelThree": "three"}),
    39  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.name", name),
    40  					resource.TestCheckResourceAttrSet("kubernetes_config_map.test", "metadata.0.generation"),
    41  					resource.TestCheckResourceAttrSet("kubernetes_config_map.test", "metadata.0.resource_version"),
    42  					resource.TestCheckResourceAttrSet("kubernetes_config_map.test", "metadata.0.self_link"),
    43  					resource.TestCheckResourceAttrSet("kubernetes_config_map.test", "metadata.0.uid"),
    44  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "data.%", "2"),
    45  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "data.one", "first"),
    46  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "data.two", "second"),
    47  					testAccCheckConfigMapData(&conf, map[string]string{"one": "first", "two": "second"}),
    48  				),
    49  			},
    50  			{
    51  				Config: testAccKubernetesConfigMapConfig_modified(name),
    52  				Check: resource.ComposeAggregateTestCheckFunc(
    53  					testAccCheckKubernetesConfigMapExists("kubernetes_config_map.test", &conf),
    54  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.annotations.%", "2"),
    55  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.annotations.TestAnnotationOne", "one"),
    56  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.annotations.Different", "1234"),
    57  					testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{"TestAnnotationOne": "one", "Different": "1234"}),
    58  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.labels.%", "2"),
    59  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.labels.TestLabelOne", "one"),
    60  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.labels.TestLabelThree", "three"),
    61  					testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{"TestLabelOne": "one", "TestLabelThree": "three"}),
    62  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.name", name),
    63  					resource.TestCheckResourceAttrSet("kubernetes_config_map.test", "metadata.0.generation"),
    64  					resource.TestCheckResourceAttrSet("kubernetes_config_map.test", "metadata.0.resource_version"),
    65  					resource.TestCheckResourceAttrSet("kubernetes_config_map.test", "metadata.0.self_link"),
    66  					resource.TestCheckResourceAttrSet("kubernetes_config_map.test", "metadata.0.uid"),
    67  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "data.%", "3"),
    68  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "data.one", "first"),
    69  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "data.two", "second"),
    70  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "data.nine", "ninth"),
    71  					testAccCheckConfigMapData(&conf, map[string]string{"one": "first", "two": "second", "nine": "ninth"}),
    72  				),
    73  			},
    74  			{
    75  				Config: testAccKubernetesConfigMapConfig_noData(name),
    76  				Check: resource.ComposeAggregateTestCheckFunc(
    77  					testAccCheckKubernetesConfigMapExists("kubernetes_config_map.test", &conf),
    78  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.annotations.%", "0"),
    79  					testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{}),
    80  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.labels.%", "0"),
    81  					testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{}),
    82  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.name", name),
    83  					resource.TestCheckResourceAttrSet("kubernetes_config_map.test", "metadata.0.generation"),
    84  					resource.TestCheckResourceAttrSet("kubernetes_config_map.test", "metadata.0.resource_version"),
    85  					resource.TestCheckResourceAttrSet("kubernetes_config_map.test", "metadata.0.self_link"),
    86  					resource.TestCheckResourceAttrSet("kubernetes_config_map.test", "metadata.0.uid"),
    87  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "data.%", "0"),
    88  					testAccCheckConfigMapData(&conf, map[string]string{}),
    89  				),
    90  			},
    91  		},
    92  	})
    93  }
    94  
    95  func TestAccKubernetesConfigMap_importBasic(t *testing.T) {
    96  	resourceName := "kubernetes_config_map.test"
    97  	name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
    98  
    99  	resource.Test(t, resource.TestCase{
   100  		PreCheck:     func() { testAccPreCheck(t) },
   101  		Providers:    testAccProviders,
   102  		CheckDestroy: testAccCheckKubernetesConfigMapDestroy,
   103  		Steps: []resource.TestStep{
   104  			{
   105  				Config: testAccKubernetesConfigMapConfig_basic(name),
   106  			},
   107  
   108  			{
   109  				ResourceName:      resourceName,
   110  				ImportState:       true,
   111  				ImportStateVerify: true,
   112  			},
   113  		},
   114  	})
   115  }
   116  
   117  func TestAccKubernetesConfigMap_generatedName(t *testing.T) {
   118  	var conf api.ConfigMap
   119  	prefix := "tf-acc-test-gen-"
   120  
   121  	resource.Test(t, resource.TestCase{
   122  		PreCheck:      func() { testAccPreCheck(t) },
   123  		IDRefreshName: "kubernetes_config_map.test",
   124  		Providers:     testAccProviders,
   125  		CheckDestroy:  testAccCheckKubernetesConfigMapDestroy,
   126  		Steps: []resource.TestStep{
   127  			{
   128  				Config: testAccKubernetesConfigMapConfig_generatedName(prefix),
   129  				Check: resource.ComposeAggregateTestCheckFunc(
   130  					testAccCheckKubernetesConfigMapExists("kubernetes_config_map.test", &conf),
   131  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.annotations.%", "0"),
   132  					testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{}),
   133  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.labels.%", "0"),
   134  					testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{}),
   135  					resource.TestCheckResourceAttr("kubernetes_config_map.test", "metadata.0.generate_name", prefix),
   136  					resource.TestMatchResourceAttr("kubernetes_config_map.test", "metadata.0.name", regexp.MustCompile("^"+prefix)),
   137  					resource.TestCheckResourceAttrSet("kubernetes_config_map.test", "metadata.0.generation"),
   138  					resource.TestCheckResourceAttrSet("kubernetes_config_map.test", "metadata.0.resource_version"),
   139  					resource.TestCheckResourceAttrSet("kubernetes_config_map.test", "metadata.0.self_link"),
   140  					resource.TestCheckResourceAttrSet("kubernetes_config_map.test", "metadata.0.uid"),
   141  				),
   142  			},
   143  		},
   144  	})
   145  }
   146  
   147  func TestAccKubernetesConfigMap_importGeneratedName(t *testing.T) {
   148  	resourceName := "kubernetes_config_map.test"
   149  	prefix := "tf-acc-test-gen-import-"
   150  
   151  	resource.Test(t, resource.TestCase{
   152  		PreCheck:     func() { testAccPreCheck(t) },
   153  		Providers:    testAccProviders,
   154  		CheckDestroy: testAccCheckKubernetesConfigMapDestroy,
   155  		Steps: []resource.TestStep{
   156  			{
   157  				Config: testAccKubernetesConfigMapConfig_generatedName(prefix),
   158  			},
   159  
   160  			{
   161  				ResourceName:      resourceName,
   162  				ImportState:       true,
   163  				ImportStateVerify: true,
   164  			},
   165  		},
   166  	})
   167  }
   168  
   169  func testAccCheckConfigMapData(m *api.ConfigMap, expected map[string]string) resource.TestCheckFunc {
   170  	return func(s *terraform.State) error {
   171  		if len(expected) == 0 && len(m.Data) == 0 {
   172  			return nil
   173  		}
   174  		if !reflect.DeepEqual(m.Data, expected) {
   175  			return fmt.Errorf("%s data don't match.\nExpected: %q\nGiven: %q",
   176  				m.Name, expected, m.Data)
   177  		}
   178  		return nil
   179  	}
   180  }
   181  
   182  func testAccCheckKubernetesConfigMapDestroy(s *terraform.State) error {
   183  	conn := testAccProvider.Meta().(*kubernetes.Clientset)
   184  
   185  	for _, rs := range s.RootModule().Resources {
   186  		if rs.Type != "kubernetes_config_map" {
   187  			continue
   188  		}
   189  		namespace, name := idParts(rs.Primary.ID)
   190  		resp, err := conn.CoreV1().ConfigMaps(namespace).Get(name)
   191  		if err == nil {
   192  			if resp.Name == rs.Primary.ID {
   193  				return fmt.Errorf("Config Map still exists: %s", rs.Primary.ID)
   194  			}
   195  		}
   196  	}
   197  
   198  	return nil
   199  }
   200  
   201  func testAccCheckKubernetesConfigMapExists(n string, obj *api.ConfigMap) resource.TestCheckFunc {
   202  	return func(s *terraform.State) error {
   203  		rs, ok := s.RootModule().Resources[n]
   204  		if !ok {
   205  			return fmt.Errorf("Not found: %s", n)
   206  		}
   207  
   208  		conn := testAccProvider.Meta().(*kubernetes.Clientset)
   209  		namespace, name := idParts(rs.Primary.ID)
   210  		out, err := conn.CoreV1().ConfigMaps(namespace).Get(name)
   211  		if err != nil {
   212  			return err
   213  		}
   214  
   215  		*obj = *out
   216  		return nil
   217  	}
   218  }
   219  
   220  func testAccKubernetesConfigMapConfig_basic(name string) string {
   221  	return fmt.Sprintf(`
   222  resource "kubernetes_config_map" "test" {
   223  	metadata {
   224  		annotations {
   225  			TestAnnotationOne = "one"
   226  			TestAnnotationTwo = "two"
   227  		}
   228  		labels {
   229  			TestLabelOne = "one"
   230  			TestLabelTwo = "two"
   231  			TestLabelThree = "three"
   232  		}
   233  		name = "%s"
   234  	}
   235  	data {
   236  		one = "first"
   237  		two = "second"
   238  	}
   239  }`, name)
   240  }
   241  
   242  func testAccKubernetesConfigMapConfig_modified(name string) string {
   243  	return fmt.Sprintf(`
   244  resource "kubernetes_config_map" "test" {
   245  	metadata {
   246  		annotations {
   247  			TestAnnotationOne = "one"
   248  			Different = "1234"
   249  		}
   250  		labels {
   251  			TestLabelOne = "one"
   252  			TestLabelThree = "three"
   253  		}
   254  		name = "%s"
   255  	}
   256  	data {
   257  		one = "first"
   258  		two = "second"
   259  		nine = "ninth"
   260  	}
   261  }`, name)
   262  }
   263  
   264  func testAccKubernetesConfigMapConfig_noData(name string) string {
   265  	return fmt.Sprintf(`
   266  resource "kubernetes_config_map" "test" {
   267  	metadata {
   268  		name = "%s"
   269  	}
   270  }`, name)
   271  }
   272  
   273  func testAccKubernetesConfigMapConfig_generatedName(prefix string) string {
   274  	return fmt.Sprintf(`
   275  resource "kubernetes_config_map" "test" {
   276  	metadata {
   277  		generate_name = "%s"
   278  	}
   279  	data {
   280  		one = "first"
   281  		two = "second"
   282  	}
   283  }`, prefix)
   284  }