From 24bd8d39d26a5ad73d41be4618b8d0bef487e864 Mon Sep 17 00:00:00 2001 From: Joeyyy09 Date: Sun, 2 Jun 2024 20:58:36 +0530 Subject: [PATCH 1/3] Add test cases and edge cases resulting in improved coverage Signed-off-by: Joeyyy09 --- pkg/utils/expression/value_from_test.go | 176 +++++++++++++++++++++++- 1 file changed, 174 insertions(+), 2 deletions(-) diff --git a/pkg/utils/expression/value_from_test.go b/pkg/utils/expression/value_from_test.go index c164cd75e..de445e37a 100644 --- a/pkg/utils/expression/value_from_test.go +++ b/pkg/utils/expression/value_from_test.go @@ -29,7 +29,10 @@ import ( func TestDurationFrom_Get(t *testing.T) { now := time.Now().Truncate(time.Second) - nowPlugOneSecond := metav1.NewTime(now.Add(time.Second)) + nowPlusOneSecond := metav1.NewTime(now.Add(time.Second)) + nowPlusOneHour := now.Add(time.Hour) + validRFC3339Time := nowPlusOneHour.Format(time.RFC3339Nano) + type args struct { value *time.Duration src *string @@ -43,6 +46,7 @@ func TestDurationFrom_Get(t *testing.T) { wantOk bool }{ { + name: "No value, no src", args: args{ value: nil, src: nil, @@ -51,6 +55,7 @@ func TestDurationFrom_Get(t *testing.T) { wantOk: false, }, { + name: "Value 0, no src", args: args{ value: format.Ptr(time.Duration(0)), src: nil, @@ -60,18 +65,185 @@ func TestDurationFrom_Get(t *testing.T) { wantOk: true, }, { + name: "Value 1, valid src", args: args{ value: format.Ptr(time.Duration(1)), src: format.Ptr(".metadata.deletionTimestamp"), v: corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ - DeletionTimestamp: &nowPlugOneSecond, + DeletionTimestamp: &nowPlusOneSecond, }, }, }, want: 1 * time.Second, wantOk: true, }, + { + name: "Valid RFC3339 time src", + args: args{ + value: nil, + src: format.Ptr(".metadata.annotations.validRFC3339"), + v: corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + "validRFC3339": validRFC3339Time, + }, + }, + }, + }, + want: time.Hour, + wantOk: true, + }, + { + name: "Invalid RFC3339 time src", + args: args{ + value: nil, + src: format.Ptr(".metadata.annotations.invalidRFC3339"), + v: corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + "invalidRFC3339": "invalid-time", + }, + }, + }, + }, + want: 0, + wantOk: false, + }, + { + name: "Valid duration string src", + args: args{ + value: nil, + src: format.Ptr(".metadata.annotations.validDuration"), + v: corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + "validDuration": "1h", + }, + }, + }, + }, + want: time.Hour, + wantOk: true, + }, + { + name: "Invalid duration string src", + args: args{ + value: nil, + src: format.Ptr(".metadata.annotations.invalidDuration"), + v: corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + "invalidDuration": "invalid-duration", + }, + }, + }, + }, + want: 0, + wantOk: false, + }, + { + name: "Empty string src", + args: args{ + value: nil, + src: format.Ptr(".metadata.annotations.emptyString"), + v: corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + "emptyString": "", + }, + }, + }, + }, + want: 0, + wantOk: false, + }, + { + name: "Noop duration", + args: args{ + value: nil, + src: nil, + v: nil, + }, + want: 0, + wantOk: false, + }, + { + name: "Simple integer duration", + args: args{ + value: format.Ptr(time.Duration(5)), + src: nil, + v: nil, + }, + want: 5, + wantOk: true, + }, + { + name: "Nil value with nil src", + args: args{ + value: nil, + src: nil, + v: corev1.Pod{}, + }, + want: 0, + wantOk: false, + }, + { + name: "Nil value with valid src", + args: args{ + value: nil, + src: format.Ptr(".metadata.deletionTimestamp"), + v: corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + DeletionTimestamp: &nowPlusOneSecond, + }, + }, + }, + want: 1 * time.Second, + wantOk: true, + }, + { + name: "Valid duration string with nil value", + args: args{ + value: nil, + src: format.Ptr(".metadata.annotations.validDuration"), + v: corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + "validDuration": "2h", + }, + }, + }, + }, + want: 2 * time.Hour, + wantOk: true, + }, + { + name: "Non-existent field in src", + args: args{ + value: nil, + src: format.Ptr(".metadata.nonExistentField"), + v: corev1.Pod{}, + }, + want: 0, + wantOk: false, + }, + { + name: "Non-string result from query", + args: args{ + value: nil, + src: format.Ptr(".spec.containers"), + v: corev1.Pod{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + {Name: "container1"}, + }, + }, + }, + }, + want: 0, + wantOk: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 3ef4f5d24436bcb53985a71d90408945b4d23e5c Mon Sep 17 00:00:00 2001 From: Harshith Mente <109957201+joeyyy09@users.noreply.github.com> Date: Mon, 3 Jun 2024 15:01:01 +0530 Subject: [PATCH 2/3] Update pkg/utils/expression/value_from_test.go Co-authored-by: Shiming Zhang --- pkg/utils/expression/value_from_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/utils/expression/value_from_test.go b/pkg/utils/expression/value_from_test.go index de445e37a..cf5797dbe 100644 --- a/pkg/utils/expression/value_from_test.go +++ b/pkg/utils/expression/value_from_test.go @@ -86,7 +86,7 @@ func TestDurationFrom_Get(t *testing.T) { v: corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ Annotations: map[string]string{ - "validRFC3339": validRFC3339Time, + "validRFC3339": now.Add(time.Hour).Format(time.RFC3339Nano), }, }, }, From cd1c054822eaad640fe3bea18df5b665dd8b775d Mon Sep 17 00:00:00 2001 From: Joeyyy09 Date: Mon, 3 Jun 2024 15:17:56 +0530 Subject: [PATCH 3/3] Add tests where both value and src are present Signed-off-by: Joeyyy09 --- pkg/utils/expression/value_from_test.go | 94 ++++++++++++++++++++----- 1 file changed, 75 insertions(+), 19 deletions(-) diff --git a/pkg/utils/expression/value_from_test.go b/pkg/utils/expression/value_from_test.go index cf5797dbe..2d562ca0c 100644 --- a/pkg/utils/expression/value_from_test.go +++ b/pkg/utils/expression/value_from_test.go @@ -1,5 +1,5 @@ /* -Copyright 2022 The Kubernetes Authors. +Copyright 2024 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -30,8 +30,6 @@ import ( func TestDurationFrom_Get(t *testing.T) { now := time.Now().Truncate(time.Second) nowPlusOneSecond := metav1.NewTime(now.Add(time.Second)) - nowPlusOneHour := now.Add(time.Hour) - validRFC3339Time := nowPlusOneHour.Format(time.RFC3339Nano) type args struct { value *time.Duration @@ -126,22 +124,6 @@ func TestDurationFrom_Get(t *testing.T) { want: time.Hour, wantOk: true, }, - { - name: "Invalid duration string src", - args: args{ - value: nil, - src: format.Ptr(".metadata.annotations.invalidDuration"), - v: corev1.Pod{ - ObjectMeta: metav1.ObjectMeta{ - Annotations: map[string]string{ - "invalidDuration": "invalid-duration", - }, - }, - }, - }, - want: 0, - wantOk: false, - }, { name: "Empty string src", args: args{ @@ -244,6 +226,80 @@ func TestDurationFrom_Get(t *testing.T) { want: 0, wantOk: false, }, + { + name: "Value present with valid RFC3339 time src", + args: args{ + value: format.Ptr(time.Duration(5)), + src: format.Ptr(".metadata.annotations.validRFC3339"), + v: corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + "validRFC3339": now.Add(time.Hour).Format(time.RFC3339Nano), + }, + }, + }, + }, + want: time.Hour, + wantOk: true, + }, + { + name: "Value present with valid duration string src", + args: args{ + value: format.Ptr(time.Duration(5)), + src: format.Ptr(".metadata.annotations.validDuration"), + v: corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + "validDuration": "1h", + }, + }, + }, + }, + want: time.Hour, + wantOk: true, + }, + { + name: "Value present with non-existent field in src", + args: args{ + value: format.Ptr(time.Duration(5)), + src: format.Ptr(".metadata.nonExistentField"), + v: corev1.Pod{}, + }, + want: 5, + wantOk: true, + }, + { + name: "Value 0 with valid RFC3339 time src", + args: args{ + value: format.Ptr(time.Duration(0)), + src: format.Ptr(".metadata.annotations.validRFC3339"), + v: corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + "validRFC3339": now.Add(time.Hour).Format(time.RFC3339Nano), + }, + }, + }, + }, + want: time.Hour, + wantOk: true, + }, + { + name: "Value 0 with valid duration string src", + args: args{ + value: format.Ptr(time.Duration(0)), + src: format.Ptr(".metadata.annotations.validDuration"), + v: corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + "validDuration": "1h", + }, + }, + }, + }, + want: time.Hour, + wantOk: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {