/
mock_service.go
201 lines (180 loc) · 7.24 KB
/
mock_service.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
package test
import (
"context"
"github.com/RHsyseng/operator-utils/pkg/logs"
oappsv1 "github.com/openshift/api/apps/v1"
buildv1 "github.com/openshift/api/build/v1"
consolev1 "github.com/openshift/api/console/v1"
oimagev1 "github.com/openshift/api/image/v1"
routev1 "github.com/openshift/api/route/v1"
imagev1 "github.com/openshift/client-go/image/clientset/versioned/typed/image/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
clientv1 "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)
var log = logs.GetLogger("operatorutils.test")
type MockPlatformService struct {
Client clientv1.Client
scheme *runtime.Scheme
CreateFunc func(ctx context.Context, obj runtime.Object, opts ...clientv1.CreateOption) error
DeleteFunc func(ctx context.Context, obj runtime.Object, opts ...clientv1.DeleteOption) error
GetFunc func(ctx context.Context, key clientv1.ObjectKey, obj runtime.Object) error
ListFunc func(ctx context.Context, list runtime.Object, opts ...clientv1.ListOption) error
UpdateFunc func(ctx context.Context, obj runtime.Object, opts ...clientv1.UpdateOption) error
UpdateStatusFunc func(ctx context.Context, obj runtime.Object) error
PatchFunc func(ctx context.Context, obj runtime.Object, patch clientv1.Patch, opts ...clientv1.PatchOption) error
DeleteAllOfFunc func(ctx context.Context, obj runtime.Object, opts ...clientv1.DeleteAllOfOption) error
GetCachedFunc func(ctx context.Context, key clientv1.ObjectKey, obj runtime.Object) error
ImageStreamTagsFunc func(namespace string) imagev1.ImageStreamTagInterface
GetSchemeFunc func() *runtime.Scheme
}
var knownTypes = map[schema.GroupVersion][]runtime.Object{
corev1.SchemeGroupVersion: {
&corev1.PersistentVolumeClaim{},
&corev1.ServiceAccount{},
&corev1.Secret{},
&corev1.Service{},
&corev1.PersistentVolumeClaimList{},
&corev1.ServiceAccountList{},
&corev1.ServiceList{}},
oappsv1.GroupVersion: {
&oappsv1.DeploymentConfig{},
&oappsv1.DeploymentConfigList{},
},
appsv1.SchemeGroupVersion: {
&appsv1.StatefulSet{},
&appsv1.StatefulSetList{},
},
routev1.GroupVersion: {
&routev1.Route{},
&routev1.RouteList{},
},
oimagev1.GroupVersion: {
&oimagev1.ImageStream{},
&oimagev1.ImageStreamList{},
},
rbacv1.SchemeGroupVersion: {
&rbacv1.Role{},
&rbacv1.RoleList{},
&rbacv1.RoleBinding{},
&rbacv1.RoleBindingList{},
},
buildv1.GroupVersion: {
&buildv1.BuildConfig{},
&buildv1.BuildConfigList{},
},
consolev1.GroupVersion: {
&consolev1.ConsoleLink{},
&consolev1.ConsoleLinkList{},
},
}
type MockPlatformServiceBuilder struct {
apiObjects []runtime.Object
extraObjects []runtime.Object
schemeBuilder *scheme.Builder
}
func NewMockPlatformServiceBuilder(schemeBuilder runtime.SchemeBuilder) *MockPlatformServiceBuilder {
builder := &scheme.Builder{
GroupVersion: corev1.SchemeGroupVersion,
SchemeBuilder: schemeBuilder,
}
return &MockPlatformServiceBuilder{schemeBuilder: builder}
}
func (builder *MockPlatformServiceBuilder) WithScheme(objs ...runtime.Object) {
builder.apiObjects = objs
}
func (builder *MockPlatformServiceBuilder) WithExtraScheme(objs ...runtime.Object) {
builder.extraObjects = objs
}
func (builder *MockPlatformServiceBuilder) Build() *MockPlatformService {
registerObjs := builder.apiObjects
registerObjs = append(registerObjs, builder.extraObjects...)
builder.schemeBuilder.Register(registerObjs...)
scheme, _ := builder.schemeBuilder.Build()
for gv, types := range knownTypes {
for _, t := range types {
scheme.AddKnownTypes(gv, t)
}
}
client := fake.NewFakeClientWithScheme(scheme)
log.Debugf("Fake client created as %v", client)
mockImageStreamTag := &MockImageStreamTag{}
return &MockPlatformService{
Client: client,
scheme: scheme,
CreateFunc: func(ctx context.Context, obj runtime.Object, opts ...clientv1.CreateOption) error {
return client.Create(ctx, obj, opts...)
},
DeleteFunc: func(ctx context.Context, obj runtime.Object, opts ...clientv1.DeleteOption) error {
return client.Delete(ctx, obj, opts...)
},
GetFunc: func(ctx context.Context, key clientv1.ObjectKey, obj runtime.Object) error {
return client.Get(ctx, key, obj)
},
ListFunc: func(ctx context.Context, list runtime.Object, opts ...clientv1.ListOption) error {
return client.List(ctx, list, opts...)
},
UpdateFunc: func(ctx context.Context, obj runtime.Object, opts ...clientv1.UpdateOption) error {
return client.Update(ctx, obj, opts...)
},
PatchFunc: func(ctx context.Context, obj runtime.Object, patch clientv1.Patch, opts ...clientv1.PatchOption) error {
return client.Patch(ctx, obj, patch, opts...)
},
DeleteAllOfFunc: func(ctx context.Context, obj runtime.Object, opts ...clientv1.DeleteAllOfOption) error {
return client.DeleteAllOf(ctx, obj, opts...)
},
UpdateStatusFunc: func(ctx context.Context, obj runtime.Object) error {
return client.Status().Update(ctx, obj)
},
GetCachedFunc: func(ctx context.Context, key clientv1.ObjectKey, obj runtime.Object) error {
return client.Get(ctx, key, obj)
},
ImageStreamTagsFunc: func(namespace string) imagev1.ImageStreamTagInterface {
return mockImageStreamTag
},
GetSchemeFunc: func() *runtime.Scheme {
return scheme
},
}
}
func (service *MockPlatformService) Create(ctx context.Context, obj runtime.Object, opts ...clientv1.CreateOption) error {
return service.CreateFunc(ctx, obj, opts...)
}
func (service *MockPlatformService) Delete(ctx context.Context, obj runtime.Object, opts ...clientv1.DeleteOption) error {
return service.DeleteFunc(ctx, obj, opts...)
}
func (service *MockPlatformService) Get(ctx context.Context, key clientv1.ObjectKey, obj runtime.Object) error {
return service.GetFunc(ctx, key, obj)
}
func (service *MockPlatformService) List(ctx context.Context, list runtime.Object, opts ...clientv1.ListOption) error {
return service.ListFunc(ctx, list, opts...)
}
func (service *MockPlatformService) Update(ctx context.Context, obj runtime.Object, opts ...clientv1.UpdateOption) error {
return service.UpdateFunc(ctx, obj, opts...)
}
func (service *MockPlatformService) UpdateStatus(ctx context.Context, obj runtime.Object) error {
return service.UpdateStatusFunc(ctx, obj)
}
func (service *MockPlatformService) Patch(ctx context.Context, obj runtime.Object, patch clientv1.Patch, opts ...clientv1.PatchOption) error {
return service.PatchFunc(ctx, obj, patch, opts...)
}
func (service *MockPlatformService) DeleteAllOf(ctx context.Context, obj runtime.Object, opts ...clientv1.DeleteAllOfOption) error {
return service.DeleteAllOfFunc(ctx, obj, opts...)
}
func (service *MockPlatformService) GetCached(ctx context.Context, key clientv1.ObjectKey, obj runtime.Object) error {
return service.GetCachedFunc(ctx, key, obj)
}
func (service *MockPlatformService) ImageStreamTags(namespace string) imagev1.ImageStreamTagInterface {
return service.ImageStreamTagsFunc(namespace)
}
func (service *MockPlatformService) GetScheme() *runtime.Scheme {
return service.GetSchemeFunc()
}
func (service *MockPlatformService) IsMockService() bool {
return true
}