| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package pinger |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/netdata/netdata/go/plugins/logger" |
| 10 | "github.com/netdata/netdata/go/plugins/pkg/confopt" |
| 11 | "github.com/stretchr/testify/assert" |
| 12 | "github.com/stretchr/testify/require" |
| 13 | ) |
| 14 | |
| 15 | func TestNewClient_DefaultsAnalysisConfig(t *testing.T) { |
| 16 | c, err := newClient(Config{ |
| 17 | Probe: ProbeConfig{ |
| 18 | Packets: 1, |
| 19 | Interval: confopt.Duration(time.Millisecond), |
| 20 | Timeout: time.Second, |
| 21 | }, |
| 22 | }, logger.NewWithWriter(nil), &fakeRunner{}) |
| 23 | require.NoError(t, err) |
| 24 | |
| 25 | assert.Equal(t, defaultJitterEWMASamples, c.cfg.Analysis.JitterEWMASamples) |
| 26 | assert.Equal(t, defaultJitterSMAWindow, c.cfg.Analysis.JitterSMAWindow) |
| 27 | } |
| 28 | |
| 29 | func TestNewClient_ValidatesConfig(t *testing.T) { |
| 30 | tests := map[string]Config{ |
| 31 | "packets": { |
| 32 | Probe: ProbeConfig{ |
| 33 | Packets: 0, |
| 34 | Interval: confopt.Duration(time.Millisecond), |
| 35 | Timeout: time.Second, |
| 36 | }, |
| 37 | }, |
| 38 | "interval": { |
| 39 | Probe: ProbeConfig{ |
| 40 | Packets: 1, |
| 41 | Timeout: time.Second, |
| 42 | }, |
| 43 | }, |
| 44 | "timeout": { |
| 45 | Probe: ProbeConfig{ |
| 46 | Packets: 1, |
| 47 | Interval: confopt.Duration(time.Millisecond), |
| 48 | }, |
| 49 | }, |
| 50 | } |
| 51 | |
| 52 | for name, cfg := range tests { |
| 53 | t.Run(name, func(t *testing.T) { |
| 54 | _, err := newClient(cfg, logger.NewWithWriter(nil), &fakeRunner{}) |
| 55 | assert.Error(t, err) |
| 56 | }) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestNewClient_RequiresRunner(t *testing.T) { |
| 61 | _, err := newClient(Config{ |
| 62 | Probe: ProbeConfig{ |
| 63 | Packets: 1, |
| 64 | Interval: confopt.Duration(time.Millisecond), |
| 65 | Timeout: time.Second, |
| 66 | }, |
| 67 | }, logger.NewWithWriter(nil), nil) |
| 68 | require.Error(t, err) |
| 69 | } |