Organize framework unit tests in subtests

This commit is contained in:
drfish 2021-01-30 12:29:26 +08:00
parent 24f13032b3
commit 0c3d8b9720
8 changed files with 194 additions and 146 deletions

View File

@ -23,6 +23,7 @@ import (
func TestStatus(t *testing.T) { func TestStatus(t *testing.T) {
tests := []struct { tests := []struct {
name string
status *Status status *Status
expectedCode Code expectedCode Code
expectedMessage string expectedMessage string
@ -30,6 +31,7 @@ func TestStatus(t *testing.T) {
expectedAsError error expectedAsError error
}{ }{
{ {
name: "success status",
status: NewStatus(Success, ""), status: NewStatus(Success, ""),
expectedCode: Success, expectedCode: Success,
expectedMessage: "", expectedMessage: "",
@ -37,6 +39,7 @@ func TestStatus(t *testing.T) {
expectedAsError: nil, expectedAsError: nil,
}, },
{ {
name: "error status",
status: NewStatus(Error, "unknown error"), status: NewStatus(Error, "unknown error"),
expectedCode: Error, expectedCode: Error,
expectedMessage: "unknown error", expectedMessage: "unknown error",
@ -44,6 +47,7 @@ func TestStatus(t *testing.T) {
expectedAsError: errors.New("unknown error"), expectedAsError: errors.New("unknown error"),
}, },
{ {
name: "nil status",
status: nil, status: nil,
expectedCode: Success, expectedCode: Success,
expectedMessage: "", expectedMessage: "",
@ -52,26 +56,28 @@ func TestStatus(t *testing.T) {
}, },
} }
for i, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.status.Code() != test.expectedCode { if test.status.Code() != test.expectedCode {
t.Errorf("test #%v, expect status.Code() returns %v, but %v", i, test.expectedCode, test.status.Code()) t.Errorf("expect status.Code() returns %v, but %v", test.expectedCode, test.status.Code())
} }
if test.status.Message() != test.expectedMessage { if test.status.Message() != test.expectedMessage {
t.Errorf("test #%v, expect status.Message() returns %v, but %v", i, test.expectedMessage, test.status.Message()) t.Errorf("expect status.Message() returns %v, but %v", test.expectedMessage, test.status.Message())
} }
if test.status.IsSuccess() != test.expectedIsSuccess { if test.status.IsSuccess() != test.expectedIsSuccess {
t.Errorf("test #%v, expect status.IsSuccess() returns %v, but %v", i, test.expectedIsSuccess, test.status.IsSuccess()) t.Errorf("expect status.IsSuccess() returns %v, but %v", test.expectedIsSuccess, test.status.IsSuccess())
} }
if test.status.AsError() == test.expectedAsError { if test.status.AsError() == test.expectedAsError {
continue return
} }
if test.status.AsError().Error() != test.expectedAsError.Error() { if test.status.AsError().Error() != test.expectedAsError.Error() {
t.Errorf("test #%v, expect status.AsError() returns %v, but %v", i, test.expectedAsError, test.status.AsError()) t.Errorf("expect status.AsError() returns %v, but %v", test.expectedAsError, test.status.AsError())
} }
})
} }
} }
@ -93,30 +99,37 @@ func assertStatusCode(t *testing.T, code Code, value int) {
func TestPluginToStatusMerge(t *testing.T) { func TestPluginToStatusMerge(t *testing.T) {
tests := []struct { tests := []struct {
name string
statusMap PluginToStatus statusMap PluginToStatus
wantCode Code wantCode Code
}{ }{
{ {
name: "merge Error and Unschedulable statuses",
statusMap: PluginToStatus{"p1": NewStatus(Error), "p2": NewStatus(Unschedulable)}, statusMap: PluginToStatus{"p1": NewStatus(Error), "p2": NewStatus(Unschedulable)},
wantCode: Error, wantCode: Error,
}, },
{ {
name: "merge Success and Unschedulable statuses",
statusMap: PluginToStatus{"p1": NewStatus(Success), "p2": NewStatus(Unschedulable)}, statusMap: PluginToStatus{"p1": NewStatus(Success), "p2": NewStatus(Unschedulable)},
wantCode: Unschedulable, wantCode: Unschedulable,
}, },
{ {
name: "merge Success, UnschedulableAndUnresolvable and Unschedulable statuses",
statusMap: PluginToStatus{"p1": NewStatus(Success), "p2": NewStatus(UnschedulableAndUnresolvable), "p3": NewStatus(Unschedulable)}, statusMap: PluginToStatus{"p1": NewStatus(Success), "p2": NewStatus(UnschedulableAndUnresolvable), "p3": NewStatus(Unschedulable)},
wantCode: UnschedulableAndUnresolvable, wantCode: UnschedulableAndUnresolvable,
}, },
{ {
name: "merge nil status",
wantCode: Success, wantCode: Success,
}, },
} }
for i, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
gotStatus := test.statusMap.Merge() gotStatus := test.statusMap.Merge()
if test.wantCode != gotStatus.Code() { if test.wantCode != gotStatus.Code() {
t.Errorf("test #%v, wantCode %v, gotCode %v", i, test.wantCode, gotStatus.Code()) t.Errorf("wantCode %v, gotCode %v", test.wantCode, gotStatus.Code())
} }
})
} }
} }

View File

@ -1295,6 +1295,7 @@ func TestPodEligibleToPreemptOthers(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var nodes []*v1.Node var nodes []*v1.Node
for _, n := range test.nodes { for _, n := range test.nodes {
nodes = append(nodes, st.MakeNode().Name(n).Obj()) nodes = append(nodes, st.MakeNode().Name(n).Obj())
@ -1303,6 +1304,7 @@ func TestPodEligibleToPreemptOthers(t *testing.T) {
if got := PodEligibleToPreemptOthers(test.pod, snapshot.NodeInfos(), test.nominatedNodeStatus); got != test.expected { if got := PodEligibleToPreemptOthers(test.pod, snapshot.NodeInfos(), test.nominatedNodeStatus); got != test.expected {
t.Errorf("expected %t, got %t for pod: %s", test.expected, got, test.pod.Name) t.Errorf("expected %t, got %t for pod: %s", test.expected, got, test.pod.Name)
} }
})
} }
} }

View File

@ -17,6 +17,7 @@ limitations under the License.
package helper package helper
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@ -63,6 +64,7 @@ func TestDefaultNormalizeScore(t *testing.T) {
} }
for i, test := range tests { for i, test := range tests {
t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) {
scores := framework.NodeScoreList{} scores := framework.NodeScoreList{}
for _, score := range test.scores { for _, score := range test.scores {
scores = append(scores, framework.NodeScore{Score: score}) scores = append(scores, framework.NodeScore{Score: score})
@ -75,7 +77,8 @@ func TestDefaultNormalizeScore(t *testing.T) {
DefaultNormalizeScore(framework.MaxNodeScore, test.reverse, scores) DefaultNormalizeScore(framework.MaxNodeScore, test.reverse, scores)
if !reflect.DeepEqual(scores, expectedScores) { if !reflect.DeepEqual(scores, expectedScores) {
t.Errorf("test %d, expected %v, got %v", i, expectedScores, scores) t.Errorf("expected %v, got %v", expectedScores, scores)
} }
})
} }
} }

View File

@ -281,10 +281,12 @@ func TestGetContainerPorts(t *testing.T) {
}, },
} }
for _, test := range tests { for i, test := range tests {
t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) {
result := getContainerPorts(test.pod1, test.pod2) result := getContainerPorts(test.pod1, test.pod2)
if !reflect.DeepEqual(test.expected, result) { if !reflect.DeepEqual(test.expected, result) {
t.Errorf("Got different result than expected.\nDifference detected on:\n%s", diff.ObjectGoPrintSideBySide(test.expected, result)) t.Errorf("Got different result than expected.\nDifference detected on:\n%s", diff.ObjectGoPrintSideBySide(test.expected, result))
} }
})
} }
} }

View File

@ -635,8 +635,10 @@ func TestValidateFitArgs(t *testing.T) {
} }
for _, test := range argsTest { for _, test := range argsTest {
t.Run(test.name, func(t *testing.T) {
if err := validateFitArgs(test.args); err != nil && !strings.Contains(err.Error(), test.expect) { if err := validateFitArgs(test.args); err != nil && !strings.Contains(err.Error(), test.expect) {
t.Errorf("case[%v]: error details do not include %v", test.name, err) t.Errorf("case[%v]: error details do not include %v", test.name, err)
} }
})
} }
} }

View File

@ -18,6 +18,7 @@ package noderesources
import ( import (
"context" "context"
"fmt"
"reflect" "reflect"
"testing" "testing"
@ -173,11 +174,13 @@ func TestBrokenLinearFunction(t *testing.T) {
}, },
} }
for _, test := range tests { for i, test := range tests {
t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) {
function := buildBrokenLinearFunction(test.points) function := buildBrokenLinearFunction(test.points)
for _, assertion := range test.assertions { for _, assertion := range test.assertions {
assert.InDelta(t, assertion.expected, function(assertion.p), 0.1, "points=%v, p=%f", test.points, assertion.p) assert.InDelta(t, assertion.expected, function(assertion.p), 0.1, "points=%v, p=%f", test.points, assertion.p)
} }
})
} }
} }

View File

@ -1716,6 +1716,7 @@ func TestPermitPlugins(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
registry := Registry{} registry := Registry{}
configPlugins := &config.Plugins{Permit: &config.PluginSet{}} configPlugins := &config.Plugins{Permit: &config.PluginSet{}}
@ -1743,6 +1744,7 @@ func TestPermitPlugins(t *testing.T) {
if !reflect.DeepEqual(status, tt.want) { if !reflect.DeepEqual(status, tt.want) {
t.Errorf("wrong status code. got %v, want %v", status, tt.want) t.Errorf("wrong status code. got %v, want %v", status, tt.want)
} }
})
} }
} }

View File

@ -30,14 +30,17 @@ import (
func TestNewResource(t *testing.T) { func TestNewResource(t *testing.T) {
tests := []struct { tests := []struct {
name string
resourceList v1.ResourceList resourceList v1.ResourceList
expected *Resource expected *Resource
}{ }{
{ {
name: "empty resource",
resourceList: map[v1.ResourceName]resource.Quantity{}, resourceList: map[v1.ResourceName]resource.Quantity{},
expected: &Resource{}, expected: &Resource{},
}, },
{ {
name: "complex resource",
resourceList: map[v1.ResourceName]resource.Quantity{ resourceList: map[v1.ResourceName]resource.Quantity{
v1.ResourceCPU: *resource.NewScaledQuantity(4, -3), v1.ResourceCPU: *resource.NewScaledQuantity(4, -3),
v1.ResourceMemory: *resource.NewQuantity(2000, resource.BinarySI), v1.ResourceMemory: *resource.NewQuantity(2000, resource.BinarySI),
@ -57,10 +60,12 @@ func TestNewResource(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
r := NewResource(test.resourceList) r := NewResource(test.resourceList)
if !reflect.DeepEqual(test.expected, r) { if !reflect.DeepEqual(test.expected, r) {
t.Errorf("expected: %#v, got: %#v", test.expected, r) t.Errorf("expected: %#v, got: %#v", test.expected, r)
} }
})
} }
} }
@ -102,11 +107,13 @@ func TestResourceList(t *testing.T) {
}, },
} }
for _, test := range tests { for i, test := range tests {
t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) {
rl := test.resource.ResourceList() rl := test.resource.ResourceList()
if !reflect.DeepEqual(test.expected, rl) { if !reflect.DeepEqual(test.expected, rl) {
t.Errorf("expected: %#v, got: %#v", test.expected, rl) t.Errorf("expected: %#v, got: %#v", test.expected, rl)
} }
})
} }
} }
@ -137,13 +144,15 @@ func TestResourceClone(t *testing.T) {
}, },
} }
for _, test := range tests { for i, test := range tests {
t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) {
r := test.resource.Clone() r := test.resource.Clone()
// Modify the field to check if the result is a clone of the origin one. // Modify the field to check if the result is a clone of the origin one.
test.resource.MilliCPU += 1000 test.resource.MilliCPU += 1000
if !reflect.DeepEqual(test.expected, r) { if !reflect.DeepEqual(test.expected, r) {
t.Errorf("expected: %#v, got: %#v", test.expected, r) t.Errorf("expected: %#v, got: %#v", test.expected, r)
} }
})
} }
} }
@ -183,10 +192,12 @@ func TestResourceAddScalar(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
t.Run(string(test.scalarName), func(t *testing.T) {
test.resource.AddScalar(test.scalarName, test.scalarQuantity) test.resource.AddScalar(test.scalarName, test.scalarQuantity)
if !reflect.DeepEqual(test.expected, test.resource) { if !reflect.DeepEqual(test.expected, test.resource) {
t.Errorf("expected: %#v, got: %#v", test.expected, test.resource) t.Errorf("expected: %#v, got: %#v", test.expected, test.resource)
} }
})
} }
} }
@ -232,11 +243,13 @@ func TestSetMaxResource(t *testing.T) {
}, },
} }
for _, test := range tests { for i, test := range tests {
t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) {
test.resource.SetMaxResource(test.resourceList) test.resource.SetMaxResource(test.resourceList)
if !reflect.DeepEqual(test.expected, test.resource) { if !reflect.DeepEqual(test.expected, test.resource) {
t.Errorf("expected: %#v, got: %#v", test.expected, test.resource) t.Errorf("expected: %#v, got: %#v", test.expected, test.resource)
} }
})
} }
} }
@ -540,7 +553,8 @@ func TestNodeInfoClone(t *testing.T) {
}, },
} }
for _, test := range tests { for i, test := range tests {
t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) {
ni := test.nodeInfo.Clone() ni := test.nodeInfo.Clone()
// Modify the field to check if the result is a clone of the origin one. // Modify the field to check if the result is a clone of the origin one.
test.nodeInfo.Generation += 10 test.nodeInfo.Generation += 10
@ -548,6 +562,7 @@ func TestNodeInfoClone(t *testing.T) {
if !reflect.DeepEqual(test.expected, ni) { if !reflect.DeepEqual(test.expected, ni) {
t.Errorf("expected: %#v, got: %#v", test.expected, ni) t.Errorf("expected: %#v, got: %#v", test.expected, ni)
} }
})
} }
} }
@ -1036,7 +1051,8 @@ func TestNodeInfoRemovePod(t *testing.T) {
}, },
} }
for _, test := range tests { for i, test := range tests {
t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) {
ni := fakeNodeInfo(pods...) ni := fakeNodeInfo(pods...)
gen := ni.Generation gen := ni.Generation
@ -1060,6 +1076,7 @@ func TestNodeInfoRemovePod(t *testing.T) {
if !reflect.DeepEqual(test.expectedNodeInfo, ni) { if !reflect.DeepEqual(test.expectedNodeInfo, ni) {
t.Errorf("expected: %#v, got: %#v", test.expectedNodeInfo, ni) t.Errorf("expected: %#v, got: %#v", test.expectedNodeInfo, ni)
} }
})
} }
} }
@ -1169,6 +1186,7 @@ func TestHostPortInfo_AddRemove(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
hp := make(HostPortInfo) hp := make(HostPortInfo)
for _, param := range test.added { for _, param := range test.added {
hp.Add(param.ip, param.protocol, param.port) hp.Add(param.ip, param.protocol, param.port)
@ -1180,6 +1198,7 @@ func TestHostPortInfo_AddRemove(t *testing.T) {
t.Errorf("%v failed: expect length %d; got %d", test.desc, test.length, hp.Len()) t.Errorf("%v failed: expect length %d; got %d", test.desc, test.length, hp.Len())
t.Error(hp) t.Error(hp)
} }
})
} }
} }
@ -1273,12 +1292,14 @@ func TestHostPortInfo_Check(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
hp := make(HostPortInfo) hp := make(HostPortInfo)
for _, param := range test.added { for _, param := range test.added {
hp.Add(param.ip, param.protocol, param.port) hp.Add(param.ip, param.protocol, param.port)
} }
if hp.CheckConflict(test.check.ip, test.check.protocol, test.check.port) != test.expect { if hp.CheckConflict(test.check.ip, test.check.protocol, test.check.port) != test.expect {
t.Errorf("%v failed, expected %t; got %t", test.desc, test.expect, !test.expect) t.Errorf("expected %t; got %t", test.expect, !test.expect)
} }
})
} }
} }