summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/logstash/logstash_test.go
blob: 6ea2e4191a7fc4430aed3edf99717a8cfcc5db10 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// SPDX-License-Identifier: GPL-3.0-or-later

package logstash

import (
	"github.com/netdata/netdata/go/go.d.plugin/agent/module"
	"net/http"
	"net/http/httptest"
	"os"
	"testing"

	"github.com/netdata/netdata/go/go.d.plugin/pkg/web"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

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

	dataNodeStatsMetrics, _ = os.ReadFile("testdata/stats.json")
)

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

	}
}

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

func TestLogstash_Init(t *testing.T) {
	tests := map[string]struct {
		wantFail bool
		config   Config
	}{
		"success with default": {
			wantFail: false,
			config:   New().Config,
		},
		"fail when URL not set": {
			wantFail: true,
			config: Config{
				HTTP: web.HTTP{
					Request: web.Request{URL: ""},
				},
			},
		},
	}

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

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

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

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

func TestLogstash_Check(t *testing.T) {
	tests := map[string]struct {
		wantFail bool
		prepare  func(t *testing.T) (ls *Logstash, cleanup func())
	}{
		"success on valid response": {
			wantFail: false,
			prepare:  caseValidResponse,
		},
		"fail on invalid data response": {
			wantFail: true,
			prepare:  caseInvalidDataResponse,
		},
		"fail on connection refused": {
			wantFail: true,
			prepare:  caseConnectionRefused,
		},
		"fail on 404 response": {
			wantFail: true,
			prepare:  case404,
		},
	}

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

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

func TestLogstash_Collect(t *testing.T) {
	tests := map[string]struct {
		prepare         func(t *testing.T) (ls *Logstash, cleanup func())
		wantNumOfCharts int
		wantMetrics     map[string]int64
	}{
		"success on valid response": {
			prepare:         caseValidResponse,
			wantNumOfCharts: len(charts) + len(pipelineChartsTmpl),
			wantMetrics: map[string]int64{
				"event_duration_in_millis":                                 0,
				"event_filtered":                                           0,
				"event_in":                                                 0,
				"event_out":                                                0,
				"event_queue_push_duration_in_millis":                      0,
				"jvm_gc_collectors_eden_collection_count":                  5796,
				"jvm_gc_collectors_eden_collection_time_in_millis":         45008,
				"jvm_gc_collectors_old_collection_count":                   7,
				"jvm_gc_collectors_old_collection_time_in_millis":          3263,
				"jvm_mem_heap_committed_in_bytes":                          528154624,
				"jvm_mem_heap_used_in_bytes":                               189973480,
				"jvm_mem_heap_used_percent":                                35,
				"jvm_mem_pools_eden_committed_in_bytes":                    69795840,
				"jvm_mem_pools_eden_used_in_bytes":                         2600120,
				"jvm_mem_pools_old_committed_in_bytes":                     449642496,
				"jvm_mem_pools_old_used_in_bytes":                          185944824,
				"jvm_mem_pools_survivor_committed_in_bytes":                8716288,
				"jvm_mem_pools_survivor_used_in_bytes":                     1428536,
				"jvm_threads_count":                                        28,
				"jvm_uptime_in_millis":                                     699809475,
				"pipelines_pipeline-1_event_duration_in_millis":            5027018,
				"pipelines_pipeline-1_event_filtered":                      567639,
				"pipelines_pipeline-1_event_in":                            567639,
				"pipelines_pipeline-1_event_out":                           567639,
				"pipelines_pipeline-1_event_queue_push_duration_in_millis": 84241,
				"process_open_file_descriptors":                            101,
			},
		},
		"fail on invalid data response": {
			prepare:         caseInvalidDataResponse,
			wantNumOfCharts: 0,
			wantMetrics:     nil,
		},
		"fail on connection refused": {
			prepare:         caseConnectionRefused,
			wantNumOfCharts: 0,
			wantMetrics:     nil,
		},
		"fail on 404 response": {
			prepare:         case404,
			wantNumOfCharts: 0,
			wantMetrics:     nil,
		},
	}

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

			mx := ls.Collect()

			require.Equal(t, test.wantMetrics, mx)
			if len(test.wantMetrics) > 0 {
				assert.Equal(t, test.wantNumOfCharts, len(*ls.Charts()))
				ensureCollectedHasAllChartsDimsVarsIDs(t, ls, mx)
			}
		})
	}
}

func ensureCollectedHasAllChartsDimsVarsIDs(t *testing.T, ls *Logstash, mx map[string]int64) {
	for _, chart := range *ls.Charts() {
		for _, dim := range chart.Dims {
			_, ok := mx[dim.ID]
			assert.Truef(t, ok, "collected metrics has no data for dim '%s' chart '%s'", dim.ID, chart.ID)
		}
		for _, v := range chart.Vars {
			_, ok := mx[v.ID]
			assert.Truef(t, ok, "collected metrics has no data for var '%s' chart '%s'", v.ID, chart.ID)
		}
	}
}

func caseValidResponse(t *testing.T) (*Logstash, func()) {
	t.Helper()
	srv := httptest.NewServer(http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			switch r.URL.Path {
			case urlPathNodeStatsAPI:
				_, _ = w.Write(dataNodeStatsMetrics)
			default:
				w.WriteHeader(http.StatusNotFound)
			}
		}))
	ls := New()
	ls.URL = srv.URL
	require.NoError(t, ls.Init())

	return ls, srv.Close
}

func caseInvalidDataResponse(t *testing.T) (*Logstash, func()) {
	t.Helper()
	srv := httptest.NewServer(http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			_, _ = w.Write([]byte("hello and\n goodbye"))
		}))
	ls := New()
	ls.URL = srv.URL
	require.NoError(t, ls.Init())

	return ls, srv.Close
}

func caseConnectionRefused(t *testing.T) (*Logstash, func()) {
	t.Helper()
	ls := New()
	ls.URL = "http://127.0.0.1:65001"
	require.NoError(t, ls.Init())

	return ls, func() {}
}

func case404(t *testing.T) (*Logstash, func()) {
	t.Helper()
	srv := httptest.NewServer(http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(http.StatusNotFound)
		}))
	ls := New()
	ls.URL = srv.URL
	require.NoError(t, ls.Init())

	return ls, srv.Close
}