Merge pull request #119058 from TommyStarK/dra-state-checkpoint-unit-test

dynamic resource allocation: Improve code coverage of state checkpoint
This commit is contained in:
Kubernetes Prow Robot 2023-07-12 07:49:14 -07:00 committed by GitHub
commit be222f38f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 0 deletions

View File

@ -67,6 +67,10 @@ type stateCheckpoint struct {
// NewCheckpointState creates new State for keeping track of claim info with checkpoint backend // NewCheckpointState creates new State for keeping track of claim info with checkpoint backend
func NewCheckpointState(stateDir, checkpointName string) (*stateCheckpoint, error) { func NewCheckpointState(stateDir, checkpointName string) (*stateCheckpoint, error) {
if len(checkpointName) == 0 {
return nil, fmt.Errorf("received empty string instead of checkpointName")
}
checkpointManager, err := checkpointmanager.NewCheckpointManager(stateDir) checkpointManager, err := checkpointmanager.NewCheckpointManager(stateDir)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to initialize checkpoint manager: %v", err) return nil, fmt.Errorf("failed to initialize checkpoint manager: %v", err)

View File

@ -183,6 +183,11 @@ func TestCheckpointStateStore(t *testing.T) {
expectedCheckpoint := `{"version":"v1","entries":[{"DriverName":"test-driver.cdi.k8s.io","ClassName":"class-name","ClaimUID":"067798be-454e-4be4-9047-1aa06aea63f7","ClaimName":"example","Namespace":"default","PodUIDs":{"139cdb46-f989-4f17-9561-ca10cfb509a6":{}},"CDIDevices":{"test-driver.cdi.k8s.io":["example.com/example=cdi-example"]}}],"checksum":153446146}` expectedCheckpoint := `{"version":"v1","entries":[{"DriverName":"test-driver.cdi.k8s.io","ClassName":"class-name","ClaimUID":"067798be-454e-4be4-9047-1aa06aea63f7","ClaimName":"example","Namespace":"default","PodUIDs":{"139cdb46-f989-4f17-9561-ca10cfb509a6":{}},"CDIDevices":{"test-driver.cdi.k8s.io":["example.com/example=cdi-example"]}}],"checksum":153446146}`
// Should return an error, stateDir cannot be an empty string
if _, err := NewCheckpointState("", testingCheckpoint); err == nil {
t.Fatal("expected error but got nil")
}
// create temp dir // create temp dir
testingDir, err := os.MkdirTemp("", "dramanager_state_test") testingDir, err := os.MkdirTemp("", "dramanager_state_test")
if err != nil { if err != nil {
@ -203,4 +208,9 @@ func TestCheckpointStateStore(t *testing.T) {
checkpointData, err := checkpoint.MarshalCheckpoint() checkpointData, err := checkpoint.MarshalCheckpoint()
assert.NoError(t, err, "could not Marshal Checkpoint") assert.NoError(t, err, "could not Marshal Checkpoint")
assert.Equal(t, expectedCheckpoint, string(checkpointData), "expected ClaimInfoState does not equal to restored one") assert.Equal(t, expectedCheckpoint, string(checkpointData), "expected ClaimInfoState does not equal to restored one")
// NewCheckpointState with an empty checkpointName should return an error
if _, err = NewCheckpointState(testingDir, ""); err == nil {
t.Fatal("expected error but got nil")
}
} }