<commit_before>import base64

import py
import pytest
import yaml

import generate_build_config


class TestGetPPASnippet(object):

    def test_unknown_url(self):
        with pytest.raises(ValueError):
            generate_build_config._get_ppa_snippet('ftp://blah')

    def test_public_ppa(self):
        result = generate_build_config._get_ppa_snippet('ppa:foo/bar')
        expected = '- chroot $CHROOT_ROOT add-apt-repository -y -u ppa:foo/bar'
        assert result == expected

    def test_https_not_private_ppa(self):
        with pytest.raises(ValueError):
            generate_build_config._get_ppa_snippet('https://blah')

    def test_private_ppa_no_key(self):
        with pytest.raises(ValueError):
            generate_build_config._get_ppa_snippet(
                'https://private-ppa.example.com')

    def test_private_ppa_with_key(self):
        result = generate_build_config._get_ppa_snippet(
            'https://private-ppa.example.com', 'DEADBEEF')
        assert 'apt-get install -y apt-transport-https' in result
        assert 'deb https://private-ppa.example.com xenial main' in result
        assert ('apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 '
                '--recv-keys DEADBEEF' in result)


class TestWriteCloudConfig(object):

    def test_writes_to_file(self, tmpdir):
        output_file = tmpdir.join('output.yaml')
        generate_build_config._write_cloud_config(output_file.strpath)
        assert output_file.check()

    def test_written_output_is_yaml(self, tmpdir):
        output_file = tmpdir.join('output.yaml')
        generate_build_config._write_cloud_config(output_file.strpath)
        yaml.load(output_file.read())

    def test_written_output_is_cloud_config(self, tmpdir):
        output_file = tmpdir.join('output.yaml')
        generate_build_config._write_cloud_config(output_file.strpath)
        assert '#cloud-config' == output_file.readlines(cr=False)[0].strip()

    def test_default_build_id_is_output(self, tmpdir):
        output_file = tmpdir.join('output.yaml')
        generate_build_config._write_cloud_config(output_file.strpath)
        assert '- export BUILD_ID=output\n' in output_file.readlines()

    def test_write_files_not_included_by_default(self, tmpdir):
        output_file = tmpdir.join('output.yaml')
        generate_build_config._write_cloud_config(output_file.strpath)
        cloud_config = yaml.load(output_file.open())
        assert 'write_files' not in cloud_config

    def test_no_ppa_included_by_default(self, tmpdir):
        output_file = tmpdir.join('output.yaml')
        generate_build_config._write_cloud_config(output_file.strpath)
        assert 'add-apt-repository' not in output_file.read()
        assert 'apt-transport-https' not in output_file.read()

    def test_ppa_snippet_included(self, tmpdir):
        output_file = tmpdir.join('output.yaml')
        generate_build_config._write_cloud_config(
            output_file.strpath, ppa='ppa:foo/bar')
        assert 'add-apt-repository -y -u ppa:foo/bar' in output_file.read()


def customisation_script_combinations():
    customisation_script_content = '#!/bin/sh\nchroot'
    binary_customisation_script_content = '#!/bin/sh\nbinary'
    return [
        {'customisation_script': customisation_script_content},
        {'binary_customisation_script': binary_customisation_script_content},
        {'customisation_script': customisation_script_content,
         'binary_customisation_script': binary_customisation_script_content},
    ]


class TestWriteCloudConfigWithCustomisationScript(object):

    @pytest.fixture(autouse=True, params=customisation_script_combinations())
    def customisation_script_tmpdir(self, request, tmpdir):
        self.output_file = tmpdir.join('output.yaml')
        self.kwargs = {}
        self.test_config = {}
        for script in request.param:
            script_file = tmpdir.join(script + '.sh')
            script_file.write(request.param[script])
            self.kwargs[script] = script_file.strpath
            self.test_config[script] = {'script_file': script_file,<commit_msg>Add test that release image is used as chroot base<commit_after>import base64

import py
import pytest
import yaml

import generate_build_config


class TestGetPPASnippet(object):

    def test_unknown_url(self):
        with pytest.raises(ValueError):
            generate_build_config._get_ppa_snippet('ftp://blah')

    def test_public_ppa(self):
        result = generate_build_config._get_ppa_snippet('ppa:foo/bar')
        expected = '- chroot $CHROOT_ROOT add-apt-repository -y -u ppa:foo/bar'
        assert result == expected

    def test_https_not_private_ppa(self):
        with pytest.raises(ValueError):
            generate_build_config._get_ppa_snippet('https://blah')

    def test_private_ppa_no_key(self):
        with pytest.raises(ValueError):
            generate_build_config._get_ppa_snippet(
                'https://private-ppa.example.com')

    def test_private_ppa_with_key(self):
        result = generate_build_config._get_ppa_snippet(
            'https://private-ppa.example.com', 'DEADBEEF')
        assert 'apt-get install -y apt-transport-https' in result
        assert 'deb https://private-ppa.example.com xenial main' in result
        assert ('apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 '
                '--recv-keys DEADBEEF' in result)


class TestWriteCloudConfig(object):

    def test_writes_to_file(self, tmpdir):
        output_file = tmpdir.join('output.yaml')
        generate_build_config._write_cloud_config(output_file.strpath)
        assert output_file.check()

    def test_written_output_is_yaml(self, tmpdir):
        output_file = tmpdir.join('output.yaml')
        generate_build_config._write_cloud_config(output_file.strpath)
        yaml.load(output_file.read())

    def test_written_output_is_cloud_config(self, tmpdir):
        output_file = tmpdir.join('output.yaml')
        generate_build_config._write_cloud_config(output_file.strpath)
        assert '#cloud-config' == output_file.readlines(cr=False)[0].strip()

    def test_default_build_id_is_output(self, tmpdir):
        output_file = tmpdir.join('output.yaml')
        generate_build_config._write_cloud_config(output_file.strpath)
        assert '- export BUILD_ID=output\n' in output_file.readlines()

    def test_write_files_not_included_by_default(self, tmpdir):
        output_file = tmpdir.join('output.yaml')
        generate_build_config._write_cloud_config(output_file.strpath)
        cloud_config = yaml.load(output_file.open())
        assert 'write_files' not in cloud_config

    def test_no_ppa_included_by_default(self, tmpdir):
        output_file = tmpdir.join('output.yaml')
        generate_build_config._write_cloud_config(output_file.strpath)
        assert 'add-apt-repository' not in output_file.read()
        assert 'apt-transport-https' not in output_file.read()

    def test_ppa_snippet_included(self, tmpdir):
        output_file = tmpdir.join('output.yaml')
        generate_build_config._write_cloud_config(
            output_file.strpath, ppa='ppa:foo/bar')
        assert 'add-apt-repository -y -u ppa:foo/bar' in output_file.read()

    def test_release_image_used(self, tmpdir):
        output_file = tmpdir.join('output.yaml')
        generate_build_config._write_cloud_config(
            output_file.strpath, ppa='ppa:foo/bar')
        wget_lines = [
            line for line in output_file.readlines() if 'wget' in line]
        assert 1 == len(wget_lines)
        assert (
            'ubuntu-16.04-server-cloudimg-amd64-root.tar.xz ' in wget_lines[0])


def customisation_script_combinations():
    customisation_script_content = '#!/bin/sh\nchroot'
    binary_customisation_script_content = '#!/bin/sh\nbinary'
    return [
        {'customisation_script': customisation_script_content},
        {'binary_customisation_script': binary_customisation_script_content},
        {'customisation_script': customisation_script_content,
         'binary_customisation_script': binary_customisation_script_content},
    ]


class TestWriteCloudConfigWithCustomisationScript(object):

    @pytest.fixture(autouse=True, params=customisation_script_combinations())
    def customisation_script_tmpdir(self, request, tmpdir):
        self.output_file = tmpdir.join('output.yaml')
        self.kwargs = {}
        self.test_config = {}
        for script in request.param:
            script_file = tmpdir.join(script + '.sh')
            script_file.write(request.param[script])
            self.kwargs[script] = script_file.strpath
            self.test_config[script] = {'script_file': script_file,
{'fraction_non_alphanumeric': 0.10084130459836349, 'fraction_numerical': 0.0020744496945949062, 'mean_word_length': 3.463991769547325, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, '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': '690601', 'n_tokens_mistral': 2741, 'n_tokens_neox': 2635, 'n_words': 417}