github.com/grafana/synthetic-monitoring-agent@v0.64.2/internal/prober/icmp/icmp_test.go (about) 1 package icmp 2 3 import ( 4 "context" 5 "os" 6 "testing" 7 "time" 8 9 "github.com/go-kit/log" 10 "github.com/grafana/synthetic-monitoring-agent/internal/model" 11 "github.com/grafana/synthetic-monitoring-agent/internal/prober/logger" 12 sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" 13 "github.com/prometheus/blackbox_exporter/config" 14 bbeprober "github.com/prometheus/blackbox_exporter/prober" 15 "github.com/prometheus/client_golang/prometheus" 16 "github.com/stretchr/testify/require" 17 ) 18 19 func TestName(t *testing.T) { 20 name := Prober.Name(Prober{}) 21 require.Equal(t, name, "ping") 22 } 23 24 func TestNewProber(t *testing.T) { 25 testcases := map[string]struct { 26 input model.Check 27 expected Prober 28 ExpectError bool 29 }{ 30 "default": { 31 input: model.Check{ 32 Check: sm.Check{ 33 Target: "www.grafana.com", 34 Settings: sm.CheckSettings{ 35 Ping: &sm.PingSettings{}, 36 }, 37 }, 38 }, 39 expected: Prober{ 40 config: Module{ 41 Prober: "ping", 42 Timeout: 0, 43 PacketCount: 3, 44 ReqSuccessCount: 1, 45 MaxResolveRetries: 3, 46 Privileged: isPrivilegedRequired(), 47 ICMP: config.ICMPProbe{ 48 IPProtocol: "ip6", 49 IPProtocolFallback: true, 50 }, 51 }, 52 }, 53 ExpectError: false, 54 }, 55 "1 packet": { 56 input: model.Check{ 57 Check: sm.Check{ 58 Target: "www.grafana.com", 59 Settings: sm.CheckSettings{ 60 Ping: &sm.PingSettings{ 61 PacketCount: 1, 62 }, 63 }, 64 }, 65 }, 66 expected: Prober{ 67 config: Module{ 68 Prober: "ping", 69 Timeout: 0, 70 PacketCount: 1, 71 ReqSuccessCount: 1, 72 MaxResolveRetries: 3, 73 Privileged: isPrivilegedRequired(), 74 ICMP: config.ICMPProbe{ 75 IPProtocol: "ip6", 76 IPProtocolFallback: true, 77 }, 78 }, 79 }, 80 ExpectError: false, 81 }, 82 "2 packets": { 83 input: model.Check{ 84 Check: sm.Check{ 85 Target: "www.grafana.com", 86 Settings: sm.CheckSettings{ 87 Ping: &sm.PingSettings{ 88 PacketCount: 2, 89 }, 90 }, 91 }, 92 }, 93 expected: Prober{ 94 config: Module{ 95 Prober: "ping", 96 Timeout: 0, 97 PacketCount: 2, 98 ReqSuccessCount: 2, 99 MaxResolveRetries: 3, 100 Privileged: isPrivilegedRequired(), 101 ICMP: config.ICMPProbe{ 102 IPProtocol: "ip6", 103 IPProtocolFallback: true, 104 }, 105 }, 106 }, 107 ExpectError: false, 108 }, 109 "no-settings": { 110 input: model.Check{ 111 Check: sm.Check{ 112 Target: "www.grafana.com", 113 Settings: sm.CheckSettings{ 114 Ping: nil, 115 }, 116 }, 117 }, 118 expected: Prober{}, 119 ExpectError: true, 120 }, 121 } 122 123 for name, testcase := range testcases { 124 t.Run(name, func(t *testing.T) { 125 actual, err := NewProber(testcase.input) 126 require.Equal(t, &testcase.expected, &actual) 127 128 if testcase.ExpectError { 129 require.Error(t, err, "unsupported check") 130 } else { 131 require.NoError(t, err) 132 } 133 }) 134 } 135 } 136 137 func TestSettingsToModule(t *testing.T) { 138 testcases := map[string]struct { 139 input sm.PingSettings 140 expected Module 141 }{ 142 "default": { 143 input: sm.PingSettings{}, 144 expected: Module{ 145 Prober: "ping", 146 Timeout: 0, 147 PacketCount: 3, 148 ReqSuccessCount: 1, 149 MaxResolveRetries: 3, 150 ICMP: config.ICMPProbe{ 151 IPProtocol: "ip6", 152 IPProtocolFallback: true, 153 }, 154 }, 155 }, 156 "partial-settings": { 157 input: sm.PingSettings{ 158 IpVersion: 1, 159 SourceIpAddress: "0.0.0.0", 160 }, 161 expected: Module{ 162 Prober: "ping", 163 Timeout: 0, 164 PacketCount: 3, 165 ReqSuccessCount: 1, 166 MaxResolveRetries: 3, 167 ICMP: config.ICMPProbe{ 168 IPProtocol: "ip4", 169 IPProtocolFallback: false, 170 SourceIPAddress: "0.0.0.0", 171 }, 172 }, 173 }, 174 "count 1": { 175 input: sm.PingSettings{ 176 IpVersion: 1, 177 PacketCount: 1, 178 }, 179 expected: Module{ 180 Prober: "ping", 181 Timeout: 0, 182 PacketCount: 1, 183 ReqSuccessCount: 1, 184 MaxResolveRetries: 3, 185 ICMP: config.ICMPProbe{ 186 IPProtocol: "ip4", 187 IPProtocolFallback: false, 188 }, 189 }, 190 }, 191 "count 2": { 192 input: sm.PingSettings{ 193 IpVersion: 1, 194 PacketCount: 2, 195 }, 196 expected: Module{ 197 Prober: "ping", 198 Timeout: 0, 199 PacketCount: 2, 200 ReqSuccessCount: 2, 201 MaxResolveRetries: 3, 202 ICMP: config.ICMPProbe{ 203 IPProtocol: "ip4", 204 IPProtocolFallback: false, 205 }, 206 }, 207 }, 208 } 209 210 for name, testcase := range testcases { 211 t.Run(name, func(t *testing.T) { 212 actual := settingsToModule(&testcase.input) 213 require.Equal(t, &testcase.expected, &actual) 214 }) 215 } 216 } 217 218 func TestProber(t *testing.T) { 219 deadline, ok := t.Deadline() 220 if !ok { 221 deadline = time.Now().Add(1 * time.Second) 222 } 223 224 ctx, cancel := context.WithDeadline(context.Background(), deadline) 225 defer cancel() 226 227 prober, err := NewProber(model.Check{ 228 Check: sm.Check{ 229 Target: "127.0.0.1", 230 Timeout: 1000, 231 Settings: sm.CheckSettings{ 232 Ping: &sm.PingSettings{ 233 IpVersion: sm.IpVersion_V4, 234 PacketCount: 1, 235 }, 236 }, 237 }, 238 }) 239 240 require.NoError(t, err) 241 require.NotNil(t, prober) 242 243 registry := prometheus.NewRegistry() 244 require.NotNil(t, registry) 245 246 logger := log.NewLogfmtLogger(os.Stdout) 247 require.NotNil(t, logger) 248 249 success, duration := prober.Probe(ctx, "127.0.0.1", registry, logger, "test-execution-id") 250 require.True(t, success) 251 require.Greater(t, duration, float64(0)) 252 } 253 254 func TestBBEProber(t *testing.T) { 255 deadline, ok := t.Deadline() 256 if !ok { 257 deadline = time.Now().Add(1 * time.Second) 258 } 259 260 ctx, cancel := context.WithDeadline(context.Background(), deadline) 261 defer cancel() 262 263 target := "127.0.0.1" 264 265 registry := prometheus.NewRegistry() 266 require.NotNil(t, registry) 267 268 l := log.NewLogfmtLogger(os.Stdout) 269 require.NotNil(t, l) 270 271 module := config.Module{ 272 Prober: "test", 273 Timeout: 100 * time.Millisecond, 274 ICMP: config.ICMPProbe{ 275 IPProtocol: "ip4", 276 IPProtocolFallback: false, 277 }, 278 } 279 slogger := logger.ToSlog(l) 280 require.NotNil(t, slogger) 281 282 success := bbeprober.ProbeICMP(ctx, target, module, registry, slogger) 283 require.True(t, success) 284 }