master
go 218 lines 4.35 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package exim
4
5 import (
6 "context"
7 "errors"
8 "os"
9 "testing"
10
11 "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/collecttest"
12
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 )
16
17 var (
18 dataConfigJSON, _ = os.ReadFile("testdata/config.json")
19 dataConfigYAML, _ = os.ReadFile("testdata/config.yaml")
20 )
21
22 func Test_testDataIsValid(t *testing.T) {
23 for name, data := range map[string][]byte{
24 "dataConfigJSON": dataConfigJSON,
25 "dataConfigYAML": dataConfigYAML,
26 } {
27 require.NotNil(t, data, name)
28
29 }
30 }
31
32 func TestCollector_Configuration(t *testing.T) {
33 collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML)
34 }
35
36 func TestCollector_Init(t *testing.T) {
37 tests := map[string]struct {
38 config Config
39 wantFail bool
40 }{
41 "success with default config": {
42 wantFail: false,
43 config: New().Config,
44 },
45 }
46
47 for name, test := range tests {
48 t.Run(name, func(t *testing.T) {
49 collr := New()
50 collr.Config = test.config
51
52 if test.wantFail {
53 assert.Error(t, collr.Init(context.Background()))
54 } else {
55 assert.NoError(t, collr.Init(context.Background()))
56 }
57 })
58 }
59 }
60
61 func TestCollector_Cleanup(t *testing.T) {
62 tests := map[string]struct {
63 prepare func() *Collector
64 }{
65 "not initialized exec": {
66 prepare: func() *Collector {
67 return New()
68 },
69 },
70 "after check": {
71 prepare: func() *Collector {
72 collr := New()
73 collr.exec = prepareMockOK()
74 _ = collr.Check(context.Background())
75 return collr
76 },
77 },
78 "after collect": {
79 prepare: func() *Collector {
80 collr := New()
81 collr.exec = prepareMockOK()
82 _ = collr.Collect(context.Background())
83 return collr
84 },
85 },
86 }
87
88 for name, test := range tests {
89 t.Run(name, func(t *testing.T) {
90 collr := test.prepare()
91
92 assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
93 })
94 }
95 }
96
97 func TestCollectorCharts(t *testing.T) {
98 assert.NotNil(t, New().Charts())
99 }
100
101 func TestCollector_Check(t *testing.T) {
102 tests := map[string]struct {
103 prepareMock func() *mockEximExec
104 wantFail bool
105 }{
106 "success case": {
107 prepareMock: prepareMockOK,
108 wantFail: false,
109 },
110 "error on exec": {
111 prepareMock: prepareMockErr,
112 wantFail: true,
113 },
114 "empty response": {
115 prepareMock: prepareMockEmptyResponse,
116 wantFail: true,
117 },
118 "unexpected response": {
119 prepareMock: prepareMockUnexpectedResponse,
120 wantFail: true,
121 },
122 }
123
124 for name, test := range tests {
125 t.Run(name, func(t *testing.T) {
126 collr := New()
127 mock := test.prepareMock()
128 collr.exec = mock
129
130 if test.wantFail {
131 assert.Error(t, collr.Check(context.Background()))
132 } else {
133 assert.NoError(t, collr.Check(context.Background()))
134 }
135 })
136 }
137 }
138
139 func TestCollector_Collect(t *testing.T) {
140 tests := map[string]struct {
141 prepareMock func() *mockEximExec
142 wantMetrics map[string]int64
143 }{
144 "success case": {
145 prepareMock: prepareMockOK,
146 wantMetrics: map[string]int64{
147 "emails": 99,
148 },
149 },
150 "error on exec": {
151 prepareMock: prepareMockErr,
152 wantMetrics: nil,
153 },
154 "empty response": {
155 prepareMock: prepareMockEmptyResponse,
156 wantMetrics: nil,
157 },
158 "unexpected response": {
159 prepareMock: prepareMockUnexpectedResponse,
160 wantMetrics: nil,
161 },
162 }
163
164 for name, test := range tests {
165 t.Run(name, func(t *testing.T) {
166 collr := New()
167 mock := test.prepareMock()
168 collr.exec = mock
169
170 mx := collr.Collect(context.Background())
171
172 assert.Equal(t, test.wantMetrics, mx)
173
174 if len(test.wantMetrics) > 0 {
175 assert.Len(t, *collr.Charts(), len(charts))
176 collecttest.TestMetricsHasAllChartsDims(t, collr.Charts(), mx)
177 }
178 })
179 }
180 }
181
182 func prepareMockOK() *mockEximExec {
183 return &mockEximExec{
184 data: []byte("99"),
185 }
186 }
187
188 func prepareMockErr() *mockEximExec {
189 return &mockEximExec{
190 err: true,
191 }
192 }
193
194 func prepareMockEmptyResponse() *mockEximExec {
195 return &mockEximExec{}
196 }
197
198 func prepareMockUnexpectedResponse() *mockEximExec {
199 return &mockEximExec{
200 data: []byte(`
201 Lorem ipsum dolor sit amet, consectetur adipiscing elit.
202 Nulla malesuada erat id magna mattis, eu viverra tellus rhoncus.
203 Fusce et felis pulvinar, posuere sem non, porttitor eros.
204 `),
205 }
206 }
207
208 type mockEximExec struct {
209 err bool
210 data []byte
211 }
212
213 func (m *mockEximExec) countMessagesInQueue() ([]byte, error) {
214 if m.err {
215 return nil, errors.New("mock.countMessagesInQueue() error")
216 }
217 return m.data, nil
218 }