-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
271 lines (256 loc) · 7.41 KB
/
server_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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package server
import (
"context"
"flag"
api "github.com/anshulsood11/loghouse/api/v1"
"github.com/anshulsood11/loghouse/internal/auth"
"github.com/anshulsood11/loghouse/internal/log"
"github.com/anshulsood11/loghouse/internal/test_util"
"github.com/stretchr/testify/require"
"go.opencensus.io/examples/exporter"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/status"
"io/ioutil"
"net"
"os"
"testing"
"time"
)
var debug = flag.Bool("debug", false, "Enable observability for debugging.")
/*
When a test file implements TestMain(m *testing.M), Go will call TestMain(m) instead
of running the tests directly. TestMain() gives us a place for setup that applies
to all tests in that file, like enabling our debug output. Flag parsing has to
go in TestMain() instead of init(), otherwise Go can’t define the flag and your code
will error and exit.
*/
func TestMain(m *testing.M) {
flag.Parse()
if *debug {
logger, err := zap.NewDevelopment()
if err != nil {
panic(err)
}
zap.ReplaceGlobals(logger)
}
os.Exit(m.Run())
}
func TestServer(t *testing.T) {
for scenario, fn := range map[string]func(
t *testing.T,
rootClient api.LogClient,
nobodyClient api.LogClient,
config *Config,
){
"produce/consume a message to/from the log succeeeds": testProduceConsume,
"produce/consume stream succeeds": testProduceConsumeStream,
"consume past log boundary fails": testConsumePastBoundary,
"unauthorized fails": testUnauthorized,
} {
t.Run(scenario, func(t *testing.T) {
rootClient, nobodyClient, config, teardown := setupTest(t)
defer teardown()
fn(t, rootClient, nobodyClient, config)
})
}
}
func setupTest(t *testing.T) (
rootClient api.LogClient,
nobodyClient api.LogClient,
cfg *Config,
teardown func(),
) {
t.Helper()
// Next, creating a listener on the local network.
// Port = 0 automatically assign us a free port.
l, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
// Root client is permitted to produce and consume
var rootConn *grpc.ClientConn
rootConn, rootClient, _ = newClient(t, l.Addr().String(), test_util.RootClientCertFile, test_util.RootClientKeyFile)
// Nobody client isn’t permitted to do anything
var nobodyConn *grpc.ClientConn
nobodyConn, nobodyClient, _ = newClient(t, l.Addr().String(), test_util.NobodyClientCertFile, test_util.NobodyClientKeyFile)
serverTLSConfig, err := test_util.SetupTLSConfig(test_util.TLSConfig{
CertFile: test_util.ServerCertFile,
KeyFile: test_util.ServerKeyFile,
CAFile: test_util.CAFile,
ServerAddress: l.Addr().String(),
Server: true,
})
require.NoError(t, err)
serverCreds := credentials.NewTLS(serverTLSConfig)
dir, err := ioutil.TempDir("", "server-test")
require.NoError(t, err)
clog, err := log.NewLog(dir, log.Config{})
require.NoError(t, err)
authorizer := auth.NewAuthorizer(test_util.ACLModelFile, test_util.ACLPolicyFile)
cfg = &Config{
CommitLog: clog,
Authorizer: authorizer,
}
server, err := NewGRPCServer(cfg, grpc.Creds(serverCreds))
require.NoError(t, err)
go func() {
server.Serve(l)
}()
var telemetryExporter *exporter.LogExporter
if *debug {
metricsLogFile, err := ioutil.TempFile("", "metrics-*.log")
require.NoError(t, err)
t.Logf("metrics log file: %s", metricsLogFile.Name())
tracesLogFile, err := ioutil.TempFile("", "traces-*.log")
require.NoError(t, err)
t.Logf("traces log file: %s", tracesLogFile.Name())
telemetryExporter, err = exporter.NewLogExporter(exporter.Options{
MetricsLogFile: metricsLogFile.Name(),
TracesLogFile: tracesLogFile.Name(),
ReportingInterval: time.Second,
})
require.NoError(t, err)
err = telemetryExporter.Start()
require.NoError(t, err)
}
return rootClient, nobodyClient, cfg, func() {
server.Stop()
rootConn.Close()
nobodyConn.Close()
l.Close()
clog.Remove()
if telemetryExporter != nil {
time.Sleep(1500 * time.Millisecond)
telemetryExporter.Stop()
telemetryExporter.Close()
}
}
}
func newClient(t *testing.T, serverAddress, crtPath, keyPath string) (
*grpc.ClientConn,
api.LogClient,
[]grpc.DialOption,
) {
clientTLSConfig, err := test_util.SetupTLSConfig(test_util.TLSConfig{
CertFile: crtPath,
KeyFile: keyPath,
CAFile: test_util.CAFile,
Server: false,
})
require.NoError(t, err)
clientCreds := credentials.NewTLS(clientTLSConfig)
opts := []grpc.DialOption{grpc.WithTransportCredentials(clientCreds)}
cc, err := grpc.Dial(serverAddress, grpc.WithTransportCredentials(clientCreds))
require.NoError(t, err)
client := api.NewLogClient(cc)
return cc, client, opts
}
func testProduceConsume(t *testing.T, client, _ api.LogClient, config *Config) {
ctx := context.Background()
want := &api.Record{
Value: []byte("hello world"),
}
produce, err := client.Produce(
ctx,
&api.ProduceRequest{
Record: want,
},
)
require.NoError(t, err)
consume, err := client.Consume(ctx, &api.ConsumeRequest{
Offset: produce.Offset,
})
require.NoError(t, err)
require.Equal(t, want.Value, consume.Record.Value)
require.Equal(t, want.Offset, consume.Record.Offset)
}
func testConsumePastBoundary(t *testing.T, client, _ api.LogClient, config *Config) {
ctx := context.Background()
produce, err := client.Produce(ctx, &api.ProduceRequest{
Record: &api.Record{
Value: []byte("hello world"),
},
})
require.NoError(t, err)
consume, err := client.Consume(ctx, &api.ConsumeRequest{
Offset: produce.Offset + 1,
})
if consume != nil {
t.Fatal("consume not nil")
}
got := grpc.Code(err)
want := grpc.Code(api.ErrOffsetOutOfRange{}.GRPCStatus().Err())
if got != want {
t.Fatalf("got err: %v, want: %v", got, want)
}
}
func testProduceConsumeStream(t *testing.T, client, _ api.LogClient, config *Config) {
ctx := context.Background()
records := []*api.Record{{
Value: []byte("first message"),
Offset: 0,
}, {
Value: []byte("second message"),
Offset: 1,
}}
{
stream, err := client.ProduceStream(ctx)
require.NoError(t, err)
for offset, record := range records {
err = stream.Send(&api.ProduceRequest{
Record: record,
})
require.NoError(t, err)
res, err := stream.Recv()
require.NoError(t, err)
if res.Offset != uint64(offset) {
t.Fatalf(
"got offset: %d, want: %d",
res.Offset,
offset,
)
}
}
}
{
stream, err := client.ConsumeStream(
ctx,
&api.ConsumeRequest{Offset: 0},
)
require.NoError(t, err)
for i, record := range records {
res, err := stream.Recv()
require.NoError(t, err)
require.Equal(t, res.Record, &api.Record{
Value: record.Value,
Offset: uint64(i),
})
}
}
}
func testUnauthorized(t *testing.T, _, unauthorizedClient api.LogClient, config *Config) {
ctx := context.Background()
produce, err := unauthorizedClient.Produce(ctx, &api.ProduceRequest{
Record: &api.Record{
Value: []byte("hello world"),
},
})
if produce != nil {
t.Fatalf("produce response should be nil")
}
gotCode, wantCode := status.Code(err), codes.PermissionDenied
if gotCode != wantCode {
t.Fatalf("got code: %d, want: %d", gotCode, wantCode)
}
consume, err := unauthorizedClient.Consume(ctx, &api.ConsumeRequest{
Offset: 0,
})
if consume != nil {
t.Fatalf("consume response should be nil")
}
gotCode, wantCode = status.Code(err), codes.PermissionDenied
if gotCode != wantCode {
t.Fatalf("got code: %d, want: %d", gotCode, wantCode)
}
}