-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathtest.go
181 lines (142 loc) · 4.12 KB
/
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package gateway
import (
"context"
"net/http"
"testing"
"time"
"github.com/gorilla/mux"
"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/test"
)
type Test struct {
URL string
testRunner *test.HTTPTestRunner
// GlobalConfig deprecate this and instead use GW.getConfig()
GlobalConfig config.Config
config TestConfig
Gw *Gateway `json:"-"`
HttpHandler *http.Server
TestServerRouter *mux.Router
ctx context.Context
cancel context.CancelFunc
dynamicHandlers map[string]http.HandlerFunc
Parent testing.TB
}
type TestConfig struct {
SeparateControlAPI bool
Delay time.Duration
HotReload bool
overrideDefaults bool
CoprocessConfig config.CoProcessConfig
}
type TestOption func(*Test)
func NewTest(tb testing.TB, genConf func(*config.Config), opts ...TestOption) *Test {
tb.Helper()
t := &Test{
Parent: tb,
dynamicHandlers: make(map[string]http.HandlerFunc),
}
for _, optfn := range opts {
optfn(t)
}
t.Gw = t.start(genConf)
tb.Cleanup(t.Close)
return t
}
func NewTestConfigOption(conf TestConfig) func(*Test) {
return func(t *Test) {
t.config = conf
}
}
// Start is the root event point where a gateway object is created, and
// can enforce lifecycle via the *Test objects, and TestOption implementation.
// For example, if somebody wanted to have some default options set up,
// one could set a timeout by implementing:
//
// - `func NewTestTimeoutOption(d time.Duration) func(*Test)`
//
// To use, it should be passed to NewTest as an argument. A default timeout
// may be implemented in the future and set from NewTest as well.
func (s *Test) start(genConf func(globalConf *config.Config)) *Gateway {
// init and create gw
ctx, cancel := context.WithCancel(context.Background())
log.Info("starting test")
s.ctx = ctx
s.cancel = func() {
cancel()
log.Info("Cancelling test context")
}
gw := s.newGateway(genConf)
gw.setupPortsWhitelist()
gw.startServer()
gw.setupGlobals()
// Set up a default org manager so we can traverse non-live paths
if !gw.GetConfig().SupressDefaultOrgStore {
gw.DefaultOrgStore.Init(gw.getGlobalStorageHandler("orgkey.", false))
gw.DefaultQuotaStore.Init(gw.getGlobalStorageHandler("orgkey.", false))
}
s.GlobalConfig = gw.GetConfig()
scheme := "http://"
if s.GlobalConfig.HttpServerOptions.UseSSL {
scheme = "https://"
}
s.URL = scheme + gw.DefaultProxyMux.getProxy(gw.GetConfig().ListenPort, gw.GetConfig()).listener.Addr().String()
s.testRunner = &test.HTTPTestRunner{
RequestBuilder: func(tc *test.TestCase) (*http.Request, error) {
tc.BaseURL = s.URL
if tc.ControlRequest {
if s.config.SeparateControlAPI {
tc.BaseURL = scheme + s.controlProxy().listener.Addr().String()
} else if s.GlobalConfig.ControlAPIHostname != "" {
tc.Domain = s.GlobalConfig.ControlAPIHostname
}
}
r, err := test.NewRequest(tc)
if tc.AdminAuth {
r = s.withAuth(r)
}
if s.config.Delay > 0 {
tc.Delay = s.config.Delay
}
return r, err
},
Do: test.HttpServerRunner(),
}
return gw
}
// Close is the shutdown lifecycle for a gateway integration test w/ storage.
func (s *Test) Close() {
defer s.cancel()
for _, p := range s.Gw.DefaultProxyMux.proxies {
if p.listener != nil {
p.listener.Close()
}
}
gwConfig := s.Gw.GetConfig()
s.Gw.DefaultProxyMux.swap(&proxyMux{}, s.Gw)
if s.config.SeparateControlAPI {
gwConfig.ControlAPIPort = 0
s.Gw.SetConfig(gwConfig)
}
// if jsvm enabled we need to unmount to prevent high memory consumption
if s.Gw.GetConfig().EnableJSVM {
s.Gw.GlobalEventsJSVM.VM = nil
}
ctxShutDown, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := s.HttpHandler.Shutdown(ctxShutDown)
if err != nil {
log.WithError(err).Error("shutting down the http handler")
} else {
log.Info("server exited properly")
}
s.Gw.Analytics.Stop()
s.Gw.ReloadTestCase.StopTicker()
s.Gw.GlobalHostChecker.StopPoller()
s.Gw.NewRelicApplication.Shutdown(5 * time.Second)
err = s.RemoveApis()
if err != nil {
log.WithError(err).Error("could not remove apis")
}
s.Gw.cacheClose()
}