mirror of https://github.com/k3s-io/k3s
Merge pull request #54680 from tianshapjq/testcase-pod/util_test.go
Automatic merge from submit-queue. 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>. new testcases to v1/pod/util.go **What this PR does / why we need it**: some new testcases to v1/pod/util.gopull/6/head
commit
bc38a3761f
|
@ -23,6 +23,7 @@ go_test(
|
||||||
embed = [":go_default_library"],
|
embed = [":go_default_library"],
|
||||||
importpath = "k8s.io/kubernetes/pkg/api/v1/pod",
|
importpath = "k8s.io/kubernetes/pkg/api/v1/pod",
|
||||||
deps = [
|
deps = [
|
||||||
|
"//vendor/github.com/stretchr/testify/assert:go_default_library",
|
||||||
"//vendor/k8s.io/api/core/v1:go_default_library",
|
"//vendor/k8s.io/api/core/v1:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
|
||||||
|
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/util/intstr"
|
"k8s.io/apimachinery/pkg/util/intstr"
|
||||||
|
@ -404,3 +405,118 @@ func TestIsPodAvailable(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetContainerStatus(t *testing.T) {
|
||||||
|
type ExpectedStruct struct {
|
||||||
|
status v1.ContainerStatus
|
||||||
|
exists bool
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
status []v1.ContainerStatus
|
||||||
|
name string
|
||||||
|
expected ExpectedStruct
|
||||||
|
desc string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
status: []v1.ContainerStatus{{Name: "test1", Ready: false, Image: "image1"}, {Name: "test2", Ready: true, Image: "image1"}},
|
||||||
|
name: "test1",
|
||||||
|
expected: ExpectedStruct{status: v1.ContainerStatus{Name: "test1", Ready: false, Image: "image1"}, exists: true},
|
||||||
|
desc: "retrieve ContainerStatus with Name=\"test1\"",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: []v1.ContainerStatus{{Name: "test2", Ready: false, Image: "image2"}},
|
||||||
|
name: "test1",
|
||||||
|
expected: ExpectedStruct{status: v1.ContainerStatus{}, exists: false},
|
||||||
|
desc: "no matching ContainerStatus with Name=\"test1\"",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: []v1.ContainerStatus{{Name: "test3", Ready: false, Image: "image3"}},
|
||||||
|
name: "",
|
||||||
|
expected: ExpectedStruct{status: v1.ContainerStatus{}, exists: false},
|
||||||
|
desc: "retrieve an empty ContainerStatus with contianer name empty",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: nil,
|
||||||
|
name: "",
|
||||||
|
expected: ExpectedStruct{status: v1.ContainerStatus{}, exists: false},
|
||||||
|
desc: "retrieve an empty ContainerStatus with status nil",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
resultStatus, exists := GetContainerStatus(test.status, test.name)
|
||||||
|
assert.Equal(t, test.expected.status, resultStatus, "GetContainerStatus: "+test.desc)
|
||||||
|
assert.Equal(t, test.expected.exists, exists, "GetContainerStatus: "+test.desc)
|
||||||
|
|
||||||
|
resultStatus = GetExistingContainerStatus(test.status, test.name)
|
||||||
|
assert.Equal(t, test.expected.status, resultStatus, "GetExistingContainerStatus: "+test.desc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUpdatePodCondition(t *testing.T) {
|
||||||
|
time := metav1.Now()
|
||||||
|
|
||||||
|
podStatus := v1.PodStatus{
|
||||||
|
Conditions: []v1.PodCondition{
|
||||||
|
{
|
||||||
|
Type: v1.PodReady,
|
||||||
|
Status: v1.ConditionTrue,
|
||||||
|
Reason: "successfully",
|
||||||
|
Message: "sync pod successfully",
|
||||||
|
LastProbeTime: time,
|
||||||
|
LastTransitionTime: metav1.NewTime(time.Add(1000)),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
status *v1.PodStatus
|
||||||
|
conditions v1.PodCondition
|
||||||
|
expected bool
|
||||||
|
desc string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
status: &podStatus,
|
||||||
|
conditions: v1.PodCondition{
|
||||||
|
Type: v1.PodReady,
|
||||||
|
Status: v1.ConditionTrue,
|
||||||
|
Reason: "successfully",
|
||||||
|
Message: "sync pod successfully",
|
||||||
|
LastProbeTime: time,
|
||||||
|
LastTransitionTime: metav1.NewTime(time.Add(1000))},
|
||||||
|
expected: false,
|
||||||
|
desc: "all equal, no update",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: &podStatus,
|
||||||
|
conditions: v1.PodCondition{
|
||||||
|
Type: v1.PodScheduled,
|
||||||
|
Status: v1.ConditionTrue,
|
||||||
|
Reason: "successfully",
|
||||||
|
Message: "sync pod successfully",
|
||||||
|
LastProbeTime: time,
|
||||||
|
LastTransitionTime: metav1.NewTime(time.Add(1000))},
|
||||||
|
expected: true,
|
||||||
|
desc: "not equal Type, should get updated",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
status: &podStatus,
|
||||||
|
conditions: v1.PodCondition{
|
||||||
|
Type: v1.PodReady,
|
||||||
|
Status: v1.ConditionFalse,
|
||||||
|
Reason: "successfully",
|
||||||
|
Message: "sync pod successfully",
|
||||||
|
LastProbeTime: time,
|
||||||
|
LastTransitionTime: metav1.NewTime(time.Add(1000))},
|
||||||
|
expected: true,
|
||||||
|
desc: "not equal Status, should get updated",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
var resultStatus bool
|
||||||
|
resultStatus = UpdatePodCondition(test.status, &test.conditions)
|
||||||
|
|
||||||
|
assert.Equal(t, test.expected, resultStatus, test.desc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue