-
Notifications
You must be signed in to change notification settings - Fork 9
/
do.go
80 lines (66 loc) · 2.06 KB
/
do.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package testing
import (
"context"
"flag"
"fmt"
"log"
"net"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"namespacelabs.dev/foundation/internal/testing/testboot"
"namespacelabs.dev/foundation/schema"
)
var (
testTimeout = flag.Duration("test_timeout", 5*time.Minute, "The maximum duration of the test.")
debug = flag.Bool("debug", false, "Output additional test runtime information.")
)
type Test struct {
testboot.TestData
}
func (t Test) Connect(ctx context.Context, endpoint *schema.Endpoint) (*grpc.ClientConn, error) {
return grpc.DialContext(ctx, endpoint.Address(),
grpc.WithBlock(),
grpc.WithTransportCredentials(insecure.NewCredentials())) /// XXX mTLS etc.
}
func (t Test) WaitForEndpoint(ctx context.Context, endpoint *schema.Endpoint) error {
ctx, done := context.WithTimeout(ctx, 30*time.Second)
defer done()
for {
if err := ctx.Err(); err != nil {
return fmt.Errorf("endpoint not ready: %v", err)
}
conn, err := net.DialTimeout("tcp", endpoint.Address(), 500*time.Millisecond)
if err == nil {
conn.Close()
return nil
}
}
}
func (t Test) MustEndpoint(owner, name string) *schema.Endpoint {
for _, endpoint := range t.Request.Endpoint {
if endpoint.EndpointOwner == owner && endpoint.ServiceName == name {
return endpoint
}
}
log.Fatalf("Expected endpoint to be present in the stack: endpoint_owner=%q service_name=%q", owner, name)
return nil
}
func (t Test) InternalOf(serverOwner string) []*schema.InternalEndpoint {
var filtered []*schema.InternalEndpoint
for _, ie := range t.Request.InternalEndpoint {
if ie.ServerOwner == serverOwner {
filtered = append(filtered, ie)
}
}
return filtered
}
func Do(testFunc func(context.Context, Test) error) {
t := testboot.BootstrapTest(*testTimeout, *debug)
if err := testFunc(context.Background(), Test{t}); err != nil {
log.Fatal(err)
}
}