dra scheduler: simplify unit tests

The guideline in
https://github.com/kubernetes/community/blob/master/sig-scheduling/CONTRIBUTING.md#technical-and-style-guidelines
is to not compare error strings. This makes the tests less precise. In return,
unit tests don't need to be updated when error strings change.
This commit is contained in:
Patrick Ohly 2024-03-27 10:18:53 +01:00
parent 458e227de0
commit 6f5696b537
2 changed files with 26 additions and 30 deletions

View File

@ -189,29 +189,29 @@ func TestController(t *testing.T) {
filter *resourceapi.NamedResourcesFilter filter *resourceapi.NamedResourcesFilter
requests []*resourceapi.NamedResourcesRequest requests []*resourceapi.NamedResourcesRequest
expectCreateErr string expectCreateErr bool
expectAllocation []string expectAllocation []string
expectAllocateErr string expectAllocateErr bool
}{ }{
"empty": {}, "empty": {},
"broken-filter": { "broken-filter": {
filter: filterBrokenType, filter: filterBrokenType,
expectCreateErr: "compile class filter CEL expression: must evaluate to bool", expectCreateErr: true,
}, },
"broken-request": { "broken-request": {
requests: []*resourceapi.NamedResourcesRequest{requestBrokenType}, requests: []*resourceapi.NamedResourcesRequest{requestBrokenType},
expectCreateErr: "compile request CEL expression: must evaluate to bool", expectCreateErr: true,
}, },
"no-resources": { "no-resources": {
filter: filterAny, filter: filterAny,
requests: []*resourceapi.NamedResourcesRequest{requestAny}, requests: []*resourceapi.NamedResourcesRequest{requestAny},
expectAllocateErr: "insufficient resources", expectAllocateErr: true,
}, },
"okay": { "okay": {
@ -227,7 +227,7 @@ func TestController(t *testing.T) {
filter: filterNone, filter: filterNone,
requests: []*resourceapi.NamedResourcesRequest{requestAny}, requests: []*resourceapi.NamedResourcesRequest{requestAny},
expectAllocateErr: "insufficient resources", expectAllocateErr: true,
}, },
"request-mismatch": { "request-mismatch": {
@ -235,7 +235,7 @@ func TestController(t *testing.T) {
filter: filterAny, filter: filterAny,
requests: []*resourceapi.NamedResourcesRequest{requestNone}, requests: []*resourceapi.NamedResourcesRequest{requestNone},
expectAllocateErr: "insufficient resources", expectAllocateErr: true,
}, },
"many": { "many": {
@ -251,7 +251,7 @@ func TestController(t *testing.T) {
filter: filterAny, filter: filterAny,
requests: []*resourceapi.NamedResourcesRequest{requestAny, requestAny}, requests: []*resourceapi.NamedResourcesRequest{requestAny, requestAny},
expectAllocateErr: "insufficient resources", expectAllocateErr: true,
}, },
"filter-evaluation-error": { "filter-evaluation-error": {
@ -259,7 +259,7 @@ func TestController(t *testing.T) {
filter: filterBrokenEvaluation, filter: filterBrokenEvaluation,
requests: []*resourceapi.NamedResourcesRequest{requestAny}, requests: []*resourceapi.NamedResourcesRequest{requestAny},
expectAllocateErr: "evaluate filter CEL expression: no such key: no-such-attribute", expectAllocateErr: true,
}, },
"request-evaluation-error": { "request-evaluation-error": {
@ -267,7 +267,7 @@ func TestController(t *testing.T) {
filter: filterAny, filter: filterAny,
requests: []*resourceapi.NamedResourcesRequest{requestBrokenEvaluation}, requests: []*resourceapi.NamedResourcesRequest{requestBrokenEvaluation},
expectAllocateErr: "evaluate request CEL expression: no such key: no-such-attribute", expectAllocateErr: true,
}, },
"filter-attribute": { "filter-attribute": {
@ -293,26 +293,24 @@ func TestController(t *testing.T) {
controller, createErr := NewClaimController(tc.filter, tc.requests) controller, createErr := NewClaimController(tc.filter, tc.requests)
if createErr != nil { if createErr != nil {
if tc.expectCreateErr == "" { if !tc.expectCreateErr {
tCtx.Fatalf("unexpected create error: %v", createErr) tCtx.Fatalf("unexpected create error: %v", createErr)
} }
require.Equal(tCtx, tc.expectCreateErr, createErr.Error())
return return
} }
if tc.expectCreateErr != "" { if tc.expectCreateErr {
tCtx.Fatalf("did not get expected create error: %v", tc.expectCreateErr) tCtx.Fatalf("did not get expected create error")
} }
allocation, createErr := controller.Allocate(tCtx, tc.model) allocation, createErr := controller.Allocate(tCtx, tc.model)
if createErr != nil { if createErr != nil {
if tc.expectAllocateErr == "" { if !tc.expectAllocateErr {
tCtx.Fatalf("unexpected allocate error: %v", createErr) tCtx.Fatalf("unexpected allocate error: %v", createErr)
} }
require.Equal(tCtx, tc.expectAllocateErr, createErr.Error())
return return
} }
if tc.expectAllocateErr != "" { if tc.expectAllocateErr {
tCtx.Fatalf("did not get expected allocate error: %v", tc.expectAllocateErr) tCtx.Fatalf("did not get expected allocate error")
} }
expectAllocation := []*resourceapi.NamedResourcesAllocationResult{} expectAllocation := []*resourceapi.NamedResourcesAllocationResult{}

View File

@ -42,14 +42,14 @@ func TestModel(t *testing.T) {
inFlight map[types.UID]resourceapi.ResourceClaimStatus inFlight map[types.UID]resourceapi.ResourceClaimStatus
wantResources resources wantResources resources
wantErr string wantErr bool
}{ }{
"empty": {}, "empty": {},
"slice-list-error": { "slice-list-error": {
slices: sliceError("slice list error"), slices: sliceError("slice list error"),
wantErr: "list node resource slices: slice list error", wantErr: true,
}, },
"unknown-model": { "unknown-model": {
@ -949,14 +949,13 @@ func TestModel(t *testing.T) {
actualResources, actualErr := newResourceModel(tCtx.Logger(), slices, claims, &inFlightAllocations) actualResources, actualErr := newResourceModel(tCtx.Logger(), slices, claims, &inFlightAllocations)
if actualErr != nil { if actualErr != nil {
if tc.wantErr == "" { if !tc.wantErr {
tCtx.Fatalf("unexpected error: %v", actualErr) tCtx.Fatalf("unexpected error: %v", actualErr)
} }
require.Equal(tCtx, tc.wantErr, actualErr.Error())
return return
} }
if tc.wantErr != "" { if tc.wantErr {
tCtx.Fatalf("did not get expected error: %v", tc.wantErr) tCtx.Fatalf("did not get expected error")
} }
expectResources := tc.wantResources expectResources := tc.wantResources
@ -1095,7 +1094,7 @@ func TestController(t *testing.T) {
classParameters *resourceapi.ResourceClassParameters classParameters *resourceapi.ResourceClassParameters
claimParameters *resourceapi.ResourceClaimParameters claimParameters *resourceapi.ResourceClaimParameters
expectCreateErr string expectCreateErr bool
expectNodeResults nodeResults expectNodeResults nodeResults
}{ }{
"empty": { "empty": {
@ -1127,7 +1126,7 @@ func TestController(t *testing.T) {
}}, }},
}, },
expectCreateErr: "claim parameters : driverRequests[0].requests[0]: no supported structured parameters found", expectCreateErr: true,
}, },
"no-resources": { "no-resources": {
@ -1212,14 +1211,13 @@ func TestController(t *testing.T) {
controller, err := newClaimController(tCtx.Logger(), tc.class, tc.classParameters, tc.claimParameters) controller, err := newClaimController(tCtx.Logger(), tc.class, tc.classParameters, tc.claimParameters)
if err != nil { if err != nil {
if tc.expectCreateErr == "" { if !tc.expectCreateErr {
tCtx.Fatalf("unexpected error: %v", err) tCtx.Fatalf("unexpected error: %v", err)
} }
require.Equal(tCtx, tc.expectCreateErr, err.Error())
return return
} }
if tc.expectCreateErr != "" { if tc.expectCreateErr {
tCtx.Fatalf("did not get expected error: %v", tc.expectCreateErr) tCtx.Fatalf("did not get expected error")
} }
for nodeName, expect := range tc.expectNodeResults { for nodeName, expect := range tc.expectNodeResults {