From f550b98cd1832a30ce28e27fdd6a1395679124c8 Mon Sep 17 00:00:00 2001 From: Aleksandr Rybolovlev Date: Wed, 29 Nov 2023 11:54:40 +0100 Subject: [PATCH] Add TestFlattenMetadata unit test --- kubernetes/structures.go | 2 +- kubernetes/structures_test.go | 165 +++++++++++++++++++++++++++++++--- 2 files changed, 152 insertions(+), 15 deletions(-) diff --git a/kubernetes/structures.go b/kubernetes/structures.go index cdcea644a3..fbdc972fbd 100644 --- a/kubernetes/structures.go +++ b/kubernetes/structures.go @@ -129,7 +129,7 @@ func flattenMetadataFields(meta metav1.ObjectMeta) []interface{} { m["namespace"] = meta.Namespace } m["resource_version"] = meta.ResourceVersion - m["uid"] = meta.UID + m["uid"] = string(meta.UID) return []interface{}{m} } diff --git a/kubernetes/structures_test.go b/kubernetes/structures_test.go index 11e07aa307..583d4d6021 100644 --- a/kubernetes/structures_test.go +++ b/kubernetes/structures_test.go @@ -8,11 +8,14 @@ import ( "reflect" "testing" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" ) func TestIsInternalKey(t *testing.T) { + t.Parallel() + testCases := []struct { Key string Expected bool @@ -49,13 +52,12 @@ func TestFlattenMetadataFields(t *testing.T) { labels := map[string]string{ "foo": "bar", } - uid := types.UID("7e9439cb-2584-4b50-81bc-441127e11b26") - cases := []struct { + uid := "7e9439cb-2584-4b50-81bc-441127e11b26" + cases := map[string]struct { meta metav1.ObjectMeta expected []interface{} }{ - { - // Default namespace and static name + "DefaultNamespaceStaticName": { metav1.ObjectMeta{ Annotations: annotations, GenerateName: "", @@ -64,7 +66,7 @@ func TestFlattenMetadataFields(t *testing.T) { Name: "foo", Namespace: "", ResourceVersion: "1", - UID: uid, + UID: types.UID(uid), }, []interface{}{map[string]interface{}{ "annotations": annotations, @@ -75,8 +77,7 @@ func TestFlattenMetadataFields(t *testing.T) { "uid": uid, }}, }, - { - // Non default namespace and static name, + "NonDefaultNamespaceStaticName": { metav1.ObjectMeta{ Annotations: annotations, GenerateName: "", @@ -85,7 +86,7 @@ func TestFlattenMetadataFields(t *testing.T) { Name: "foo", Namespace: "Test", ResourceVersion: "1", - UID: uid, + UID: types.UID(uid), }, []interface{}{map[string]interface{}{ "annotations": annotations, @@ -97,8 +98,7 @@ func TestFlattenMetadataFields(t *testing.T) { "uid": uid, }}, }, - { - // Default namespace and generated name + "DefaultNamespaceGeneratedName": { metav1.ObjectMeta{ Annotations: annotations, GenerateName: "gen-foo", @@ -107,7 +107,7 @@ func TestFlattenMetadataFields(t *testing.T) { Name: "", Namespace: "", ResourceVersion: "1", - UID: uid, + UID: types.UID(uid), }, []interface{}{map[string]interface{}{ "annotations": annotations, @@ -119,8 +119,7 @@ func TestFlattenMetadataFields(t *testing.T) { "uid": uid, }}, }, - { - // Non default namespace and generated name + "NonDefaultNamespaceGeneratedName": { metav1.ObjectMeta{ Annotations: annotations, GenerateName: "gen-foo", @@ -129,7 +128,7 @@ func TestFlattenMetadataFields(t *testing.T) { Name: "", Namespace: "Test", ResourceVersion: "1", - UID: uid, + UID: types.UID(uid), }, []interface{}{map[string]interface{}{ "annotations": annotations, @@ -150,3 +149,141 @@ func TestFlattenMetadataFields(t *testing.T) { } } } + +// TestFlattenMetadata aims to validate whether or not 'ignore_annotations' and 'ignore_labels' +// are cut out along with well-known Kubernetes annotations and labels. +func TestFlattenMetadata(t *testing.T) { + t.Parallel() + + uid := "7e9439cb-2584-4b50-81bc-441127e11b26" + cases := map[string]struct { + meta metav1.ObjectMeta + providerMeta kubeClientsets + expected []interface{} + }{ + "IgnoreAnnotations": { + metav1.ObjectMeta{ + Annotations: map[string]string{ + "fake.kubernetes.io": "fake", + "foo.example.com": "bar", + "bar.example.com": "foo", + }, + GenerateName: "", + Generation: 1, + Labels: map[string]string{ + "foo": "bar", + "bar": "foo", + }, + Name: "foo", + Namespace: "", + ResourceVersion: "1", + UID: types.UID(uid), + }, + kubeClientsets{ + IgnoreAnnotations: []string{"foo.example.com"}, + IgnoreLabels: []string{}, + }, + []interface{}{map[string]interface{}{ + "annotations": map[string]string{ + "bar.example.com": "foo", + }, + "generation": int64(1), + "labels": map[string]string{ + "foo": "bar", + "bar": "foo", + }, + "name": "foo", + "resource_version": "1", + "uid": uid, + }}, + }, + "IgnoreLabels": { + metav1.ObjectMeta{ + Annotations: map[string]string{ + "fake.kubernetes.io": "fake", + "foo.example.com": "bar", + "bar.example.com": "foo", + }, + GenerateName: "", + Generation: 1, + Labels: map[string]string{ + "foo": "bar", + "bar": "foo", + }, + Name: "foo", + Namespace: "", + ResourceVersion: "1", + UID: types.UID(uid), + }, + kubeClientsets{ + IgnoreAnnotations: []string{}, + IgnoreLabels: []string{"foo"}, + }, + []interface{}{map[string]interface{}{ + "annotations": map[string]string{ + "foo.example.com": "bar", + "bar.example.com": "foo", + }, + "generation": int64(1), + "labels": map[string]string{ + "bar": "foo", + }, + "name": "foo", + "resource_version": "1", + "uid": uid, + }}, + }, + "IgnoreAnnotationsAndLabels": { + metav1.ObjectMeta{ + Annotations: map[string]string{ + "fake.kubernetes.io": "fake", + "foo.example.com": "bar", + "bar.example.com": "foo", + }, + GenerateName: "", + Generation: 1, + Labels: map[string]string{ + "foo": "bar", + "bar": "foo", + }, + Name: "foo", + Namespace: "", + ResourceVersion: "1", + UID: types.UID(uid), + }, + kubeClientsets{ + IgnoreAnnotations: []string{"foo.example.com"}, + IgnoreLabels: []string{"foo"}, + }, + []interface{}{map[string]interface{}{ + "annotations": map[string]string{ + "bar.example.com": "foo", + }, + "generation": int64(1), + "labels": map[string]string{ + "bar": "foo", + }, + "name": "foo", + "resource_version": "1", + "uid": uid, + }}, + }, + } + rawData := map[string]interface{}{ + "metadata": []interface{}{map[string]interface{}{ + "annotations": map[string]interface{}{}, + "generation": 0, + "labels": map[string]interface{}{}, + "name": "", + "resource_version": "", + "uid": "", + }}, + } + d := schema.TestResourceDataRaw(t, map[string]*schema.Schema{"metadata": namespacedMetadataSchema("fake", true)}, rawData) + for _, c := range cases { + out := flattenMetadata(c.meta, d, c.providerMeta) + if !reflect.DeepEqual(out, c.expected) { + t.Fatalf("Error matching output and expected: %#v vs %#v", out, c.expected) + } + } +}