-
Notifications
You must be signed in to change notification settings - Fork 4
/
authlog_exporter_test.go
47 lines (45 loc) · 1011 Bytes
/
authlog_exporter_test.go
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
package main
import (
"crypto/rand"
"fmt"
"io"
"math/big"
"net/http"
"os"
"strconv"
"testing"
"time"
)
func TestMain(t *testing.T) {
n, err := rand.Int(rand.Reader, big.NewInt(1000))
if err != nil {
t.Errorf("\nGet error during generate random int value:\n%v", err)
}
port := 50000 + int(n.Int64())
os.Args = []string{
"authlog_exporter",
"--web.listen-address=:" + strconv.Itoa(port),
"--auth.log",
"./test_data/auth.log"}
finished := make(chan struct{})
go func() {
main()
close(finished)
}()
time.Sleep(time.Second)
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/metrics", port))
if err != nil {
t.Errorf("\nGet error during GET:\n%v", err)
}
if resp.StatusCode != 200 {
t.Errorf("\nVariables do not match:\n%v\nwant:\n%v", resp.StatusCode, 200)
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("\nGet error during read resp body:\n%v", err)
}
if string(b) == "" {
t.Errorf("\nGet zero body:\n%s", string(b))
}
}