Newer
Older
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
package interaction
import (
"context"
"log"
"net"
"testing"
"code.fbi.h-da.de/danet/costaquanta/ctrl/internal/service"
pb "code.fbi.h-da.de/danet/costaquanta/gen/go/ctrl/v1"
sharedv1 "code.fbi.h-da.de/danet/costaquanta/gen/go/shared/v1"
"code.fbi.h-da.de/danet/costaquanta/libs/tracing"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/test/bufconn"
)
func newRoutingServer() pb.CtrlRoutingServiceServer {
logs := zap.NewExample()
traceProvider := tracing.GetTestTracer()
defer func() {
if err := traceProvider.Shutdown(context.Background()); err != nil {
logs.Info(err.Error())
}
}()
svc := service.NewRoutingService(logs.Sugar(), traceProvider.Tracer("test"))
srv := NewRoutingServer(
zap.NewExample().Sugar(),
traceProvider.Tracer("test"),
svc,
)
return srv
}
//nolint:staticcheck
func getTestRoutingServer(ctx context.Context) (pb.CtrlRoutingServiceClient, func()) {
buffer := 101024 * 1024
lis := bufconn.Listen(buffer)
baseServer := grpc.NewServer()
pb.RegisterCtrlRoutingServiceServer(baseServer, newRoutingServer())
go func() {
if err := baseServer.Serve(lis); err != nil {
log.Printf("error serving server: %v", err)
}
}()
conn, err := grpc.DialContext(ctx, "",
grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Printf("error connecting to server: %v", err)
}
closer := func() {
err := lis.Close()
if err != nil {
log.Printf("error closing listener: %v", err)
}
baseServer.Stop()
}
client := pb.NewCtrlRoutingServiceClient(conn)
return client, closer
}
//nolint:paralleltest
func TestRoutingServer_RequestRoute(t *testing.T) {
ctx := context.Background()
client, closer := getTestRoutingServer(ctx)
defer closer()
type expectation struct {
out *pb.RequestRouteResponse
err error
}
tests := map[string]struct {
in *pb.RequestRouteRequest
expected expectation
}{
"first-test": {
in: &pb.RequestRouteRequest{
SourceKmsId: "1337",
TargetKmsId: "42",
CryptoAlgorithm: sharedv1.CryptoAlgorithm_CRYPTO_ALGORITHM_AES_256_GCM,
},
expected: expectation{
out: &pb.RequestRouteResponse{
RouteId: "1337",
},
err: nil,
},
},
}
for scenario, tt := range tests {
t.Run(scenario, func(t *testing.T) {
out, err := client.RequestRoute(ctx, tt.in)
if err != nil {
if tt.expected.err.Error() != err.Error() {
t.Errorf("Err -> \nWant: %q\nGot: %q\n", tt.expected.err, err)
}
} else {
if tt.expected.out.GetRouteId() != out.GetRouteId() {
t.Errorf("Out -> \nWant: %q\nGot : %q", tt.expected.out, out)
}
}
})
}
}