-
Notifications
You must be signed in to change notification settings - Fork 596
/
SessionTest.groovy
496 lines (387 loc) · 14.2 KB
/
SessionTest.groovy
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
/*
* Copyright 2013-2019, Centre for Genomic Regulation (CRG)
*
* 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 nextflow
import spock.lang.Specification
import spock.lang.Unroll
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.attribute.PosixFilePermission
import nextflow.container.ContainerConfig
import nextflow.script.ScriptFile
import nextflow.script.WorkflowMetadata
import nextflow.trace.StatsObserver
import nextflow.trace.TraceFileObserver
import nextflow.util.Duration
import test.TestHelper
/**
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
class SessionTest extends Specification {
def 'test baseDir and binDir'() {
setup:
def base = Files.createTempDirectory('test')
def bin = base.resolve('bin'); bin.mkdir()
when:
def session = new Session()
then:
session.baseDir == null
session.binDir == null
when:
session = new Session()
session.baseDir = Paths.get('some/folder')
then:
session.baseDir == Paths.get('some/folder')
session.binDir == null
when:
session = new Session()
session.baseDir = base
then:
session.baseDir == base
session.binDir.exists()
cleanup:
base.deleteDir()
}
def 'test get queue size'() {
when:
def session = [:] as Session
session.config = [ executor:['$sge':[queueSize: 123] ] ]
then:
session.getQueueSize('sge', 1) == 123
session.getQueueSize('xxx', 1) == 1
session.getQueueSize(null, 1) == 1
when:
def session2 = [:] as Session
session2.config = [ executor:[ queueSize: 321, '$sge':[queueSize:789] ] ]
then:
session2.getQueueSize('sge', 2) == 789
session2.getQueueSize('xxx', 2) == 321
session2.getQueueSize(null, 2) == 321
when:
def session3 = [:] as Session
session3.config = [ executor: 'sge' ]
then:
session3.getQueueSize('sge', 1) == 1
session3.getQueueSize('xxx', 2) == 2
session3.getQueueSize(null, 3) == 3
}
def 'test get poll interval'() {
when:
def session1 = [:] as Session
session1.config = [ executor:['$sge':[pollInterval: 345] ] ]
then:
session1.getPollInterval('sge').toMillis() == 345
session1.getPollInterval('xxx').toMillis() == 1_000
session1.getPollInterval(null).toMillis() == 1_000
session1.getPollInterval(null, 2_000 as Duration).toMillis() == 2_000
when:
def session2 = [:] as Session
session2.config = [ executor:[ pollInterval: 321, '$sge':[pollInterval:789] ] ]
then:
session2.getPollInterval('sge').toMillis() == 789
session2.getPollInterval('xxx').toMillis() == 321
session2.getPollInterval(null).toMillis() == 321
when:
def session3 = [:] as Session
session3.config = [ executor: 'lsf' ]
then:
session3.getPollInterval('sge', 33 as Duration ).toMillis() == 33
session3.getPollInterval('xxx', 44 as Duration ).toMillis() == 44
session3.getPollInterval(null, 55 as Duration).toMillis() == 55
}
def 'test get exit read timeout'() {
setup:
def session1 = [:] as Session
session1.config = [ executor:['$sge':[exitReadTimeout: '5s'] ] ]
expect:
session1.getExitReadTimeout('sge') == '5sec' as Duration
session1.getExitReadTimeout('lsf', '3sec' as Duration) == '3sec' as Duration
}
def 'test get queue stat interval'() {
setup:
def session1 = [:] as Session
session1.config = [ executor:['$sge':[queueStatInterval: '4sec'] ] ]
expect:
session1.getQueueStatInterval('sge') == '4sec' as Duration
session1.getQueueStatInterval('lsf', '1sec' as Duration) == '1sec' as Duration
}
def 'test monitor dump interval'() {
setup:
def session1 = [:] as Session
session1.config = [ executor:['$sge':[dumpInterval: '6sec'] ] ]
expect:
session1.getMonitorDumpInterval('sge') == '6sec' as Duration
session1.getMonitorDumpInterval('lsf', '2sec' as Duration) == '2sec' as Duration
}
def 'test get exec config prop'() {
when:
def session = [:] as Session
session.config = [ executor: [x:123, y:222, '$hazelcast': [y:333] ] ]
then:
session.getExecConfigProp( 'hazelcast', 'x', null ) == 123
session.getExecConfigProp( 'hazelcast', 'y', null ) == 333
session.getExecConfigProp( 'local', 'y', null ) == 222
session.getExecConfigProp( 'local', 'y', 'beta') == 222
session.getExecConfigProp( 'hazelcast', 'z', null ) == null
session.getExecConfigProp( 'hazelcast', 'z', 'alpha') == 'alpha'
session.getExecConfigProp( 'hazelcast', 'z', 'alpha', [NXF_EXECUTOR_Z:'hola']) == 'hola'
session.getExecConfigProp( 'hazelcast', 'p.q.z', null, [NXF_EXECUTOR_P_Q_Z:'hello']) == 'hello'
}
def 'test add lib path'() {
setup:
def path1 = Files.createTempDirectory('test')
def path2 = Files.createTempDirectory('test')
when:
def session = new Session()
session.setLibDir( path1.toString() )
then:
session.getLibDir() == [ path1 ]
when:
session = new Session()
session.setLibDir( "${path1}:${path2}" )
then:
session.getLibDir() == [ path1, path2 ]
when:
session = new Session()
session.setBaseDir(Paths.get('/some/path'))
then:
session.getLibDir() == []
when:
def base = Files.createTempDirectory('test')
base.resolve('lib').mkdir()
session = new Session()
session.setBaseDir(base)
then:
session.getLibDir() == [base.resolve('lib')]
cleanup:
base?.deleteDir()
path1.deleteDir()
path2.deleteDir()
}
def 'test create observers'() {
def session
def result
def observer
when:
session = [:] as Session
result = session.createObservers()
then:
result.size()==1
result.any { it instanceof StatsObserver }
when:
session = [:] as Session
session.config = [trace: [enabled: true, file:'name.txt']]
result = session.createObservers()
observer = result[1] as TraceFileObserver
then:
result.size() == 2
observer.tracePath == Paths.get('name.txt').complete()
observer.separator == '\t'
when:
session = [:] as Session
session.config = [trace: [enabled: true, sep: 'x', fields: 'task_id,name,exit', file: 'alpha.txt']]
result = session.createObservers()
observer = result[1] as TraceFileObserver
then:
result.size() == 2
observer.tracePath == Paths.get('alpha.txt').complete()
observer.separator == 'x'
observer.fields == ['task_id','name','exit']
when:
session = [:] as Session
session.config = [trace: [sep: 'x', fields: 'task_id,name,exit']]
result = session.createObservers()
then:
!result.any { it instanceof TraceFileObserver }
when:
session = [:] as Session
session.config = [trace: [enabled: true, fields: 'task_id,name,exit,vmem']]
result = session.createObservers()
observer = result[1] as TraceFileObserver
then:
result.size() == 2
observer.tracePath == Paths.get('trace.txt').complete()
observer.separator == '\t'
observer.fields == ['task_id','name','exit','vmem']
}
def 'should return absolute workDir' () {
given:
def folder = TestHelper.createInMemTempDir()
def file = folder.resolve('pipeline.nf'); file.text = 'println "hello"'
def script = new ScriptFile(file)
when:
def session = new Session([workDir: '../work'])
session.init(script)
then:
session.binding != null
session.baseDir == folder
session.workDir.isAbsolute()
!session.workDir.toString().contains('..')
session.scriptName == 'pipeline.nf'
session.classesDir.exists()
session.observers != null
session.workflowMetadata != null
cleanup:
session.classesDir?.deleteDir()
}
def 'should collect bin executable files' () {
given:
def folder = Files.createTempDirectory('test')
Files.createFile(folder.resolve('foo.sh'))
Files.createFile(folder.resolve('bar.sh'))
Files.createFile(folder.resolve('baz.sh'))
Files.setPosixFilePermissions(folder.resolve('foo.sh'), [PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_EXECUTE] as Set)
Files.setPosixFilePermissions(folder.resolve('bar.sh'), [PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_EXECUTE] as Set)
def session = [:] as Session
when:
def result = session.findBinEntries(folder)
then:
result.size() == 2
result['foo.sh'] == folder.resolve('foo.sh')
result['bar.sh'] == folder.resolve('bar.sh')
cleanup:
folder?.deleteDir()
}
def 'should get a warning message' () {
given:
def session = new Session([process: ['$foo': [cpus:1], '$bar':[mem:'10GB']]])
expect:
session.validateConfig0(['foo','bar','baz']) == []
session.validateConfig0(['foo','baz']) == ["There's no process matching config selector: bar -- Did you mean: baz?"]
}
@Unroll
def 'should return engine type' () {
given:
def session = new Session([(engine): config])
expect:
session.containerConfig == config as ContainerConfig
session.containerConfig.enabled
session.containerConfig.engine == engine
where:
engine | config
'docker' | [enabled: true, x:'alpha', y: 'beta']
'docker' | [enabled: true, x:'alpha', y: 'beta', registry: 'd.reg']
'udocker' | [enabled: true, x:'alpha', y: 'beta']
'shifter' | [enabled: true, x:'delta', y: 'gamma']
}
def 'should get manifest object' () {
given:
def MAN = [author: 'pablo', nextflowVersion: '1.2.3', name: 'foo']
when:
def session = new Session([manifest: MAN])
then:
session.manifest.with {
author == 'pablo'
nextflowVersion == '1.2.3'
name == 'foo'
description == null
}
}
def 'should get config attribute' () {
given:
def session = Spy(Session)
when:
def result = session.getConfigAttribute('alpha', 'hello')
then:
result == 'hello'
when:
result = session.getConfigAttribute('delta', 'hello')
then:
session.getConfig() >> [delta: '1234']
result == '1234'
when:
result = session.getConfigAttribute('omega', 'hello')
then:
session.getSystemEnv() >> [NXF_OMEGA: '6789']
result == '6789'
}
def 'should get config nested attribute' () {
given:
def session = Spy(Session)
when:
def result = session.getConfigAttribute('alpha.beta.delta', 'hello')
then:
result == 'hello'
when:
result = session.getConfigAttribute('alpha.beta.gamma', 'hello')
then:
session.getConfig() >> [alpha: [beta: [gamma: 'abc']]]
result == 'abc'
when:
result = session.getConfigAttribute('alpha.beta.omega', 'hello')
then:
session.getSystemEnv() >> [NXF_ALPHA_BETA_OMEGA: 'OK']
result == 'OK'
}
@Unroll
def 'should check valid process name with selector=#SELECTOR' () {
given:
def session = new Session()
when:
def error = []
session.checkValidProcessName(NAMES, SELECTOR, error)
then:
error[0] == MSG
where:
SELECTOR | NAMES | MSG
'foo' | ['foo','bar'] | null
'bar' | ['foo','bar'] | null
'baz' | ['foo','bar'] | "There's no process matching config selector: baz -- Did you mean: bar?"
'ba.*' | ['foo','bar'] | null
'foo|bar' | ['foo','bar'] | null
'foo|bar' | ['baz'] | "There's no process matching config selector: foo|bar"
}
static Map cfg(String config) {
new ConfigSlurper().parse(config).toMap()
}
def 'should fetch containers definition' () {
String text
when:
text = '''
process.container = 'beta'
'''
then:
new Session(cfg(text)).fetchContainers() == 'beta'
when:
text = '''
process {
$proc1 { container = 'alpha' }
$proc2 { container ='beta' }
}
'''
then:
new Session(cfg(text)).fetchContainers() == ['$proc1': 'alpha', '$proc2': 'beta']
when:
text = '''
process {
$proc1 { container = 'alpha' }
$proc2 { container ='beta' }
}
process.container = 'gamma'
'''
then:
new Session(cfg(text)).fetchContainers() == ['$proc1': 'alpha', '$proc2': 'beta', default: 'gamma']
when:
text = '''
process.container = { "ngi/rnaseq:${workflow.getRevision() ?: 'latest'}" }
'''
def meta = Mock(WorkflowMetadata); meta.getRevision() >> '1.2'
def session = new Session(cfg(text))
session.binding.setVariable('workflow',meta)
then:
session.fetchContainers() == 'ngi/rnaseq:1.2'
}
}