Skip to content

Commit

Permalink
Add TestFlattenMetadata unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
arybolovlev committed Nov 29, 2023
1 parent f45e73f commit f550b98
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 15 deletions.
2 changes: 1 addition & 1 deletion kubernetes/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down
165 changes: 151 additions & 14 deletions kubernetes/structures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: "",
Expand All @@ -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,
Expand All @@ -75,8 +77,7 @@ func TestFlattenMetadataFields(t *testing.T) {
"uid": uid,
}},
},
{
// Non default namespace and static name,
"NonDefaultNamespaceStaticName": {
metav1.ObjectMeta{
Annotations: annotations,
GenerateName: "",
Expand All @@ -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,
Expand All @@ -97,8 +98,7 @@ func TestFlattenMetadataFields(t *testing.T) {
"uid": uid,
}},
},
{
// Default namespace and generated name
"DefaultNamespaceGeneratedName": {
metav1.ObjectMeta{
Annotations: annotations,
GenerateName: "gen-foo",
Expand All @@ -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,
Expand All @@ -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",
Expand All @@ -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,
Expand All @@ -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)
}
}
}

0 comments on commit f550b98

Please sign in to comment.