forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake.go
213 lines (162 loc) · 3.82 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
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
package registry
import (
"errors"
"sort"
"sync"
"time"
"github.com/coreos/fleet/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
"github.com/coreos/fleet/job"
"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/pkg"
"github.com/coreos/fleet/unit"
)
func NewFakeRegistry() *FakeRegistry {
return &FakeRegistry{
machines: []machine.MachineState{},
jobStates: map[string]*unit.UnitState{},
jobs: map[string]job.Job{},
units: []unit.Unit{},
version: nil,
bids: map[string]pkg.Set{},
}
}
type FakeRegistry struct {
// Not all methods of required by the Registry interface are implemented
// by the TestRegistry. Any calls to these unimplemented methods will
// result in a panic.
Registry
sync.RWMutex
machines []machine.MachineState
jobStates map[string]*unit.UnitState
jobs map[string]job.Job
units []unit.Unit
version *semver.Version
bids map[string]pkg.Set
}
func (f *FakeRegistry) SetMachines(machines []machine.MachineState) {
f.Lock()
defer f.Unlock()
f.machines = machines
}
func (f *FakeRegistry) SetJobs(jobs []job.Job) {
f.Lock()
defer f.Unlock()
f.jobs = make(map[string]job.Job, len(jobs))
for _, j := range jobs {
f.jobs[j.Name] = j
}
}
func (f *FakeRegistry) SetUnitStates(jobStates map[string]*unit.UnitState) {
f.Lock()
defer f.Unlock()
f.jobStates = jobStates
}
func (f *FakeRegistry) SetUnits(units []unit.Unit) {
f.Lock()
defer f.Unlock()
f.units = units
}
func (f *FakeRegistry) SetLatestVersion(v semver.Version) {
f.Lock()
defer f.Unlock()
f.version = &v
}
func (f *FakeRegistry) Machines() ([]machine.MachineState, error) {
f.RLock()
defer f.RUnlock()
return f.machines, nil
}
func (f *FakeRegistry) Jobs() ([]job.Job, error) {
f.RLock()
defer f.RUnlock()
var sorted sort.StringSlice
for _, j := range f.jobs {
sorted = append(sorted, j.Name)
}
sorted.Sort()
jobs := make([]job.Job, 0, len(f.jobs))
for _, jName := range sorted {
jobs = append(jobs, f.jobs[jName])
}
return jobs, nil
}
func (f *FakeRegistry) Job(name string) (*job.Job, error) {
f.RLock()
defer f.RUnlock()
j, ok := f.jobs[name]
if !ok {
return nil, nil
}
j.UnitState = f.jobStates[name]
return &j, nil
}
func (f *FakeRegistry) CreateJob(j *job.Job) error {
f.Lock()
defer f.Unlock()
_, ok := f.jobs[j.Name]
if ok {
return errors.New("Job already exists")
}
f.jobs[j.Name] = *j
return nil
}
func (f *FakeRegistry) DestroyJob(name string) error {
f.Lock()
defer f.Unlock()
delete(f.jobs, name)
return nil
}
func (f *FakeRegistry) Bids(jName string) (pkg.Set, error) {
f.RLock()
defer f.RUnlock()
return f.bids[jName], nil
}
func (f *FakeRegistry) SubmitJobBid(jName, machID string) {
f.Lock()
defer f.Unlock()
_, ok := f.bids[jName]
if !ok {
f.bids[jName] = pkg.NewUnsafeSet()
}
f.bids[jName].Add(machID)
}
func (f *FakeRegistry) SetJobTargetState(name string, target job.JobState) error {
f.Lock()
defer f.Unlock()
j, ok := f.jobs[name]
if !ok {
return errors.New("job does not exist")
}
j.TargetState = target
f.jobs[name] = j
return nil
}
func (f *FakeRegistry) ScheduleJob(name string, machID string) error {
f.Lock()
defer f.Unlock()
j, ok := f.jobs[name]
if !ok {
return errors.New("job does not exist")
}
j.TargetMachineID = machID
f.jobs[name] = j
return nil
}
func (f *FakeRegistry) SaveUnitState(jobName string, unitState *unit.UnitState) {
f.Lock()
defer f.Unlock()
f.jobStates[jobName] = unitState
}
func (f *FakeRegistry) RemoveUnitState(jobName string) error {
delete(f.jobStates, jobName)
return nil
}
func (f *FakeRegistry) LatestVersion() (*semver.Version, error) {
f.RLock()
defer f.RUnlock()
return f.version, nil
}
func (f *FakeRegistry) JobHeartbeat(jobName, agentMachID string, ttl time.Duration) error {
return nil
}
func (f *FakeRegistry) ClearJobHeartbeat(string) {}