Fix Kubelet unit tests

This commit is contained in:
Tim Allclair 2025-03-18 12:31:28 -07:00
parent 9be73c0d67
commit cd1a5c6d5c
4 changed files with 59 additions and 15 deletions

View File

@ -4026,12 +4026,15 @@ func TestValidateAllowSidecarResizePolicy(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
gotOptions := GetValidationOptionsFromPodSpecAndMeta(&api.PodSpec{}, tc.oldPodSpec, nil, nil)
if tc.wantOption != gotOptions.AllowSidecarResizePolicy {
t.Errorf("Got AllowSidecarResizePolicy=%t, want %t", gotOptions.AllowSidecarResizePolicy, tc.wantOption)
}
})
for _, ippvsEnabled := range []bool{true, false} {
t.Run(fmt.Sprintf("%s/%t", tc.name, ippvsEnabled), func(t *testing.T) {
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.InPlacePodVerticalScaling, ippvsEnabled)
gotOptions := GetValidationOptionsFromPodSpecAndMeta(&api.PodSpec{}, tc.oldPodSpec, nil, nil)
expected := tc.wantOption || ippvsEnabled
assert.Equal(t, expected, gotOptions.AllowSidecarResizePolicy, "AllowSidecarResizePolicy")
})
}
}
}

View File

@ -3349,6 +3349,10 @@ func Test_generateAPIPodStatus(t *testing.T) {
},
},
}
withResources := func(cs v1.ContainerStatus) v1.ContainerStatus {
cs.Resources = &v1.ResourceRequirements{}
return cs
}
now := metav1.Now()
normalized_now := now.Rfc3339Copy()
@ -3725,7 +3729,7 @@ func Test_generateAPIPodStatus(t *testing.T) {
},
ContainerStatuses: []v1.ContainerStatus{
ready(waitingStateWithReason("containerA", "ContainerCreating")),
ready(withID(runningStateWithStartedAt("containerB", time.Unix(1, 0).UTC()), "://foo")),
withResources(ready(withID(runningStateWithStartedAt("containerB", time.Unix(1, 0).UTC()), "://foo"))),
},
},
expectedPodReadyToStartContainersCondition: v1.PodCondition{
@ -3783,8 +3787,8 @@ func Test_generateAPIPodStatus(t *testing.T) {
{Type: v1.PodScheduled, Status: v1.ConditionTrue},
},
ContainerStatuses: []v1.ContainerStatus{
ready(withID(runningStateWithStartedAt("containerA", time.Unix(1, 0).UTC()), "://c1")),
ready(withID(runningStateWithStartedAt("containerB", time.Unix(2, 0).UTC()), "://c2")),
withResources(ready(withID(runningStateWithStartedAt("containerA", time.Unix(1, 0).UTC()), "://c1"))),
withResources(ready(withID(runningStateWithStartedAt("containerB", time.Unix(2, 0).UTC()), "://c2"))),
},
},
expectedPodReadyToStartContainersCondition: v1.PodCondition{
@ -4606,11 +4610,6 @@ func TestSortPodIPs(t *testing.T) {
}
}
// func init() {
// klog.InitFlags(flag.CommandLine)
// flag.CommandLine.Lookup("v").Value.Set("5")
// }
func TestConvertToAPIContainerStatusesDataRace(t *testing.T) {
pod := podWithUIDNameNs("12345", "test-pod", "test-namespace")
@ -5170,6 +5169,7 @@ func TestConvertToAPIContainerStatusesForUser(t *testing.T) {
ContainerID: testContainerID.String(),
Image: "img",
State: v1.ContainerState{Running: &v1.ContainerStateRunning{StartedAt: metav1.NewTime(nowTime)}},
Resources: &v1.ResourceRequirements{},
User: user,
},
}

View File

@ -1262,6 +1262,10 @@ func verifyActions(t *testing.T, expected, actual *podActions, desc string) {
actual.ContainersToKill[k] = info
}
}
if expected.ContainersToUpdate == nil && actual.ContainersToUpdate != nil {
// No need to distinguish empty and nil maps for the test.
expected.ContainersToUpdate = map[v1.ResourceName][]containerToUpdateInfo{}
}
assert.Equal(t, expected, actual, desc)
}

View File

@ -40,6 +40,7 @@ import (
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/apiserver/pkg/warning"
"k8s.io/client-go/tools/cache"
"k8s.io/component-base/featuregate"
featuregatetesting "k8s.io/component-base/featuregate/testing"
ptr "k8s.io/utils/ptr"
@ -3330,6 +3331,7 @@ func TestStatusPrepareForUpdate(t *testing.T) {
oldPod *api.Pod
newPod *api.Pod
expected *api.Pod
features map[featuregate.Feature]bool
}{
{
description: "preserve old owner references",
@ -3371,7 +3373,10 @@ func TestStatusPrepareForUpdate(t *testing.T) {
},
},
{
description: "drop disabled status fields",
description: "drop disabled status fields/InPlacePodVerticalScaling=false",
features: map[featuregate.Feature]bool{
features.InPlacePodVerticalScaling: false,
},
oldPod: &api.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "pod"},
Status: api.PodStatus{},
@ -3394,6 +3399,35 @@ func TestStatusPrepareForUpdate(t *testing.T) {
},
},
},
{
description: "drop disabled status fields/InPlacePodVerticalScaling=true",
features: map[featuregate.Feature]bool{
features.InPlacePodVerticalScaling: true,
},
oldPod: &api.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "pod"},
Status: api.PodStatus{},
},
newPod: &api.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "pod"},
Status: api.PodStatus{
ResourceClaimStatuses: []api.PodResourceClaimStatus{
{Name: "my-claim", ResourceClaimName: ptr.To("pod-my-claim")},
},
ContainerStatuses: []api.ContainerStatus{
{Resources: &api.ResourceRequirements{}},
},
},
},
expected: &api.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "pod"},
Status: api.PodStatus{
ContainerStatuses: []api.ContainerStatus{
{Resources: &api.ResourceRequirements{}},
},
},
},
},
{
description: "preserve old status.observedGeneration if empty",
oldPod: &api.Pod{
@ -3553,6 +3587,9 @@ func TestStatusPrepareForUpdate(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
for f, v := range tc.features {
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, f, v)
}
StatusStrategy.PrepareForUpdate(genericapirequest.NewContext(), tc.newPod, tc.oldPod)
if !cmp.Equal(tc.expected, tc.newPod) {
t.Errorf("StatusStrategy.PrepareForUpdate() diff = %v", cmp.Diff(tc.expected, tc.newPod))