Merge pull request #106244 from cncal/fix-state-checkpoint-testcase

fix test for CheckpointStateRestore
This commit is contained in:
Kubernetes Prow Robot 2022-07-29 15:41:14 -07:00 committed by GitHub
commit 5d446b205e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,6 +23,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/require"
"k8s.io/kubernetes/pkg/kubelet/checkpointmanager" "k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
"k8s.io/kubernetes/pkg/kubelet/cm/containermap" "k8s.io/kubernetes/pkg/kubelet/cm/containermap"
testutil "k8s.io/kubernetes/pkg/kubelet/cm/cpumanager/state/testing" testutil "k8s.io/kubernetes/pkg/kubelet/cm/cpumanager/state/testing"
@ -201,15 +202,11 @@ func TestCheckpointStateRestore(t *testing.T) {
// create temp dir // create temp dir
testingDir, err := ioutil.TempDir("", "cpumanager_state_test") testingDir, err := ioutil.TempDir("", "cpumanager_state_test")
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
defer os.RemoveAll(testingDir) defer os.RemoveAll(testingDir)
// create checkpoint manager for testing // create checkpoint manager for testing
cpm, err := checkpointmanager.NewCheckpointManager(testingDir) cpm, err := checkpointmanager.NewCheckpointManager(testingDir)
if err != nil { require.NoErrorf(t, err, "could not create testing checkpoint manager: %v", err)
t.Fatalf("could not create testing checkpoint manager: %v", err)
}
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) { t.Run(tc.description, func(t *testing.T) {
@ -219,21 +216,18 @@ func TestCheckpointStateRestore(t *testing.T) {
// prepare checkpoint for testing // prepare checkpoint for testing
if strings.TrimSpace(tc.checkpointContent) != "" { if strings.TrimSpace(tc.checkpointContent) != "" {
checkpoint := &testutil.MockCheckpoint{Content: tc.checkpointContent} checkpoint := &testutil.MockCheckpoint{Content: tc.checkpointContent}
if err := cpm.CreateCheckpoint(testingCheckpoint, checkpoint); err != nil { err = cpm.CreateCheckpoint(testingCheckpoint, checkpoint)
t.Fatalf("could not create testing checkpoint: %v", err) require.NoErrorf(t, err, "could not create testing checkpoint: %v", err)
}
} }
restoredState, err := NewCheckpointState(testingDir, testingCheckpoint, tc.policyName, tc.initialContainers) restoredState, err := NewCheckpointState(testingDir, testingCheckpoint, tc.policyName, tc.initialContainers)
if err != nil { if strings.TrimSpace(tc.expectedError) == "" {
if strings.TrimSpace(tc.expectedError) != "" { require.NoError(t, err)
if strings.Contains(err.Error(), "could not restore state from checkpoint") && } else {
strings.Contains(err.Error(), tc.expectedError) { require.Error(t, err)
t.Logf("got expected error: %v", err) require.Contains(t, err.Error(), "could not restore state from checkpoint")
return require.Contains(t, err.Error(), tc.expectedError)
} return
}
t.Fatalf("unexpected error while creatng checkpointState: %v", err)
} }
// compare state after restoration with the one expected // compare state after restoration with the one expected