/
mocks.go
239 lines (195 loc) · 6.48 KB
/
mocks.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
/*
Copyright 2019 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package mock provides fake OAM resources for use in tests.
package mock
import (
"encoding/json"
"reflect"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
"github.com/oam-dev/kubevela/apis/core.oam.dev/condition"
)
// Conditioned is a mock that implements Conditioned interface.
type Conditioned struct{ Conditions []condition.Condition }
// SetConditions sets the Conditions.
func (m *Conditioned) SetConditions(c ...condition.Condition) { m.Conditions = c }
// GetCondition get the Condition with the given ConditionType.
func (m *Conditioned) GetCondition(ct condition.ConditionType) condition.Condition {
return condition.Condition{Type: ct, Status: corev1.ConditionUnknown}
}
// ManagedResourceReferencer is a mock that implements ManagedResourceReferencer interface.
type ManagedResourceReferencer struct{ Ref *corev1.ObjectReference }
// SetResourceReference sets the ResourceReference.
func (m *ManagedResourceReferencer) SetResourceReference(r *corev1.ObjectReference) { m.Ref = r }
// GetResourceReference gets the ResourceReference.
func (m *ManagedResourceReferencer) GetResourceReference() *corev1.ObjectReference { return m.Ref }
// A WorkloadReferencer references an OAM Workload type.
type WorkloadReferencer struct{ Ref corev1.ObjectReference }
// GetWorkloadReference gets the WorkloadReference.
func (w *WorkloadReferencer) GetWorkloadReference() corev1.ObjectReference {
return w.Ref
}
// SetWorkloadReference sets the WorkloadReference.
func (w *WorkloadReferencer) SetWorkloadReference(r corev1.ObjectReference) {
w.Ref = r
}
// Object is a mock that implements Object interface.
type Object struct {
metav1.ObjectMeta
runtime.Object
}
// GetObjectKind returns schema.ObjectKind.
func (o *Object) GetObjectKind() schema.ObjectKind {
return schema.EmptyObjectKind
}
// DeepCopyObject returns a copy of the object as runtime.Object
func (o *Object) DeepCopyObject() runtime.Object {
out := &Object{}
j, err := json.Marshal(o)
if err != nil {
panic(err)
}
_ = json.Unmarshal(j, out)
return out
}
// Trait is a mock that implements Trait interface.
type Trait struct {
metav1.ObjectMeta
runtime.Object
condition.ConditionedStatus
WorkloadReferencer
}
// GetObjectKind returns schema.ObjectKind.
func (t *Trait) GetObjectKind() schema.ObjectKind {
return schema.EmptyObjectKind
}
// DeepCopyObject returns a copy of the object as runtime.Object
func (t *Trait) DeepCopyObject() runtime.Object {
out := &Trait{}
j, err := json.Marshal(t)
if err != nil {
panic(err)
}
_ = json.Unmarshal(j, out)
return out
}
// Workload is a mock that implements Workload interface.
type Workload struct {
metav1.ObjectMeta
runtime.Object
condition.ConditionedStatus
}
// GetObjectKind returns schema.ObjectKind.
func (w *Workload) GetObjectKind() schema.ObjectKind {
return schema.EmptyObjectKind
}
// DeepCopyObject returns a copy of the object as runtime.Object
func (w *Workload) DeepCopyObject() runtime.Object {
out := &Workload{}
j, err := json.Marshal(w)
if err != nil {
panic(err)
}
_ = json.Unmarshal(j, out)
return out
}
// Manager is a mock object that satisfies manager.Manager interface.
type Manager struct {
manager.Manager
Client client.Client
Scheme *runtime.Scheme
}
// GetClient returns the client.
func (m *Manager) GetClient() client.Client { return m.Client }
// GetScheme returns the scheme.
func (m *Manager) GetScheme() *runtime.Scheme { return m.Scheme }
// GetConfig returns the config for test.
func (m *Manager) GetConfig() *rest.Config {
return &rest.Config{}
}
// GV returns a mock schema.GroupVersion.
var GV = schema.GroupVersion{Group: "g", Version: "v"}
// GVK returns the mock GVK of the given object.
func GVK(o runtime.Object) schema.GroupVersionKind {
return GV.WithKind(reflect.TypeOf(o).Elem().Name())
}
// SchemeWith returns a scheme with list of `runtime.Object`s registered.
func SchemeWith(o ...runtime.Object) *runtime.Scheme {
s := runtime.NewScheme()
s.AddKnownTypes(GV, o...)
return s
}
// NotFoundErr describes NotFound Resource error
type NotFoundErr struct {
NotFoundStatus metav1.Status
}
// NewMockNotFoundErr return a mock NotFoundErr
func NewMockNotFoundErr() NotFoundErr {
return NotFoundErr{
NotFoundStatus: metav1.Status{
Reason: metav1.StatusReasonNotFound,
},
}
}
// Status returns the Status Reason
func (mock NotFoundErr) Status() metav1.Status {
return mock.NotFoundStatus
}
// Error return error info
func (mock NotFoundErr) Error() string {
return "Not Found Resource"
}
// A LocalSecretReference is a reference to a secret in the same namespace as
// the referencer.
type LocalSecretReference struct {
// Name of the secret.
Name string `json:"name"`
}
// LocalConnectionSecretWriterTo is a mock that implements LocalConnectionSecretWriterTo interface.
type LocalConnectionSecretWriterTo struct {
Ref *LocalSecretReference
}
// SetWriteConnectionSecretToReference sets the WriteConnectionSecretToReference.
func (m *LocalConnectionSecretWriterTo) SetWriteConnectionSecretToReference(r *LocalSecretReference) {
m.Ref = r
}
// GetWriteConnectionSecretToReference gets the WriteConnectionSecretToReference.
func (m *LocalConnectionSecretWriterTo) GetWriteConnectionSecretToReference() *LocalSecretReference {
return m.Ref
}
// Target is a mock that implements Target interface.
type Target struct {
metav1.ObjectMeta
ManagedResourceReferencer
LocalConnectionSecretWriterTo
condition.ConditionedStatus
}
// GetObjectKind returns schema.ObjectKind.
func (m *Target) GetObjectKind() schema.ObjectKind {
return schema.EmptyObjectKind
}
// DeepCopyObject returns a deep copy of Target as runtime.Object.
func (m *Target) DeepCopyObject() runtime.Object {
out := &Target{}
j, err := json.Marshal(m)
if err != nil {
panic(err)
}
_ = json.Unmarshal(j, out)
return out
}