Skip to content

Commit 6be16d5

Browse files
authored
765 test seperation (#875)
have different structs for different tests (report, diff, explain, lint); have a dedicated package for tests functionality
1 parent 58498fc commit 6be16d5

28 files changed

+1399
-1022
lines changed

pkg/awsvpc/analysis_output_test.go

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,61 +10,73 @@ import (
1010
"fmt"
1111
"testing"
1212

13-
"github.com/np-guard/vpc-network-config-analyzer/pkg/commonvpc"
13+
"github.com/np-guard/vpc-network-config-analyzer/pkg/commonvpc/testfunc"
1414
"github.com/np-guard/vpc-network-config-analyzer/pkg/vpcmodel"
1515
)
1616

1717
const analysisOut = "analysis_out"
1818

19-
var tests = []*commonvpc.VpcGeneralTest{
19+
var tests = []*testfunc.VpcAnalysisTest{
2020
{
21-
InputConfig: "basic_config_with_sg",
22-
UseCases: []vpcmodel.OutputUseCase{vpcmodel.AllEndpoints, vpcmodel.AllSubnets},
23-
Format: vpcmodel.Text,
21+
VpcTestCommon: testfunc.VpcTestCommon{
22+
InputConfig: "basic_config_with_sg",
23+
UseCases: []vpcmodel.OutputUseCase{vpcmodel.AllEndpoints, vpcmodel.AllSubnets},
24+
Format: vpcmodel.Text,
25+
},
2426
},
2527
{
26-
InputConfig: "aws_sg_1",
27-
UseCases: []vpcmodel.OutputUseCase{vpcmodel.AllEndpoints, vpcmodel.AllSubnets},
28-
Format: vpcmodel.Text,
28+
VpcTestCommon: testfunc.VpcTestCommon{
29+
InputConfig: "aws_sg_1",
30+
UseCases: []vpcmodel.OutputUseCase{vpcmodel.AllEndpoints, vpcmodel.AllSubnets},
31+
Format: vpcmodel.Text,
32+
},
2933
},
3034
{
31-
InputConfig: "aws_sg_1",
32-
UseCases: []vpcmodel.OutputUseCase{vpcmodel.AllEndpoints},
33-
Format: vpcmodel.HTML,
35+
VpcTestCommon: testfunc.VpcTestCommon{
36+
InputConfig: "aws_sg_1",
37+
UseCases: []vpcmodel.OutputUseCase{vpcmodel.AllEndpoints},
38+
Format: vpcmodel.HTML,
39+
},
3440
},
3541
{
36-
InputConfig: "aws_acl_1",
37-
UseCases: []vpcmodel.OutputUseCase{vpcmodel.AllEndpoints, vpcmodel.AllSubnets},
38-
Format: vpcmodel.Text,
42+
VpcTestCommon: testfunc.VpcTestCommon{
43+
InputConfig: "aws_acl_1",
44+
UseCases: []vpcmodel.OutputUseCase{vpcmodel.AllEndpoints, vpcmodel.AllSubnets},
45+
Format: vpcmodel.Text,
46+
},
3947
},
4048
{
41-
InputConfig: "aws_mixed",
42-
UseCases: []vpcmodel.OutputUseCase{vpcmodel.AllEndpoints, vpcmodel.AllSubnets},
43-
Format: vpcmodel.Text,
49+
VpcTestCommon: testfunc.VpcTestCommon{
50+
InputConfig: "aws_mixed",
51+
UseCases: []vpcmodel.OutputUseCase{vpcmodel.AllEndpoints, vpcmodel.AllSubnets},
52+
Format: vpcmodel.Text,
53+
},
4454
},
4555
{
46-
InputConfig: "aws_mixed",
47-
UseCases: []vpcmodel.OutputUseCase{vpcmodel.AllEndpoints},
48-
Format: vpcmodel.HTML,
56+
VpcTestCommon: testfunc.VpcTestCommon{
57+
InputConfig: "aws_mixed",
58+
UseCases: []vpcmodel.OutputUseCase{vpcmodel.AllEndpoints},
59+
Format: vpcmodel.HTML,
60+
},
4961
},
5062
}
5163

5264
// uncomment the function below to run for updating the expected output
53-
/*
54-
func TestAllWithGeneration(t *testing.T) {
65+
66+
/*func TestReportWithGeneration(t *testing.T) {
5567
// tests is the list of tests to run
5668
for testIdx := range tests {
5769
tt := tests[testIdx]
58-
commonvpc.TestAll(tt, t, commonvpc.OutputGeneration, &AWSresourcesContainer{}, analysisOut, tt.InputConfig)
70+
tt.TestAnalysisSingleTest(t, testfunc.OutputGeneration, &AWSresourcesContainer{}, analysisOut, tt.InputConfig)
5971
}
6072
fmt.Println("done")
61-
}
62-
*/
63-
func TestAllWithComparison(t *testing.T) {
73+
}*/
74+
75+
func TestReportWithComparison(t *testing.T) {
6476
// tests is the list of tests to run
6577
for testIdx := range tests {
6678
tt := tests[testIdx]
67-
commonvpc.TestAll(tt, t, commonvpc.OutputComparison, &AWSresourcesContainer{}, analysisOut, tt.InputConfig)
79+
tt.TestAnalysisSingleTest(t, testfunc.OutputComparison, &AWSresourcesContainer{}, analysisOut, tt.InputConfig)
6880
}
6981
fmt.Println("done")
7082
}

pkg/awsvpc/explainability_test.go

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,111 +10,109 @@ import (
1010
"fmt"
1111
"testing"
1212

13-
"github.com/np-guard/vpc-network-config-analyzer/pkg/commonvpc"
14-
"github.com/np-guard/vpc-network-config-analyzer/pkg/vpcmodel"
13+
"github.com/np-guard/vpc-network-config-analyzer/pkg/commonvpc/testfunc"
1514
)
1615

17-
var explainTests = []*commonvpc.VpcGeneralTest{
16+
var explainTests = []*testfunc.VpcExplainTest{
1817
// existing connection between two endpoints of different subnets
1918
{
20-
Name: "ip_to_ip",
21-
InputConfig: "aws_sg_1",
19+
VpcTestCommon: testfunc.VpcTestCommon{
20+
Name: "ip_to_ip",
21+
InputConfig: "aws_sg_1",
22+
},
2223
ESrc: "10.240.40.217",
2324
EDst: "10.240.20.43",
24-
Format: vpcmodel.Text,
2525
DetailExplain: true,
2626
},
2727
// non-existing connection between two endpoints of different subnets due to one of the nacls
2828
{
29-
Name: "nacl_blocking",
30-
InputConfig: "aws_mixed",
29+
VpcTestCommon: testfunc.VpcTestCommon{
30+
Name: "nacl_blocking",
31+
InputConfig: "aws_mixed",
32+
},
3133
ESrc: "10.240.2.28",
3234
EDst: "10.240.32.122",
33-
Format: vpcmodel.Text,
3435
DetailExplain: true,
3536
},
3637
// existing sub-connection between two endpoints of the same subnet
3738
// todo: https://github.com/np-guard/vpc-network-config-analyzer/issues/859
3839
{
39-
Name: "same_subnet_partial_connection",
40-
InputConfig: "aws_mixed",
40+
VpcTestCommon: testfunc.VpcTestCommon{
41+
Name: "same_subnet_partial_connection",
42+
InputConfig: "aws_mixed",
43+
},
4144
ESrc: "10.240.32.122",
4245
EDst: "10.240.32.91",
43-
Format: vpcmodel.Text,
4446
DetailExplain: true,
4547
},
4648
// no connection between two endpoints of the same subnet
4749
{
48-
Name: "subnet_to_subnet",
49-
InputConfig: "aws_mixed",
50+
VpcTestCommon: testfunc.VpcTestCommon{
51+
Name: "subnet_to_subnet",
52+
InputConfig: "aws_mixed",
53+
},
5054
ESrc: "private2",
5155
EDst: "private1",
52-
Format: vpcmodel.Text,
5356
DetailExplain: true,
5457
},
5558
// connection to the public internet blocked by sg and private subnet
5659
{
57-
Name: "to_external_private_subnet",
58-
InputConfig: "aws_sg_1",
60+
VpcTestCommon: testfunc.VpcTestCommon{
61+
Name: "to_external_private_subnet",
62+
InputConfig: "aws_sg_1",
63+
},
5964
ESrc: "10.240.20.245",
6065
EDst: "161.26.0.0",
61-
Format: vpcmodel.Text,
6266
DetailExplain: true,
6367
},
6468
// existing connection to the public internet
6569
{
66-
Name: "to_external_public_subnet",
67-
InputConfig: "aws_sg_1",
70+
VpcTestCommon: testfunc.VpcTestCommon{
71+
Name: "to_external_public_subnet",
72+
InputConfig: "aws_sg_1",
73+
},
6874
ESrc: "10.240.10.42",
6975
EDst: "161.26.0.0",
70-
Format: vpcmodel.Text,
7176
DetailExplain: true,
7277
},
7378
// existing connection from the public internet
7479
{
75-
Name: "from_external_public_subnet",
76-
InputConfig: "aws_mixed",
80+
VpcTestCommon: testfunc.VpcTestCommon{
81+
Name: "from_external_public_subnet",
82+
InputConfig: "aws_mixed",
83+
},
7784
ESrc: "147.235.0.0/16",
7885
EDst: "10.240.0.96",
79-
Format: vpcmodel.Text,
8086
DetailExplain: true,
8187
},
8288
// connection to the public internet blocked only by private subnet
8389
{
84-
Name: "to_external_blocked_only_private_subnet",
85-
InputConfig: "aws_sg_1_allow_sg_private",
90+
VpcTestCommon: testfunc.VpcTestCommon{
91+
Name: "to_external_blocked_only_private_subnet",
92+
InputConfig: "aws_sg_1_allow_sg_private",
93+
},
8694
ESrc: "10.240.20.245",
8795
EDst: "161.26.0.0",
88-
Format: vpcmodel.Text,
8996
DetailExplain: true,
9097
},
9198
}
9299

93-
func TestAll(t *testing.T) {
100+
func TestExplainWithComparsion(t *testing.T) {
94101
// explainTests is the list of tests to run
95102
for testIdx := range explainTests {
96103
tt := explainTests[testIdx]
97-
t.Run(tt.Name, func(t *testing.T) {
98-
t.Parallel()
99-
rc := &AWSresourcesContainer{}
100-
commonvpc.RunExplainTest(tt, t, rc)
101-
})
104+
tt.TestSingleExplain(t, testfunc.OutputComparison, &AWSresourcesContainer{}, tt.Name)
102105
}
103106
fmt.Println("done")
104107
}
105108

106109
// uncomment the function below for generating the expected output files instead of comparing
107-
/*
108-
func TestAllWithGeneration(t *testing.T) {
110+
111+
/*func TestExplainWithGeneration(t *testing.T) {
109112
// tests is the list of tests to run
110113
for testIdx := range explainTests {
111114
tt := explainTests[testIdx]
112-
tt.Mode = commonvpc.OutputGeneration
113-
t.Run(tt.Name, func(t *testing.T) {
114-
t.Parallel()
115-
rc := &AWSresourcesContainer{}
116-
commonvpc.RunExplainTest(tt, t,rc)
117-
})
115+
tt.TestSingleExplain(t, commonvpc.OutputGeneration, &AWSresourcesContainer{}, tt.Name)
118116
}
119117
fmt.Println("done")
120118
}*/

pkg/awsvpc/lint_test.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,45 @@ import (
1010
"fmt"
1111
"testing"
1212

13-
"github.com/np-guard/vpc-network-config-analyzer/pkg/commonvpc"
13+
"github.com/np-guard/vpc-network-config-analyzer/pkg/commonvpc/testfunc"
1414
)
1515

16-
var lintTests = []*commonvpc.VpcGeneralTest{
16+
var lintTests = []*testfunc.VpcLintTest{
1717
{
18-
Name: "aws_acl1",
19-
InputConfig: "aws_acl_1",
20-
Enable: []string{"sg-split-subnet"},
18+
VpcTestCommon: testfunc.VpcTestCommon{
19+
Name: "aws_acl1",
20+
InputConfig: "aws_acl_1",
21+
},
22+
Enable: []string{"sg-split-subnet"},
2123
},
2224
{
23-
Name: "aws_mixed",
24-
InputConfig: "aws_mixed",
25-
Enable: []string{"sg-split-subnet"},
25+
VpcTestCommon: testfunc.VpcTestCommon{
26+
Name: "aws_mixed",
27+
InputConfig: "aws_mixed",
28+
},
29+
Enable: []string{"sg-split-subnet"},
2630
},
2731
{
28-
Name: "aws_sg_1",
29-
InputConfig: "aws_sg_1",
30-
Enable: []string{"sg-split-subnet"},
32+
VpcTestCommon: testfunc.VpcTestCommon{
33+
Name: "aws_sg_1",
34+
InputConfig: "aws_sg_1",
35+
},
36+
Enable: []string{"sg-split-subnet"},
3137
Disable: []string{"nacl-split-subnet", "subnet-cidr-overlap", "nacl-unattached",
3238
"sg-unattached", "sg-rule-cidr-out-of-range", "nacl-rule-cidr-out-of-range",
3339
"tcp-response-blocked", "sg-rule-implied", "nacl-rule-shadowed"},
3440
},
3541
}
3642

37-
func TestAllLint(t *testing.T) {
43+
func TestLintWithComparsion(t *testing.T) {
3844
// lintTests is the list of tests to run
3945
for testIdx := range lintTests {
4046
tt := lintTests[testIdx]
41-
tt.Mode = commonvpc.OutputComparison
47+
tt.Mode = testfunc.OutputComparison
4248
t.Run(tt.Name, func(t *testing.T) {
4349
t.Parallel()
4450
rc := &AWSresourcesContainer{}
45-
commonvpc.RunLintTest(tt, t, rc)
51+
tt.TestSingleLint(t, rc)
4652
})
4753
}
4854
fmt.Println("done")
@@ -54,11 +60,11 @@ func TestAllLint(t *testing.T) {
5460
// tests is the list of tests to run
5561
for testIdx := range lintTests {
5662
tt := lintTests[testIdx]
57-
tt.Mode = commonvpc.OutputGeneration
63+
tt.Mode = testfunc.OutputGeneration
5864
t.Run(tt.Name, func(t *testing.T) {
5965
t.Parallel()
6066
rc := &AWSresourcesContainer{}
61-
commonvpc.RunLintTest(tt, t, rc)
67+
tt.TestSingleLint(t, rc)
6268
})
6369
}
6470
fmt.Println("done")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2023- IBM Inc. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package testfunc
8+
9+
import (
10+
_ "embed"
11+
"testing"
12+
13+
"github.com/np-guard/vpc-network-config-analyzer/pkg/commonvpc"
14+
)
15+
16+
type VpcAnalysisTest struct {
17+
VpcTestCommon
18+
Grouping bool
19+
NoLbAbstract bool
20+
}
21+
22+
func (tt *VpcAnalysisTest) TestAnalysisSingleTest(t *testing.T, mode testMode, rc commonvpc.ResourcesContainer, testDir, testName string) {
23+
tt.Name = testName
24+
tt.setMode(mode)
25+
t.Run(tt.Name, func(t *testing.T) {
26+
t.Parallel()
27+
tt.runSingleCommonTest(t, testDir, rc, tt.Grouping, tt.NoLbAbstract, nil)
28+
})
29+
}

0 commit comments

Comments
 (0)