mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Merge pull request #53042 from chentao1596/support-unit-test-case-for-pod-format
Automatic merge from submit-queue (batch tested with PRs 67177, 53042). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Adding unit tests to methods of pod's format What this PR does / why we need it: Add unit test cases, thank you!
This commit is contained in:
commit
57bb26911d
@ -1813,7 +1813,7 @@ func (kl *Kubelet) syncLoopIteration(configCh <-chan kubetypes.PodUpdate, handle
|
||||
// once we have checkpointing.
|
||||
handler.HandlePodAdditions(u.Pods)
|
||||
case kubetypes.UPDATE:
|
||||
glog.V(2).Infof("SyncLoop (UPDATE, %q): %q", u.Source, format.PodsWithDeletiontimestamps(u.Pods))
|
||||
glog.V(2).Infof("SyncLoop (UPDATE, %q): %q", u.Source, format.PodsWithDeletionTimestamps(u.Pods))
|
||||
handler.HandlePodUpdates(u.Pods)
|
||||
case kubetypes.REMOVE:
|
||||
glog.V(2).Infof("SyncLoop (REMOVE, %q): %q", u.Source, format.Pods(u.Pods))
|
||||
|
@ -1,10 +1,4 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
@ -13,6 +7,7 @@ go_library(
|
||||
"resources.go",
|
||||
],
|
||||
importpath = "k8s.io/kubernetes/pkg/kubelet/util/format",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
|
||||
@ -21,11 +16,17 @@ go_library(
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["resources_test.go"],
|
||||
srcs = [
|
||||
"pod_test.go",
|
||||
"resources_test.go",
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
|
||||
"//vendor/github.com/stretchr/testify/assert:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
@ -40,4 +41,5 @@ filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
@ -57,9 +57,9 @@ func Pods(pods []*v1.Pod) string {
|
||||
return aggregatePods(pods, Pod)
|
||||
}
|
||||
|
||||
// PodsWithDeletiontimestamps is the same as Pods. In addition, it prints the
|
||||
// PodsWithDeletionTimestamps is the same as Pods. In addition, it prints the
|
||||
// deletion timestamps of the pods if they are not nil.
|
||||
func PodsWithDeletiontimestamps(pods []*v1.Pod) string {
|
||||
func PodsWithDeletionTimestamps(pods []*v1.Pod) string {
|
||||
return aggregatePods(pods, PodWithDeletionTimestamp)
|
||||
}
|
||||
|
||||
|
151
pkg/kubelet/util/format/pod_test.go
Normal file
151
pkg/kubelet/util/format/pod_test.go
Normal file
@ -0,0 +1,151 @@
|
||||
/*
|
||||
Copyright 2017 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.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package format
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
)
|
||||
|
||||
func fakeCreatePod(name, namespace string, uid types.UID) *v1.Pod {
|
||||
return &v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: namespace,
|
||||
UID: uid,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func fakeCreatePodWithDeletionTimestamp(name, namespace string, uid types.UID, deletionTimestamp *metav1.Time) *v1.Pod {
|
||||
return &v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: namespace,
|
||||
UID: uid,
|
||||
DeletionTimestamp: deletionTimestamp,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestPod(t *testing.T) {
|
||||
testCases := []struct {
|
||||
caseName string
|
||||
pod *v1.Pod
|
||||
expectedValue string
|
||||
}{
|
||||
{"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)"},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
realPod := Pod(testCase.pod)
|
||||
assert.Equalf(t, testCase.expectedValue, realPod, "Failed to test: %s", testCase.caseName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPodAndPodDesc(t *testing.T) {
|
||||
testCases := []struct {
|
||||
caseName string
|
||||
podName string
|
||||
podNamesapce string
|
||||
podUID types.UID
|
||||
expectedValue string
|
||||
}{
|
||||
{"field_empty_case", "", "", "", "_()"},
|
||||
{"field_normal_case", "test-pod", metav1.NamespaceDefault, "551f5a43-9f2f-11e7-a589-fa163e148d75", "test-pod_default(551f5a43-9f2f-11e7-a589-fa163e148d75)"},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
realPodDesc := PodDesc(testCase.podName, testCase.podNamesapce, testCase.podUID)
|
||||
assert.Equalf(t, testCase.expectedValue, realPodDesc, "Failed to test: %s", testCase.caseName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPodWithDeletionTimestamp(t *testing.T) {
|
||||
normalDeletionTime := metav1.Date(2017, time.September, 26, 14, 37, 50, 00, time.UTC)
|
||||
|
||||
testCases := []struct {
|
||||
caseName string
|
||||
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"},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
fakePod := fakeCreatePodWithDeletionTimestamp("test-pod", metav1.NamespaceDefault, "551f5a43-9f2f-11e7-a589-fa163e148d75", &testCase.deletionTimestamp)
|
||||
|
||||
if testCase.isdeletionTimestampNil {
|
||||
fakePod.SetDeletionTimestamp(nil)
|
||||
}
|
||||
|
||||
realPodWithDeletionTimestamp := PodWithDeletionTimestamp(fakePod)
|
||||
assert.Equalf(t, testCase.expectedValue, realPodWithDeletionTimestamp, "Failed to test: %s", testCase.caseName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPods(t *testing.T) {
|
||||
pod1 := fakeCreatePod("pod1", metav1.NamespaceDefault, "551f5a43-9f2f-11e7-a589-fa163e148d75")
|
||||
pod2 := fakeCreatePod("pod2", metav1.NamespaceDefault, "e84a99bf-d1f9-43c2-9fa5-044ac85f794b")
|
||||
|
||||
testCases := []struct {
|
||||
caseName string
|
||||
pods []*v1.Pod
|
||||
expectedValue string
|
||||
}{
|
||||
{"input_nil_case", nil, ""},
|
||||
{"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)"},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
realPods := Pods(testCase.pods)
|
||||
assert.Equalf(t, testCase.expectedValue, realPods, "Failed to test: %s", testCase.caseName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPodsWithDeletionTimestamps(t *testing.T) {
|
||||
normalDeletionTime := metav1.Date(2017, time.September, 26, 14, 37, 50, 00, time.UTC)
|
||||
pod1 := fakeCreatePodWithDeletionTimestamp("pod1", metav1.NamespaceDefault, "551f5a43-9f2f-11e7-a589-fa163e148d75", &normalDeletionTime)
|
||||
pod2 := fakeCreatePodWithDeletionTimestamp("pod2", metav1.NamespaceDefault, "e84a99bf-d1f9-43c2-9fa5-044ac85f794b", &normalDeletionTime)
|
||||
|
||||
testCases := []struct {
|
||||
caseName string
|
||||
pods []*v1.Pod
|
||||
expectedValue string
|
||||
}{
|
||||
{"input_nil_case", nil, ""},
|
||||
{"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"},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
realPods := PodsWithDeletionTimestamps(testCase.pods)
|
||||
assert.Equalf(t, testCase.expectedValue, realPods, "Failed to test: %s", testCase.caseName)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user