master
go 241 lines 6.72 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package logstash
4
5 import (
6 "context"
7 "net/http"
8 "net/http/httptest"
9 "os"
10 "testing"
11
12 "github.com/netdata/netdata/go/plugins/pkg/web"
13 "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/collecttest"
14
15 "github.com/stretchr/testify/assert"
16 "github.com/stretchr/testify/require"
17 )
18
19 var (
20 dataConfigJSON, _ = os.ReadFile("testdata/config.json")
21 dataConfigYAML, _ = os.ReadFile("testdata/config.yaml")
22
23 dataNodeStatsMetrics, _ = os.ReadFile("testdata/stats.json")
24 )
25
26 func Test_testDataIsValid(t *testing.T) {
27 for name, data := range map[string][]byte{
28 "dataConfigJSON": dataConfigJSON,
29 "dataConfigYAML": dataConfigYAML,
30 "dataNodeStatsMetrics": dataNodeStatsMetrics,
31 } {
32 require.NotNilf(t, data, name)
33
34 }
35 }
36
37 func TestCollector_ConfigurationSerialize(t *testing.T) {
38 collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML)
39 }
40
41 func TestCollector_Init(t *testing.T) {
42 tests := map[string]struct {
43 wantFail bool
44 config Config
45 }{
46 "success with default": {
47 wantFail: false,
48 config: New().Config,
49 },
50 "fail when URL not set": {
51 wantFail: true,
52 config: Config{
53 HTTPConfig: web.HTTPConfig{
54 RequestConfig: web.RequestConfig{URL: ""},
55 },
56 },
57 },
58 }
59
60 for name, test := range tests {
61 t.Run(name, func(t *testing.T) {
62 collr := New()
63 collr.Config = test.config
64
65 if test.wantFail {
66 assert.Error(t, collr.Init(context.Background()))
67 } else {
68 assert.NoError(t, collr.Init(context.Background()))
69 }
70 })
71 }
72 }
73
74 func TestCollector_Charts(t *testing.T) {
75 assert.NotNil(t, New().Charts())
76 }
77
78 func TestCollector_Cleanup(t *testing.T) {
79 assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
80 }
81
82 func TestCollector_Check(t *testing.T) {
83 tests := map[string]struct {
84 wantFail bool
85 prepare func(t *testing.T) (ls *Collector, cleanup func())
86 }{
87 "success on valid response": {
88 wantFail: false,
89 prepare: caseValidResponse,
90 },
91 "fail on invalid data response": {
92 wantFail: true,
93 prepare: caseInvalidDataResponse,
94 },
95 "fail on connection refused": {
96 wantFail: true,
97 prepare: caseConnectionRefused,
98 },
99 "fail on 404 response": {
100 wantFail: true,
101 prepare: case404,
102 },
103 }
104
105 for name, test := range tests {
106 t.Run(name, func(t *testing.T) {
107 collr, cleanup := test.prepare(t)
108 defer cleanup()
109
110 if test.wantFail {
111 assert.Error(t, collr.Check(context.Background()))
112 } else {
113 assert.NoError(t, collr.Check(context.Background()))
114 }
115 })
116 }
117 }
118
119 func TestCollector_Collect(t *testing.T) {
120 tests := map[string]struct {
121 prepare func(t *testing.T) (ls *Collector, cleanup func())
122 wantNumOfCharts int
123 wantMetrics map[string]int64
124 }{
125 "success on valid response": {
126 prepare: caseValidResponse,
127 wantNumOfCharts: len(charts) + len(pipelineChartsTmpl),
128 wantMetrics: map[string]int64{
129 "event_duration_in_millis": 0,
130 "event_filtered": 0,
131 "event_in": 0,
132 "event_out": 0,
133 "event_queue_push_duration_in_millis": 0,
134 "jvm_gc_collectors_eden_collection_count": 5796,
135 "jvm_gc_collectors_eden_collection_time_in_millis": 45008,
136 "jvm_gc_collectors_old_collection_count": 7,
137 "jvm_gc_collectors_old_collection_time_in_millis": 3263,
138 "jvm_mem_heap_committed_in_bytes": 528154624,
139 "jvm_mem_heap_used_in_bytes": 189973480,
140 "jvm_mem_heap_used_percent": 35,
141 "jvm_mem_pools_eden_committed_in_bytes": 69795840,
142 "jvm_mem_pools_eden_used_in_bytes": 2600120,
143 "jvm_mem_pools_old_committed_in_bytes": 449642496,
144 "jvm_mem_pools_old_used_in_bytes": 185944824,
145 "jvm_mem_pools_survivor_committed_in_bytes": 8716288,
146 "jvm_mem_pools_survivor_used_in_bytes": 1428536,
147 "jvm_threads_count": 28,
148 "jvm_uptime_in_millis": 699809475,
149 "pipelines_pipeline-1_event_duration_in_millis": 5027018,
150 "pipelines_pipeline-1_event_filtered": 567639,
151 "pipelines_pipeline-1_event_in": 567639,
152 "pipelines_pipeline-1_event_out": 567639,
153 "pipelines_pipeline-1_event_queue_push_duration_in_millis": 84241,
154 "process_open_file_descriptors": 101,
155 },
156 },
157 "fail on invalid data response": {
158 prepare: caseInvalidDataResponse,
159 wantNumOfCharts: 0,
160 wantMetrics: nil,
161 },
162 "fail on connection refused": {
163 prepare: caseConnectionRefused,
164 wantNumOfCharts: 0,
165 wantMetrics: nil,
166 },
167 "fail on 404 response": {
168 prepare: case404,
169 wantNumOfCharts: 0,
170 wantMetrics: nil,
171 },
172 }
173
174 for name, test := range tests {
175 t.Run(name, func(t *testing.T) {
176 collr, cleanup := test.prepare(t)
177 defer cleanup()
178
179 mx := collr.Collect(context.Background())
180
181 require.Equal(t, test.wantMetrics, mx)
182 if len(test.wantMetrics) > 0 {
183 assert.Equal(t, test.wantNumOfCharts, len(*collr.Charts()))
184 collecttest.TestMetricsHasAllChartsDims(t, collr.Charts(), mx)
185 }
186 })
187 }
188 }
189
190 func caseValidResponse(t *testing.T) (*Collector, func()) {
191 t.Helper()
192 srv := httptest.NewServer(http.HandlerFunc(
193 func(w http.ResponseWriter, r *http.Request) {
194 switch r.URL.Path {
195 case urlPathNodeStatsAPI:
196 _, _ = w.Write(dataNodeStatsMetrics)
197 default:
198 w.WriteHeader(http.StatusNotFound)
199 }
200 }))
201 collr := New()
202 collr.URL = srv.URL
203 require.NoError(t, collr.Init(context.Background()))
204
205 return collr, srv.Close
206 }
207
208 func caseInvalidDataResponse(t *testing.T) (*Collector, func()) {
209 t.Helper()
210 srv := httptest.NewServer(http.HandlerFunc(
211 func(w http.ResponseWriter, r *http.Request) {
212 _, _ = w.Write([]byte("hello and\n goodbye"))
213 }))
214 collr := New()
215 collr.URL = srv.URL
216 require.NoError(t, collr.Init(context.Background()))
217
218 return collr, srv.Close
219 }
220
221 func caseConnectionRefused(t *testing.T) (*Collector, func()) {
222 t.Helper()
223 collr := New()
224 collr.URL = "http://127.0.0.1:65001"
225 require.NoError(t, collr.Init(context.Background()))
226
227 return collr, func() {}
228 }
229
230 func case404(t *testing.T) (*Collector, func()) {
231 t.Helper()
232 srv := httptest.NewServer(http.HandlerFunc(
233 func(w http.ResponseWriter, r *http.Request) {
234 w.WriteHeader(http.StatusNotFound)
235 }))
236 collr := New()
237 collr.URL = srv.URL
238 require.NoError(t, collr.Init(context.Background()))
239
240 return collr, srv.Close
241 }