def test_generate_script_golden(self):
     config_name = 'cloudbuild_ok.yaml'
     config = os.path.join(self.testdata_dir, config_name)
     expected_output_script = os.path.join(self.testdata_dir,
                                           config_name + '_golden.sh')
     cloudbuild = local_cloudbuild.CloudBuild(
         output_script='test_generate_script',
         run=False,
         steps=[
             local_cloudbuild.Step(
                 args=['/bin/sh', '-c', 'printenv MESSAGE'],
                 dir_='',
                 env=['MESSAGE=Hello World!'],
                 name='debian',
             ),
             local_cloudbuild.Step(
                 args=['/bin/sh', '-c', 'printenv MESSAGE'],
                 dir_='',
                 env=['MESSAGE=Goodbye\\n And Farewell!', 'UNUSED=unused'],
                 name='debian',
             )
         ],
         substitutions=local_cloudbuild.DEFAULT_SUBSTITUTIONS,
     )
     actual = local_cloudbuild.generate_script(cloudbuild)
     self.maxDiff = 2**16
     # Compare output against golden
     with open(expected_output_script, 'r', encoding='utf8') as expected:
         self.assertEqual(actual, expected.read())
Beispiel #2
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(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_get_step(self):
        valid_cases = (
            # Empty step
            ({}, local_cloudbuild.Step(
                args=[],
                dir_='',
                env=[],
                name='',
            )),
            # Full step
            ({
                'name': 'aname',
                'args': [
                    'arg1',
                    2,
                    'arg3 with \n newline',
                ],
                'env': ['ENV1=value1', 'ENV2=space in value2'],
                'dir': 'adir',
            },
             local_cloudbuild.Step(
                 args=[
                     'arg1',
                     '2',
                     'arg3 with \n newline',
                 ],
                 env=['ENV1=value1', 'ENV2=space in value2'],
                 dir_='adir',
                 name='aname',
             )),
        )
        for valid_case in valid_cases:
            with self.subTest(valid_case=valid_case):
                raw_step, expected = valid_case
                actual = local_cloudbuild.get_step(raw_step)
                self.assertEqual(actual, expected)

        invalid_cases = (
            # Wrong type
            [],
            # More wrong types
            {
                'args': 'not_a_list'
            },
            {
                'args': [[]]
            },
            {
                'env': 'not_a_list'
            },
            {
                'env': [{}]
            },
            {
                'dir': {}
            },
            {
                'name': []
            },
        )
        for invalid_case in invalid_cases:
            with self.subTest(invalid_case=invalid_case):
                with self.assertRaises(ValueError):
                    local_cloudbuild.get_step(invalid_case)
        # Steps not a list
        'steps: astring\n',
    ])
def test_get_cloudbuild_invalid(raw_yaml):
    raw_config = yaml.safe_load(raw_yaml)
    with pytest.raises(ValueError):
        local_cloudbuild.get_cloudbuild(raw_config, _args)


@pytest.mark.parametrize(
    'raw_step, expected',
    [
        # Empty step
        ({}, local_cloudbuild.Step(
            args=[],
            dir_='',
            env=[],
            name='',
        )),
        # Full step
        ({
            'name': 'aname',
            'args': ['arg1', 2, 'arg3 with \n newline'],
            'env': ['ENV1=value1', 'ENV2=space in value2'],
            'dir': 'adir',
        },
         local_cloudbuild.Step(
             args=['arg1', '2', 'arg3 with \n newline'],
             env=['ENV1=value1', 'ENV2=space in value2'],
             dir_='adir',
             name='aname',
         )),