• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubevirt / kubevirt / 3d853798-7ba7-487a-b6a2-97f4a0664cc6

18 Jun 2025 10:06PM UTC coverage: 71.186% (-0.06%) from 71.245%
3d853798-7ba7-487a-b6a2-97f4a0664cc6

push

prow

web-flow
Merge pull request #13764 from xpivarc/say-no-more-pdbs-2

No more PDBs #2

3 of 3 new or added lines in 2 files covered. (100.0%)

51 existing lines in 3 files now uncovered.

66345 of 93200 relevant lines covered (71.19%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.15
/pkg/testutils/mock_queue.go
1
package testutils
2

3
import (
4
        "sync"
5
        "sync/atomic"
6
        "time"
7

8
        k8sv1 "k8s.io/api/core/v1"
9
        policyv1 "k8s.io/api/policy/v1"
10
        "k8s.io/apimachinery/pkg/runtime"
11
        "k8s.io/client-go/tools/cache"
12
        framework "k8s.io/client-go/tools/cache/testing"
13
        "k8s.io/client-go/util/workqueue"
14

15
        v1 "kubevirt.io/api/core/v1"
16

17
        "kubevirt.io/kubevirt/pkg/virt-launcher/virtwrap/api"
18
)
19

20
/*
21
MockWorkQueue is a helper workqueue which can be wrapped around
22
any RateLimitingInterface implementing queue. This allows synchronous
23
testing of the controller. The typical pattern is:
24

25
        MockQueue.ExpectAdd(3)
26
        vmiSource.Add(vmi)
27
        vmiSource.Add(vmi1)
28
        vmiSource.Add(vmi2)
29
        MockQueue.Wait()
30

31
This ensures that Source callbacks which are listening on vmiSource
32
enqueued three times an object. Since enqueing is typically the last
33
action in listener callbacks, we can assume that the wanted scenario for
34
a controller is set up, and an execution will process this scenario.
35
*/
36
type MockWorkQueue[T comparable] struct {
37
        workqueue.TypedRateLimitingInterface[T]
38
        addWG            *sync.WaitGroup
39
        rateLimitedEnque int32
40
        addAfterEnque    int32
41
        wgLock           sync.Mutex
42
}
43

44
func (q *MockWorkQueue[T]) Add(item T) {
1✔
45
        q.TypedRateLimitingInterface.Add(item)
1✔
46
        q.wgLock.Lock()
1✔
47
        defer q.wgLock.Unlock()
1✔
48
        if q.addWG != nil {
2✔
49
                q.addWG.Done()
1✔
50
        }
1✔
51
}
52

53
func (q *MockWorkQueue[T]) AddRateLimited(item T) {
1✔
54
        q.TypedRateLimitingInterface.AddRateLimited(item)
1✔
55
        atomic.AddInt32(&q.rateLimitedEnque, 1)
1✔
56
}
1✔
57

58
func (q *MockWorkQueue[T]) AddAfter(item T, duration time.Duration) {
1✔
59
        q.TypedRateLimitingInterface.AddAfter(item, duration)
1✔
60
        atomic.AddInt32(&q.addAfterEnque, 1)
1✔
61
        q.wgLock.Lock()
1✔
62
        defer q.wgLock.Unlock()
1✔
63
        if q.addWG != nil {
2✔
64
                q.addWG.Done()
1✔
65
        }
1✔
66
}
67

68
func (q *MockWorkQueue[T]) GetRateLimitedEnqueueCount() int {
1✔
69
        return int(atomic.LoadInt32(&q.rateLimitedEnque))
1✔
70
}
1✔
71

72
func (q *MockWorkQueue[T]) GetAddAfterEnqueueCount() int {
1✔
73
        return int(atomic.LoadInt32(&q.addAfterEnque))
1✔
74
}
1✔
75

76
// ExpectAdds allows setting the amount of expected enqueues.
77
func (q *MockWorkQueue[T]) ExpectAdds(diff int) {
1✔
78
        q.wgLock.Lock()
1✔
79
        defer q.wgLock.Unlock()
1✔
80
        q.addWG = &sync.WaitGroup{}
1✔
81
        q.addWG.Add(diff)
1✔
82
}
1✔
83

84
// Wait waits until the expected amount of ExpectedAdds has happened.
85
// It will not block if there were no expectations set.
86
func (q *MockWorkQueue[T]) Wait() {
1✔
87
        q.wgLock.Lock()
1✔
88
        wg := q.addWG
1✔
89
        q.wgLock.Unlock()
1✔
90
        if wg != nil {
2✔
91
                wg.Wait()
1✔
92
                q.wgLock.Lock()
1✔
93
                q.addWG = nil
1✔
94
                q.wgLock.Unlock()
1✔
95
        }
1✔
96
}
97

98
func NewMockWorkQueue[T comparable](queue workqueue.TypedRateLimitingInterface[T]) *MockWorkQueue[T] {
1✔
99
        return &MockWorkQueue[T]{queue, nil, 0, 0, sync.Mutex{}}
1✔
100
}
1✔
101

102
func NewFakeInformerFor(obj runtime.Object) (cache.SharedIndexInformer, *framework.FakeControllerSource) {
1✔
103
        objSource := framework.NewFakeControllerSource()
1✔
104
        objInformer := cache.NewSharedIndexInformer(objSource, obj, 0, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
1✔
105
        return objInformer, objSource
1✔
106
}
1✔
107

108
func NewFakeInformerWithIndexersFor(obj runtime.Object, indexers cache.Indexers) (cache.SharedIndexInformer, *framework.FakeControllerSource) {
1✔
109
        objSource := framework.NewFakeControllerSource()
1✔
110
        objInformer := cache.NewSharedIndexInformer(objSource, obj, 0, indexers)
1✔
111
        return objInformer, objSource
1✔
112
}
1✔
113

114
type VirtualMachineFeeder[T comparable] struct {
115
        MockQueue *MockWorkQueue[T]
116
        Source    *framework.FakeControllerSource
117
}
118

119
func (v *VirtualMachineFeeder[T]) Add(vmi *v1.VirtualMachineInstance) {
1✔
120
        v.MockQueue.ExpectAdds(1)
1✔
121
        v.Source.Add(vmi)
1✔
122
        v.MockQueue.Wait()
1✔
123
}
1✔
124

125
func (v *VirtualMachineFeeder[T]) Modify(vmi *v1.VirtualMachineInstance) {
1✔
126
        v.MockQueue.ExpectAdds(1)
1✔
127
        v.Source.Modify(vmi)
1✔
128
        v.MockQueue.Wait()
1✔
129
}
1✔
130

131
func (v *VirtualMachineFeeder[T]) Delete(vmi *v1.VirtualMachineInstance) {
1✔
132
        v.MockQueue.ExpectAdds(1)
1✔
133
        v.Source.Delete(vmi)
1✔
134
        v.MockQueue.Wait()
1✔
135
}
1✔
136

137
func NewVirtualMachineFeeder[T comparable](queue *MockWorkQueue[T], source *framework.FakeControllerSource) *VirtualMachineFeeder[T] {
1✔
138
        return &VirtualMachineFeeder[T]{
1✔
139
                MockQueue: queue,
1✔
140
                Source:    source,
1✔
141
        }
1✔
142
}
1✔
143

144
type PodFeeder[T comparable] struct {
145
        MockQueue *MockWorkQueue[T]
146
        Source    *framework.FakeControllerSource
147
}
148

149
func (v *PodFeeder[T]) Add(pod *k8sv1.Pod) {
×
150
        v.MockQueue.ExpectAdds(1)
×
151
        v.Source.Add(pod)
×
152
        v.MockQueue.Wait()
×
153
}
×
154

155
func (v *PodFeeder[T]) Modify(pod *k8sv1.Pod) {
×
156
        v.MockQueue.ExpectAdds(1)
×
157
        v.Source.Modify(pod)
×
158
        v.MockQueue.Wait()
×
159
}
×
160

161
func (v *PodFeeder[T]) Delete(pod *k8sv1.Pod) {
×
162
        v.MockQueue.ExpectAdds(1)
×
163
        v.Source.Delete(pod)
×
164
        v.MockQueue.Wait()
×
165
}
×
166

167
func NewPodFeeder[T comparable](queue *MockWorkQueue[T], source *framework.FakeControllerSource) *PodFeeder[T] {
×
168
        return &PodFeeder[T]{
×
169
                MockQueue: queue,
×
170
                Source:    source,
×
171
        }
×
172
}
×
173

174
type PodDisruptionBudgetFeeder[T comparable] struct {
175
        MockQueue *MockWorkQueue[T]
176
        Source    *framework.FakeControllerSource
177
}
178

179
func (v *PodDisruptionBudgetFeeder[T]) Add(pdb *policyv1.PodDisruptionBudget) {
1✔
180
        v.MockQueue.ExpectAdds(1)
1✔
181
        v.Source.Add(pdb)
1✔
182
        v.MockQueue.Wait()
1✔
183
}
1✔
184

UNCOV
185
func (v *PodDisruptionBudgetFeeder[T]) Modify(pdb *policyv1.PodDisruptionBudget) {
×
UNCOV
186
        v.MockQueue.ExpectAdds(1)
×
UNCOV
187
        v.Source.Modify(pdb)
×
UNCOV
188
        v.MockQueue.Wait()
×
UNCOV
189
}
×
190

UNCOV
191
func (v *PodDisruptionBudgetFeeder[T]) Delete(pdb *policyv1.PodDisruptionBudget) {
×
UNCOV
192
        v.MockQueue.ExpectAdds(1)
×
UNCOV
193
        v.Source.Delete(pdb)
×
UNCOV
194
        v.MockQueue.Wait()
×
UNCOV
195
}
×
196

197
func NewPodDisruptionBudgetFeeder[T comparable](queue *MockWorkQueue[T], source *framework.FakeControllerSource) *PodDisruptionBudgetFeeder[T] {
1✔
198
        return &PodDisruptionBudgetFeeder[T]{
1✔
199
                MockQueue: queue,
1✔
200
                Source:    source,
1✔
201
        }
1✔
202
}
1✔
203

204
type MigrationFeeder[T comparable] struct {
205
        MockQueue *MockWorkQueue[T]
206
        Source    *framework.FakeControllerSource
207
}
208

209
func (v *MigrationFeeder[T]) Add(migration *v1.VirtualMachineInstanceMigration) {
1✔
210
        v.MockQueue.ExpectAdds(1)
1✔
211
        v.Source.Add(migration)
1✔
212
        v.MockQueue.Wait()
1✔
213
}
1✔
214

215
func (v *MigrationFeeder[T]) Modify(migration *v1.VirtualMachineInstanceMigration) {
1✔
216
        v.MockQueue.ExpectAdds(1)
1✔
217
        v.Source.Modify(migration)
1✔
218
        v.MockQueue.Wait()
1✔
219
}
1✔
220

221
func (v *MigrationFeeder[T]) Delete(migration *v1.VirtualMachineInstanceMigration) {
×
222
        v.MockQueue.ExpectAdds(1)
×
223
        v.Source.Delete(migration)
×
224
        v.MockQueue.Wait()
×
225
}
×
226

227
func NewMigrationFeeder[T comparable](queue *MockWorkQueue[T], source *framework.FakeControllerSource) *MigrationFeeder[T] {
1✔
228
        return &MigrationFeeder[T]{
1✔
229
                MockQueue: queue,
1✔
230
                Source:    source,
1✔
231
        }
1✔
232
}
1✔
233

234
type DomainFeeder[T comparable] struct {
235
        MockQueue *MockWorkQueue[T]
236
        Source    *framework.FakeControllerSource
237
}
238

239
func (v *DomainFeeder[T]) Add(vmi *api.Domain) {
×
240
        v.MockQueue.ExpectAdds(1)
×
241
        v.Source.Add(vmi)
×
242
        v.MockQueue.Wait()
×
243
}
×
244

245
func (v *DomainFeeder[T]) Modify(vmi *api.Domain) {
×
246
        v.MockQueue.ExpectAdds(1)
×
247
        v.Source.Modify(vmi)
×
248
        v.MockQueue.Wait()
×
249
}
×
250

251
func (v *DomainFeeder[T]) Delete(vmi *api.Domain) {
×
252
        v.MockQueue.ExpectAdds(1)
×
253
        v.Source.Delete(vmi)
×
254
        v.MockQueue.Wait()
×
255
}
×
256

257
func NewDomainFeeder[T comparable](queue *MockWorkQueue[T], source *framework.FakeControllerSource) *DomainFeeder[T] {
×
258
        return &DomainFeeder[T]{
×
259
                MockQueue: queue,
×
260
                Source:    source,
×
261
        }
×
262
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc