forked from kedacore/http-add-on
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain_test.go
80 lines (70 loc) · 1.6 KB
/
main_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
"time"
"github.com/go-logr/logr"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
"k8s.io/apimachinery/pkg/util/rand"
)
func TestHealthChecks(t *testing.T) {
ctx := context.Background()
ctx, done := context.WithCancel(ctx)
defer done()
lggr := logr.Discard()
r := require.New(t)
port := rand.Intn(100) + 8000
cfg := &config{
GRPCPort: port + 1,
HealthPort: port,
TargetNamespace: "test123",
TargetService: "testsvc",
TargetPort: port + 123,
TargetPendingRequests: 100,
}
errgrp, ctx := errgroup.WithContext(ctx)
ticker, pinger, err := newFakeQueuePinger(ctx, lggr)
r.NoError(err)
defer ticker.Stop()
srvFunc := func() error {
return startAdminServer(
ctx,
lggr,
cfg,
port,
pinger,
)
}
newURL := func(path string) string {
return fmt.Sprintf("http://0.0.0.0:%d/%s", port, path)
}
errgrp.Go(srvFunc)
time.Sleep(500 * time.Millisecond)
res, err := http.Get(newURL("healthz"))
r.NoError(err)
defer res.Body.Close()
r.Equal(200, res.StatusCode)
res, err = http.Get(newURL("livez"))
r.NoError(err)
defer res.Body.Close()
r.Equal(200, res.StatusCode)
res, err = http.Get(newURL("config"))
r.NoError(err)
defer res.Body.Close()
r.Equal(200, res.StatusCode)
bodyBytes, err := io.ReadAll(res.Body)
r.NoError(err)
retCfg := map[string][]config{}
r.NoError(json.Unmarshal(bodyBytes, &retCfg))
expected := map[string][]config{
"configs": {*cfg},
}
r.Equal(expected, retCfg)
done()
r.Error(errgrp.Wait())
}