summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/ping/ping_test.go
blob: 856449d334e93a1558f823cd44f186849b4b9eb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// SPDX-License-Identifier: GPL-3.0-or-later

package ping

import (
	"errors"
	"os"
	"testing"
	"time"

	"github.com/netdata/netdata/go/go.d.plugin/agent/module"
	"github.com/netdata/netdata/go/go.d.plugin/logger"

	probing "github.com/prometheus-community/pro-bing"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

var (
	dataConfigJSON, _ = os.ReadFile("testdata/config.json")
	dataConfigYAML, _ = os.ReadFile("testdata/config.yaml")
)

func Test_testDataIsValid(t *testing.T) {
	for name, data := range map[string][]byte{
		"dataConfigJSON": dataConfigJSON,
		"dataConfigYAML": dataConfigYAML,
	} {
		require.NotNil(t, data, name)
	}
}

func TestPing_ConfigurationSerialize(t *testing.T) {
	module.TestConfigurationSerialize(t, &Ping{}, dataConfigJSON, dataConfigYAML)
}

func TestPing_Init(t *testing.T) {
	tests := map[string]struct {
		wantFail bool
		config   Config
	}{
		"fail with default": {
			wantFail: true,
			config:   New().Config,
		},
		"success when 'hosts' set": {
			wantFail: false,
			config: Config{
				SendPackets: 1,
				Hosts:       []string{"192.0.2.0"},
			},
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			ping := New()
			ping.Config = test.config
			ping.UpdateEvery = 1

			if test.wantFail {
				assert.Error(t, ping.Init())
			} else {
				assert.NoError(t, ping.Init())
			}
		})
	}
}

func TestPing_Charts(t *testing.T) {
	assert.NotNil(t, New().Charts())
}

func TestPing_Cleanup(t *testing.T) {
	assert.NotPanics(t, New().Cleanup)
}

func TestPing_Check(t *testing.T) {
	tests := map[string]struct {
		wantFail bool
		prepare  func(t *testing.T) *Ping
	}{
		"success when ping does not return an error": {
			wantFail: false,
			prepare:  casePingSuccess,
		},
		"fail when ping returns an error": {
			wantFail: true,
			prepare:  casePingError,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			ping := test.prepare(t)

			if test.wantFail {
				assert.Error(t, ping.Check())
			} else {
				assert.NoError(t, ping.Check())
			}
		})
	}
}

func TestPing_Collect(t *testing.T) {
	tests := map[string]struct {
		prepare       func(t *testing.T) *Ping
		wantMetrics   map[string]int64
		wantNumCharts int
	}{
		"success when ping does not return an error": {
			prepare: casePingSuccess,
			wantMetrics: map[string]int64{
				"host_192.0.2.1_avg_rtt":        15000,
				"host_192.0.2.1_max_rtt":        20000,
				"host_192.0.2.1_min_rtt":        10000,
				"host_192.0.2.1_packet_loss":    0,
				"host_192.0.2.1_packets_recv":   5,
				"host_192.0.2.1_packets_sent":   5,
				"host_192.0.2.1_std_dev_rtt":    5000,
				"host_192.0.2.2_avg_rtt":        15000,
				"host_192.0.2.2_max_rtt":        20000,
				"host_192.0.2.2_min_rtt":        10000,
				"host_192.0.2.2_packet_loss":    0,
				"host_192.0.2.2_packets_recv":   5,
				"host_192.0.2.2_packets_sent":   5,
				"host_192.0.2.2_std_dev_rtt":    5000,
				"host_example.com_avg_rtt":      15000,
				"host_example.com_max_rtt":      20000,
				"host_example.com_min_rtt":      10000,
				"host_example.com_packet_loss":  0,
				"host_example.com_packets_recv": 5,
				"host_example.com_packets_sent": 5,
				"host_example.com_std_dev_rtt":  5000,
			},
			wantNumCharts: 3 * len(hostChartsTmpl),
		},
		"fail when ping returns an error": {
			prepare:       casePingError,
			wantMetrics:   nil,
			wantNumCharts: 0,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			ping := test.prepare(t)

			mx := ping.Collect()

			require.Equal(t, test.wantMetrics, mx)

			if len(test.wantMetrics) > 0 {
				assert.Len(t, *ping.Charts(), test.wantNumCharts)
			}
		})
	}
}

func casePingSuccess(t *testing.T) *Ping {
	ping := New()
	ping.UpdateEvery = 1
	ping.Hosts = []string{"192.0.2.1", "192.0.2.2", "example.com"}
	ping.newProber = func(_ pingProberConfig, _ *logger.Logger) prober {
		return &mockProber{}
	}
	require.NoError(t, ping.Init())
	return ping
}

func casePingError(t *testing.T) *Ping {
	ping := New()
	ping.UpdateEvery = 1
	ping.Hosts = []string{"192.0.2.1", "192.0.2.2", "example.com"}
	ping.newProber = func(_ pingProberConfig, _ *logger.Logger) prober {
		return &mockProber{errOnPing: true}
	}
	require.NoError(t, ping.Init())
	return ping
}

type mockProber struct {
	errOnPing bool
}

func (m *mockProber) ping(host string) (*probing.Statistics, error) {
	if m.errOnPing {
		return nil, errors.New("mock.ping() error")
	}

	stats := probing.Statistics{
		PacketsRecv:           5,
		PacketsSent:           5,
		PacketsRecvDuplicates: 0,
		PacketLoss:            0,
		Addr:                  host,
		Rtts:                  nil,
		MinRtt:                time.Millisecond * 10,
		MaxRtt:                time.Millisecond * 20,
		AvgRtt:                time.Millisecond * 15,
		StdDevRtt:             time.Millisecond * 5,
	}

	return &stats, nil
}