@@ -891,14 +891,18 @@ func Test_npCollectorImpl_getReverseDNSResult(t *testing.T) {
891
891
}
892
892
893
893
var subnetSkippedStat = teststatsd.MetricsArgs {Name : netpathConnsSkippedMetricName , Value : 1 , Tags : []string {"reason:skip_intra_vpc" }, Rate : 1 }
894
+ var cidrExcludedStat = teststatsd.MetricsArgs {Name : netpathConnsSkippedMetricName , Value : 1 , Tags : []string {"reason:skip_cidr_excluded" }, Rate : 1 }
894
895
895
896
func Test_npCollectorImpl_shouldScheduleNetworkPathForConn (t * testing.T ) {
896
897
tests := []struct {
897
- name string
898
- conn * model.Connection
899
- vpcSubnets []* net.IPNet
900
- shouldSchedule bool
901
- subnetSkipped bool
898
+ name string
899
+ conn * model.Connection
900
+ vpcSubnets []* net.IPNet
901
+ shouldSchedule bool
902
+ subnetSkipped bool
903
+ sourceExcludes map [string ][]string
904
+ destExcludes map [string ][]string
905
+ connectionExcluded bool
902
906
}{
903
907
{
904
908
name : "should schedule" ,
@@ -946,6 +950,7 @@ func Test_npCollectorImpl_shouldScheduleNetworkPathForConn(t *testing.T) {
946
950
Raddr : & model.Addr {Ip : "127.0.0.2" , Port : int32 (80 )},
947
951
Direction : model .ConnectionDirection_outgoing ,
948
952
Family : model .ConnectionFamily_v4 ,
953
+ IntraHost : true , // loopback is always IntraHost
949
954
},
950
955
shouldSchedule : false ,
951
956
},
@@ -1025,13 +1030,110 @@ func Test_npCollectorImpl_shouldScheduleNetworkPathForConn(t *testing.T) {
1025
1030
shouldSchedule : false ,
1026
1031
subnetSkipped : true ,
1027
1032
},
1033
+ // connection exclusion tests
1034
+ {
1035
+ name : "exclusion: block dest exactly" ,
1036
+ conn : & model.Connection {
1037
+ Laddr : & model.Addr {Ip : "10.0.0.1" , Port : int32 (30000 )},
1038
+ Raddr : & model.Addr {Ip : "10.0.0.2" , Port : int32 (80 )},
1039
+ Direction : model .ConnectionDirection_outgoing ,
1040
+ },
1041
+ destExcludes : map [string ][]string {
1042
+ "10.0.0.2" : {"80" },
1043
+ },
1044
+ shouldSchedule : false ,
1045
+ connectionExcluded : true ,
1046
+ },
1047
+ {
1048
+ name : "exclusion: block dest but different port" ,
1049
+ conn : & model.Connection {
1050
+ Laddr : & model.Addr {Ip : "10.0.0.1" , Port : int32 (30000 )},
1051
+ Raddr : & model.Addr {Ip : "10.0.0.2" , Port : int32 (80 )},
1052
+ Direction : model .ConnectionDirection_outgoing ,
1053
+ },
1054
+ destExcludes : map [string ][]string {
1055
+ "10.0.0.2" : {"42" },
1056
+ },
1057
+ shouldSchedule : true ,
1058
+ connectionExcluded : false ,
1059
+ },
1060
+ {
1061
+ name : "exclusion: block source with port range" ,
1062
+ conn : & model.Connection {
1063
+ Laddr : & model.Addr {Ip : "10.0.0.1" , Port : int32 (30000 )},
1064
+ Raddr : & model.Addr {Ip : "10.0.0.2" , Port : int32 (80 )},
1065
+ Direction : model .ConnectionDirection_outgoing ,
1066
+ },
1067
+ sourceExcludes : map [string ][]string {
1068
+ "10.0.0.1" : {"30000-30005" },
1069
+ },
1070
+ shouldSchedule : false ,
1071
+ connectionExcluded : true ,
1072
+ },
1073
+ {
1074
+ name : "exclusion: block dest subnet" ,
1075
+ conn : & model.Connection {
1076
+ Laddr : & model.Addr {Ip : "10.0.0.1" , Port : int32 (30000 )},
1077
+ Raddr : & model.Addr {Ip : "10.0.0.2" , Port : int32 (80 )},
1078
+ Direction : model .ConnectionDirection_outgoing ,
1079
+ },
1080
+ destExcludes : map [string ][]string {
1081
+ "10.0.0.0/8" : {"*" },
1082
+ },
1083
+ shouldSchedule : false ,
1084
+ connectionExcluded : true ,
1085
+ },
1086
+ {
1087
+ name : "exclusion: block dest subnet, no match" ,
1088
+ conn : & model.Connection {
1089
+ Laddr : & model.Addr {Ip : "10.0.0.1" , Port : int32 (30000 )},
1090
+ Raddr : & model.Addr {Ip : "192.168.1.1" , Port : int32 (80 )},
1091
+ Direction : model .ConnectionDirection_outgoing ,
1092
+ },
1093
+ destExcludes : map [string ][]string {
1094
+ "10.0.0.0/8" : {"*" },
1095
+ },
1096
+ shouldSchedule : true ,
1097
+ connectionExcluded : false ,
1098
+ },
1099
+ {
1100
+ name : "exclusion: only UDP, matching case" ,
1101
+ conn : & model.Connection {
1102
+ Type : model .ConnectionType_udp ,
1103
+ Laddr : & model.Addr {Ip : "10.0.0.1" , Port : int32 (30000 )},
1104
+ Raddr : & model.Addr {Ip : "10.0.0.2" , Port : int32 (123 )},
1105
+ Direction : model .ConnectionDirection_outgoing ,
1106
+ },
1107
+ sourceExcludes : map [string ][]string {
1108
+ "10.0.0.0/8" : {"udp *" },
1109
+ },
1110
+ shouldSchedule : false ,
1111
+ connectionExcluded : true ,
1112
+ },
1113
+ {
1114
+ name : "exclusion: only UDP, non-matching case" ,
1115
+ conn : & model.Connection {
1116
+ // (tcp is 0 so this doesn't actually do anything)
1117
+ Type : model .ConnectionType_tcp ,
1118
+ Laddr : & model.Addr {Ip : "10.0.0.1" , Port : int32 (30000 )},
1119
+ Raddr : & model.Addr {Ip : "10.0.0.2" , Port : int32 (123 )},
1120
+ Direction : model .ConnectionDirection_outgoing ,
1121
+ },
1122
+ sourceExcludes : map [string ][]string {
1123
+ "10.0.0.0/8" : {"udp *" },
1124
+ },
1125
+ shouldSchedule : true ,
1126
+ connectionExcluded : false ,
1127
+ },
1028
1128
}
1029
1129
1030
1130
for _ , tt := range tests {
1031
1131
t .Run (tt .name , func (t * testing.T ) {
1032
1132
agentConfigs := map [string ]any {
1033
1133
"network_path.connections_monitoring.enabled" : true ,
1034
1134
"network_path.collector.disable_intra_vpc_collection" : true ,
1135
+ "network_path.collector.source_excludes" : tt .sourceExcludes ,
1136
+ "network_path.collector.dest_excludes" : tt .destExcludes ,
1035
1137
}
1036
1138
stats := & teststatsd.Client {}
1037
1139
_ , npCollector := newTestNpCollector (t , agentConfigs , stats )
@@ -1043,6 +1145,11 @@ func Test_npCollectorImpl_shouldScheduleNetworkPathForConn(t *testing.T) {
1043
1145
} else {
1044
1146
require .NotContains (t , stats .CountCalls , subnetSkippedStat )
1045
1147
}
1148
+ if tt .connectionExcluded {
1149
+ require .Contains (t , stats .CountCalls , cidrExcludedStat )
1150
+ } else {
1151
+ require .NotContains (t , stats .CountCalls , cidrExcludedStat )
1152
+ }
1046
1153
})
1047
1154
}
1048
1155
}
0 commit comments