use require to simplify testcases

This commit is contained in:
cncal 2022-04-09 10:45:13 +08:00
parent a64b9cee21
commit fa1d1edbef

View File

@ -17,6 +17,7 @@ limitations under the License.
package state package state
import ( import (
"github.com/stretchr/testify/require"
"io/ioutil" "io/ioutil"
"os" "os"
"reflect" "reflect"
@ -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,26 +216,19 @@ 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 strings.TrimSpace(tc.expectedError) == "" { if strings.TrimSpace(tc.expectedError) == "" {
if err != nil { require.NoError(t, err)
t.Fatalf("unexpected error while creating checkpointState: %v", err)
}
} else { } else {
if err == nil { require.Error(t, err)
t.Fatalf("expected error: %s, got nil", tc.expectedError) require.Contains(t, err.Error(), "could not restore state from checkpoint")
} require.Contains(t, err.Error(), tc.expectedError)
if strings.Contains(err.Error(), "could not restore state from checkpoint") &&
strings.Contains(err.Error(), tc.expectedError) {
t.Logf("got expected error: %v", err)
return return
} }
}
// compare state after restoration with the one expected // compare state after restoration with the one expected
AssertStateEqual(t, restoredState, tc.expectedState) AssertStateEqual(t, restoredState, tc.expectedState)