summaryrefslogtreecommitdiffstats
path: root/src/go/plugin/go.d/modules/gearman/gearman_test.go
blob: 43069abce41cefb63cfa6a3fc5110a754b970703 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// SPDX-License-Identifier: GPL-3.0-or-later

package gearman

import (
	"errors"
	"os"
	"testing"

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

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

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

	dataStatus, _         = os.ReadFile("testdata/status.txt")
	dataPriorityStatus, _ = os.ReadFile("testdata/priority-status.txt")
)

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

		"dataStatus":         dataStatus,
		"dataPriorityStatus": dataPriorityStatus,
	} {
		require.NotNil(t, data, name)
	}
}

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

func TestGearman_Init(t *testing.T) {
	tests := map[string]struct {
		config   Config
		wantFail bool
	}{
		"success with default config": {
			wantFail: false,
			config:   New().Config,
		},
		"fails if address not set": {
			wantFail: true,
			config: func() Config {
				conf := New().Config
				conf.Address = ""
				return conf
			}(),
		},
	}

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

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

func TestGearman_Cleanup(t *testing.T) {
	tests := map[string]struct {
		prepare func() *Gearman
	}{
		"not initialized": {
			prepare: func() *Gearman {
				return New()
			},
		},
		"after check": {
			prepare: func() *Gearman {
				gear := New()
				gear.newConn = func(config Config) gearmanConn { return prepareMockOk() }
				_ = gear.Check()
				return gear
			},
		},
		"after collect": {
			prepare: func() *Gearman {
				gear := New()
				gear.newConn = func(config Config) gearmanConn { return prepareMockOk() }
				_ = gear.Collect()
				return gear
			},
		},
	}

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

			assert.NotPanics(t, gear.Cleanup)
		})
	}
}

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

func TestGearman_Check(t *testing.T) {
	tests := map[string]struct {
		prepareMock func() *mockGearmanConn
		wantFail    bool
	}{
		"success case": {
			wantFail:    false,
			prepareMock: prepareMockOk,
		},
		"err on connect": {
			wantFail:    true,
			prepareMock: prepareMockErrOnConnect,
		},
		"unexpected response": {
			wantFail:    true,
			prepareMock: prepareMockUnexpectedResponse,
		},
		"empty response": {
			wantFail:    false,
			prepareMock: prepareMockEmptyResponse,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			gear := New()
			mock := test.prepareMock()
			gear.newConn = func(config Config) gearmanConn { return mock }

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

func TestGearman_Collect(t *testing.T) {
	tests := map[string]struct {
		prepareMock             func() *mockGearmanConn
		wantMetrics             map[string]int64
		wantCharts              int
		disconnectBeforeCleanup bool
		disconnectAfterCleanup  bool
	}{
		"success case": {
			prepareMock:             prepareMockOk,
			disconnectBeforeCleanup: false,
			disconnectAfterCleanup:  true,
			wantCharts:              len(summaryCharts) + len(functionStatusChartsTmpl)*4 + len(functionPriorityStatusChartsTmpl)*4,
			wantMetrics: map[string]int64{
				"function_generic_worker1_high_priority_jobs":          10,
				"function_generic_worker1_jobs_queued":                 4,
				"function_generic_worker1_jobs_running":                3,
				"function_generic_worker1_jobs_waiting":                1,
				"function_generic_worker1_low_priority_jobs":           12,
				"function_generic_worker1_normal_priority_jobs":        11,
				"function_generic_worker1_workers_available":           500,
				"function_generic_worker2_high_priority_jobs":          4,
				"function_generic_worker2_jobs_queued":                 78,
				"function_generic_worker2_jobs_running":                78,
				"function_generic_worker2_jobs_waiting":                0,
				"function_generic_worker2_low_priority_jobs":           6,
				"function_generic_worker2_normal_priority_jobs":        5,
				"function_generic_worker2_workers_available":           500,
				"function_generic_worker3_high_priority_jobs":          7,
				"function_generic_worker3_jobs_queued":                 2,
				"function_generic_worker3_jobs_running":                1,
				"function_generic_worker3_jobs_waiting":                1,
				"function_generic_worker3_low_priority_jobs":           9,
				"function_generic_worker3_normal_priority_jobs":        8,
				"function_generic_worker3_workers_available":           760,
				"function_prefix_generic_worker4_high_priority_jobs":   1,
				"function_prefix_generic_worker4_jobs_queued":          78,
				"function_prefix_generic_worker4_jobs_running":         78,
				"function_prefix_generic_worker4_jobs_waiting":         0,
				"function_prefix_generic_worker4_low_priority_jobs":    3,
				"function_prefix_generic_worker4_normal_priority_jobs": 2,
				"function_prefix_generic_worker4_workers_available":    500,
				"total_high_priority_jobs":                             22,
				"total_jobs_queued":                                    162,
				"total_jobs_running":                                   160,
				"total_jobs_waiting":                                   2,
				"total_low_priority_jobs":                              30,
				"total_normal_priority_jobs":                           26,
				"total_workers_avail":                                  0,
				"total_workers_available":                              2260,
			},
		},
		"unexpected response": {
			prepareMock:             prepareMockUnexpectedResponse,
			disconnectBeforeCleanup: false,
			disconnectAfterCleanup:  true,
		},
		"empty response": {
			prepareMock:             prepareMockEmptyResponse,
			disconnectBeforeCleanup: false,
			disconnectAfterCleanup:  true,
			wantCharts:              len(summaryCharts),
			wantMetrics: map[string]int64{
				"total_high_priority_jobs":   0,
				"total_jobs_queued":          0,
				"total_jobs_running":         0,
				"total_jobs_waiting":         0,
				"total_low_priority_jobs":    0,
				"total_normal_priority_jobs": 0,
				"total_workers_avail":        0,
			},
		},
		"err on connect": {
			prepareMock:             prepareMockErrOnConnect,
			disconnectBeforeCleanup: false,
			disconnectAfterCleanup:  false,
		},
		"err on query status": {
			prepareMock:             prepareMockErrOnQueryStatus,
			disconnectBeforeCleanup: true,
			disconnectAfterCleanup:  true,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			gear := New()
			mock := test.prepareMock()
			gear.newConn = func(config Config) gearmanConn { return mock }

			mx := gear.Collect()

			require.Equal(t, test.wantMetrics, mx, "want metrics")

			if len(test.wantMetrics) > 0 {
				module.TestMetricsHasAllChartsDims(t, gear.Charts(), mx)
				assert.Equal(t, test.wantCharts, len(*gear.Charts()), "want charts")
			}

			assert.Equal(t, test.disconnectBeforeCleanup, mock.disconnectCalled, "disconnect before cleanup")
			gear.Cleanup()
			assert.Equal(t, test.disconnectAfterCleanup, mock.disconnectCalled, "disconnect after cleanup")
		})
	}
}

func prepareMockOk() *mockGearmanConn {
	return &mockGearmanConn{
		responseStatus:         dataStatus,
		responsePriorityStatus: dataPriorityStatus,
	}
}

func prepareMockErrOnConnect() *mockGearmanConn {
	return &mockGearmanConn{
		errOnConnect: true,
	}
}

func prepareMockErrOnQueryStatus() *mockGearmanConn {
	return &mockGearmanConn{
		errOnQueryStatus: true,
	}
}

func prepareMockUnexpectedResponse() *mockGearmanConn {
	resp := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
	return &mockGearmanConn{
		responseStatus:         resp,
		responsePriorityStatus: resp,
	}
}

func prepareMockEmptyResponse() *mockGearmanConn {
	return &mockGearmanConn{
		responseStatus:         []byte("."),
		responsePriorityStatus: []byte("."),
	}
}

type mockGearmanConn struct {
	errOnConnect bool

	responseStatus   []byte
	errOnQueryStatus bool

	responsePriorityStatus   []byte
	errOnQueryPriorityStatus bool

	disconnectCalled bool
}

func (m *mockGearmanConn) connect() error {
	if m.errOnConnect {
		return errors.New("mock.connect() error")
	}
	return nil
}

func (m *mockGearmanConn) disconnect() {
	m.disconnectCalled = true
}

func (m *mockGearmanConn) queryStatus() ([]byte, error) {
	if m.errOnQueryStatus {
		return nil, errors.New("mock.queryStatus() error")
	}
	return m.responseStatus, nil
}

func (m *mockGearmanConn) queryPriorityStatus() ([]byte, error) {
	if m.errOnQueryPriorityStatus {
		return nil, errors.New("mock.queryPriorityStatus() error")
	}
	return m.responsePriorityStatus, nil
}