-
Notifications
You must be signed in to change notification settings - Fork 568
/
testing.go
187 lines (158 loc) · 5.52 KB
/
testing.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
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
package serviceenv
import (
"context"
"github.com/pachyderm/pachyderm/v2/src/identity"
"github.com/pachyderm/pachyderm/v2/src/internal/client"
col "github.com/pachyderm/pachyderm/v2/src/internal/collection"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/pachconfig"
"github.com/pachyderm/pachyderm/v2/src/internal/pachsql"
"github.com/pachyderm/pachyderm/v2/src/internal/task"
auth_server "github.com/pachyderm/pachyderm/v2/src/server/auth"
enterprise_server "github.com/pachyderm/pachyderm/v2/src/server/enterprise"
pfs_server "github.com/pachyderm/pachyderm/v2/src/server/pfs"
pps_server "github.com/pachyderm/pachyderm/v2/src/server/pps"
dex_storage "github.com/dexidp/dex/storage"
loki "github.com/pachyderm/pachyderm/v2/src/internal/lokiutil/client"
etcd "go.etcd.io/etcd/client/v3"
"golang.org/x/sync/errgroup"
"k8s.io/client-go/dynamic"
kube "k8s.io/client-go/kubernetes"
)
// TestServiceEnv is a simple implementation of ServiceEnv that can be constructed with
// existing clients.
type TestServiceEnv struct {
Configuration *pachconfig.Configuration
PachClient *client.APIClient
EtcdClient *etcd.Client
KubeClient kube.Interface
DynamicKubeClient dynamic.Interface
LokiClient *loki.Client
DBClient, DirectDBClient *pachsql.DB
PostgresListener col.PostgresListener
DexDB dex_storage.Storage
Ctx context.Context
// Auth is the registered auth APIServer
Auth auth_server.APIServer
// Identity is the registered auth APIServer
Identity identity.APIServer
// Pps is the registered pps APIServer
Pps pps_server.APIServer
// Pfs is the registered pfs APIServer
Pfs pfs_server.APIServer
// Enterprise is the registered pfs APIServer
Enterprise enterprise_server.APIServer
// Ready is a channel that blocks `GetPachClient` until it's closed.
// This avoids a race when we need to instantiate the server before
// getting a client pointing at the same server.
Ready chan interface{}
}
func (s *TestServiceEnv) Config() *pachconfig.Configuration {
return s.Configuration
}
func (s *TestServiceEnv) GetPachClient(ctx context.Context) *client.APIClient {
<-s.Ready
return s.PachClient.WithCtx(ctx)
}
func (s *TestServiceEnv) GetEtcdClient() *etcd.Client {
return s.EtcdClient
}
func (s *TestServiceEnv) GetTaskService(prefix string) task.Service {
return task.NewEtcdService(s.EtcdClient, prefix)
}
func (s *TestServiceEnv) GetKubeClient() kube.Interface {
return s.KubeClient
}
func (s *TestServiceEnv) GetDynamicKubeClient() dynamic.Interface {
return s.DynamicKubeClient
}
func (s *TestServiceEnv) GetLokiClient() (*loki.Client, error) {
return s.LokiClient, nil
}
func (s *TestServiceEnv) GetDBClient() *pachsql.DB {
return s.DBClient
}
func (s *TestServiceEnv) GetDirectDBClient() *pachsql.DB {
return s.DirectDBClient
}
func (s *TestServiceEnv) GetPostgresListener() col.PostgresListener {
return s.PostgresListener
}
func (s *TestServiceEnv) Context() context.Context {
return s.Ctx
}
func (s *TestServiceEnv) ClusterID() string {
return "testing"
}
func (s *TestServiceEnv) GetDexDB() dex_storage.Storage {
return s.DexDB
}
func (s *TestServiceEnv) Close() error {
eg := &errgroup.Group{}
if client := s.GetPachClient(context.Background()); client != nil {
eg.Go(client.Close)
}
if client := s.GetEtcdClient(); client != nil {
eg.Go(client.Close)
}
if client := s.GetDBClient(); client != nil {
eg.Go(client.Close)
}
if client := s.GetDexDB(); client != nil {
eg.Go(client.Close)
}
if listener := s.GetPostgresListener(); listener != nil {
eg.Go(listener.Close)
}
return errors.EnsureStack(eg.Wait())
}
// AuthServer returns the registered Auth APIServer
func (env *TestServiceEnv) AuthServer() auth_server.APIServer {
return env.Auth
}
// IdentityServer returns the registered Identity APIServer
func (env *TestServiceEnv) IdentityServer() identity.APIServer {
return env.Identity
}
// PpsServer returns the registered PPS APIServer
func (env *TestServiceEnv) PpsServer() pps_server.APIServer {
return env.Pps
}
// PfsServer returns the registered PFS APIServer
func (env *TestServiceEnv) PfsServer() pfs_server.APIServer {
return env.Pfs
}
// SetAuthServer returns the registered Auth APIServer
func (env *TestServiceEnv) SetAuthServer(s auth_server.APIServer) {
env.Auth = s
}
// SetIdentityServer returns the registered Identity APIServer
func (env *TestServiceEnv) SetIdentityServer(s identity.APIServer) {
env.Identity = s
}
// SetPpsServer returns the registered PPS APIServer
func (env *TestServiceEnv) SetPpsServer(s pps_server.APIServer) {
env.Pps = s
}
// SetPfsServer returns the registered PFS APIServer
func (env *TestServiceEnv) SetPfsServer(s pfs_server.APIServer) {
env.Pfs = s
}
// EnterpriseServer returns the registered Enterprise APIServer
func (env *TestServiceEnv) EnterpriseServer() enterprise_server.APIServer {
return env.Enterprise
}
// SetEnterpriseServer returns the registered Enterprise APIServer
func (env *TestServiceEnv) SetEnterpriseServer(s enterprise_server.APIServer) {
env.Enterprise = s
}
// SetKubeClient can be used to override the kubeclient in testing.
func (env *TestServiceEnv) SetKubeClient(s kube.Interface) {
env.KubeClient = s
}
// SetDynamicKubeClient can be used to override the dynamic kubeclient in testing.
func (env *TestServiceEnv) SetDynamicKubeClient(s dynamic.Interface) {
env.DynamicKubeClient = s
}
// InitDexDB implements the ServiceEnv interface.
func (end *TestServiceEnv) InitDexDB() {}