forked from hashicorp/nomad
/
mock.go
108 lines (91 loc) · 2.77 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
package device
import (
"context"
"time"
"github.com/hashicorp/nomad/plugins/base"
)
type FingerprintFn func(context.Context) (<-chan *FingerprintResponse, error)
type ReserveFn func([]string) (*ContainerReservation, error)
type StatsFn func(context.Context, time.Duration) (<-chan *StatsResponse, error)
// MockDevicePlugin is used for testing.
// Each function can be set as a closure to make assertions about how data
// is passed through the base plugin layer.
type MockDevicePlugin struct {
*base.MockPlugin
FingerprintF FingerprintFn
ReserveF ReserveFn
StatsF StatsFn
}
func (p *MockDevicePlugin) Fingerprint(ctx context.Context) (<-chan *FingerprintResponse, error) {
return p.FingerprintF(ctx)
}
func (p *MockDevicePlugin) Reserve(devices []string) (*ContainerReservation, error) {
return p.ReserveF(devices)
}
func (p *MockDevicePlugin) Stats(ctx context.Context, interval time.Duration) (<-chan *StatsResponse, error) {
return p.StatsF(ctx, interval)
}
// Below are static implementations of the device functions
// StaticFingerprinter fingerprints the passed devices just once
func StaticFingerprinter(devices []*DeviceGroup) FingerprintFn {
return func(_ context.Context) (<-chan *FingerprintResponse, error) {
outCh := make(chan *FingerprintResponse, 1)
outCh <- &FingerprintResponse{
Devices: devices,
}
return outCh, nil
}
}
// ErrorChFingerprinter returns an error fingerprinting over the channel
func ErrorChFingerprinter(err error) FingerprintFn {
return func(_ context.Context) (<-chan *FingerprintResponse, error) {
outCh := make(chan *FingerprintResponse, 1)
outCh <- &FingerprintResponse{
Error: err,
}
return outCh, nil
}
}
// StaticReserve returns the passed container reservation
func StaticReserve(out *ContainerReservation) ReserveFn {
return func(_ []string) (*ContainerReservation, error) {
return out, nil
}
}
// ErrorReserve returns the passed error
func ErrorReserve(err error) ReserveFn {
return func(_ []string) (*ContainerReservation, error) {
return nil, err
}
}
// StaticStats returns the passed statistics
func StaticStats(out []*DeviceGroupStats) StatsFn {
return func(ctx context.Context, intv time.Duration) (<-chan *StatsResponse, error) {
outCh := make(chan *StatsResponse, 1)
go func() {
ticker := time.NewTimer(0)
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
ticker.Reset(intv)
}
outCh <- &StatsResponse{
Groups: out,
}
}
}()
return outCh, nil
}
}
// ErrorChStats returns an error collecting stats over the channel
func ErrorChStats(err error) StatsFn {
return func(_ context.Context, _ time.Duration) (<-chan *StatsResponse, error) {
outCh := make(chan *StatsResponse, 1)
outCh <- &StatsResponse{
Error: err,
}
return outCh, nil
}
}