-
Notifications
You must be signed in to change notification settings - Fork 474
/
mock.go
283 lines (243 loc) · 8.53 KB
/
mock.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
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
package kubetest
import (
"sync"
"github.com/go-jose/go-jose/jwt"
osapps_v1 "github.com/openshift/api/apps/v1"
"github.com/stretchr/testify/mock"
istio_fake "istio.io/client-go/pkg/clientset/versioned/fake"
apps_v1 "k8s.io/api/apps/v1"
batch_v1 "k8s.io/api/batch/v1"
core_v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/tools/clientcmd/api"
gatewayapifake "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned/fake"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/kubernetes"
)
//// Mock for the K8SClientFactory
type K8SClientFactoryMock struct {
conf *config.Config
lock sync.RWMutex
Clients map[string]kubernetes.ClientInterface
}
// Interface guard to ensure K8SClientFactoryMock implements ClientFactory.
var _ kubernetes.ClientFactory = &K8SClientFactoryMock{}
// Constructor
// Deprecated: use NewFakeClientFactory instead since it doesn't rely on the global config.Get()
func NewK8SClientFactoryMock(k8s kubernetes.ClientInterface) *K8SClientFactoryMock {
conf := config.Get()
clients := map[string]kubernetes.ClientInterface{conf.KubernetesConfig.ClusterName: k8s}
return NewFakeClientFactory(conf, clients)
}
func NewFakeClientFactory(conf *config.Config, clients map[string]kubernetes.ClientInterface) *K8SClientFactoryMock {
return &K8SClientFactoryMock{conf: conf, Clients: clients}
}
// Testing specific methods
func (o *K8SClientFactoryMock) SetClients(clients map[string]kubernetes.ClientInterface) {
o.lock.Lock()
defer o.lock.Unlock()
o.Clients = clients
}
// Business Methods
func (o *K8SClientFactoryMock) GetClient(authInfo *api.AuthInfo, cluster string) (kubernetes.ClientInterface, error) {
o.lock.RLock()
defer o.lock.RUnlock()
return o.Clients[cluster], nil
}
// Business Methods
func (o *K8SClientFactoryMock) GetClients(authInfo *api.AuthInfo) (map[string]kubernetes.ClientInterface, error) {
o.lock.RLock()
defer o.lock.RUnlock()
return o.Clients, nil
}
func (o *K8SClientFactoryMock) GetSAClient(cluster string) kubernetes.ClientInterface {
o.lock.RLock()
defer o.lock.RUnlock()
return o.Clients[cluster]
}
func (o *K8SClientFactoryMock) GetSAClients() map[string]kubernetes.ClientInterface {
o.lock.RLock()
defer o.lock.RUnlock()
return o.Clients
}
func (o *K8SClientFactoryMock) GetSAHomeClusterClient() kubernetes.ClientInterface {
o.lock.RLock()
defer o.lock.RUnlock()
return o.Clients[o.conf.KubernetesConfig.ClusterName]
}
/////
type K8SClientMock struct {
mock.Mock
istioClientset *istio_fake.Clientset
gatewayapiClientSet *gatewayapifake.Clientset
}
// Interface guard to ensure K8SClientMock implements ClientInterface.
var _ kubernetes.ClientInterface = &K8SClientMock{}
// Constructor
func NewK8SClientMock() *K8SClientMock {
k8s := new(K8SClientMock)
k8s.On("IsOpenShift").Return(true)
k8s.On("IsExpGatewayAPI").Return(false)
k8s.On("IsGatewayAPI").Return(false)
k8s.On("IsIstioAPI").Return(true)
k8s.On("GetKialiTokenForHomeCluster").Return("", "")
return k8s
}
// Business methods
// MockEmptyWorkloads setup the current mock to return empty workloads for every type of workloads (deployment, dc, rs, jobs, etc.)
func (o *K8SClientMock) MockEmptyWorkloads(namespace interface{}) {
o.On("GetDeployments", namespace).Return([]apps_v1.Deployment{}, nil)
o.On("GetReplicaSets", namespace).Return([]apps_v1.ReplicaSet{}, nil)
o.On("GetReplicationControllers", namespace).Return([]core_v1.ReplicationController{}, nil)
o.On("GetDeploymentConfigs", namespace).Return([]osapps_v1.DeploymentConfig{}, nil)
o.On("GetStatefulSets", namespace).Return([]apps_v1.StatefulSet{}, nil)
o.On("GetDaemonSets", namespace).Return([]apps_v1.DaemonSet{}, nil)
o.On("GetJobs", namespace).Return([]batch_v1.Job{}, nil)
o.On("GetCronJobs", namespace).Return([]batch_v1.CronJob{}, nil)
}
// MockEmptyWorkload setup the current mock to return an empty workload for every type of workloads (deployment, dc, rs, jobs, etc.)
func (o *K8SClientMock) MockEmptyWorkload(namespace interface{}, workload interface{}) {
gr := schema.GroupResource{
Group: "test-group",
Resource: "test-resource",
}
notfound := errors.NewNotFound(gr, "not found")
o.On("GetDeployment", namespace, workload).Return(&apps_v1.Deployment{}, notfound)
o.On("GetStatefulSet", namespace, workload).Return(&apps_v1.StatefulSet{}, notfound)
o.On("GetDaemonSet", namespace, workload).Return(&apps_v1.DaemonSet{}, notfound)
o.On("GetDeploymentConfig", namespace, workload).Return(&osapps_v1.DeploymentConfig{}, notfound)
o.On("GetReplicaSets", namespace).Return([]apps_v1.ReplicaSet{}, nil)
o.On("GetReplicationControllers", namespace).Return([]core_v1.ReplicationController{}, nil)
o.On("GetJobs", namespace).Return([]batch_v1.Job{}, nil)
o.On("GetCronJobs", namespace).Return([]batch_v1.CronJob{}, nil)
}
func (o *K8SClientMock) IsOpenShift() bool {
args := o.Called()
return args.Get(0).(bool)
}
func (o *K8SClientMock) IsExpGatewayAPI() bool {
args := o.Called()
return args.Get(0).(bool)
}
func (o *K8SClientMock) IsGatewayAPI() bool {
args := o.Called()
return args.Get(0).(bool)
}
func (o *K8SClientMock) IsIstioAPI() bool {
args := o.Called()
return args.Get(0).(bool)
}
func (o *K8SClientMock) GetServerVersion() (*version.Info, error) {
args := o.Called()
return args.Get(0).(*version.Info), args.Error(1)
}
func (o *K8SClientMock) GetToken() string {
args := o.Called()
return args.Get(0).(string)
}
func (o *K8SClientMock) ClusterInfo() kubernetes.ClusterInfo {
args := o.Called()
return args.Get(0).(kubernetes.ClusterInfo)
}
// GetTokenSubject returns the subject of the authInfo using
// the TokenReview api
func (o *K8SClientMock) GetTokenSubject(authInfo *api.AuthInfo) (string, error) {
parsedToken, err := jwt.ParseSigned(authInfo.Token)
if err != nil {
return authInfo.Token, nil
}
var claims map[string]interface{} // generic map to store parsed token
err = parsedToken.UnsafeClaimsWithoutVerification(&claims)
if err != nil {
return authInfo.Token, nil
}
if sub, ok := claims["sub"]; ok {
return sub.(string), nil
}
return authInfo.Token, nil
}
func (o *K8SClientMock) ForwardGetRequest(namespace, podName string, destinationPort int, path string) ([]byte, error) {
args := o.Called(namespace, podName, destinationPort, path)
return args.Get(0).([]byte), args.Error(1)
}
func (o *K8SClientMock) MockService(namespace, name string) {
s := FakeService(namespace, name)
o.On("GetService", namespace, name).Return(&s, nil)
}
func (o *K8SClientMock) MockServices(namespace string, names []string) {
services := []core_v1.Service{}
for _, name := range names {
services = append(services, FakeService(namespace, name))
}
o.On("GetServices", namespace, mock.AnythingOfType("map[string]string")).Return(services, nil)
o.On("GetDeployments", mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return([]apps_v1.Deployment{}, nil)
}
func FakeService(namespace, name string) core_v1.Service {
return core_v1.Service{
ObjectMeta: meta_v1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
"app": name,
},
},
Spec: core_v1.ServiceSpec{
ClusterIP: "fromservice",
Type: "ClusterIP",
Selector: map[string]string{"app": name},
Ports: []core_v1.ServicePort{
{
Name: "http",
Protocol: "TCP",
Port: 3001,
},
{
Name: "http",
Protocol: "TCP",
Port: 3000,
},
},
},
}
}
func FakePodList() []core_v1.Pod {
return []core_v1.Pod{
{
ObjectMeta: meta_v1.ObjectMeta{
Name: "reviews-v1",
Namespace: "ns",
Labels: map[string]string{"app": "reviews", "version": "v1"},
Annotations: FakeIstioAnnotations(),
},
},
{
ObjectMeta: meta_v1.ObjectMeta{
Name: "reviews-v2",
Namespace: "ns",
Labels: map[string]string{"app": "reviews", "version": "v2"},
Annotations: FakeIstioAnnotations(),
},
},
{
ObjectMeta: meta_v1.ObjectMeta{
Name: "httpbin-v1",
Namespace: "ns",
Labels: map[string]string{"app": "httpbin", "version": "v1"},
Annotations: FakeIstioAnnotations(),
},
},
}
}
func FakeIstioAnnotations() map[string]string {
return map[string]string{"sidecar.istio.io/status": "{\"version\":\"\",\"initContainers\":[\"istio-init\",\"enable-core-dump\"],\"containers\":[\"istio-proxy\"],\"volumes\":[\"istio-envoy\",\"istio-certs\"]}"}
}
func FakeNamespace(name string) *core_v1.Namespace {
return &core_v1.Namespace{
ObjectMeta: meta_v1.ObjectMeta{
Name: name,
},
}
}