-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmock.go
204 lines (168 loc) · 5.61 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
// SPDX-License-Identifier: Apache-2.0
package kubernetes
// Everything in this file should only be used in test code.
// It is exported for use in tests of other packages.
import (
"context"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/cache"
velav1alpha1 "github.com/go-vela/worker/runtime/kubernetes/apis/vela/v1alpha1"
fakeVelaK8sClient "github.com/go-vela/worker/runtime/kubernetes/generated/clientset/versioned/fake"
)
// NewMock returns an Engine implementation that
// integrates with a Kubernetes runtime.
//
// This function is intended for running tests only.
//
//nolint:revive // ignore returning unexported client
func NewMock(_pod *v1.Pod, opts ...ClientOpt) (*client, error) {
// create new Kubernetes client
c := new(client)
// create new fields
c.config = new(config)
c.Pod = new(v1.Pod)
c.containersLookup = map[string]int{}
for i, ctn := range _pod.Spec.Containers {
c.containersLookup[ctn.Name] = i
}
// create new logger for the client
//
// https://pkg.go.dev/github.com/sirupsen/logrus#StandardLogger
logger := logrus.StandardLogger()
// create new logger for the client
//
// https://pkg.go.dev/github.com/sirupsen/logrus#NewEntry
c.Logger = logrus.NewEntry(logger)
// set the Kubernetes namespace in the runtime client
c.config.Namespace = "test"
// set the Kubernetes pod in the runtime client
c.Pod = _pod.DeepCopy()
c.Pod.SetResourceVersion("0")
// apply all provided configuration options
for _, opt := range opts {
err := opt(c)
if err != nil {
return nil, err
}
}
// set the Kubernetes fake client in the runtime client
//
// https://pkg.go.dev/k8s.io/client-go/kubernetes/fake#NewSimpleClientset
c.Kubernetes = fake.NewSimpleClientset(c.Pod)
// set the VelaKubernetes fake client in the runtime client
c.VelaKubernetes = fakeVelaK8sClient.NewSimpleClientset(
&velav1alpha1.PipelinePodsTemplate{
ObjectMeta: metav1.ObjectMeta{
Namespace: c.config.Namespace,
Name: "mock-pipeline-pods-template",
},
},
)
// set the PodTracker (normally populated in SetupBuild)
tracker, err := mockPodTracker(c.Logger, c.Kubernetes, c.Pod)
if err != nil {
return c, err
}
c.PodTracker = tracker
// The test is responsible for calling c.PodTracker.Start(ctx) if needed.
// In some cases it is more convenient to call c.(MockKubernetesRuntime).StartPodTracker(ctx)
return c, nil
}
// MockKubernetesRuntime makes it possible to use the client mocks in other packages.
//
// This interface is intended for running tests only.
type MockKubernetesRuntime interface {
SetupMock() error
MarkPodTrackerReady()
StartPodTracker(context.Context)
WaitForPodTrackerReady()
WaitForPodCreate(string, string)
SimulateResync(*v1.Pod)
SimulateStatusUpdate(*v1.Pod, []v1.ContainerStatus) error
}
// SetupMock allows the Kubernetes runtime to perform additional Mock-related config.
// Many tests should call this right after they call runtime.SetupBuild (or executor.CreateBuild).
//
// This function is intended for running tests only.
func (c *client) SetupMock() error {
// This assumes that c.Pod.ObjectMeta.Namespace and c.Pod.ObjectMeta.Name are filled in.
return c.PodTracker.setupMockFor(c.Pod)
}
// MarkPodTrackerReady signals that PodTracker has been setup with ContainerTrackers.
//
// This function is intended for running tests only.
func (c *client) MarkPodTrackerReady() {
close(c.PodTracker.Ready)
}
// StartPodTracker tells the podTracker it can start populating the cache.
//
// This function is intended for running tests only.
func (c *client) StartPodTracker(ctx context.Context) {
c.PodTracker.Start(ctx)
}
// WaitForPodTrackerReady waits for PodTracker.Ready to be closed (which happens in AssembleBuild).
//
// This function is intended for running tests only.
func (c *client) WaitForPodTrackerReady() {
<-c.PodTracker.Ready
}
// WaitForPodCreate waits for PodTracker.Ready to be closed (which happens in AssembleBuild).
//
// This function is intended for running tests only.
func (c *client) WaitForPodCreate(namespace, name string) {
created := make(chan struct{})
c.PodTracker.podInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
select {
case <-created:
// not interested in any other create events.
return
default:
break
}
var (
pod *v1.Pod
ok bool
)
if pod, ok = obj.(*v1.Pod); !ok {
return
}
if pod.GetNamespace() == namespace && pod.GetName() == name {
close(created)
}
},
})
<-created
}
// SimulateResync simulates an resync where the PodTracker refreshes its cache.
// This resync is from oldPod to runtime.Pod. If nil, oldPod defaults to runtime.Pod.
//
// This function is intended for running tests only.
func (c *client) SimulateResync(oldPod *v1.Pod) {
if oldPod == nil {
oldPod = c.Pod
}
oldPod = oldPod.DeepCopy()
oldPod.SetResourceVersion("older")
// simulate a re-sync/PodUpdate event
c.PodTracker.HandlePodUpdate(oldPod, c.Pod)
}
// SimulateUpdate simulates an update event from the k8s API.
//
// This function is intended for running tests only.
func (c *client) SimulateStatusUpdate(pod *v1.Pod, containerStatuses []v1.ContainerStatus) error {
// We have to have a full copy here because the k8s client Mock
// replaces the pod it is storing, it does not just update the status.
updatedPod := pod.DeepCopy()
updatedPod.Status.ContainerStatuses = containerStatuses
_, err := c.Kubernetes.CoreV1().Pods(pod.GetNamespace()).
UpdateStatus(
context.Background(),
updatedPod,
metav1.UpdateOptions{},
)
return err
}