Ejemplo n.º 1
0
    def test_generate_command(self):
        # Basic valid case
        base_step = local_cloudbuild.Step(
            args=['arg1', 'arg2'],
            dir_='',
            env=['ENV1=value1', 'ENV2=value2'],
            name='aname',
        )
        command = local_cloudbuild.generate_command(base_step)
        self.assertEqual(command, [
            'docker',
            'run',
            '--volume',
            '/var/run/docker.sock:/var/run/docker.sock',
            '--volume',
            '/root/.docker:/root/.docker',
            '--volume',
            '${HOST_WORKSPACE}:/workspace',
            '--workdir',
            '/workspace',
            '--env',
            'ENV1=value1',
            '--env',
            'ENV2=value2',
            'aname',
            'arg1',
            'arg2',
        ])

        # dir specified
        step = base_step._replace(dir_='adir')
        command = local_cloudbuild.generate_command(step)
        self.assertIn('--workdir', command)
        self.assertIn('/workspace/adir', command)

        # Shell quoting
        step = base_step._replace(args=['arg with \n newline'])
        command = local_cloudbuild.generate_command(step)
        self.assertIn("'arg with \n newline'", command)

        step = base_step._replace(dir_='dir/ with space/')
        command = local_cloudbuild.generate_command(step)
        self.assertIn("/workspace/'dir/ with space/'", command)

        step = base_step._replace(env=['env with space'])
        command = local_cloudbuild.generate_command(step)
        self.assertIn("'env with space'", command)

        step = base_step._replace(name='a name')
        command = local_cloudbuild.generate_command(step)
        self.assertIn("'a name'", command)
def test_generate_command_basic():
    command = local_cloudbuild.generate_command(_base_step, _subs, set())
    assert command == [
        'docker',
        'run',
        '--volume',
        '/var/run/docker.sock:/var/run/docker.sock',
        '--volume',
        '/root/.docker:/root/.docker',
        '--volume',
        '${HOST_WORKSPACE}:/workspace',
        '--workdir',
        '/workspace',
        '--env',
        'ENV1=value1',
        '--env',
        'ENV2=value2',
        'aname',
        'arg1',
        'arg2',
    ]
def test_generate_command_basic():
    command = local_cloudbuild.generate_command(_base_step, _subs, set())
    assert command == [
        'docker',
        'run',
        '--volume',
        '/var/run/docker.sock:/var/run/docker.sock',
        '--volume',
        '/root/.docker:/root/.docker',
        '--volume',
        '${HOST_WORKSPACE}:/workspace',
        '--workdir',
        '/workspace',
        '--env',
        'ENV1=value1',
        '--env',
        'ENV2=value2',
        'aname',
        'arg1',
        'arg2',
    ]
def test_generate_command_invalid(step):
    with pytest.raises(ValueError):
        local_cloudbuild.generate_command(step, _subs, set())
def test_generate_command_valid(step, args):
    command = local_cloudbuild.generate_command(step, _subs, set())
    for arg in args:
        assert arg in command
    def test_generate_command(self):
        # Basic valid case
        base_step = local_cloudbuild.Step(
            args=['arg1', 'arg2'],
            dir_='',
            env=['ENV1=value1', 'ENV2=value2'],
            name='aname',
        )
        subs = {'BUILTIN': 'builtin', '_USER': '******'}
        command = local_cloudbuild.generate_command(base_step, subs, set())
        self.assertEqual(command, [
            'docker',
            'run',
            '--volume',
            '/var/run/docker.sock:/var/run/docker.sock',
            '--volume',
            '/root/.docker:/root/.docker',
            '--volume',
            '${HOST_WORKSPACE}:/workspace',
            '--workdir',
            '/workspace',
            '--env',
            'ENV1=value1',
            '--env',
            'ENV2=value2',
            'aname',
            'arg1',
            'arg2',
        ])

        valid_cases = (
            # dir specified
            (base_step._replace(dir_='adir'), ['--workdir',
                                               '/workspace/adir']),
            # Shell quoting
            (base_step._replace(args=['arg with \n newline']),
             ["'arg with \n newline'"]),
            (base_step._replace(dir_='dir/ with space/'),
             ["/workspace/'dir/ with space/'"]),
            (base_step._replace(env=['env with space']), ["'env with space'"]),
            (base_step._replace(name='a name'), ["'a name'"]),
            # Variable substitution
            (base_step._replace(name='a $BUILTIN substitution'),
             ["'a builtin substitution'"]),
            (base_step._replace(name='a $_USER substitution'),
             ["'a _user substitution'"]),
            (base_step._replace(name='a curly brace ${BUILTIN} substitution'),
             ["'a curly brace builtin substitution'"]),
            (base_step._replace(
                name='an escaped $$ or $$$$ or $$FOO or $${_FOO} is unescaped'
            ), ["'an escaped $ or $$ or $FOO or ${_FOO} is unescaped'"]),
        )
        for valid_case in valid_cases:
            with self.subTest(valid_case=valid_case):
                step, args = valid_case
                command = local_cloudbuild.generate_command(step, subs, set())
                for arg in args:
                    self.assertIn(arg, command)

        invalid_cases = (
            base_step._replace(name='a $UNSET_BUILTIN substitution'),
            base_step._replace(name='a $_UNSET_USER substitution'),
        )
        for invalid_case in invalid_cases:
            with self.subTest(invalid_case=invalid_case):
                step = invalid_case
                with self.assertRaises(ValueError):
                    local_cloudbuild.generate_command(step, subs, set())
def test_generate_command_invalid(step):
    with pytest.raises(ValueError):
        local_cloudbuild.generate_command(step, _subs, set())
def test_generate_command_valid(step, args):
    command = local_cloudbuild.generate_command(step, _subs, set())
    for arg in args:
        assert arg in command