| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package postfix |
| 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 | dataPostqueue, _ = os.ReadFile("testdata/postqueue.txt") |
| 22 | ) |
| 23 | |
| 24 | func Test_testDataIsValid(t *testing.T) { |
| 25 | for name, data := range map[string][]byte{ |
| 26 | "dataConfigJSON": dataConfigJSON, |
| 27 | "dataConfigYAML": dataConfigYAML, |
| 28 | "dataPostqueue": dataPostqueue, |
| 29 | } { |
| 30 | require.NotNil(t, data, name) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestCollector_Configuration(t *testing.T) { |
| 35 | collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML) |
| 36 | } |
| 37 | |
| 38 | func TestCollector_Init(t *testing.T) { |
| 39 | tests := map[string]struct { |
| 40 | config Config |
| 41 | wantFail bool |
| 42 | }{ |
| 43 | "fails if 'binary_path' is not set": { |
| 44 | wantFail: true, |
| 45 | config: Config{ |
| 46 | BinaryPath: "", |
| 47 | }, |
| 48 | }, |
| 49 | "fails if failed to find binary": { |
| 50 | wantFail: true, |
| 51 | config: Config{ |
| 52 | BinaryPath: "postqueue!!!", |
| 53 | }, |
| 54 | }, |
| 55 | } |
| 56 | |
| 57 | for name, test := range tests { |
| 58 | t.Run(name, func(t *testing.T) { |
| 59 | collr := New() |
| 60 | collr.Config = test.config |
| 61 | |
| 62 | if test.wantFail { |
| 63 | assert.Error(t, collr.Init(context.Background())) |
| 64 | } else { |
| 65 | assert.NoError(t, collr.Init(context.Background())) |
| 66 | } |
| 67 | }) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestCollector_Cleanup(t *testing.T) { |
| 72 | tests := map[string]struct { |
| 73 | prepare func() *Collector |
| 74 | }{ |
| 75 | "not initialized exec": { |
| 76 | prepare: func() *Collector { |
| 77 | return New() |
| 78 | }, |
| 79 | }, |
| 80 | "after check": { |
| 81 | prepare: func() *Collector { |
| 82 | pf := New() |
| 83 | pf.exec = prepareMockOK() |
| 84 | _ = pf.Check(context.Background()) |
| 85 | return pf |
| 86 | }, |
| 87 | }, |
| 88 | "after collect": { |
| 89 | prepare: func() *Collector { |
| 90 | pf := New() |
| 91 | pf.exec = prepareMockOK() |
| 92 | _ = pf.Collect(context.Background()) |
| 93 | return pf |
| 94 | }, |
| 95 | }, |
| 96 | } |
| 97 | |
| 98 | for name, test := range tests { |
| 99 | t.Run(name, func(t *testing.T) { |
| 100 | collr := test.prepare() |
| 101 | |
| 102 | assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) |
| 103 | }) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func TestCollector_Charts(t *testing.T) { |
| 108 | assert.NotNil(t, New().Charts()) |
| 109 | } |
| 110 | |
| 111 | func TestCollector_Check(t *testing.T) { |
| 112 | tests := map[string]struct { |
| 113 | prepareMock func() *mockPostqueueExec |
| 114 | wantFail bool |
| 115 | }{ |
| 116 | "success case": { |
| 117 | wantFail: false, |
| 118 | prepareMock: prepareMockOK, |
| 119 | }, |
| 120 | "mail queue is empty": { |
| 121 | wantFail: false, |
| 122 | prepareMock: prepareMockEmptyMailQueue, |
| 123 | }, |
| 124 | "error on list call": { |
| 125 | wantFail: true, |
| 126 | prepareMock: prepareMockErrOnList, |
| 127 | }, |
| 128 | "empty response": { |
| 129 | wantFail: true, |
| 130 | prepareMock: prepareMockEmptyResponse, |
| 131 | }, |
| 132 | "unexpected response": { |
| 133 | wantFail: true, |
| 134 | prepareMock: prepareMockUnexpectedResponse, |
| 135 | }, |
| 136 | } |
| 137 | |
| 138 | for name, test := range tests { |
| 139 | t.Run(name, func(t *testing.T) { |
| 140 | collr := New() |
| 141 | mock := test.prepareMock() |
| 142 | collr.exec = mock |
| 143 | |
| 144 | if test.wantFail { |
| 145 | assert.Error(t, collr.Check(context.Background())) |
| 146 | } else { |
| 147 | assert.NoError(t, collr.Check(context.Background())) |
| 148 | } |
| 149 | }) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | func TestCollector_Collect(t *testing.T) { |
| 154 | tests := map[string]struct { |
| 155 | prepareMock func() *mockPostqueueExec |
| 156 | wantMetrics map[string]int64 |
| 157 | }{ |
| 158 | "success case": { |
| 159 | prepareMock: prepareMockOK, |
| 160 | wantMetrics: map[string]int64{ |
| 161 | "emails": 12991, |
| 162 | "size": 132422, |
| 163 | }, |
| 164 | }, |
| 165 | "mail queue is empty": { |
| 166 | prepareMock: prepareMockEmptyMailQueue, |
| 167 | wantMetrics: map[string]int64{ |
| 168 | "emails": 0, |
| 169 | "size": 0, |
| 170 | }, |
| 171 | }, |
| 172 | "error on list call": { |
| 173 | prepareMock: prepareMockErrOnList, |
| 174 | wantMetrics: nil, |
| 175 | }, |
| 176 | "empty response": { |
| 177 | prepareMock: prepareMockEmptyResponse, |
| 178 | wantMetrics: nil, |
| 179 | }, |
| 180 | "unexpected response": { |
| 181 | prepareMock: prepareMockUnexpectedResponse, |
| 182 | wantMetrics: nil, |
| 183 | }, |
| 184 | } |
| 185 | |
| 186 | for name, test := range tests { |
| 187 | t.Run(name, func(t *testing.T) { |
| 188 | collr := New() |
| 189 | mock := test.prepareMock() |
| 190 | collr.exec = mock |
| 191 | |
| 192 | mx := collr.Collect(context.Background()) |
| 193 | |
| 194 | assert.Equal(t, test.wantMetrics, mx) |
| 195 | }) |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | func prepareMockOK() *mockPostqueueExec { |
| 200 | return &mockPostqueueExec{ |
| 201 | listData: dataPostqueue, |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | func prepareMockEmptyMailQueue() *mockPostqueueExec { |
| 206 | return &mockPostqueueExec{ |
| 207 | listData: []byte("Mail queue is empty"), |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | func prepareMockErrOnList() *mockPostqueueExec { |
| 212 | return &mockPostqueueExec{ |
| 213 | errOnList: true, |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | func prepareMockEmptyResponse() *mockPostqueueExec { |
| 218 | return &mockPostqueueExec{} |
| 219 | } |
| 220 | |
| 221 | func prepareMockUnexpectedResponse() *mockPostqueueExec { |
| 222 | return &mockPostqueueExec{ |
| 223 | listData: []byte(` |
| 224 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. |
| 225 | Nulla malesuada erat id magna mattis, eu viverra tellus rhoncus. |
| 226 | Fusce et felis pulvinar, posuere sem non, porttitor eros. |
| 227 | `), |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | type mockPostqueueExec struct { |
| 232 | errOnList bool |
| 233 | listData []byte |
| 234 | } |
| 235 | |
| 236 | func (m *mockPostqueueExec) list() ([]byte, error) { |
| 237 | if m.errOnList { |
| 238 | return nil, errors.New("mock.list() error") |
| 239 | } |
| 240 | |
| 241 | return m.listData, nil |
| 242 | } |