-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpbackup_exporter_test.go
55 lines (53 loc) · 1.32 KB
/
gpbackup_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
48
49
50
51
52
53
54
55
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())
tempFile, err := os.CreateTemp("", "gpbackup_history*.db")
if err != nil {
t.Errorf("Failed to create temp file: %v", err)
}
defer os.Remove(tempFile.Name())
os.Args = []string{"gpbackup_exporter", "--web.listen-address=:" + strconv.Itoa(port), "--gpbackup.history-file=" + tempFile.Name()}
finished := make(chan struct{})
go func() {
main()
close(finished)
}()
time.Sleep(time.Second)
urlList := []string{
fmt.Sprintf("http://localhost:%d/metrics", port),
fmt.Sprintf("http://localhost:%d/", port),
fmt.Sprintf("http://localhost:%d/health", port),
}
for _, url := range urlList {
resp, err := http.Get(url)
if err != nil {
t.Errorf("\nGet error during GET:\n%v", err)
}
if resp.StatusCode != 200 {
t.Errorf("\nGet bad response code:\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 len(string(b)) == 0 {
t.Errorf("\nGet zero body:\n%s", string(b))
}
}
}