<commit_before>/*
 * Copyright (c) 2013-2014, Centre for Genomic Regulation (CRG).
 * Copyright (c) 2013-2014, <NAME> and the respective authors.
 *
 *   This file is part of 'Nextflow'.
 *
 *   Nextflow is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Nextflow is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Nextflow.  If not, see <http://www.gnu.org/licenses/>.
 */

package nextflow.util
import java.nio.file.Paths

import spock.lang.Specification
/**
 *
 * @author <NAME> <<EMAIL>>
 */
class DockerBuilderTest extends Specification {


    def 'test docker mounts'() {

        setup:
        def files =  [Paths.get('/folder/data'),  Paths.get('/folder/db'), Paths.get('/folder/db') ]
        def real = [ Paths.get('/user/yo/nextflow/bin'), Paths.get('/user/yo/nextflow/work'), Paths.get('/db/pdb/local/data') ]

        expect:
        DockerBuilder.makeVolumes([]).toString() == '-v $PWD:$PWD'
        DockerBuilder.makeVolumes(files).toString() == '-v /folder:/folder -v $PWD:$PWD'
        DockerBuilder.makeVolumes(real).toString()  == '-v /user/yo/nextflow:/user/yo/nextflow -v /db/pdb/local/data:/db/pdb/local/data -v $PWD:$PWD'
    }


    def 'test docker env'() {

        expect:
        DockerBuilder.makeEnv('X=1').toString() == '-e "X=1"'
        DockerBuilder.makeEnv([VAR_X:1, VAR_Y: 2]).toString() == '-e "VAR_X=1" -e "VAR_Y=2"'
        DockerBuilder.makeEnv( Paths.get('/some/file.env') ).toString() == '-e "BASH_ENV=/some/file.env"'
        DockerBuilder.makeEnv( new File('/some/file.env') ).toString() == '-e "BASH_ENV=/some/file.env"'
    }

    def 'test docker run command line'() {

        setup:
        def envFile = Paths.get('env-file')
        def db_file = Paths.get('/home/db')

        expect:
        new DockerBuilder('fedora').build() == 'docker run -i -v $PWD:$PWD -w $PWD fedora'
        new DockerBuilder('fedora').addEnv(envFile).build() == 'docker run -i -e "BASH_ENV=env-file" -v $PWD:$PWD -w $PWD fedora'
        new DockerBuilder('ubuntu').params(temp:'/hola').build() == 'docker run -i -v /hola:/tmp -v $PWD:$PWD -w $PWD ubuntu'
        new DockerBuilder('busybox').params(sudo: true).build() == 'sudo docker run -i -v $PWD:$PWD -w $PWD busybox'
        new DockerBuilder('busybox').params(entry: '/bin/bash').build() == 'docker run -i -v $PWD:$PWD -w $PWD --entrypoint /bin/bash busybox'
        new DockerBuilder('busybox').params(runOptions: '-x --zeta').build() == 'docker run -i -v $PWD:$PWD -w $PWD -x --zeta busybox'
        new DockerBuilder('busybox').params(userEmulation:true).build() == 'docker run -i -u $(id -u) -e "HOME=${HOME}" -v /etc/passwd:/etc/passwd:ro -v /etc/shadow:/etc/shadow:ro -v /etc/group:/etc/group:ro -v $HOME:$HOME -v $PWD:$PWD -w $PWD busybox'
        new DockerBuilder('busybox').setName('hola').build() == 'docker run -i -v $PWD:$PWD -w $PWD --name hola busybox'

        new DockerBuilder('fedora')
                .addEnv(envFile)
                .addMount(db_file)
                .addMount(db_file) // <-- add twice the same to prove that the final string won't contain duplicates
                .build() == 'docker run -i -e "BASH_ENV=env-file" -v /home/db:/home/db -v $PWD:$PWD -w $PWD fedora'

    }

    def 'test memory and cpuset' () {

        expect:
        new DockerBuilder('fedora').setCpus('1,2').build() == 'docker run -i --cpuset 1,2 -v $PWD:$PWD -w $PWD fedora'
        new DockerBuilder('fedora').setMemory('10g').build() == 'docker run -i --memory 10g -v $PWD:$PWD -w $PWD fedora'
        new DockerBuilder('fedora').setMemory(new MemoryUnit('100M')).build() == 'docker run -i --memory 100m -v $PWD:$PWD -w $PWD fedora'
        new DockerBuilder('fedora').setCpus('1-3').setMemory(new MemoryUnit('100M')).build() == 'docker run -i --cpuset 1-3 --memory 100m -v $PWD:$PWD -w $PWD fedora'

    }

    def 'test add mount'() {

        when:
        def docker = new DockerBuilder('fedora')
        docker.addMount(Paths.get('hello'))
        then:
        docker.mounts.size() == 1
        docker.mounts.contains(Paths.get('hello'))<commit_msg>Add test case for engineOptions to DockerBuilderTest.<commit_after>/*
 * Copyright (c) 2013-2014, Centre for Genomic Regulation (CRG).
 * Copyright (c) 2013-2014, <NAME> and the respective authors.
 *
 *   This file is part of 'Nextflow'.
 *
 *   Nextflow is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Nextflow is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Nextflow.  If not, see <http://www.gnu.org/licenses/>.
 */

package nextflow.util
import java.nio.file.Paths

import spock.lang.Specification
/**
 *
 * @author <NAME> <<EMAIL>>
 */
class DockerBuilderTest extends Specification {


    def 'test docker mounts'() {

        setup:
        def files =  [Paths.get('/folder/data'),  Paths.get('/folder/db'), Paths.get('/folder/db') ]
        def real = [ Paths.get('/user/yo/nextflow/bin'), Paths.get('/user/yo/nextflow/work'), Paths.get('/db/pdb/local/data') ]

        expect:
        DockerBuilder.makeVolumes([]).toString() == '-v $PWD:$PWD'
        DockerBuilder.makeVolumes(files).toString() == '-v /folder:/folder -v $PWD:$PWD'
        DockerBuilder.makeVolumes(real).toString()  == '-v /user/yo/nextflow:/user/yo/nextflow -v /db/pdb/local/data:/db/pdb/local/data -v $PWD:$PWD'
    }


    def 'test docker env'() {

        expect:
        DockerBuilder.makeEnv('X=1').toString() == '-e "X=1"'
        DockerBuilder.makeEnv([VAR_X:1, VAR_Y: 2]).toString() == '-e "VAR_X=1" -e "VAR_Y=2"'
        DockerBuilder.makeEnv( Paths.get('/some/file.env') ).toString() == '-e "BASH_ENV=/some/file.env"'
        DockerBuilder.makeEnv( new File('/some/file.env') ).toString() == '-e "BASH_ENV=/some/file.env"'
    }

    def 'test docker run command line'() {

        setup:
        def envFile = Paths.get('env-file')
        def db_file = Paths.get('/home/db')

        expect:
        new DockerBuilder('fedora').build() == 'docker run -i -v $PWD:$PWD -w $PWD fedora'
        new DockerBuilder('fedora').addEnv(envFile).build() == 'docker run -i -e "BASH_ENV=env-file" -v $PWD:$PWD -w $PWD fedora'
        new DockerBuilder('ubuntu').params(temp:'/hola').build() == 'docker run -i -v /hola:/tmp -v $PWD:$PWD -w $PWD ubuntu'
        new DockerBuilder('busybox').params(sudo: true).build() == 'sudo docker run -i -v $PWD:$PWD -w $PWD busybox'
        new DockerBuilder('busybox').params(entry: '/bin/bash').build() == 'docker run -i -v $PWD:$PWD -w $PWD --entrypoint /bin/bash busybox'
        new DockerBuilder('busybox').params(runOptions: '-x --zeta').build() == 'docker run -i -v $PWD:$PWD -w $PWD -x --zeta busybox'
        new DockerBuilder('busybox').params(userEmulation:true).build() == 'docker run -i -u $(id -u) -e "HOME=${HOME}" -v /etc/passwd:/etc/passwd:ro -v /etc/shadow:/etc/shadow:ro -v /etc/group:/etc/group:ro -v $HOME:$HOME -v $PWD:$PWD -w $PWD busybox'
        new DockerBuilder('busybox').setName('hola').build() == 'docker run -i -v $PWD:$PWD -w $PWD --name hola busybox'
        new DockerBuilder('busybox').params(engineOptions: '--tlsverify --tlscert="/path/to/my/cert"').build() == 'docker --tlsverify --tlscert="/path/to/my/cert" run -i -v $PWD:$PWD -w $PWD busybox'

        new DockerBuilder('fedora')
                .addEnv(envFile)
                .addMount(db_file)
                .addMount(db_file) // <-- add twice the same to prove that the final string won't contain duplicates
                .build() == 'docker run -i -e "BASH_ENV=env-file" -v /home/db:/home/db -v $PWD:$PWD -w $PWD fedora'

    }

    def 'test memory and cpuset' () {

        expect:
        new DockerBuilder('fedora').setCpus('1,2').build() == 'docker run -i --cpuset 1,2 -v $PWD:$PWD -w $PWD fedora'
        new DockerBuilder('fedora').setMemory('10g').build() == 'docker run -i --memory 10g -v $PWD:$PWD -w $PWD fedora'
        new DockerBuilder('fedora').setMemory(new MemoryUnit('100M')).build() == 'docker run -i --memory 100m -v $PWD:$PWD -w $PWD fedora'
        new DockerBuilder('fedora').setCpus('1-3').setMemory(new MemoryUnit('100M')).build() == 'docker run -i --cpuset 1-3 --memory 100m -v $PWD:$PWD -w $PWD fedora'

    }

    def 'test add mount'() {

        when:
        def docker = new DockerBuilder('fedora')
        docker.addMount(Paths.get('hello'))
        then:
        docker.mounts.size() == 1
        docker.mounts.contains(Paths.get('hello'))
{'fraction_non_alphanumeric': 0.1718985164480757, 'fraction_numerical': 0.010320361212642442, 'mean_word_length': 2.863372093023256, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5896729', 'n_tokens_mistral': 3367, 'n_tokens_neox': 3110, 'n_words': 847}