Skip to content

Commit

Permalink
resource/*: Treat non-string label values as invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Mar 5, 2018
1 parent ec962d6 commit 8e81538
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
32 changes: 32 additions & 0 deletions kubernetes/resource_kubernetes_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ func TestAccKubernetesNamespace_basic(t *testing.T) {
})
}

func TestAccKubernetesNamespace_invalidLabelValueType(t *testing.T) {
var conf api.Namespace
nsName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "kubernetes_namespace.test",
Providers: testAccProviders,
CheckDestroy: testAccCheckKubernetesNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesNamespaceConfig_invalidLabelValueType(nsName),
ExpectError: regexp.MustCompile("Something"),
},
},
})
}

func TestAccKubernetesNamespace_importBasic(t *testing.T) {
resourceName := "kubernetes_namespace.test"
nsName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
Expand Down Expand Up @@ -400,3 +418,17 @@ resource "kubernetes_namespace" "test" {
}
}`, nsName)
}

func testAccKubernetesNamespaceConfig_invalidLabelValueType(nsName string) string {
return fmt.Sprintf(`
resource "kubernetes_namespace" "test" {
metadata {
labels {
"first" = "one"
"integer" = 2
"bool" = true
}
name = "%s"
}
}`, nsName)
}
2 changes: 2 additions & 0 deletions kubernetes/schema_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func metadataFields(objectName string) map[string]*schema.Schema {
Type: schema.TypeMap,
Description: fmt.Sprintf("An unstructured key value map stored with the %s that may be used to store arbitrary metadata. More info: http://kubernetes.io/docs/user-guide/annotations", objectName),
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ValidateFunc: validateAnnotations,
},
"generation": {
Expand All @@ -23,6 +24,7 @@ func metadataFields(objectName string) map[string]*schema.Schema {
Type: schema.TypeMap,
Description: fmt.Sprintf("Map of string keys and values that can be used to organize and categorize (scope and select) the %s. May match selectors of replication controllers and services. More info: http://kubernetes.io/docs/user-guide/labels", objectName),
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ValidateFunc: validateLabels,
},
"name": {
Expand Down
6 changes: 5 additions & 1 deletion kubernetes/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ func validateLabels(value interface{}, key string) (ws []string, es []error) {
for _, msg := range utilValidation.IsQualifiedName(k) {
es = append(es, fmt.Errorf("%s (%q) %s", key, k, msg))
}
val := v.(string)
val, isString := v.(string)
if !isString {
es = append(es, fmt.Errorf("%s.%s (%#v): Expected value to be string", key, k, v))
return
}
for _, msg := range utilValidation.IsValidLabelValue(val) {
es = append(es, fmt.Errorf("%s (%q) %s", key, val, msg))
}
Expand Down

0 comments on commit 8e81538

Please sign in to comment.