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