-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathsdk_test.go
More file actions
472 lines (400 loc) · 14 KB
/
sdk_test.go
File metadata and controls
472 lines (400 loc) · 14 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
Copyright 2023 The Crossplane 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 function
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
grpcprometheus "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/testing/protocmp"
"github.com/crossplane/function-sdk-go/errors"
v1 "github.com/crossplane/function-sdk-go/proto/v1"
"github.com/crossplane/function-sdk-go/proto/v1beta1"
"github.com/crossplane/function-sdk-go/request"
"github.com/crossplane/function-sdk-go/resource"
"github.com/crossplane/function-sdk-go/resource/composed"
"github.com/crossplane/function-sdk-go/response"
)
var _ v1beta1.FunctionRunnerServiceServer = &BetaServer{}
var req = &v1.RunFunctionRequest{
Observed: &v1.State{
Composite: &v1.Resource{
Resource: resource.MustStructJSON(`{"spec":{"widgets":9001}}`),
},
},
}
func Example() {
// Create a response to the request passed to your RunFunction method.
rsp := response.To(req, response.DefaultTTL)
// Get the observed composite resource (XR) from the request.
oxr, _ := request.GetObservedCompositeResource(req)
// Read the desired number of widgets from our observed XR.
widgets, _ := oxr.Resource.GetInteger("spec.widgets")
// Get any existing desired composed resources from the request.
// Desired composed resources would exist if a previous Function in the
// pipeline added them.
desired, _ := request.GetDesiredComposedResources(req)
// Create a desired composed resource using unstructured data.
desired["new"] = &resource.DesiredComposed{Resource: composed.New()}
desired["new"].Resource.SetAPIVersion("example.org/v1")
desired["new"].Resource.SetKind("CoolResource")
// Set the desired composed resource's widgets to the value extracted from
// the observed XR.
desired["new"].Resource.SetInteger("spec.widgets", widgets)
// Create a desired composed resource using structured data.
// db, _ := composed.From(&v1.Instance{})
// desired["database"] = &resource.DesiredComposed{Resource: db}
// Add a label to our new desired resource, and any other.
for _, r := range desired {
r.Resource.SetLabels(map[string]string{"coolness": "high"})
}
// Set our updated desired composed resource in the response we'll return.
if err := response.SetDesiredComposedResources(rsp, desired); err != nil {
// You can set a custom status condition on the claim. This allows you to
// communicate with the user. See the link below for status condition
// guidance.
// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#typical-status-properties
response.ConditionFalse(rsp, "FunctionSuccess", "InternalError").
WithMessage("Something went wrong.").
TargetCompositeAndClaim()
// You can emit an event regarding the claim. This allows you to communicate
// with the user. Note that events should be used sparingly and are subject
// to throttling; see the issue below for more information.
// https://github.com/crossplane/crossplane/issues/5802
response.Warning(rsp, errors.New("something went wrong")).
TargetCompositeAndClaim()
} else {
response.ConditionTrue(rsp, "FunctionSuccess", "Success").
TargetCompositeAndClaim()
}
// protojson.Marshal injects random whitespace based on a hash of the
// message descriptor. The whitespace changes whenever the proto is
// regenerated (e.g. adding new fields), which breaks Example test output
// matching. Compact the JSON to strip optional whitespace so the test is
// stable across proto regenerations.
j, _ := protojson.Marshal(rsp)
var buf bytes.Buffer
json.Compact(&buf, j)
fmt.Println(buf.String())
// Output:
// {"meta":{"ttl":"60s"},"desired":{"resources":{"new":{"resource":{"apiVersion":"example.org/v1","kind":"CoolResource","metadata":{"labels":{"coolness":"high"}},"spec":{"widgets":9001}}}}},"conditions":[{"type":"FunctionSuccess","status":"STATUS_CONDITION_TRUE","reason":"Success","target":"TARGET_COMPOSITE_AND_CLAIM"}]}
}
func TestBetaServer(t *testing.T) {
type args struct {
ctx context.Context
req *v1beta1.RunFunctionRequest
}
type want struct {
rsp *v1beta1.RunFunctionResponse
err error
}
cases := map[string]struct {
reason string
wrapped v1.FunctionRunnerServiceServer
args args
want want
}{
"RunFunctionError": {
reason: "We should return any error the wrapped server encounters",
wrapped: &MockFunctionServer{err: errors.New("boom")},
args: args{
req: &v1beta1.RunFunctionRequest{
Meta: &v1beta1.RequestMeta{
Tag: "hi",
},
},
},
want: want{
err: cmpopts.AnyError,
},
},
"Success": {
reason: "We should return the response the wrapped server returns",
wrapped: &MockFunctionServer{
rsp: &v1.RunFunctionResponse{
Meta: &v1.ResponseMeta{
Tag: "hello",
},
},
},
args: args{
req: &v1beta1.RunFunctionRequest{
Meta: &v1beta1.RequestMeta{
Tag: "hi",
},
},
},
want: want{
rsp: &v1beta1.RunFunctionResponse{
Meta: &v1beta1.ResponseMeta{
Tag: "hello",
},
},
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
s := ServeBeta(tc.wrapped)
rsp, err := s.RunFunction(tc.args.ctx, tc.args.req)
if diff := cmp.Diff(tc.want.rsp, rsp, protocmp.Transform()); diff != "" {
t.Errorf("\n%s\ns.RunFunction(...): -want rsp, +got rsp:\n%s", tc.reason, diff)
}
if diff := cmp.Diff(tc.want.err, err, cmpopts.EquateErrors()); diff != "" {
t.Errorf("\n%s\ns.RunFunction(...): -want err, +got err:\n%s", tc.reason, diff)
}
})
}
}
type MockFunctionServer struct {
v1.UnimplementedFunctionRunnerServiceServer
rsp *v1.RunFunctionResponse
err error
}
func (s *MockFunctionServer) RunFunction(context.Context, *v1.RunFunctionRequest) (*v1.RunFunctionResponse, error) {
return s.rsp, s.err
}
// TestMetricsServer_WithCustomRegistryAndCustomPort verifies that metrics server starts on custom port with custom registry as input.
func TestMetricsServer_WithCustomRegistryAndCustomPort(t *testing.T) {
// Create mock server
mockServer := &MockFunctionServer{
rsp: &v1.RunFunctionResponse{
Meta: &v1.ResponseMeta{Tag: "traffic-test"},
},
}
// Get ports
grpcPort := getAvailablePort(t)
metricsPort := getAvailablePort(t)
// Start server
serverDone := make(chan error, 1)
go func() {
err := Serve(mockServer,
Listen("tcp", fmt.Sprintf(":%d", grpcPort)),
Insecure(true),
WithMetricsServer(fmt.Sprintf(":%d", metricsPort)),
WithMetricsRegistry(prometheus.NewRegistry()),
)
serverDone <- err
}()
// Wait for server to start
time.Sleep(3 * time.Second)
t.Run("MetricsServerTest On CustomPort With CustomRegistry", func(t *testing.T) {
conn, err := grpc.NewClient(fmt.Sprintf("localhost:%d", grpcPort),
grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("Failed to connect: %v", err)
}
defer conn.Close()
client := v1.NewFunctionRunnerServiceClient(conn)
// Make the request
req1 := &v1.RunFunctionRequest{
Meta: &v1.RequestMeta{Tag: "request-test"},
}
_, err = client.RunFunction(context.Background(), req1)
if err != nil {
t.Errorf("request failed: %v", err)
}
// Wait for metrics to be collected
time.Sleep(2 * time.Second)
// Verify metrics endpoint has our custom metrics
metricsURL := fmt.Sprintf("http://localhost:%d/metrics", metricsPort)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, metricsURL, nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("Failed to get metrics: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read metrics: %v", err)
}
metricsContent := string(body)
// Verify Prometheus format is working
if !strings.Contains(metricsContent, "# HELP") {
t.Error("Expected Prometheus format")
}
// Verify gRPC metrics are present
if !strings.Contains(metricsContent, "grpc_server_started_total") {
t.Error("Expected grpc_server_started_total metric to be present")
}
})
}
// TestMetricsServer_WithDefaultRegistryAndDefaultPort verifies that metrics server starts by default on :8080 with default registry with no input.
func TestMetricsServer_WithDefaultRegistryAndDefaultPort(t *testing.T) {
// Create mock server
mockServer := &MockFunctionServer{
rsp: &v1.RunFunctionResponse{
Meta: &v1.ResponseMeta{Tag: "default-metrics-test"},
},
}
// Get ports
grpcPort := getAvailablePort(t)
// Should use default metrics port 8080
metricsPort := 8080
serverDone := make(chan error, 1)
go func() {
err := Serve(mockServer,
Listen("tcp", fmt.Sprintf(":%d", grpcPort)),
Insecure(true),
)
serverDone <- err
}()
// Wait for server to start
time.Sleep(3 * time.Second)
t.Run("MetricsServerTest On DefaultPort With DefaultRegistry", func(t *testing.T) {
// Test gRPC connection
conn, err := grpc.NewClient(fmt.Sprintf("localhost:%d", grpcPort),
grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("Failed to connect: %v", err)
}
defer conn.Close()
client := v1.NewFunctionRunnerServiceClient(conn)
// Make the request
req := &v1.RunFunctionRequest{
Meta: &v1.RequestMeta{Tag: "default-metrics-test"},
}
_, err = client.RunFunction(context.Background(), req)
if err != nil {
t.Errorf("Request failed: %v", err)
}
// Wait for metrics to be collected
time.Sleep(2 * time.Second)
// Verify metrics endpoint is accessible
metricsURL := fmt.Sprintf("http://localhost:%d/metrics", metricsPort)
httpReq, err := http.NewRequestWithContext(context.Background(), http.MethodGet, metricsURL, nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
resp, err := http.DefaultClient.Do(httpReq)
if err != nil {
t.Fatalf("Failed to get metrics: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read metrics: %v", err)
}
metricsContent := string(body)
// Verify metrics are present
if !strings.Contains(metricsContent, "# HELP") {
t.Error("Expected Prometheus format")
}
// Verify gRPC metrics are present
if !strings.Contains(metricsContent, "grpc_server_started_total") {
t.Error("Expected grpc_server_started_total metric to be present")
}
})
}
// TestMetricsServer_WithCustomMetricsServerOpts verifies that metrics server uses custom metrics server opts.
func TestMetricsServer_WithCustomMetricsServerOpts(t *testing.T) {
// Create mock server
mockServer := &MockFunctionServer{
rsp: &v1.RunFunctionResponse{
Meta: &v1.ResponseMeta{Tag: "default-metrics-test"},
},
}
// Get ports
grpcPort := getAvailablePort(t)
metricsPort := getAvailablePort(t)
serverDone := make(chan error, 1)
go func() {
err := Serve(mockServer,
Listen("tcp", fmt.Sprintf(":%d", grpcPort)),
Insecure(true),
WithMetricsServer(fmt.Sprintf(":%d", metricsPort)),
WithMetricsRegistry(prometheus.NewRegistry()),
WithMetricsServerOpts(
grpcprometheus.WithServerHandlingTimeHistogram(),
),
)
serverDone <- err
}()
// Wait for server to start
time.Sleep(3 * time.Second)
t.Run("MetricsServerTest with custom metrics server opts", func(t *testing.T) {
// Test gRPC connection
conn, err := grpc.NewClient(fmt.Sprintf("localhost:%d", grpcPort),
grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("Failed to connect: %v", err)
}
defer conn.Close()
client := v1.NewFunctionRunnerServiceClient(conn)
// Make the request
req := &v1.RunFunctionRequest{
Meta: &v1.RequestMeta{Tag: "default-metrics-test"},
}
_, err = client.RunFunction(context.Background(), req)
if err != nil {
t.Errorf("Request failed: %v", err)
}
// Wait for metrics to be collected
time.Sleep(2 * time.Second)
// Verify metrics endpoint is accessible
metricsURL := fmt.Sprintf("http://localhost:%d/metrics", metricsPort)
httpReq, err := http.NewRequestWithContext(context.Background(), http.MethodGet, metricsURL, nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
resp, err := http.DefaultClient.Do(httpReq)
if err != nil {
t.Fatalf("Failed to get metrics: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read metrics: %v", err)
}
metricsContent := string(body)
// Verify metrics are present
if !strings.Contains(metricsContent, "# HELP") {
t.Error("Expected Prometheus format")
}
// Verify gRPC metrics are present
if !strings.Contains(metricsContent, "grpc_server_started_total") {
t.Error("Expected grpc_server_started_total metric to be present")
}
// Verify gRPC Histogram metrics are present
if !strings.Contains(metricsContent, "grpc_server_handling_seconds_bucket") {
t.Error("Expected grpc_server_handling_seconds_bucket metric to be present")
}
})
}
// Helper function to get an available port.
func getAvailablePort(t *testing.T) int {
t.Helper()
listenConfig := &net.ListenConfig{}
listener, err := listenConfig.Listen(context.Background(), "tcp", ":0")
if err != nil {
t.Fatalf("Failed to get available port: %v", err)
}
defer listener.Close()
return listener.Addr().(*net.TCPAddr).Port
}