Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/*: Treat non-string label values as invalid #135

Merged
merged 1 commit into from
Mar 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions kubernetes/resource_kubernetes_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ func TestAccKubernetesNamespace_basic(t *testing.T) {
})
}

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

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckKubernetesNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesNamespaceConfig_invalidLabelValueType(nsName),
ExpectError: regexp.MustCompile("Expected value to be string"),
},
},
})
}

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 +416,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