-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain_test.go
150 lines (136 loc) · 3.56 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
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
package main_test
import (
"bytes"
"context"
"crypto/sha256"
_ "embed"
"encoding/hex"
"log/slog"
"net"
"os"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/test/bufconn"
"example/federation"
)
const bufSize = 1024
var (
listener *bufconn.Listener
//go:embed regexp.wasm
wasm []byte
)
func dialer(ctx context.Context, address string) (net.Conn, error) {
return listener.Dial()
}
func toSha256(v []byte) string {
hash := sha256.Sum256(v)
return hex.EncodeToString(hash[:])
}
func TestFederation(t *testing.T) {
ctx := context.Background()
listener = bufconn.Listen(bufSize)
if os.Getenv("ENABLE_JAEGER") != "" {
exporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithInsecure())
if err != nil {
t.Fatal(err)
}
tp := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(
resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String("example15/celplugin"),
semconv.ServiceVersionKey.String("1.0.0"),
attribute.String("environment", "dev"),
),
),
sdktrace.WithSampler(sdktrace.AlwaysSample()),
)
defer tp.Shutdown(ctx)
otel.SetTextMapPropagator(propagation.TraceContext{})
otel.SetTracerProvider(tp)
}
conn, err := grpc.DialContext(
ctx, "",
grpc.WithContextDialer(dialer),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(grpc.WaitForReady(true)),
)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
grpcServer := grpc.NewServer()
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))
federationServer, err := federation.NewFederationService(federation.FederationServiceConfig{
CELPlugin: &federation.FederationServiceCELPluginConfig{
Regexp: federation.FederationServiceCELPluginWasmConfig{
Reader: bytes.NewReader(wasm),
Sha256: toSha256(wasm),
},
},
Logger: logger,
})
if err != nil {
t.Fatal(err)
}
federation.RegisterFederationServiceServer(grpcServer, federationServer)
go func() {
if err := grpcServer.Serve(listener); err != nil {
t.Fatal(err)
}
}()
client := federation.NewFederationServiceClient(conn)
t.Run("success", func(t *testing.T) {
res, err := client.IsMatch(ctx, &federation.IsMatchRequest{
Expr: "hello world",
Target: "hello world world",
})
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(res, &federation.IsMatchResponse{
Result: true,
}, cmpopts.IgnoreUnexported(
federation.IsMatchResponse{},
)); diff != "" {
t.Errorf("(-got, +want)\n%s", diff)
}
})
t.Run("failure", func(t *testing.T) {
_, err := client.IsMatch(ctx, &federation.IsMatchRequest{
Expr: "[]",
Target: "hello world world",
})
if err == nil {
t.Fatal("expected error")
}
t.Logf("expected error is %s", err)
})
t.Run("example", func(t *testing.T) {
res, err := client.Example(ctx, &federation.ExampleRequest{})
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(res, &federation.ExampleResponse{
Size: 2,
Str: "abc",
}, cmpopts.IgnoreUnexported(
federation.ExampleResponse{},
)); diff != "" {
t.Errorf("(-got, +want)\n%s", diff)
}
})
}