diff --git a/pkg/kubelet/status/state/checkpoint.go b/pkg/kubelet/status/state/checkpoint.go index 957fed102cd..68692f8c1f3 100644 --- a/pkg/kubelet/status/state/checkpoint.go +++ b/pkg/kubelet/status/state/checkpoint.go @@ -19,7 +19,8 @@ package state import ( "encoding/json" "fmt" - "k8s.io/api/core/v1" + + v1 "k8s.io/api/core/v1" "k8s.io/kubernetes/pkg/kubelet/checkpointmanager" "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum" ) diff --git a/pkg/kubelet/status/state/state_checkpoint_test.go b/pkg/kubelet/status/state/state_checkpoint_test.go index 4d7c1ca86de..ca3546553ff 100644 --- a/pkg/kubelet/status/state/state_checkpoint_test.go +++ b/pkg/kubelet/status/state/state_checkpoint_test.go @@ -31,16 +31,7 @@ import ( const testCheckpoint = "pod_status_manager_state" func newTestStateCheckpoint(t *testing.T) *stateCheckpoint { - // create temp dir - testingDir, err := os.MkdirTemp("", "pod_resource_allocation_state_test") - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { - if err := os.RemoveAll(testingDir); err != nil { - t.Fatal(err) - } - }) + testingDir := getTestDir(t) cache := NewStateMemory() checkpointManager, err := checkpointmanager.NewCheckpointManager(testingDir) require.NoError(t, err, "failed to create checkpoint manager") @@ -55,9 +46,7 @@ func newTestStateCheckpoint(t *testing.T) *stateCheckpoint { func getTestDir(t *testing.T) string { testingDir, err := os.MkdirTemp("", "pod_resource_allocation_state_test") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err, "failed to create temp dir") t.Cleanup(func() { if err := os.RemoveAll(testingDir); err != nil { t.Fatal(err) @@ -146,16 +135,6 @@ func Test_stateCheckpoint_formatUpgraded(t *testing.T) { // prepare old checkpoint, ResizeStatusEntries is unset, // pretend that the old checkpoint is unaware for the field ResizeStatusEntries const checkpointContent = `{"data":"{\"allocationEntries\":{\"pod1\":{\"container1\":{\"requests\":{\"cpu\":\"1Ki\",\"memory\":\"1Ki\"}}}}}","checksum":1555601526}` - checkpoint := &Checkpoint{} - err := checkpoint.UnmarshalCheckpoint([]byte(checkpointContent)) - require.NoError(t, err, "failed to unmarshal checkpoint") - - err = sc.checkpointManager.CreateCheckpoint(sc.checkpointName, checkpoint) - require.NoError(t, err, "failed to create old checkpoint") - - err = sc.restoreState() - require.NoError(t, err, "failed to restore state") - expectedPodResourceAllocationInfo := &PodResourceAllocationInfo{ AllocationEntries: map[string]map[string]v1.ResourceRequirements{ "pod1": { @@ -169,6 +148,16 @@ func Test_stateCheckpoint_formatUpgraded(t *testing.T) { }, ResizeStatusEntries: map[string]v1.PodResizeStatus{}, } + checkpoint := &Checkpoint{} + err := checkpoint.UnmarshalCheckpoint([]byte(checkpointContent)) + require.NoError(t, err, "failed to unmarshal checkpoint") + + err = sc.checkpointManager.CreateCheckpoint(sc.checkpointName, checkpoint) + require.NoError(t, err, "failed to create old checkpoint") + + err = sc.restoreState() + require.NoError(t, err, "failed to restore state") + actualPodResourceAllocationInfo := &PodResourceAllocationInfo{} actualPodResourceAllocationInfo.AllocationEntries = sc.cache.GetPodResourceAllocation() actualPodResourceAllocationInfo.ResizeStatusEntries = sc.cache.GetResizeStatus()