-
Notifications
You must be signed in to change notification settings - Fork 784
Expand file tree
/
Copy pathMapperUtilTest.groovy
More file actions
613 lines (498 loc) · 18.6 KB
/
MapperUtilTest.groovy
File metadata and controls
613 lines (498 loc) · 18.6 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*
* Copyright 2013-2026, Seqera Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.seqera.util
import io.seqera.config.MachineRequirementOpts
import io.seqera.sched.api.schema.v1a1.DiskAllocation
import io.seqera.sched.api.schema.v1a1.EcsCapacityMode
import io.seqera.sched.api.schema.v1a1.PriceModel as SchedPriceModel
import io.seqera.sched.api.schema.v1a1.ProvisioningModel
import nextflow.cloud.types.PriceModel
import nextflow.fusion.FusionConfig
import nextflow.util.MemoryUnit
import spock.lang.Specification
/**
* Unit tests for SchemaMapper
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
class MapperUtilTest extends Specification {
def 'should return null for null opts' () {
expect:
SchemaMapperUtil.toMachineRequirement(null) == null
}
def 'should return null for empty opts' () {
expect:
SchemaMapperUtil.toMachineRequirement(new MachineRequirementOpts([:])) == null
}
def 'should map all fields' () {
when:
def result = SchemaMapperUtil.toMachineRequirement(new MachineRequirementOpts([
provisioning: 'spotFirst',
maxSpotAttempts: 3,
machineTypes: ['m5', 'c5']
]))
then:
result.provisioning == ProvisioningModel.SPOT_FIRST
result.maxSpotAttempts == 3
result.machineTypes == ['m5', 'c5']
}
def 'should map provisioning model' () {
expect:
SchemaMapperUtil.toProvisioningModel(null) == null
SchemaMapperUtil.toProvisioningModel('spot') == ProvisioningModel.SPOT
SchemaMapperUtil.toProvisioningModel('ondemand') == ProvisioningModel.ONDEMAND
SchemaMapperUtil.toProvisioningModel('spotFirst') == ProvisioningModel.SPOT_FIRST
}
def 'should map price model' () {
expect:
SchemaMapperUtil.toPriceModel(null) == null
SchemaMapperUtil.toPriceModel(SchedPriceModel.SPOT) == PriceModel.spot
SchemaMapperUtil.toPriceModel(SchedPriceModel.STANDARD) == PriceModel.standard
}
// tests for toMachineRequirement with task arch
def 'should return null when both opts and taskArch are null' () {
expect:
SchemaMapperUtil.toMachineRequirement(null, null) == null
}
def 'should use taskArch when opts is null' () {
when:
def result = SchemaMapperUtil.toMachineRequirement(null, 'arm64')
then:
result.arch == 'arm64'
result.provisioning == null
}
def 'should use taskArch with config settings' () {
when:
def result = SchemaMapperUtil.toMachineRequirement(
new MachineRequirementOpts([provisioning: 'spot']),
'arm64'
)
then:
result.arch == 'arm64'
result.provisioning == ProvisioningModel.SPOT
}
def 'should have null arch when taskArch is null' () {
when:
def result = SchemaMapperUtil.toMachineRequirement(
new MachineRequirementOpts([provisioning: 'spot']),
null
)
then:
result.arch == null
result.provisioning == ProvisioningModel.SPOT
}
def 'should merge config settings with taskArch' () {
when:
def result = SchemaMapperUtil.toMachineRequirement(
new MachineRequirementOpts([
provisioning: 'spotFirst',
maxSpotAttempts: 3,
machineTypes: ['m5', 'c5']
]),
'arm64'
)
then:
result.arch == 'arm64'
result.provisioning == ProvisioningModel.SPOT_FIRST
result.maxSpotAttempts == 3
result.machineTypes == ['m5', 'c5']
}
// tests for disk requirement mapping
def 'should return null disk requirement for null disk size' () {
expect:
SchemaMapperUtil.toDiskRequirement(null) == null
}
def 'should return null disk requirement for zero disk size' () {
expect:
SchemaMapperUtil.toDiskRequirement(MemoryUnit.of(0)) == null
}
def 'should map disk size to disk requirement with defaults' () {
when:
def result = SchemaMapperUtil.toDiskRequirement(MemoryUnit.of('100 GB'))
then: 'disk size is set'
result.sizeGiB == 100
and: 'allocation defaults to node'
result.allocation == DiskAllocation.NODE
and: 'node allocation does not set EBS options'
result.volumeType == null
result.throughputMiBps == null
result.encrypted == null
result.iops == null
}
def 'should map disk size in different units' () {
expect:
SchemaMapperUtil.toDiskRequirement(MemoryUnit.of('1 TB')).sizeGiB == 1024
SchemaMapperUtil.toDiskRequirement(MemoryUnit.of('50 GB')).sizeGiB == 50
and: 'defaults to node allocation'
SchemaMapperUtil.toDiskRequirement(MemoryUnit.of('1 TB')).allocation == DiskAllocation.NODE
}
def 'should include disk in machine requirement' () {
when:
def result = SchemaMapperUtil.toMachineRequirement(
new MachineRequirementOpts([:]),
'x86_64',
MemoryUnit.of('200 GB'),
false
)
then:
result.arch == 'x86_64'
result.disk != null
result.disk.sizeGiB == 200
}
def 'should return machine requirement with only disk' () {
when:
def result = SchemaMapperUtil.toMachineRequirement(null, null, MemoryUnit.of('100 GB'), false)
then:
result != null
result.arch == null
result.disk != null
result.disk.sizeGiB == 100
}
def 'should return null when no taskArch, no opts, and no disk' () {
expect:
SchemaMapperUtil.toMachineRequirement(null, null, null, false) == null
}
// tests for custom disk configuration options
def 'should throw exception for invalid disk type' () {
given:
def opts = new MachineRequirementOpts([diskAllocation: 'task', diskType: 'local/nvme'])
when:
SchemaMapperUtil.toDiskRequirement(MemoryUnit.of('100 GB'), opts)
then:
def e = thrown(IllegalArgumentException)
e.message.contains("Invalid disk type: local/nvme")
e.message.contains("Supported types:")
}
def 'should use custom disk type from config' () {
given:
def opts = new MachineRequirementOpts([diskAllocation: 'task', diskType: 'ebs/io1', diskIops: 10000])
when:
def result = SchemaMapperUtil.toDiskRequirement(MemoryUnit.of('100 GB'), opts)
then:
result.sizeGiB == 100
result.allocation == DiskAllocation.TASK
result.volumeType == 'ebs/io1'
result.iops == 10000
result.throughputMiBps == null // throughput only for gp3
}
def 'should use custom throughput from config' () {
given:
def opts = new MachineRequirementOpts([diskAllocation: 'task', diskThroughputMiBps: 500])
when:
def result = SchemaMapperUtil.toDiskRequirement(MemoryUnit.of('100 GB'), opts)
then:
result.allocation == DiskAllocation.TASK
result.volumeType == SchemaMapperUtil.DEFAULT_DISK_TYPE
result.throughputMiBps == 500
}
def 'should use encryption from config' () {
given:
def opts = new MachineRequirementOpts([diskAllocation: 'task', diskEncrypted: true])
when:
def result = SchemaMapperUtil.toDiskRequirement(MemoryUnit.of('100 GB'), opts)
then:
result.allocation == DiskAllocation.TASK
result.encrypted == true
}
def 'should use all disk options from config' () {
given:
def opts = new MachineRequirementOpts([
diskAllocation: 'task',
diskType: 'ebs/gp3',
diskThroughputMiBps: 600,
diskIops: 8000,
diskEncrypted: true
])
when:
def result = SchemaMapperUtil.toDiskRequirement(MemoryUnit.of('200 GB'), opts)
then:
result.sizeGiB == 200
result.allocation == DiskAllocation.TASK
result.volumeType == 'ebs/gp3'
result.throughputMiBps == 600
result.iops == 8000
result.encrypted == true
}
def 'should pass disk options through machine requirement' () {
given:
def opts = new MachineRequirementOpts([
diskAllocation: 'task',
diskType: 'ebs/io2',
diskIops: 15000,
diskEncrypted: true
])
when:
def result = SchemaMapperUtil.toMachineRequirement(opts, 'arm64', MemoryUnit.of('500 GB'), false)
then:
result.arch == 'arm64'
result.disk.sizeGiB == 500
result.disk.allocation == DiskAllocation.TASK
result.disk.volumeType == 'ebs/io2'
result.disk.iops == 15000
result.disk.encrypted == true
result.disk.throughputMiBps == null // io2 doesn't use throughput
}
// tests for disk allocation mapping
def 'should map disk allocation' () {
expect:
SchemaMapperUtil.toDiskAllocation(null) == null
SchemaMapperUtil.toDiskAllocation('task') == DiskAllocation.TASK
SchemaMapperUtil.toDiskAllocation('node') == DiskAllocation.NODE
SchemaMapperUtil.toDiskAllocation('nvme') == DiskAllocation.NVME
}
def 'should throw exception for invalid disk allocation' () {
when:
SchemaMapperUtil.toDiskAllocation('invalid')
then:
thrown(IllegalArgumentException)
}
def 'should use task disk allocation from config' () {
given:
def opts = new MachineRequirementOpts([diskAllocation: 'task'])
when:
def result = SchemaMapperUtil.toDiskRequirement(MemoryUnit.of('100 GB'), opts)
then:
result.allocation == DiskAllocation.TASK
}
def 'should use node disk allocation from config' () {
given:
def opts = new MachineRequirementOpts([diskAllocation: 'node'])
when:
def result = SchemaMapperUtil.toDiskRequirement(MemoryUnit.of('100 GB'), opts)
then:
result.allocation == DiskAllocation.NODE
result.sizeGiB == 100
result.volumeType == null // node allocation doesn't set EBS options
result.throughputMiBps == null
result.iops == null
result.encrypted == null
}
def 'should default to node disk allocation when not specified' () {
when:
def result = SchemaMapperUtil.toDiskRequirement(MemoryUnit.of('100 GB'))
then:
result.allocation == DiskAllocation.NODE
}
def 'should include disk allocation in machine requirement' () {
given:
def opts = new MachineRequirementOpts([
diskAllocation: 'node'
])
when:
def result = SchemaMapperUtil.toMachineRequirement(opts, 'x86_64', MemoryUnit.of('200 GB'), false)
then:
result.arch == 'x86_64'
result.disk.sizeGiB == 200
result.disk.allocation == DiskAllocation.NODE
}
// tests for node allocation validation
def 'should throw error when diskType is set with node allocation' () {
given:
def opts = new MachineRequirementOpts([
diskAllocation: 'node',
diskType: 'ebs/gp3'
])
when:
SchemaMapperUtil.toDiskRequirement(MemoryUnit.of('100 GB'), opts)
then:
def e = thrown(IllegalArgumentException)
e.message.contains('diskType')
e.message.contains('node')
}
def 'should throw error when diskIops is set with node allocation' () {
given:
def opts = new MachineRequirementOpts([
diskAllocation: 'node',
diskIops: 10000
])
when:
SchemaMapperUtil.toDiskRequirement(MemoryUnit.of('100 GB'), opts)
then:
def e = thrown(IllegalArgumentException)
e.message.contains('diskIops')
}
def 'should throw error when diskThroughputMiBps is set with node allocation' () {
given:
def opts = new MachineRequirementOpts([
diskAllocation: 'node',
diskThroughputMiBps: 500
])
when:
SchemaMapperUtil.toDiskRequirement(MemoryUnit.of('100 GB'), opts)
then:
def e = thrown(IllegalArgumentException)
e.message.contains('diskThroughputMiBps')
}
def 'should throw error when diskEncrypted is set with node allocation' () {
given:
def opts = new MachineRequirementOpts([
diskAllocation: 'node',
diskEncrypted: true
])
when:
SchemaMapperUtil.toDiskRequirement(MemoryUnit.of('100 GB'), opts)
then:
def e = thrown(IllegalArgumentException)
e.message.contains('diskEncrypted')
}
def 'should report all invalid options with node allocation' () {
given:
def opts = new MachineRequirementOpts([
diskAllocation: 'node',
diskType: 'ebs/io1',
diskIops: 10000,
diskEncrypted: true
])
when:
SchemaMapperUtil.toDiskRequirement(MemoryUnit.of('100 GB'), opts)
then:
def e = thrown(IllegalArgumentException)
e.message.contains('diskType')
e.message.contains('diskIops')
e.message.contains('diskEncrypted')
}
// tests for snapshot maxSpotAttempts defaulting
def 'should return machine requirement with only snapshot enabled' () {
when:
def result = SchemaMapperUtil.toMachineRequirement(null, null, null, true)
then:
result != null
result.snapshotEnabled == true
result.maxSpotAttempts == FusionConfig.DEFAULT_SNAPSHOT_MAX_SPOT_ATTEMPTS
}
def 'should use explicit maxSpotAttempts when snapshot enabled' () {
when:
def result = SchemaMapperUtil.toMachineRequirement(new MachineRequirementOpts([maxSpotAttempts: 2]), null, null, true)
then:
result.snapshotEnabled == true
result.maxSpotAttempts == 2
}
def 'should not default maxSpotAttempts when snapshot disabled' () {
when:
def result = SchemaMapperUtil.toMachineRequirement(new MachineRequirementOpts([:]), 'x86_64', null, false)
then:
result.snapshotEnabled == null
result.maxSpotAttempts == null
}
// tests for capacity mode mapping
def 'should map capacity mode' () {
expect:
SchemaMapperUtil.toEcsCapacityMode(null) == null
SchemaMapperUtil.toEcsCapacityMode('managed') == EcsCapacityMode.MANAGED
SchemaMapperUtil.toEcsCapacityMode('asg') == EcsCapacityMode.ASG
}
def 'should throw exception for invalid capacity mode' () {
when:
SchemaMapperUtil.toEcsCapacityMode('invalid')
then:
thrown(IllegalArgumentException)
}
def 'should include capacity mode in machine requirement' () {
when:
def result = SchemaMapperUtil.toMachineRequirement(new MachineRequirementOpts([capacityMode: 'asg']))
then:
result != null
result.capacityMode == EcsCapacityMode.ASG
}
def 'should include capacity mode in machine requirement with task arch' () {
when:
def result = SchemaMapperUtil.toMachineRequirement(
new MachineRequirementOpts([capacityMode: 'managed']),
'arm64',
null,
false
)
then:
result.arch == 'arm64'
result.capacityMode == EcsCapacityMode.MANAGED
}
def 'should combine snapshot with other machine requirement settings' () {
when:
def result = SchemaMapperUtil.toMachineRequirement(
new MachineRequirementOpts([provisioning: 'spot']),
'arm64',
MemoryUnit.of('100 GB'),
true
)
then:
result.arch == 'arm64'
result.provisioning == ProvisioningModel.SPOT
result.disk.sizeGiB == 100
result.snapshotEnabled == true
result.maxSpotAttempts == FusionConfig.DEFAULT_SNAPSHOT_MAX_SPOT_ATTEMPTS
}
// tests for NVMe disk allocation
def 'should create nvme disk requirement without disk size' () {
given:
def opts = new MachineRequirementOpts([diskAllocation: 'nvme'])
when:
def result = SchemaMapperUtil.toDiskRequirement(null, opts)
then:
result != null
result.allocation == DiskAllocation.NVME
result.sizeGiB == 0
result.volumeType == null
result.throughputMiBps == null
result.iops == null
result.encrypted == null
}
def 'should create nvme disk requirement with disk size (ignored)' () {
given:
def opts = new MachineRequirementOpts([diskAllocation: 'nvme'])
when:
def result = SchemaMapperUtil.toDiskRequirement(MemoryUnit.of('100 GB'), opts)
then:
result != null
result.allocation == DiskAllocation.NVME
result.sizeGiB == 100
}
def 'should include nvme disk in machine requirement' () {
given:
def opts = new MachineRequirementOpts([diskAllocation: 'nvme', capacityMode: 'asg'])
when:
def result = SchemaMapperUtil.toMachineRequirement(opts, 'x86_64', null, false)
then:
result != null
result.arch == 'x86_64'
result.disk != null
result.disk.allocation == DiskAllocation.NVME
result.disk.sizeGiB == 0
}
def 'should set diskMountPath for all allocation types' () {
given:
def opts = new MachineRequirementOpts([diskAllocation: ALLOCATION, diskMountPath: '/data'])
when:
def result = SchemaMapperUtil.toDiskRequirement(MemoryUnit.of('100 GB'), opts)
then:
result.allocation == EXPECTED_ALLOCATION
result.mountPath == '/data'
where:
ALLOCATION | EXPECTED_ALLOCATION
'node' | DiskAllocation.NODE
'task' | DiskAllocation.TASK
'nvme' | DiskAllocation.NVME
}
def 'should throw error when EBS options are set with nvme allocation' () {
given:
def opts = new MachineRequirementOpts([diskAllocation: 'nvme', diskType: 'ebs/gp3'])
when:
SchemaMapperUtil.toDiskRequirement(null, opts)
then:
def e = thrown(IllegalArgumentException)
e.message.contains('diskType')
e.message.contains('nvme')
}
}