-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathresources_test.go
More file actions
324 lines (286 loc) · 10.2 KB
/
resources_test.go
File metadata and controls
324 lines (286 loc) · 10.2 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package rep_test
import (
"archive/tar"
"bytes"
"fmt"
"os"
"code.cloudfoundry.org/bbs/models"
"code.cloudfoundry.org/rep"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Resources", func() {
var (
cellState rep.CellState
linuxRootFSURL string
)
BeforeEach(func() {
linuxOnlyRootFSProviders := rep.RootFSProviders{models.PreloadedRootFSScheme: rep.NewFixedSetRootFSProvider("linux")}
total := rep.NewResources(1000, 2000, 10)
avail := rep.NewResources(950, 1900, 3)
linuxRootFSURL = models.PreloadedRootFS("linux")
lrps := []rep.LRP{
*buildLRP("ig-1", "pg-1", "domain", 0, linuxRootFSURL, 10, 20, 30, []string{}, []string{}, models.ActualLRPStateClaimed),
*buildLRP("ig-2", "pg-1", "domain", 1, linuxRootFSURL, 10, 20, 30, []string{}, []string{}, models.ActualLRPStateClaimed),
*buildLRP("ig-3", "pg-2", "domain", 0, linuxRootFSURL, 10, 20, 30, []string{}, []string{}, models.ActualLRPStateClaimed),
*buildLRP("ig-4", "pg-3", "domain", 0, linuxRootFSURL, 10, 20, 30, []string{}, []string{}, models.ActualLRPStateClaimed),
*buildLRP("ig-5", "pg-4", "domain", 0, linuxRootFSURL, 10, 20, 30, []string{}, []string{}, models.ActualLRPStateClaimed),
}
tasks := []rep.Task{
*buildTask("tg-big", "domain", linuxRootFSURL, 20, 10, 10, []string{}, []string{}, models.Task_Running, false),
*buildTask("tg-small", "domain", linuxRootFSURL, 10, 10, 10, []string{}, []string{}, models.Task_Running, false),
}
cellState = rep.NewCellState(
"cell-id",
0,
"https://foo.cell.service.cf.internal",
linuxOnlyRootFSProviders,
avail,
total,
lrps,
tasks,
"my-zone",
7,
false,
nil,
nil,
nil,
0,
)
})
Describe("MatchPlacementTags", func() {
Context("when cell state does not have placement tags", func() {
It("does not allow lrps with placement tags", func() {
state := rep.CellState{
PlacementTags: []string{},
OptionalPlacementTags: []string{},
}
Expect(state.MatchPlacementTags([]string{"foo"})).To(BeFalse())
Expect(state.MatchPlacementTags([]string{})).To(BeTrue())
})
})
Context("when it has require placement tags", func() {
It("requires the placement tags to be present in the lrp", func() {
state := rep.CellState{
PlacementTags: []string{"foo", "bar"},
OptionalPlacementTags: []string{},
}
Expect(state.MatchPlacementTags([]string{})).To(BeFalse())
Expect(state.MatchPlacementTags([]string{"foo"})).To(BeFalse())
Expect(state.MatchPlacementTags([]string{"foo", "bar"})).To(BeTrue())
})
})
Context("when it has optional placement tags", func() {
It("does not require placement tags to be present on the desired lrp", func() {
state := rep.CellState{
PlacementTags: []string{},
OptionalPlacementTags: []string{"foo"},
}
Expect(state.MatchPlacementTags([]string{})).To(BeTrue())
Expect(state.MatchPlacementTags([]string{"foo"})).To(BeTrue())
})
It("does not allow extra placement tags to be defined in the lrp", func() {
state := rep.CellState{
PlacementTags: []string{},
OptionalPlacementTags: []string{"foo"},
}
Expect(state.MatchPlacementTags([]string{"bar"})).To(BeFalse())
})
})
Context("when both placement tags and optional placement tags are present", func() {
It("requires all required placement tags to be on the lrp", func() {
state := rep.CellState{
PlacementTags: []string{"foo"},
OptionalPlacementTags: []string{"bar"},
}
Expect(state.MatchPlacementTags([]string{})).To(BeFalse())
Expect(state.MatchPlacementTags([]string{"bar"})).To(BeFalse())
Expect(state.MatchPlacementTags([]string{"foo"})).To(BeTrue())
Expect(state.MatchPlacementTags([]string{"foo", "bar"})).To(BeTrue())
Expect(state.MatchPlacementTags([]string{"foo", "bar", "baz"})).To(BeFalse())
})
})
})
Describe("Resource Matching", func() {
var requiredResource rep.Resource
var err error
BeforeEach(func() {
requiredResource = rep.NewResource(10, 10, 10)
})
JustBeforeEach(func() {
err = cellState.ResourceMatch(&requiredResource)
})
Context("when insufficient memory", func() {
BeforeEach(func() {
requiredResource.MemoryMB = 5000
})
It("returns an error", func() {
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("insufficient resources: memory"))
})
})
Context("when insufficient disk", func() {
BeforeEach(func() {
requiredResource.DiskMB = 5000
})
It("returns an error", func() {
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("insufficient resources: disk"))
})
})
Context("when insufficient disk and memory", func() {
BeforeEach(func() {
requiredResource.MemoryMB = 5000
requiredResource.DiskMB = 5000
})
It("returns an error", func() {
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("insufficient resources: disk, memory"))
})
})
Context("when insufficient disk, memory and containers", func() {
BeforeEach(func() {
requiredResource.MemoryMB = 5000
requiredResource.DiskMB = 5000
cellState.AvailableResources.Containers = 0
})
It("returns an error", func() {
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("insufficient resources: containers, disk, memory"))
})
})
Context("when there are no available containers", func() {
BeforeEach(func() {
cellState.AvailableResources.Containers = 0
})
It("returns an error", func() {
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("insufficient resources: containers"))
})
})
Context("when there is sufficient room", func() {
It("does not return an error", func() {
Expect(err).NotTo(HaveOccurred())
})
})
})
Describe("StackPathMap", func() {
Describe("PathForRootFS", func() {
var stackPathMap rep.StackPathMap
BeforeEach(func() {
stackPathMap = rep.StackPathMap{
"cflinuxfs3": "cflinuxfs3:/var/vcap/packages/cflinuxfs3/rootfs.tar",
}
})
It("returns the resolved path if the RootFS URL scheme is preloaded", func() {
p, err := stackPathMap.PathForRootFS("preloaded:cflinuxfs3")
Expect(err).NotTo(HaveOccurred())
Expect(p).To(Equal("cflinuxfs3:/var/vcap/packages/cflinuxfs3/rootfs.tar"))
})
It("returns the correct URL if the RootFS URL scheme is preloaded+layer", func() {
queryString := "?layer=https://blobstore.internal/layer1.tgz?layer_path=/tmp/asset1&layer_digest=alkjsdflkj"
p, err := stackPathMap.PathForRootFS(fmt.Sprintf("preloaded+layer:cflinuxfs3%s", queryString))
Expect(err).NotTo(HaveOccurred())
Expect(p).To(Equal(fmt.Sprintf("preloaded+layer:cflinuxfs3:/var/vcap/packages/cflinuxfs3/rootfs.tar%s", queryString)))
})
It("returns a blank string and no error if the RootFS URL is blank", func() {
p, err := stackPathMap.PathForRootFS("")
Expect(err).NotTo(HaveOccurred())
Expect(p).To(Equal(""))
})
It("returns the same URL and no error if the RootFS scheme is docker", func() {
p, err := stackPathMap.PathForRootFS("docker:///cloudfoundry/grace")
Expect(err).NotTo(HaveOccurred())
Expect(p).To(Equal("docker:///cloudfoundry/grace"))
})
It("returns an error if the RootFS URL is invalid", func() {
_, err := stackPathMap.PathForRootFS("%x")
Expect(err).To(HaveOccurred())
})
It("returns an error if the Preloaded RootFS path could not be found in the map", func() {
_, err := stackPathMap.PathForRootFS("preloaded:not-on-cell")
Expect(err).To(MatchError(rep.ErrPreloadedRootFSNotFound))
})
})
Describe("StackVersionList", func() {
It("returns an empty list when the map is empty", func() {
m := rep.StackPathMap{}
Expect(m.StackVersionList()).To(Equal([]string{}))
})
It("returns only the stack name when version is empty", func() {
m := rep.StackPathMap{
"cflinuxfs3": "/nonexistent/path/to/rootfs.tar",
}
list := m.StackVersionList()
Expect(list).To(HaveLen(1))
Expect(list[0]).To(Equal("cflinuxfs3"))
})
It("returns name@version when version is non-empty", func() {
tarPath := createTarWithVersion("1.2.3")
defer os.Remove(tarPath)
m := rep.StackPathMap{
"cflinuxfs3": tarPath,
}
list := m.StackVersionList()
Expect(list).To(HaveLen(1))
Expect(list[0]).To(Equal("cflinuxfs3@1.2.3"))
})
It("returns mixed name and name@version for multiple stacks", func() {
tarPath := createTarWithVersion("2.0.0")
defer os.Remove(tarPath)
m := rep.StackPathMap{
"with-version": tarPath,
"without-version": "/nonexistent/path.tar",
}
list := m.StackVersionList()
Expect(list).To(HaveLen(2))
Expect(list).To(ContainElement("with-version@2.0.0"))
Expect(list).To(ContainElement("without-version"))
})
})
})
})
func buildLRP(instanceGuid,
guid,
domain string,
index int,
rootFS string,
memoryMB,
diskMB,
maxPids int32,
placementTags,
volumeDrivers []string,
state string,
) *rep.LRP {
lrpKey := models.NewActualLRPKey(guid, int32(index), domain)
lrp := rep.NewLRP(instanceGuid, lrpKey, rep.NewResource(memoryMB, diskMB, maxPids), rep.PlacementConstraint{RootFs: rootFS,
PlacementTags: placementTags,
VolumeDrivers: volumeDrivers,
})
lrp.State = state
return &lrp
}
func buildTask(taskGuid, domain, rootFS string, memoryMB, diskMB, maxPids int32, placementTags, volumeDrivers []string, state models.Task_State, failed bool) *rep.Task {
task := rep.NewTask(taskGuid, domain, rep.NewResource(memoryMB, diskMB, maxPids), rep.PlacementConstraint{RootFs: rootFS, VolumeDrivers: volumeDrivers})
return &task
}
func createTarWithVersion(version string) string {
tmpDir := GinkgoT().TempDir()
tmpFile, err := os.CreateTemp(tmpDir, "stack-*.tar")
Expect(err).NotTo(HaveOccurred())
tarPath := tmpFile.Name()
Expect(tmpFile.Close()).To(Succeed())
buf := new(bytes.Buffer)
tarWriter := tar.NewWriter(buf)
// Match loadVersionFromPath: header.Name after TrimPrefix(., ".") must equal rep.StackVersionFile ("/etc/stack-version")
Expect(tarWriter.WriteHeader(&tar.Header{
Name: rep.StackVersionFile,
Size: int64(len(version)),
Mode: 0644,
})).To(Succeed())
_, err = tarWriter.Write([]byte(version))
Expect(err).NotTo(HaveOccurred())
Expect(tarWriter.Close()).To(Succeed())
Expect(os.WriteFile(tarPath, buf.Bytes(), 0644)).To(Succeed())
return tarPath
}