This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
harness.go
202 lines (171 loc) · 5.37 KB
/
harness.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2021 the Exposure Notifications Verification Server authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package envstest
import (
"context"
"net"
"net/http"
"os"
"testing"
"time"
"github.com/google/exposure-notifications-server/pkg/keys"
"github.com/google/exposure-notifications-server/pkg/observability"
"github.com/google/exposure-notifications-server/pkg/server"
"github.com/google/exposure-notifications-verification-server/internal/project"
"github.com/google/exposure-notifications-verification-server/pkg/cache"
"github.com/google/exposure-notifications-verification-server/pkg/controller/middleware"
"github.com/google/exposure-notifications-verification-server/pkg/database"
"github.com/google/exposure-notifications-verification-server/pkg/ratelimit"
"github.com/sethvargo/go-limiter"
"github.com/sethvargo/go-limiter/memorystore"
)
// NewHarnessServer creates a new server for the mux on a random port. Cleanup
// is handled automatically.
func NewHarnessServer(tb testing.TB, mux http.Handler) *server.Server {
// Inject the test logger into the context instead of the default sugared
// logger.
mux = middleware.PopulateLogger(project.TestLogger(tb))(mux)
// Create a stoppable context.
doneCtx, cancel := context.WithCancel(context.Background())
tb.Cleanup(func() {
cancel()
})
// As of 2020-10-29, our CI infrastructure does not support IPv6. `server.New`
// binds to "tcp", which picks the "best" address, but it prefers IPv6. As a
// result, the server binds to the IPv6 loopback`[::]`, but then our browser
// instance cannot actually contact that loopback interface. To mitigate this,
// create a custom listener and force IPv4. The listener will still pick a
// randomly available port, but it will only choose an IPv4 address upon which
// to bind.
listener, err := net.Listen("tcp4", ":0")
if err != nil {
tb.Fatalf("failed to create listener: %v", err)
}
// Start the server on a random port. Closing doneCtx will stop the server
// (which the cleanup step does).
srv, err := server.NewFromListener(listener)
if err != nil {
tb.Fatal(err)
}
go func() {
if err := srv.ServeHTTPHandler(doneCtx, mux); err != nil {
tb.Error(err)
}
}()
return srv
}
type TestHarnessResponse struct {
Cacher cache.Cacher
CacheConfig *cache.Config
Database *database.Database
DatabaseConfig *database.Config
BadDatabase *database.Database
ObservabilityConfig *observability.Config
KeyManager keys.KeyManager
KeyManagerConfig *keys.Config
RateLimiter limiter.Store
RateLimiterConfig *ratelimit.Config
}
func NewTestHarness(tb testing.TB, testDatabaseInstance *database.TestInstance) *TestHarnessResponse {
tb.Helper()
if testing.Short() {
tb.Skip()
}
ctx := project.TestContext(tb)
// Create the cacher.
cacher, err := cache.NewInMemory(nil)
if err != nil {
tb.Fatal(err)
}
tb.Cleanup(func() {
if err := cacher.Close(); err != nil {
tb.Fatal(err)
}
})
cacheConfig := &cache.Config{
Type: cache.TypeInMemory,
HMACKey: randomBytes(tb, 64),
}
// Create the key manager.
tmpdir, err := os.MkdirTemp("", "")
if err != nil {
tb.Fatal(err)
}
tb.Cleanup(func() {
if err := os.RemoveAll(tmpdir); err != nil {
tb.Fatal(err)
}
})
keyManager, err := keys.NewFilesystem(ctx, &keys.Config{
FilesystemRoot: tmpdir,
})
if err != nil {
tb.Fatal(err)
}
signingKeyManager, ok := keyManager.(keys.SigningKeyManager)
if !ok {
tb.Fatalf("%T is not SigningKeyManager", keyManager)
}
keyManagerConfig := &keys.Config{
Type: "FILESYSTEM",
FilesystemRoot: tmpdir,
}
// Create the database.
db, dbConfig := testDatabaseInstance.NewDatabase(tb, cacher,
database.WithKeyManager(keyManagerConfig, keyManager),
database.WithSigningKeyManager(keyManagerConfig, signingKeyManager))
// Create the bad database.
badDB := *db //nolint:govet
badDB.SetRawDB(NewFailingDatabase())
badDB.SetSecretResolver(database.NewSecretResolver())
// Create the rate limiter.
limiterStore, err := memorystore.New(&memorystore.Config{
Tokens: 30,
Interval: time.Second,
})
if err != nil {
tb.Fatal(err)
}
tb.Cleanup(func() {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if err := limiterStore.Close(ctx); err != nil {
tb.Fatal(err)
}
})
limiterConfig := &ratelimit.Config{
Type: ratelimit.RateLimiterTypeMemory,
HMACKey: randomBytes(tb, 64),
}
return &TestHarnessResponse{
Cacher: cacher,
CacheConfig: cacheConfig,
Database: db,
DatabaseConfig: dbConfig,
BadDatabase: &badDB,
ObservabilityConfig: &observability.Config{ExporterType: "NOOP"},
KeyManager: keyManager,
KeyManagerConfig: keyManagerConfig,
RateLimiter: limiterStore,
RateLimiterConfig: limiterConfig,
}
}
func randomBytes(tb testing.TB, length int) []byte {
tb.Helper()
b, err := project.RandomBytes(length)
if err != nil {
tb.Fatal(err)
}
return b
}