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 all commits
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
234 changes: 231 additions & 3 deletions pkg/utils/expression/value_from_test.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -29,7 +29,8 @@ 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))

type args struct {
value *time.Duration
src *string
Expand All @@ -43,6 +44,7 @@ func TestDurationFrom_Get(t *testing.T) {
wantOk bool
}{
{
name: "No value, no src",
args: args{
value: nil,
src: nil,
Expand All @@ -51,6 +53,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 +63,243 @@ 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": now.Add(time.Hour).Format(time.RFC3339Nano),
},
},
},
},
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: "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,
},
{
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) {
Expand Down
Loading