-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.go
63 lines (52 loc) · 1.53 KB
/
cluster.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
package gcp
import (
"context"
"fmt"
"net"
container "cloud.google.com/go/container/apiv1"
"cloud.google.com/go/container/apiv1/containerpb"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type mockClusterServer struct {
containerpb.ClusterManagerServer
}
func setupMockClusterServer() (option.ClientOption, error) {
srv := &mockClusterServer{}
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
return nil, fmt.Errorf("create mock cluster server for test: %w", err)
}
gsrv := grpc.NewServer()
containerpb.RegisterClusterManagerServer(gsrv, srv)
go func() {
if err := gsrv.Serve(l); err != nil {
panic(err)
}
}()
conn, err := grpc.NewClient(l.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, fmt.Errorf("create gRPC client for test: %w", err)
}
return option.WithGRPCConn(conn), nil
}
func NewClusterMockClient() (*container.ClusterManagerClient, error) {
// Should be fine to setup ephemeral server per client as only used for tests
clientOpt, err := setupMockClusterServer()
if err != nil {
return nil, err
}
client, err := container.NewClusterManagerClient(context.Background(), clientOpt)
if err != nil {
return nil, err
}
return client, nil
}
func (f *mockClusterServer) GetCluster(ctx context.Context, req *containerpb.GetClusterRequest) (*containerpb.Cluster, error) {
resp := &containerpb.Cluster{
Name: "gcp-predev-1234",
Locations: []string{"us-west-2"},
}
return resp, nil
}