github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/kubernetes/resource_kubernetes_secret_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 TestAccKubernetesSecret_basic(t *testing.T) { 17 var conf api.Secret 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_secret.test", 23 Providers: testAccProviders, 24 CheckDestroy: testAccCheckKubernetesSecretDestroy, 25 Steps: []resource.TestStep{ 26 { 27 Config: testAccKubernetesSecretConfig_basic(name), 28 Check: resource.ComposeAggregateTestCheckFunc( 29 testAccCheckKubernetesSecretExists("kubernetes_secret.test", &conf), 30 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.annotations.%", "2"), 31 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.annotations.TestAnnotationOne", "one"), 32 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.annotations.TestAnnotationTwo", "two"), 33 testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{"TestAnnotationOne": "one", "TestAnnotationTwo": "two"}), 34 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.labels.%", "3"), 35 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.labels.TestLabelOne", "one"), 36 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.labels.TestLabelTwo", "two"), 37 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.labels.TestLabelThree", "three"), 38 testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{"TestLabelOne": "one", "TestLabelTwo": "two", "TestLabelThree": "three"}), 39 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.name", name), 40 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.generation"), 41 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.resource_version"), 42 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.self_link"), 43 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.uid"), 44 resource.TestCheckResourceAttr("kubernetes_secret.test", "data.%", "2"), 45 resource.TestCheckResourceAttr("kubernetes_secret.test", "data.one", "first"), 46 resource.TestCheckResourceAttr("kubernetes_secret.test", "data.two", "second"), 47 resource.TestCheckResourceAttr("kubernetes_secret.test", "type", "Opaque"), 48 testAccCheckSecretData(&conf, map[string]string{"one": "first", "two": "second"}), 49 ), 50 }, 51 { 52 Config: testAccKubernetesSecretConfig_modified(name), 53 Check: resource.ComposeAggregateTestCheckFunc( 54 testAccCheckKubernetesSecretExists("kubernetes_secret.test", &conf), 55 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.annotations.%", "2"), 56 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.annotations.TestAnnotationOne", "one"), 57 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.annotations.Different", "1234"), 58 testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{"TestAnnotationOne": "one", "Different": "1234"}), 59 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.labels.%", "2"), 60 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.labels.TestLabelOne", "one"), 61 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.labels.TestLabelThree", "three"), 62 testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{"TestLabelOne": "one", "TestLabelThree": "three"}), 63 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.name", name), 64 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.generation"), 65 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.resource_version"), 66 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.self_link"), 67 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.uid"), 68 resource.TestCheckResourceAttr("kubernetes_secret.test", "data.%", "3"), 69 resource.TestCheckResourceAttr("kubernetes_secret.test", "data.one", "first"), 70 resource.TestCheckResourceAttr("kubernetes_secret.test", "data.two", "second"), 71 resource.TestCheckResourceAttr("kubernetes_secret.test", "data.nine", "ninth"), 72 resource.TestCheckResourceAttr("kubernetes_secret.test", "type", "Opaque"), 73 testAccCheckSecretData(&conf, map[string]string{"one": "first", "two": "second", "nine": "ninth"}), 74 ), 75 }, 76 { 77 Config: testAccKubernetesSecretConfig_noData(name), 78 Check: resource.ComposeAggregateTestCheckFunc( 79 testAccCheckKubernetesSecretExists("kubernetes_secret.test", &conf), 80 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.annotations.%", "0"), 81 testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{}), 82 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.labels.%", "0"), 83 testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{}), 84 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.name", name), 85 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.generation"), 86 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.resource_version"), 87 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.self_link"), 88 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.uid"), 89 resource.TestCheckResourceAttr("kubernetes_secret.test", "data.%", "0"), 90 testAccCheckSecretData(&conf, map[string]string{}), 91 ), 92 }, 93 { 94 Config: testAccKubernetesSecretConfig_typeSpecified(name), 95 Check: resource.ComposeAggregateTestCheckFunc( 96 testAccCheckKubernetesSecretExists("kubernetes_secret.test", &conf), 97 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.annotations.%", "0"), 98 testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{}), 99 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.labels.%", "0"), 100 testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{}), 101 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.name", name), 102 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.generation"), 103 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.resource_version"), 104 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.self_link"), 105 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.uid"), 106 resource.TestCheckResourceAttr("kubernetes_secret.test", "data.%", "2"), 107 resource.TestCheckResourceAttr("kubernetes_secret.test", "data.username", "admin"), 108 resource.TestCheckResourceAttr("kubernetes_secret.test", "data.password", "password"), 109 resource.TestCheckResourceAttr("kubernetes_secret.test", "type", "kubernetes.io/basic-auth"), 110 testAccCheckSecretData(&conf, map[string]string{"username": "admin", "password": "password"}), 111 ), 112 }, 113 }, 114 }) 115 } 116 117 func TestAccKubernetesSecret_importBasic(t *testing.T) { 118 resourceName := "kubernetes_secret.test" 119 name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 120 121 resource.Test(t, resource.TestCase{ 122 PreCheck: func() { testAccPreCheck(t) }, 123 Providers: testAccProviders, 124 CheckDestroy: testAccCheckKubernetesSecretDestroy, 125 Steps: []resource.TestStep{ 126 { 127 Config: testAccKubernetesSecretConfig_basic(name), 128 }, 129 130 { 131 ResourceName: resourceName, 132 ImportState: true, 133 ImportStateVerify: true, 134 }, 135 }, 136 }) 137 } 138 139 func TestAccKubernetesSecret_generatedName(t *testing.T) { 140 var conf api.Secret 141 prefix := "tf-acc-test-gen-" 142 143 resource.Test(t, resource.TestCase{ 144 PreCheck: func() { testAccPreCheck(t) }, 145 IDRefreshName: "kubernetes_secret.test", 146 Providers: testAccProviders, 147 CheckDestroy: testAccCheckKubernetesSecretDestroy, 148 Steps: []resource.TestStep{ 149 { 150 Config: testAccKubernetesSecretConfig_generatedName(prefix), 151 Check: resource.ComposeAggregateTestCheckFunc( 152 testAccCheckKubernetesSecretExists("kubernetes_secret.test", &conf), 153 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.annotations.%", "0"), 154 testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{}), 155 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.labels.%", "0"), 156 testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{}), 157 resource.TestCheckResourceAttr("kubernetes_secret.test", "metadata.0.generate_name", prefix), 158 resource.TestMatchResourceAttr("kubernetes_secret.test", "metadata.0.name", regexp.MustCompile("^"+prefix)), 159 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.generation"), 160 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.resource_version"), 161 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.self_link"), 162 resource.TestCheckResourceAttrSet("kubernetes_secret.test", "metadata.0.uid"), 163 ), 164 }, 165 }, 166 }) 167 } 168 169 func TestAccKubernetesSecret_importGeneratedName(t *testing.T) { 170 resourceName := "kubernetes_secret.test" 171 prefix := "tf-acc-test-gen-import-" 172 173 resource.Test(t, resource.TestCase{ 174 PreCheck: func() { testAccPreCheck(t) }, 175 Providers: testAccProviders, 176 CheckDestroy: testAccCheckKubernetesSecretDestroy, 177 Steps: []resource.TestStep{ 178 { 179 Config: testAccKubernetesSecretConfig_generatedName(prefix), 180 }, 181 182 { 183 ResourceName: resourceName, 184 ImportState: true, 185 ImportStateVerify: true, 186 }, 187 }, 188 }) 189 } 190 191 func testAccCheckSecretData(m *api.Secret, expected map[string]string) resource.TestCheckFunc { 192 return func(s *terraform.State) error { 193 if len(expected) == 0 && len(m.Data) == 0 { 194 return nil 195 } 196 if !reflect.DeepEqual(byteMapToStringMap(m.Data), expected) { 197 return fmt.Errorf("%s data don't match.\nExpected: %q\nGiven: %q", 198 m.Name, expected, m.Data) 199 } 200 return nil 201 } 202 } 203 204 func testAccCheckKubernetesSecretDestroy(s *terraform.State) error { 205 conn := testAccProvider.Meta().(*kubernetes.Clientset) 206 207 for _, rs := range s.RootModule().Resources { 208 if rs.Type != "kubernetes_secret" { 209 continue 210 } 211 namespace, name := idParts(rs.Primary.ID) 212 resp, err := conn.CoreV1().Secrets(namespace).Get(name) 213 if err == nil { 214 if resp.Name == rs.Primary.ID { 215 return fmt.Errorf("Secret still exists: %s", rs.Primary.ID) 216 } 217 } 218 } 219 220 return nil 221 } 222 223 func testAccCheckKubernetesSecretExists(n string, obj *api.Secret) resource.TestCheckFunc { 224 return func(s *terraform.State) error { 225 rs, ok := s.RootModule().Resources[n] 226 if !ok { 227 return fmt.Errorf("Not found: %s", n) 228 } 229 230 conn := testAccProvider.Meta().(*kubernetes.Clientset) 231 namespace, name := idParts(rs.Primary.ID) 232 out, err := conn.CoreV1().Secrets(namespace).Get(name) 233 if err != nil { 234 return err 235 } 236 237 *obj = *out 238 return nil 239 } 240 } 241 242 func testAccKubernetesSecretConfig_basic(name string) string { 243 return fmt.Sprintf(` 244 resource "kubernetes_secret" "test" { 245 metadata { 246 annotations { 247 TestAnnotationOne = "one" 248 TestAnnotationTwo = "two" 249 } 250 labels { 251 TestLabelOne = "one" 252 TestLabelTwo = "two" 253 TestLabelThree = "three" 254 } 255 name = "%s" 256 } 257 data { 258 one = "first" 259 two = "second" 260 } 261 }`, name) 262 } 263 264 func testAccKubernetesSecretConfig_modified(name string) string { 265 return fmt.Sprintf(` 266 resource "kubernetes_secret" "test" { 267 metadata { 268 annotations { 269 TestAnnotationOne = "one" 270 Different = "1234" 271 } 272 labels { 273 TestLabelOne = "one" 274 TestLabelThree = "three" 275 } 276 name = "%s" 277 } 278 data { 279 one = "first" 280 two = "second" 281 nine = "ninth" 282 } 283 }`, name) 284 } 285 286 func testAccKubernetesSecretConfig_noData(name string) string { 287 return fmt.Sprintf(` 288 resource "kubernetes_secret" "test" { 289 metadata { 290 name = "%s" 291 } 292 }`, name) 293 } 294 295 func testAccKubernetesSecretConfig_typeSpecified(name string) string { 296 return fmt.Sprintf(` 297 resource "kubernetes_secret" "test" { 298 metadata { 299 name = "%s" 300 } 301 data { 302 username = "admin" 303 password = "password" 304 } 305 type = "kubernetes.io/basic-auth" 306 }`, name) 307 } 308 309 func testAccKubernetesSecretConfig_generatedName(prefix string) string { 310 return fmt.Sprintf(` 311 resource "kubernetes_secret" "test" { 312 metadata { 313 generate_name = "%s" 314 } 315 data { 316 one = "first" 317 two = "second" 318 } 319 }`, prefix) 320 }