Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Test Coverage and Handle Edge Cases in value_from_test. #1131

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 174 additions & 2 deletions pkg/utils/expression/value_from_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,6 +46,7 @@ func TestDurationFrom_Get(t *testing.T) {
wantOk bool
}{
{
name: "No value, no src",
args: args{
value: nil,
src: nil,
Expand All @@ -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,
Expand All @@ -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,
joeyyy09 marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
},
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",
joeyyy09 marked this conversation as resolved.
Show resolved Hide resolved
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) {
Expand Down
Loading