-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor RemoveFieldForExport function
Refactor the RemoveFieldForExport function to improve readability and maintainability. add finalizers to be removed from the metadata and spec fields Grouped metadata and spec fields into slices and used loops to remove them. Added unit tests to verify the functionality.
- Loading branch information
Showing
2 changed files
with
98 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package export | ||
|
||
import ( | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
func TestRemoveFieldForExport(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input *unstructured.Unstructured | ||
expected map[string]interface{} | ||
}{ | ||
{ | ||
name: "Remove status and metadata fields", | ||
input: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"status": "some-status", | ||
"metadata": map[string]interface{}{ | ||
"managedFields": "some-managed-fields", | ||
"resourceVersion": "some-resource-version", | ||
"uid": "some-uid", | ||
"finalizers": "some-finalizers", | ||
"generation": "some-generation", | ||
"namespace": "some-namespace", | ||
"creationTimestamp": "some-timestamp", | ||
"ownerReferences": "some-owner-references", | ||
"annotations": map[string]interface{}{ | ||
"kubectl.kubernetes.io/last-applied-configuration": "some-configuration", | ||
}, | ||
}, | ||
"spec": map[string]interface{}{ | ||
"status": "some-spec-status", | ||
"statusMessage": "some-status-message", | ||
}, | ||
}, | ||
}, | ||
expected: map[string]interface{}{ | ||
"metadata": map[string]interface{}{ | ||
"annotations": map[string]interface{}{}, | ||
}, | ||
"spec": map[string]interface{}{}, | ||
}, | ||
}, | ||
{ | ||
name: "Remove name if generateName exists", | ||
input: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"metadata": map[string]interface{}{ | ||
"generateName": "some-generate-name", | ||
"name": "some-name", | ||
}, | ||
}, | ||
}, | ||
expected: map[string]interface{}{ | ||
"metadata": map[string]interface{}{ | ||
"generateName": "some-generate-name", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := RemoveFieldForExport(tt.input) | ||
assert.NilError(t, err) | ||
assert.DeepEqual(t, tt.expected, tt.input.Object) | ||
}) | ||
} | ||
} |