This repository has been archived by the owner on Jun 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfake.go
142 lines (113 loc) · 5.08 KB
/
fake.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
/*
Copyright 2018 The GitLab-Controller 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 test
import (
"context"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var _ client.Client = &MockClient{}
// A MockGetFn is used to mock client.Client's Get implementation.
type MockGetFn func(ctx context.Context, key client.ObjectKey, obj runtime.Object) error
// A MockListFn is used to mock client.Client's List implementation.
type MockListFn func(ctx context.Context, opts *client.ListOptions, list runtime.Object) error
// A MockCreateFn is used to mock client.Client's Create implementation.
type MockCreateFn func(ctx context.Context, obj runtime.Object) error
// A MockDeleteFn is used to mock client.Client's Delete implementation.
type MockDeleteFn func(ctx context.Context, obj runtime.Object, opts ...client.DeleteOptionFunc) error
// A MockUpdateFn is used to mock client.Client's Update implementation.
type MockUpdateFn func(ctx context.Context, obj runtime.Object) error
// A MockStatusUpdateFn is used to mock client.Client's StatusUpdate implementation.
type MockStatusUpdateFn func(ctx context.Context, obj runtime.Object) error
// NewMockGetFn returns a MockGetFn that returns the supplied error.
func NewMockGetFn(err error) MockGetFn {
return func(_ context.Context, _ client.ObjectKey, _ runtime.Object) error { return err }
}
// NewMockListFn returns a MockListFn that returns the supplied error.
func NewMockListFn(err error) MockListFn {
return func(_ context.Context, _ *client.ListOptions, _ runtime.Object) error { return err }
}
// NewMockCreateFn returns a MockCreateFn that returns the supplied error.
func NewMockCreateFn(err error) MockCreateFn {
return func(_ context.Context, _ runtime.Object) error { return err }
}
// NewMockDeleteFn returns a MockDeleteFn that returns the supplied error.
func NewMockDeleteFn(err error) MockDeleteFn {
return func(_ context.Context, _ runtime.Object, _ ...client.DeleteOptionFunc) error { return err }
}
// NewMockUpdateFn returns a MockUpdateFn that returns the supplied error.
func NewMockUpdateFn(err error) MockUpdateFn {
return func(_ context.Context, _ runtime.Object) error { return err }
}
// NewMockStatusUpdateFn returns a MockStatusUpdateFn that returns the supplied error.
func NewMockStatusUpdateFn(err error) MockStatusUpdateFn {
return func(_ context.Context, _ runtime.Object) error { return err }
}
// MockClient implements controller-runtime's Client interface, allowing each
// method to be overridden for testing. The controller-runtime provides a fake
// client, but it is has surprising side effects (e.g. silently calling
// os.Exit(1)) and does not allow us control over the errors it returns.
type MockClient struct {
MockGet MockGetFn
MockList MockListFn
MockCreate MockCreateFn
MockDelete MockDeleteFn
MockUpdate MockUpdateFn
MockStatusUpdate MockStatusUpdateFn
}
// NewMockClient returns a MockClient that does nothing when its methods are
// called.
func NewMockClient() *MockClient {
return &MockClient{
MockGet: NewMockGetFn(nil),
MockList: NewMockListFn(nil),
MockCreate: NewMockCreateFn(nil),
MockDelete: NewMockDeleteFn(nil),
MockUpdate: NewMockUpdateFn(nil),
MockStatusUpdate: NewMockStatusUpdateFn(nil),
}
}
// Get calls MockClient's MockGet function.
func (c *MockClient) Get(ctx context.Context, key client.ObjectKey, obj runtime.Object) error {
return c.MockGet(ctx, key, obj)
}
// List calls MockClient's MockList function.
func (c *MockClient) List(ctx context.Context, opts *client.ListOptions, list runtime.Object) error {
return c.MockList(ctx, opts, list)
}
// Create calls MockClient's MockCreate function.
func (c *MockClient) Create(ctx context.Context, obj runtime.Object) error {
return c.MockCreate(ctx, obj)
}
// Delete calls MockClient's MockDelete function.
func (c *MockClient) Delete(ctx context.Context, obj runtime.Object, opts ...client.DeleteOptionFunc) error {
return c.MockDelete(ctx, obj, opts...)
}
// Update calls MockClient's MockUpdate function.
func (c *MockClient) Update(ctx context.Context, obj runtime.Object) error {
return c.MockUpdate(ctx, obj)
}
// Status returns status writer for status sub-resource
func (c *MockClient) Status() client.StatusWriter {
return &MockStatusWriter{
MockUpdate: c.MockStatusUpdate,
}
}
// MockStatusWriter provides mock functionality for status sub-resource
type MockStatusWriter struct {
MockUpdate MockStatusUpdateFn
}
// Update status sub-resource
func (m *MockStatusWriter) Update(ctx context.Context, obj runtime.Object) error {
return m.MockUpdate(ctx, obj)
}