Skip to content

Commit

Permalink
Use Equal instead of ContainedIn and avoid duplicated lines in final …
Browse files Browse the repository at this point in the history
…presentation
  • Loading branch information
ShiriMoran committed Nov 12, 2023
1 parent 025228f commit b4fbe0b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pkg/ibmvpc/examples/acl_testing5subnetsDiff.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Analysis for diff between VPC test-vpc-ky1 and VPC test-vpc-ky2
diff-type: added source: sub1-2-ky destination: sub1-1-ky dir1: protocol: TCP dir2: No connection
diff-type: added source: sub1-3-ky destination: sub1-1-ky dir1: protocol: TCP dir2: No connection

diff-type: changed source: sub2-1-ky destination: Public Internet [8.8.8.8/32] dir1: protocol: UDP dst-ports: 53 dir2: protocol: UDP dst-ports: 43,53

diff-type: removed source: sub2-1-ky destination: Public Internet [8.8.8.0/29] dir1: No connection dir2: protocol: UDP dst-ports: 53 *
diff-type: removed source: sub2-1-ky destination: Public Internet [8.8.8.10/31] dir1: No connection dir2: protocol: UDP dst-ports: 53 *
diff-type: removed source: sub2-1-ky destination: Public Internet [8.8.8.12/30] dir1: No connection dir2: protocol: UDP dst-ports: 53 *
Expand Down
14 changes: 7 additions & 7 deletions pkg/vpcmodel/diffSubnets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ func configSimpleSubnetSubtract() (subnetConfigConn1, subnetConfigConn2 *SubnetC

func TestSimpleSubnetSubtract(t *testing.T) {
subnetConfigConn1, subnetConfigConn2 := configSimpleSubnetSubtract()
subnet1Subtract2, err := subnetConfigConn1.subtract(subnetConfigConn2)
subnet1Subtract2, err := subnetConfigConn1.subtract(subnetConfigConn2, true)
if err != nil {
fmt.Println("error:", err.Error())
}
subnet1Subtract2Str := subnet1Subtract2.EnhancedString(true)
fmt.Printf("subnet1Subtract2:\n%v\n", subnet1Subtract2Str)
require.Equal(t, err, nil)
newLines := strings.Count(subnet1Subtract2Str, "\n")
require.Equal(t, 4, newLines)
require.Equal(t, 5, newLines)
require.Contains(t, subnet1Subtract2Str, "diff-type: added source: subnet0 destination: subnet1 "+
"dir1: All Connections dir2: No connection, workloads-diff-info: workloads subnet0 and subnet1 added")
require.Contains(t, subnet1Subtract2Str, "diff-type: added source: subnet1 destination: subnet2 "+
Expand All @@ -97,17 +97,17 @@ func TestSimpleSubnetSubtract(t *testing.T) {
"dir1: All Connections dir2: No connection")
require.Contains(t, subnet1Subtract2Str, "diff-type: added source: subnet3 destination: subnet1 "+
"dir1: All Connections dir2: No connection, workloads-diff-info: workload subnet1 added")
require.Contains(t, subnet1Subtract2Str, "diff-type: changed source: subnet3 destination: subnet4 dir1: "+
"protocol: TCP src-ports: 10-100 dst-ports: 443 dir2: All Connections\n")

cfg2Subtract1, err := subnetConfigConn2.subtract(subnetConfigConn1)
cfg2Subtract1, err := subnetConfigConn2.subtract(subnetConfigConn1, false)
if err != nil {
fmt.Println("error:", err.Error())
}
require.Equal(t, err, nil)
subnet2Subtract1Str := cfg2Subtract1.EnhancedString(false)
fmt.Printf("cfg2Subtract1:\n%v", subnet2Subtract1Str)
require.Contains(t, subnet2Subtract1Str, "diff-type: changed source: subnet3 destination: subnet4 dir1: "+
"protocol: TCP src-ports: 10-100 dst-ports: 443 dir2: All Connections\n")
require.Contains(t, subnet2Subtract1Str, "diff-type: removed source: subnet4 destination: subnet5 dir1: "+
require.Equal(t, subnet2Subtract1Str, "diff-type: removed source: subnet4 destination: subnet5 dir1: "+
"No connection dir2: All Connections, workloads-diff-info: workload subnet5 removed\n")
}

Expand Down Expand Up @@ -165,7 +165,7 @@ func TestSimpleIPAndSubnetSubtract(t *testing.T) {
}

// verified bit by bit :-)
cfg1SubCfg2, err := alignedCfgConn1.subtract(alignedCfgConn2)
cfg1SubCfg2, err := alignedCfgConn1.subtract(alignedCfgConn2, true)
if err != nil {
fmt.Println("error:", err.Error())
}
Expand Down
15 changes: 9 additions & 6 deletions pkg/vpcmodel/semanticDiffSubnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ func (configs ConfigsForDiff) GetSubnetsDiff(grouping bool) (*DiffBetweenSubnets
if err != nil {
return nil, err
}
subnet1Subtract2, err1 := alignedConfigConnectivity1.subtract(alignedConfigConnectivity2)
subnet1Subtract2, err1 := alignedConfigConnectivity1.subtract(alignedConfigConnectivity2, true)
if err1 != nil {
return nil, err1
}
subnet2Subtract1, err2 := alignedConfigConnectivity2.subtract(alignedConfigConnectivity1)
subnet2Subtract1, err2 := alignedConfigConnectivity2.subtract(alignedConfigConnectivity1, false)
if err2 != nil {
return nil, err2
}
Expand Down Expand Up @@ -105,9 +105,12 @@ func (c *VPCConfig) getVPCResourceInfInOtherConfig(other *VPCConfig, ep VPCResou
return nil, nil
}

// subtract Subtract one SubnetConnectivityMap from the other
// subtract Subtract one SubnetConnectivityMap from the other:
// connections may be identical, non-existing in other or existing in other but changed;
// the latter are included only if includeChanged, to avoid duplication in the final presentation
//
// assumption: any connection from connectivity and "other" have src (dst) which are either disjoint or equal
func (subnetConfConnectivity *SubnetConfigConnectivity) subtract(other *SubnetConfigConnectivity) (
func (subnetConfConnectivity *SubnetConfigConnectivity) subtract(other *SubnetConfigConnectivity, includeChanged bool) (
connectivitySubtract SubnetsDiff, err error) {
connectivitySubtract = map[VPCResourceIntf]map[VPCResourceIntf]*connectionDiff{}
for src, endpointConns := range subnetConfConnectivity.subnetConnectivity {
Expand All @@ -131,8 +134,8 @@ func (subnetConfConnectivity *SubnetConfigConnectivity) subtract(other *SubnetCo
if otherSrc, ok := other.subnetConnectivity[srcInOther]; ok {
if otherConn, ok := otherSrc[dstInOther]; ok {
// ToDo: https://github.com/np-guard/vpc-network-config-analyzer/issues/199
if conns.Equals(otherConn) {
continue // no diff
if !includeChanged || conns.Equal(otherConn) {
continue
}
connDiff.conn2 = otherConn
connDiff.diff = ChangedConnection
Expand Down

0 comments on commit b4fbe0b

Please sign in to comment.