github.com/mackerelio/mackerel-agent-plugins@v0.89.3/mackerel-plugin-mailq/lib/main_test.go (about) 1 //go:build linux 2 3 package mpmailq 4 5 import ( 6 "os" 7 "path/filepath" 8 "strconv" 9 "strings" 10 "testing" 11 ) 12 13 func TestGetNthLine(t *testing.T) { 14 { 15 s := `0000` 16 l, _ := getNthLine(strings.NewReader(s), 0) 17 if l != "0000" { 18 t.Errorf("0-th line is expected to be 0000") 19 } 20 } 21 22 { 23 s := ` 24 1111` 25 l, _ := getNthLine(strings.NewReader(s), 0) 26 if l != "" { 27 t.Errorf("0-th line is expected to be empty") 28 } 29 } 30 31 { 32 s := `0000 33 1111 34 ` 35 l, _ := getNthLine(strings.NewReader(s), 0) 36 if l != "0000" { 37 t.Errorf("0-th line is expected to be 0000") 38 } 39 } 40 41 { 42 s := `` 43 l, _ := getNthLine(strings.NewReader(s), 0) 44 if l != "" { 45 t.Errorf("0-th line is expected to be empty") 46 } 47 } 48 49 { 50 s := `0000` 51 l, _ := getNthLine(strings.NewReader(s), 1) 52 if l != "" { 53 t.Errorf("1-st line is expected to be empty") 54 } 55 } 56 57 { 58 s := ` 59 1111` 60 l, _ := getNthLine(strings.NewReader(s), 1) 61 if l != "1111" { 62 t.Errorf("1-st line is expected to be 1111") 63 } 64 } 65 66 { 67 s := `0000 68 1111 69 ` 70 l, _ := getNthLine(strings.NewReader(s), 1) 71 if l != "1111" { 72 t.Errorf("1-st line is expected to be 1111") 73 } 74 } 75 76 { 77 s := `` 78 l, _ := getNthLine(strings.NewReader(s), 1) 79 if l != "" { 80 t.Errorf("1-st line is expected to be empty") 81 } 82 } 83 84 { 85 s := `0000` 86 l, _ := getNthLine(strings.NewReader(s), -1) 87 if l != "0000" { 88 t.Errorf("-1-st line is expected to be 0000") 89 } 90 } 91 92 { 93 s := ` 94 1111` 95 l, _ := getNthLine(strings.NewReader(s), -1) 96 if l != "1111" { 97 t.Errorf("1-st line is expected to be 1111") 98 } 99 } 100 101 { 102 s := `0000 103 1111 104 ` 105 l, _ := getNthLine(strings.NewReader(s), -1) 106 if l != "1111" { 107 t.Errorf("1-st line is expected to be 1111") 108 } 109 } 110 111 { 112 s := `` 113 l, _ := getNthLine(strings.NewReader(s), -1) 114 if l != "" { 115 t.Errorf("-1-st line is expected to be empty") 116 } 117 } 118 119 { 120 s := `0000` 121 l, _ := getNthLine(strings.NewReader(s), -2) 122 if l != "" { 123 t.Errorf("-2-nd line is expected to be empty") 124 } 125 } 126 127 { 128 s := ` 129 1111` 130 l, _ := getNthLine(strings.NewReader(s), -2) 131 if l != "" { 132 t.Errorf("-2-nd line is expected to be empty") 133 } 134 } 135 136 { 137 s := `0000 138 1111 139 ` 140 l, _ := getNthLine(strings.NewReader(s), -2) 141 if l != "0000" { 142 t.Errorf("-2-nd line is expected to be 0000") 143 } 144 } 145 146 { 147 s := `` 148 l, _ := getNthLine(strings.NewReader(s), -2) 149 if l != "" { 150 t.Errorf("-2-nd line is expected to be empty") 151 } 152 } 153 } 154 155 func TestParseMailqPostfix(t *testing.T) { 156 mailq := mailqFormats["postfix"] 157 158 { 159 output := `-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient------- 160 DD0C740001C 274 Thu Mar 3 23:52:37 foobar@example.com 161 (connect to mail.invalid[192.0.2.100]:25: Connection timed out) 162 nyao@mail.invalid 163 164 -- 15 Kbytes in 42 Requests. 165 ` 166 167 count, err := mailq.parse(strings.NewReader(output)) 168 if err != nil { 169 t.Errorf("Error in parseMailq: %s", err.Error()) 170 } 171 if count != 42 { 172 t.Errorf("Incorrect parse result %d", count) 173 } 174 } 175 176 // #1185 177 { 178 output := `-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient------- 179 DD0C740001C 274 Thu Mar 3 23:52:37 foobar@example.com 180 (connect to mail.invalid[192.0.2.100]:25: Connection timed out) 181 nyao@mail.invalid 182 183 -- 1 Kbytes in 1 Request. 184 ` 185 186 count, err := mailq.parse(strings.NewReader(output)) 187 if err != nil { 188 t.Errorf("Error in parseMailq: %s", err.Error()) 189 } 190 if count != 1 { 191 t.Errorf("Incorrect parse result %d", count) 192 } 193 } 194 195 { 196 output := `Mail queue is empty 197 ` 198 199 count, err := mailq.parse(strings.NewReader(output)) 200 if err != nil { 201 t.Errorf("Error in parseMailq: %s", err.Error()) 202 } 203 if count != 0 { 204 t.Errorf("Incorrect parse result %d", count) 205 } 206 } 207 } 208 209 func TestParseMailqQmain(t *testing.T) { 210 mailq := mailqFormats["qmail"] 211 212 { 213 output := `messages in queue: 42 214 messages in queue but not yet preprocessed: 3 215 ` 216 217 count, err := mailq.parse(strings.NewReader(output)) 218 if err != nil { 219 t.Errorf("Error in parseMailq: %s", err.Error()) 220 } 221 if count != 42 { 222 t.Errorf("Incorrect parse result %d", count) 223 } 224 } 225 } 226 227 func TestParseMailqExim(t *testing.T) { 228 mailq := mailqFormats["exim"] 229 230 { 231 output := `42 232 ` 233 234 count, err := mailq.parse(strings.NewReader(output)) 235 if err != nil { 236 t.Errorf("Error in parseMailq: %s", err.Error()) 237 } 238 if count != 42 { 239 t.Errorf("Incorrect parse result %d", count) 240 } 241 } 242 } 243 244 func TestGraphDefinition(t *testing.T) { 245 plugin := plugin{ 246 mailq: mailqFormats["postfix"], 247 keyPrefix: "mailq", 248 labelPrefix: "Mailq", 249 } 250 251 { 252 graphs := plugin.GraphDefinition() 253 graphMailq, ok := graphs["mailq"] 254 if !ok { 255 t.Errorf("No graph definition for mailq") 256 } 257 258 if graphMailq.Unit != "integer" { 259 t.Errorf("Mailq is expected to be an integral graph") 260 } 261 262 if graphMailq.Label == "" { 263 t.Errorf("Mailq is expected to have a label") 264 } 265 266 if len(graphMailq.Metrics) != 1 { 267 t.Errorf("Mailq is expected to have one definition of metrics") 268 } 269 270 if graphMailq.Metrics[0].Name != "count" { 271 t.Errorf("Mailq is expected to have count metric") 272 } 273 274 if graphMailq.Metrics[0].Type != "uint64" { 275 t.Errorf("Mailq is expected to have type uint64") 276 } 277 } 278 } 279 280 func TestFetchMetricsPostfix(t *testing.T) { 281 cwd, _ := os.Getwd() 282 283 plugin := plugin{ 284 mailq: mailqFormats["postfix"], 285 path: filepath.Join(cwd, "fixtures/postqueue"), 286 keyPrefix: "mailq", 287 labelPrefix: "Mailq", 288 } 289 290 for _, tc := range []struct { 291 mailqCount int 292 }{ 293 {mailqCount: 42}, 294 {mailqCount: 0}, 295 {mailqCount: 1}, // #1185 296 } { 297 os.Setenv("TEST_MAILQ_COUNT", strconv.Itoa(tc.mailqCount)) 298 defer os.Unsetenv("TEST_MAILQ_COUNT") 299 300 metrics, err := plugin.FetchMetrics() 301 if err != nil { 302 t.Errorf("Error %s", err.Error()) 303 } 304 if metrics["count"].(uint64) != uint64(tc.mailqCount) { 305 t.Errorf("Incorrect value: %d", metrics["count"].(uint64)) 306 } 307 } 308 } 309 310 func TestFetchMetricsQmail(t *testing.T) { 311 cwd, _ := os.Getwd() 312 313 plugin := plugin{ 314 mailq: mailqFormats["qmail"], 315 path: filepath.Join(cwd, "fixtures/qmail/qmail-qstat"), 316 keyPrefix: "mailq", 317 labelPrefix: "Mailq", 318 } 319 320 origPath := os.Getenv("PATH") 321 os.Setenv("PATH", "/bin:/usr/bin") 322 defer os.Setenv("PATH", origPath) 323 324 { 325 os.Setenv("TEST_MAILQ_COUNT", "42") 326 defer os.Unsetenv("TEST_MAILQ_COUNT") 327 328 metrics, err := plugin.FetchMetrics() 329 if err != nil { 330 t.Errorf("Error %s", err.Error()) 331 } 332 if metrics["count"].(uint64) != 42 { 333 t.Errorf("Incorrect value: %d", metrics["count"].(uint64)) 334 } 335 } 336 }