Skip to content

Commit 3517411

Browse files
committed
unit-tests improvements
1 parent 3254942 commit 3517411

File tree

2 files changed

+65
-40
lines changed

2 files changed

+65
-40
lines changed

pkg/ibmvpc/connectivityAnalysis_test.go

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -156,52 +156,74 @@ var nc6 = &naclConfig{
156156

157157
// tests below
158158

159-
var expectedConnStrTest1 = `vsi-0-subnet-1[10.240.10.4] => vsi-0-subnet-2[10.240.20.4] : All Connections
160-
vsi-0-subnet-2[10.240.20.4] => vsi-0-subnet-1[10.240.10.4] : All Connections
161-
`
162-
163-
func TestAnalyzeConnectivity1(t *testing.T) {
164-
runConnectivityTest(t, tc1, []*naclConfig{nc1}, expectedConnStrTest1)
165-
}
166-
167-
var expectedConnStrTest2 = `vsi-0-subnet-1[10.240.10.4] => vsi-0-subnet-2[10.240.20.4] : TCP * ; ICMP,UDP
168-
`
169-
170-
func TestAnalyzeConnectivity2(t *testing.T) {
171-
runConnectivityTest(t, tc1, []*naclConfig{nc2, nc3}, expectedConnStrTest2)
159+
type ConnectivityAnalysisTest struct {
160+
name string
161+
tc *testNodesConfig
162+
ncList []*naclConfig
163+
expectedStrResult string
172164
}
173165

174-
var expectedConnStrTest2a = `vsi-0-subnet-1[10.240.10.4] => vsi-0-subnet-2[10.240.20.4] : ICMP
175-
` // ICMP is actually enabled only unidirectional in this case, but responsive analysis does not apply to ICMP
176-
177-
func TestAnalyzeConnectivity2a(t *testing.T) {
178-
runConnectivityTest(t, tc1, []*naclConfig{nc2a, nc3a}, expectedConnStrTest2a)
179-
}
180-
181-
var expectedConnStrTest3 = `vsi-0-subnet-1[10.240.10.4] => vsi-0-subnet-2[10.240.20.4] : All Connections
166+
var connectivityAnalysisTests = []*ConnectivityAnalysisTest{
167+
{
168+
name: "testAnalyzeConnectivity1",
169+
tc: tc1,
170+
ncList: []*naclConfig{nc1},
171+
expectedStrResult: `vsi-0-subnet-1[10.240.10.4] => vsi-0-subnet-2[10.240.20.4] : All Connections
172+
vsi-0-subnet-2[10.240.20.4] => vsi-0-subnet-1[10.240.10.4] : All Connections
173+
`,
174+
},
175+
{
176+
name: "testAnalyzeConnectivity2",
177+
tc: tc1,
178+
ncList: []*naclConfig{nc2, nc3},
179+
expectedStrResult: `vsi-0-subnet-1[10.240.10.4] => vsi-0-subnet-2[10.240.20.4] : TCP * ; ICMP,UDP
180+
`,
181+
},
182+
{
183+
name: "testAnalyzeConnectivity2a",
184+
tc: tc1,
185+
ncList: []*naclConfig{nc2a, nc3a},
186+
expectedStrResult: `vsi-0-subnet-1[10.240.10.4] => vsi-0-subnet-2[10.240.20.4] : ICMP
187+
`, // ICMP is actually enabled only unidirectional in this case, but responsive analysis does not apply to ICMP
188+
},
189+
{
190+
name: "testAnalyzeConnectivity3",
191+
tc: tc1,
192+
ncList: []*naclConfig{nc2, nc4},
193+
expectedStrResult: `vsi-0-subnet-1[10.240.10.4] => vsi-0-subnet-2[10.240.20.4] : All Connections
182194
vsi-0-subnet-2[10.240.20.4] => vsi-0-subnet-1[10.240.10.4] : TCP
183-
`
184-
185-
func TestAnalyzeConnectivity3(t *testing.T) {
186-
runConnectivityTest(t, tc1, []*naclConfig{nc2, nc4}, expectedConnStrTest3)
187-
}
188-
189-
var expectedConnStrTest4 = `vsi-0-subnet-1[10.240.10.4] => vsi-0-subnet-2[10.240.20.4] : TCP src-ports: 10-100 dst-ports: 443
195+
`,
196+
},
197+
{
198+
name: "testAnalyzeConnectivity4",
199+
tc: tc1,
200+
ncList: []*naclConfig{nc5, nc6},
201+
expectedStrResult: `vsi-0-subnet-1[10.240.10.4] => vsi-0-subnet-2[10.240.20.4] : TCP src-ports: 10-100 dst-ports: 443
190202
vsi-0-subnet-2[10.240.20.4] => vsi-0-subnet-1[10.240.10.4] : TCP src-ports: 443 dst-ports: 10-100
191-
`
203+
`,
204+
},
205+
}
192206

193-
func TestAnalyzeConnectivity4(t *testing.T) {
194-
runConnectivityTest(t, tc1, []*naclConfig{nc5, nc6}, expectedConnStrTest4)
207+
func TestConnectivityAnalysis(t *testing.T) {
208+
// connectivityAnalysisTests is the list of tests to run
209+
for testIdx := range connectivityAnalysisTests {
210+
tt := connectivityAnalysisTests[testIdx]
211+
t.Run(tt.name, func(t *testing.T) {
212+
t.Parallel()
213+
tt.runConnectivityTest(t)
214+
})
215+
}
216+
fmt.Println("done")
195217
}
196218

197-
func runConnectivityTest(t *testing.T, tc *testNodesConfig, ncList []*naclConfig, expectedStrResult string) {
198-
c := createConfigFromTestConfig(tc, ncList)
219+
func (tt *ConnectivityAnalysisTest) runConnectivityTest(t *testing.T) {
220+
c := createConfigFromTestConfig(tt.tc, tt.ncList)
199221
connectivity, err := c.GetVPCNetworkConnectivity(false, vpcmodel.NoGroupingNoConsistencyEdges)
200222
require.Nil(t, err)
201223
connectivityStr := connectivity.String()
202224
fmt.Println(connectivityStr)
203225
fmt.Println("done")
204-
require.Equal(t, expectedStrResult, connectivityStr)
226+
require.Equal(t, tt.expectedStrResult, connectivityStr)
205227
}
206228

207229
////////////////////////////////////////////////////////////////////////////////////////////////

pkg/ibmvpc/naclAnalysis_test.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func testSingleNACL(nacl *commonvpc.NACL) {
5050
}
5151
}
5252

53-
func TestGetAllowedXgressConnections(t *testing.T) {
53+
func TestNaclAnalysis(t *testing.T) {
5454
subnet := newIPBlockFromCIDROrAddressWithoutValidation("10.0.0.0/24")
5555

5656
tests := []struct {
@@ -60,7 +60,7 @@ func TestGetAllowedXgressConnections(t *testing.T) {
6060
}{
6161
{
6262

63-
testName: "a",
63+
testName: "naclAnalysis1",
6464
naclRules: []*commonvpc.NACLRule{
6565
{
6666
Src: newIPBlockFromCIDROrAddressWithoutValidation("1.2.3.4/32"),
@@ -148,7 +148,7 @@ func TestGetAllowedXgressConnections(t *testing.T) {
148148
},
149149
},
150150
{
151-
testName: "b",
151+
testName: "naclAnalysis2",
152152
naclRules: []*commonvpc.NACLRule{
153153
{
154154
Src: newIPBlockFromCIDROrAddressWithoutValidation("1.2.3.4/32"),
@@ -250,7 +250,7 @@ func TestGetAllowedXgressConnections(t *testing.T) {
250250
},
251251
},
252252
{
253-
testName: "c",
253+
testName: "naclAnalysis3",
254254
naclRules: []*commonvpc.NACLRule{
255255
{
256256
Dst: newIPBlockFromCIDROrAddressWithoutValidation("1.2.3.4/32"),
@@ -294,8 +294,11 @@ func TestGetAllowedXgressConnections(t *testing.T) {
294294
}
295295

296296
for _, test := range tests {
297-
connectivityMap := commonvpc.AnalyzeNACLRulesPerDisjointTargets(test.naclRules, subnet, true)
298-
require.True(t, equalConnectivityMap(connectivityMap, test.expectedConnectivityMap))
297+
t.Run(test.testName, func(t *testing.T) {
298+
t.Parallel()
299+
connectivityMap := commonvpc.AnalyzeNACLRulesPerDisjointTargets(test.naclRules, subnet, true)
300+
require.True(t, equalConnectivityMap(connectivityMap, test.expectedConnectivityMap))
301+
})
299302
}
300303
fmt.Printf("done\n")
301304
}

0 commit comments

Comments
 (0)