cleanup: Reduce cyclomatic complexity of topology_test

Reduce redundant code to make the test simpler.

Signed-off-by: Yug <yuggupta27@gmail.com>
This commit is contained in:
Yug 2020-07-06 11:01:52 +05:30 committed by mergify[bot]
parent f05c8ed2a7
commit 34593c9af0

View File

@ -23,7 +23,18 @@ import (
"github.com/container-storage-interface/spec/lib/go/csi" "github.com/container-storage-interface/spec/lib/go/csi"
) )
// nolint: gocyclo func checkError(t *testing.T, msg string, err error) {
if err == nil {
t.Errorf(msg)
}
}
func checkAndReportError(t *testing.T, msg string, err error) {
if err != nil {
t.Errorf("%s (%v)", msg, err)
}
}
// TestFindPoolAndTopology also tests MatchTopologyForPool // TestFindPoolAndTopology also tests MatchTopologyForPool
func TestFindPoolAndTopology(t *testing.T) { func TestFindPoolAndTopology(t *testing.T) {
var err error var err error
@ -225,9 +236,7 @@ func TestFindPoolAndTopology(t *testing.T) {
} }
// Test nil values // Test nil values
_, _, _, err = FindPoolAndTopology(nil, nil) _, _, _, err = FindPoolAndTopology(nil, nil)
if err != nil { checkAndReportError(t, "expected success due to nil in-args", err)
t.Errorf("expected success due to nil in-args (%v)", err)
}
poolName, _, _, err := FindPoolAndTopology(&validMultipleTopoPools, nil) poolName, _, _, err := FindPoolAndTopology(&validMultipleTopoPools, nil)
if err != nil || poolName != "" { if err != nil || poolName != "" {
@ -241,87 +250,59 @@ func TestFindPoolAndTopology(t *testing.T) {
// Test valid accessibility requirement, with invalid topology pools values // Test valid accessibility requirement, with invalid topology pools values
_, _, _, err = FindPoolAndTopology(&emptyTopoPools, &validAccReq) _, _, _, err = FindPoolAndTopology(&emptyTopoPools, &validAccReq)
if err == nil { checkError(t, "expected failure due to empty topology pools", err)
t.Errorf("expected failure due to empty topology pools")
}
_, _, _, err = FindPoolAndTopology(&emptyPoolNameTopoPools, &validAccReq) _, _, _, err = FindPoolAndTopology(&emptyPoolNameTopoPools, &validAccReq)
if err == nil { checkError(t, "expected failure due to missing pool name in topology pools", err)
t.Errorf("expected failure due to missing pool name in topology pools")
}
_, _, _, err = FindPoolAndTopology(&differentDomainsInTopoPools, &validAccReq) _, _, _, err = FindPoolAndTopology(&differentDomainsInTopoPools, &validAccReq)
if err == nil { checkError(t, "expected failure due to mismatching domains in topology pools", err)
t.Errorf("expected failure due to mismatching domains in topology pools")
}
// Test valid topology pools, with invalid accessibility requirements // Test valid topology pools, with invalid accessibility requirements
_, _, _, err = FindPoolAndTopology(&validMultipleTopoPools, &emptyAccReq) _, _, _, err = FindPoolAndTopology(&validMultipleTopoPools, &emptyAccReq)
if err == nil { checkError(t, "expected failure due to empty accessibility requirements", err)
t.Errorf("expected failure due to empty accessibility requirements")
}
_, _, _, err = FindPoolAndTopology(&validSingletonTopoPools, &emptySegmentAccReq) _, _, _, err = FindPoolAndTopology(&validSingletonTopoPools, &emptySegmentAccReq)
if err == nil { checkError(t, "expected failure due to empty segments in accessibility requirements", err)
t.Errorf("expected failure due to empty segments in accessibility requirements")
}
_, _, _, err = FindPoolAndTopology(&validMultipleTopoPools, &partialHigherSegmentAccReq) _, _, _, err = FindPoolAndTopology(&validMultipleTopoPools, &partialHigherSegmentAccReq)
if err == nil { checkError(t, "expected failure due to partial segments in accessibility requirements", err)
t.Errorf("expected failure due to partial segments in accessibility requirements")
}
_, _, _, err = FindPoolAndTopology(&validSingletonTopoPools, &partialLowerSegmentAccReq) _, _, _, err = FindPoolAndTopology(&validSingletonTopoPools, &partialLowerSegmentAccReq)
if err == nil { checkError(t, "expected failure due to partial segments in accessibility requirements", err)
t.Errorf("expected failure due to partial segments in accessibility requirements")
}
_, _, _, err = FindPoolAndTopology(&validMultipleTopoPools, &partialLowerSegmentAccReq) _, _, _, err = FindPoolAndTopology(&validMultipleTopoPools, &partialLowerSegmentAccReq)
if err == nil { checkError(t, "expected failure due to partial segments in accessibility requirements", err)
t.Errorf("expected failure due to partial segments in accessibility requirements")
}
_, _, _, err = FindPoolAndTopology(&validMultipleTopoPools, &differentSegmentAccReq) _, _, _, err = FindPoolAndTopology(&validMultipleTopoPools, &differentSegmentAccReq)
if err == nil { checkError(t, "expected failure due to mismatching segments in accessibility requirements", err)
t.Errorf("expected failure due to mismatching segments in accessibility requirements")
}
// Test success cases // Test success cases
// If a pool is a superset of domains (either empty domain labels or partial), it can be selected // If a pool is a superset of domains (either empty domain labels or partial), it can be selected
poolName, _, topoSegment, err := FindPoolAndTopology(&emptyDomainsInTopoPools, &validAccReq) poolName, _, topoSegment, err := FindPoolAndTopology(&emptyDomainsInTopoPools, &validAccReq)
err = checkOutput(err, poolName, topoSegment) err = checkOutput(err, poolName, topoSegment)
if err != nil { checkAndReportError(t, "expected success got:", err)
t.Errorf("expected success got: (%v)", err)
}
poolName, _, topoSegment, err = FindPoolAndTopology(&partialDomainsInTopoPools, &validAccReq) poolName, _, topoSegment, err = FindPoolAndTopology(&partialDomainsInTopoPools, &validAccReq)
err = checkOutput(err, poolName, topoSegment) err = checkOutput(err, poolName, topoSegment)
if err != nil { checkAndReportError(t, "expected success got:", err)
t.Errorf("expected success got: (%v)", err)
}
// match in a singleton topology pools // match in a singleton topology pools
poolName, _, topoSegment, err = FindPoolAndTopology(&validSingletonTopoPools, &validAccReq) poolName, _, topoSegment, err = FindPoolAndTopology(&validSingletonTopoPools, &validAccReq)
err = checkOutput(err, poolName, topoSegment) err = checkOutput(err, poolName, topoSegment)
if err != nil { checkAndReportError(t, "expected success got:", err)
t.Errorf("expected success got: (%v)", err)
}
// match first in multiple topology pools // match first in multiple topology pools
poolName, _, topoSegment, err = FindPoolAndTopology(&validMultipleTopoPools, &validAccReq) poolName, _, topoSegment, err = FindPoolAndTopology(&validMultipleTopoPools, &validAccReq)
err = checkOutput(err, poolName, topoSegment) err = checkOutput(err, poolName, topoSegment)
if err != nil { checkAndReportError(t, "expected success got:", err)
t.Errorf("expected success got: (%v)", err)
}
// match non-first in multiple topology pools // match non-first in multiple topology pools
switchPoolOrder := []TopologyConstrainedPool{} switchPoolOrder := []TopologyConstrainedPool{}
switchPoolOrder = append(switchPoolOrder, validMultipleTopoPools[1], validMultipleTopoPools[0]) switchPoolOrder = append(switchPoolOrder, validMultipleTopoPools[1], validMultipleTopoPools[0])
poolName, _, topoSegment, err = FindPoolAndTopology(&switchPoolOrder, &validAccReq) poolName, _, topoSegment, err = FindPoolAndTopology(&switchPoolOrder, &validAccReq)
err = checkOutput(err, poolName, topoSegment) err = checkOutput(err, poolName, topoSegment)
if err != nil { checkAndReportError(t, "expected success got:", err)
t.Errorf("expected success got: (%v)", err)
}
// test valid dataPool return // test valid dataPool return
for i := range switchPoolOrder { for i := range switchPoolOrder {
@ -329,9 +310,7 @@ func TestFindPoolAndTopology(t *testing.T) {
} }
poolName, dataPoolName, topoSegment, err := FindPoolAndTopology(&switchPoolOrder, &validAccReq) poolName, dataPoolName, topoSegment, err := FindPoolAndTopology(&switchPoolOrder, &validAccReq)
err = checkOutput(err, poolName, topoSegment) err = checkOutput(err, poolName, topoSegment)
if err != nil { checkAndReportError(t, "expected success got:", err)
t.Errorf("expected success got: (%v)", err)
}
if dataPoolName != "ec-"+poolName { if dataPoolName != "ec-"+poolName {
t.Errorf("expected data pool to be named ec-%s, got %s", poolName, dataPoolName) t.Errorf("expected data pool to be named ec-%s, got %s", poolName, dataPoolName)
} }
@ -346,9 +325,7 @@ func TestFindPoolAndTopology(t *testing.T) {
// check for existing pool // check for existing pool
topoSegment, err = MatchTopologyForPool(&validMultipleTopoPools, &validAccReq, pool1) topoSegment, err = MatchTopologyForPool(&validMultipleTopoPools, &validAccReq, pool1)
err = checkOutput(err, pool1, topoSegment) err = checkOutput(err, pool1, topoSegment)
if err != nil { checkAndReportError(t, "expected success got:", err)
t.Errorf("expected success got: (%v)", err)
}
} }
/* /*