mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 19:31:44 +00:00
Merge pull request #89286 from uzuku/mar-fix-nil-pointer-in-format
Handle nil pod in pod format
This commit is contained in:
commit
69681d7df8
@ -30,6 +30,9 @@ type podHandler func(*v1.Pod) string
|
||||
// Pod returns a string representing a pod in a consistent human readable format,
|
||||
// with pod UID as part of the string.
|
||||
func Pod(pod *v1.Pod) string {
|
||||
if pod == nil {
|
||||
return "<nil>"
|
||||
}
|
||||
return PodDesc(pod.Name, pod.Namespace, pod.UID)
|
||||
}
|
||||
|
||||
@ -44,6 +47,9 @@ func PodDesc(podName, podNamespace string, podUID types.UID) string {
|
||||
// PodWithDeletionTimestamp is the same as Pod. In addition, it prints the
|
||||
// deletion timestamp of the pod if it's not nil.
|
||||
func PodWithDeletionTimestamp(pod *v1.Pod) string {
|
||||
if pod == nil {
|
||||
return "<nil>"
|
||||
}
|
||||
var deletionTimestamp string
|
||||
if pod.DeletionTimestamp != nil {
|
||||
deletionTimestamp = ":DeletionTimestamp=" + pod.DeletionTimestamp.UTC().Format(time.RFC3339)
|
||||
|
@ -56,6 +56,7 @@ func TestPod(t *testing.T) {
|
||||
}{
|
||||
{"field_empty_case", fakeCreatePod("", "", ""), "_()"},
|
||||
{"field_normal_case", fakeCreatePod("test-pod", metav1.NamespaceDefault, "551f5a43-9f2f-11e7-a589-fa163e148d75"), "test-pod_default(551f5a43-9f2f-11e7-a589-fa163e148d75)"},
|
||||
{"nil_pod_case", nil, "<nil>"},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
@ -87,12 +88,14 @@ func TestPodWithDeletionTimestamp(t *testing.T) {
|
||||
|
||||
testCases := []struct {
|
||||
caseName string
|
||||
isPodNil bool
|
||||
isdeletionTimestampNil bool
|
||||
deletionTimestamp metav1.Time
|
||||
expectedValue string
|
||||
}{
|
||||
{"timestamp_is_nil_case", true, normalDeletionTime, "test-pod_default(551f5a43-9f2f-11e7-a589-fa163e148d75)"},
|
||||
{"timestamp_is_normal_case", false, normalDeletionTime, "test-pod_default(551f5a43-9f2f-11e7-a589-fa163e148d75):DeletionTimestamp=2017-09-26T14:37:50Z"},
|
||||
{"timestamp_is_nil_case", false, true, normalDeletionTime, "test-pod_default(551f5a43-9f2f-11e7-a589-fa163e148d75)"},
|
||||
{"timestamp_is_normal_case", false, false, normalDeletionTime, "test-pod_default(551f5a43-9f2f-11e7-a589-fa163e148d75):DeletionTimestamp=2017-09-26T14:37:50Z"},
|
||||
{"pod_is_nil_case", true, false, normalDeletionTime, "<nil>"},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
@ -101,6 +104,9 @@ func TestPodWithDeletionTimestamp(t *testing.T) {
|
||||
if testCase.isdeletionTimestampNil {
|
||||
fakePod.SetDeletionTimestamp(nil)
|
||||
}
|
||||
if testCase.isPodNil {
|
||||
fakePod = nil
|
||||
}
|
||||
|
||||
realPodWithDeletionTimestamp := PodWithDeletionTimestamp(fakePod)
|
||||
assert.Equalf(t, testCase.expectedValue, realPodWithDeletionTimestamp, "Failed to test: %s", testCase.caseName)
|
||||
@ -120,6 +126,7 @@ func TestPods(t *testing.T) {
|
||||
{"input_empty_case", []*v1.Pod{}, ""},
|
||||
{"input_length_one_case", []*v1.Pod{pod1}, "pod1_default(551f5a43-9f2f-11e7-a589-fa163e148d75)"},
|
||||
{"input_length_more_than_one_case", []*v1.Pod{pod1, pod2}, "pod1_default(551f5a43-9f2f-11e7-a589-fa163e148d75), pod2_default(e84a99bf-d1f9-43c2-9fa5-044ac85f794b)"},
|
||||
{"input_include_nil_case", []*v1.Pod{pod1, nil}, "pod1_default(551f5a43-9f2f-11e7-a589-fa163e148d75), <nil>"},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
@ -142,6 +149,7 @@ func TestPodsWithDeletionTimestamps(t *testing.T) {
|
||||
{"input_empty_case", []*v1.Pod{}, ""},
|
||||
{"input_length_one_case", []*v1.Pod{pod1}, "pod1_default(551f5a43-9f2f-11e7-a589-fa163e148d75):DeletionTimestamp=2017-09-26T14:37:50Z"},
|
||||
{"input_length_more_than_one_case", []*v1.Pod{pod1, pod2}, "pod1_default(551f5a43-9f2f-11e7-a589-fa163e148d75):DeletionTimestamp=2017-09-26T14:37:50Z, pod2_default(e84a99bf-d1f9-43c2-9fa5-044ac85f794b):DeletionTimestamp=2017-09-26T14:37:50Z"},
|
||||
{"input_include_nil_case", []*v1.Pod{pod1, nil}, "pod1_default(551f5a43-9f2f-11e7-a589-fa163e148d75):DeletionTimestamp=2017-09-26T14:37:50Z, <nil>"},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
|
Loading…
Reference in New Issue
Block a user