From ace0807deb7677c31679f43711f6fc75ab0a0a68 Mon Sep 17 00:00:00 2001 From: Alejandro Ruiz Date: Thu, 20 Jun 2024 12:23:41 +0200 Subject: [PATCH] Add unit test --- .../agent/deployer/normalizers/status_test.go | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 internal/cmd/agent/deployer/normalizers/status_test.go diff --git a/internal/cmd/agent/deployer/normalizers/status_test.go b/internal/cmd/agent/deployer/normalizers/status_test.go new file mode 100644 index 0000000000..69fedebc2a --- /dev/null +++ b/internal/cmd/agent/deployer/normalizers/status_test.go @@ -0,0 +1,55 @@ +package normalizers + +import ( + "errors" + "testing" + + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" +) + +func TestStatusNormalizer_Normalize(t *testing.T) { + tests := []struct { + name string + obj runtime.Object + check func(object runtime.Object) error + }{ + { + name: "object with status", + obj: &corev1.Pod{ + Status: corev1.PodStatus{ + PodIP: "1.2.3.4", + }, + }, + check: func(obj runtime.Object) error { + if obj.(*corev1.Pod).Status.PodIP != "" { + return errors.New("status was not removed") + } + return nil + }, + }, + { + name: "object without status", + obj: &corev1.ConfigMap{}, + check: func(_ runtime.Object) error { return nil }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + un, err := runtime.DefaultUnstructuredConverter.ToUnstructured(tt.obj) + if err != nil { + t.Fatal(err) + } + if err := (StatusNormalizer{}).Normalize(&unstructured.Unstructured{Object: un}); err != nil { + t.Fatal(err) + } + if err := runtime.DefaultUnstructuredConverter.FromUnstructured(un, tt.obj); err != nil { + t.Fatal(err) + } + if err := tt.check(tt.obj); err != nil { + t.Error(err) + } + }) + } +}