forked from kedacore/http-add-on
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain_test.go
79 lines (68 loc) · 1.93 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
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"
"github.com/kedacore/http-add-on/operator/controllers/http/config"
"github.com/kedacore/http-add-on/pkg/routing"
"github.com/kedacore/http-add-on/pkg/test"
)
func TestRunAdminServerConfig(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
baseCfg := &config.Base{}
interceptorCfg := &config.Interceptor{}
externalScalerCfg := &config.ExternalScaler{}
errgrp, ctx := errgroup.WithContext(ctx)
errgrp.Go(func() error {
return runAdminServer(
ctx,
lggr,
routing.NewTable(),
port,
baseCfg,
interceptorCfg,
externalScalerCfg,
)
})
time.Sleep(1 * time.Second)
urlStr := func(path string) string {
return fmt.Sprintf("http://0.0.0.0:%d/%s", port, path)
}
res, err := http.Get(urlStr("config"))
r.NoError(err)
defer res.Body.Close()
r.Equal(200, res.StatusCode)
bodyBytes, err := io.ReadAll(res.Body)
r.NoError(err)
decodedIfaces := map[string][]interface{}{}
r.NoError(json.Unmarshal(bodyBytes, &decodedIfaces))
r.Equal(1, len(decodedIfaces))
_, hasKey := decodedIfaces["configs"]
r.True(hasKey, "config body doesn't have 'configs' key")
configs := decodedIfaces["configs"]
r.Equal(3, len(configs))
retBaseCfg := &config.Base{}
r.NoError(test.JSONRoundTrip(configs[0], retBaseCfg))
retInterceptorCfg := &config.Interceptor{}
r.NoError(test.JSONRoundTrip(configs[1], retInterceptorCfg))
retExternalScalerCfg := &config.ExternalScaler{}
r.NoError(test.JSONRoundTrip(configs[2], retExternalScalerCfg))
r.Equal(*baseCfg, *retBaseCfg)
r.Equal(*interceptorCfg, *retInterceptorCfg)
r.Equal(*externalScalerCfg, *retExternalScalerCfg)
done()
r.Error(errgrp.Wait())
}