<commit_before>import os
import pytest
import yaml

from mock import patch, Mock

from teuthology.task import install


class TestInstall(object):

    def _get_default_package_list(self, project='ceph', debug=False):
        path = os.path.join(
            os.path.dirname(__file__),
            '..', '..', 'task', 'install', 'packages.yaml',
        )
        pkgs = yaml.safe_load(open(path))[project]
        if not debug:
            pkgs['deb'] = filter(
                lambda p: not p.endswith('-dbg'),
                pkgs['deb']
            )
            pkgs['rpm'] = filter(
                lambda p: not p.endswith('-debuginfo'),
                pkgs['rpm']
            )
        return pkgs

    def test_get_package_list_debug(self):
        default_pkgs = self._get_default_package_list(debug=True)
        default_pkgs['rpm'].sort()
        default_pkgs['deb'].sort()
        config = dict(debuginfo=True)
        result = install.get_package_list(ctx=None, config=config)
        result['rpm'].sort()
        result['deb'].sort()
        assert result == default_pkgs

    def test_get_package_list_no_debug(self):
        default_pkgs = self._get_default_package_list(debug=False)
        default_pkgs['rpm'].sort()
        default_pkgs['deb'].sort()
        config = dict(debuginfo=False)
        result = install.get_package_list(ctx=None, config=config)
        result['rpm'].sort()
        result['deb'].sort()
        assert result == default_pkgs

    def test_get_package_list_custom_rpm(self):
        default_pkgs = self._get_default_package_list(debug=False)
        default_pkgs['rpm'].sort()
        default_pkgs['deb'].sort()
        rpms = ['rpm1', 'rpm2', 'rpm2-debuginfo']
        config = dict(packages=dict(rpm=rpms))
        result = install.get_package_list(ctx=None, config=config)
        result['rpm'].sort()
        result['deb'].sort()
        assert result['rpm'] == ['rpm1', 'rpm2']
        assert result['deb'] == default_pkgs['deb']

    @patch("teuthology.task.install._get_builder_project")
    @patch("teuthology.task.install.packaging.get_package_version")
    def test_get_upgrade_version(self, m_get_package_version,
                                         m_gitbuilder_project):
        gb = Mock()
        gb.version = "11.0.0"
        gb.project = "ceph"
        m_gitbuilder_project.return_value = gb
        m_get_package_version.return_value = "11.0.0"
        install.get_upgrade_version(Mock(), Mock(), Mock())

    @patch("teuthology.task.install._get_builder_project")
    @patch("teuthology.task.install.packaging.get_package_version")
    def test_verify_ceph_version_success(self, m_get_package_version,
                                         m_gitbuilder_project):
        gb = Mock()
        gb.version = "0.89.0"
        gb.project = "ceph"
        m_gitbuilder_project.return_value = gb
        m_get_package_version.return_value = "0.89.0"
        install.verify_package_version(Mock(), Mock(), Mock())

    @patch("teuthology.task.install._get_builder_project")
    @patch("teuthology.task.install.packaging.get_package_version")
    def test_verify_ceph_version_failed(self, m_get_package_version,
                                        m_gitbuilder_project):
        gb = Mock()
        gb.version = "0.89.0"
        gb.project = "ceph"
        m_gitbuilder_project.return_value = gb
        m_get_package_version.return_value = "0.89.1"
        config = Mock()
        # when it looks for config.get('extras') it won't find it
        config.get.return_value = False
        with pytest.raises(RuntimeError):
            install.verify_package_version(Mock(), config, Mock())

    @patch("teuthology.task.install._get_builder_project")
    @patch("teuthology.task.install.packaging.get_package_version")
    def test_skip_when_using_ceph_deploy(self, m_get_package_version,
                                         m_gitbuilder_project):
        gb = Mock()
        gb.version = "0.89.0"
        gb.project = "ceph"
        m_gitbuilder_project.return_value = gb
        # ceph isn't installed because ceph-deploy would install it
        m_get_package_version.return_value = None
        config = Mock()
        config.extras = True
        install.verify_package_version(Mock(), config, Mock())

    def test_get_flavor_default(self):
        config = dict()
        assert install.get_flavor(config) == 'basic'

    def test_get_flavor_simple(self):
        config = dict(
            flavor='notcmalloc'
        )
        assert install.get_flavor(config) == 'notcmalloc'

    def test_get_flavor_valgrind(self):
        config = dict(
            valgrind=True
        )
        assert install.get_flavor(config) == 'notcmalloc'

    def test_upgrade_is_downgrade(self):
        assert_ok_vals = [
	    ('9.0.0', '10.0.0'),
	    ('10.2.2-63-g8542898-1trusty', '10.2.2-64-gabcdef1-1trusty'),
	    ('11.0.0-918.g13c13c7', '11.0.0-2165.gabcdef1')
	]
	for t in assert_ok_vals:
            assert install._upgrade_is_downgrade(t[0], t[1]) == False

    @patch("teuthology.packaging.get_package_version")
    @patch("teuthology.misc.get_system_type")
    @patch("teuthology.task.install.verify_package_version")<commit_msg>tests: use dict() instead Mock() to test config<commit_after>import os
import pytest
import yaml

from mock import patch, Mock

from teuthology.task import install


class TestInstall(object):

    def _get_default_package_list(self, project='ceph', debug=False):
        path = os.path.join(
            os.path.dirname(__file__),
            '..', '..', 'task', 'install', 'packages.yaml',
        )
        pkgs = yaml.safe_load(open(path))[project]
        if not debug:
            pkgs['deb'] = filter(
                lambda p: not p.endswith('-dbg'),
                pkgs['deb']
            )
            pkgs['rpm'] = filter(
                lambda p: not p.endswith('-debuginfo'),
                pkgs['rpm']
            )
        return pkgs

    def test_get_package_list_debug(self):
        default_pkgs = self._get_default_package_list(debug=True)
        default_pkgs['rpm'].sort()
        default_pkgs['deb'].sort()
        config = dict(debuginfo=True)
        result = install.get_package_list(ctx=None, config=config)
        result['rpm'].sort()
        result['deb'].sort()
        assert result == default_pkgs

    def test_get_package_list_no_debug(self):
        default_pkgs = self._get_default_package_list(debug=False)
        default_pkgs['rpm'].sort()
        default_pkgs['deb'].sort()
        config = dict(debuginfo=False)
        result = install.get_package_list(ctx=None, config=config)
        result['rpm'].sort()
        result['deb'].sort()
        assert result == default_pkgs

    def test_get_package_list_custom_rpm(self):
        default_pkgs = self._get_default_package_list(debug=False)
        default_pkgs['rpm'].sort()
        default_pkgs['deb'].sort()
        rpms = ['rpm1', 'rpm2', 'rpm2-debuginfo']
        config = dict(packages=dict(rpm=rpms))
        result = install.get_package_list(ctx=None, config=config)
        result['rpm'].sort()
        result['deb'].sort()
        assert result['rpm'] == ['rpm1', 'rpm2']
        assert result['deb'] == default_pkgs['deb']

    @patch("teuthology.task.install._get_builder_project")
    @patch("teuthology.task.install.packaging.get_package_version")
    def test_get_upgrade_version(self, m_get_package_version,
                                         m_gitbuilder_project):
        gb = Mock()
        gb.version = "11.0.0"
        gb.project = "ceph"
        m_gitbuilder_project.return_value = gb
        m_get_package_version.return_value = "11.0.0"
        install.get_upgrade_version(Mock(), Mock(), Mock())

    @patch("teuthology.task.install._get_builder_project")
    @patch("teuthology.task.install.packaging.get_package_version")
    def test_verify_ceph_version_success(self, m_get_package_version,
                                         m_gitbuilder_project):
        gb = Mock()
        gb.version = "0.89.0"
        gb.project = "ceph"
        m_gitbuilder_project.return_value = gb
        m_get_package_version.return_value = "0.89.0"
        config = dict()
        install.verify_package_version(Mock(), config, Mock())

    @patch("teuthology.task.install._get_builder_project")
    @patch("teuthology.task.install.packaging.get_package_version")
    def test_verify_ceph_version_failed(self, m_get_package_version,
                                        m_gitbuilder_project):
        gb = Mock()
        gb.version = "0.89.0"
        gb.project = "ceph"
        m_gitbuilder_project.return_value = gb
        m_get_package_version.return_value = "0.89.1"
        config = dict()
        with pytest.raises(RuntimeError):
            install.verify_package_version(Mock(), config, Mock())

    @patch("teuthology.task.install._get_builder_project")
    @patch("teuthology.task.install.packaging.get_package_version")
    def test_skip_when_using_ceph_deploy(self, m_get_package_version,
                                         m_gitbuilder_project):
        gb = Mock()
        gb.version = "0.89.0"
        gb.project = "ceph"
        m_gitbuilder_project.return_value = gb
        # ceph isn't installed because ceph-deploy would install it
        m_get_package_version.return_value = None
        config = dict()
        config['extras'] = True
        install.verify_package_version(Mock(), config, Mock())

    def test_get_flavor_default(self):
        config = dict()
        assert install.get_flavor(config) == 'basic'

    def test_get_flavor_simple(self):
        config = dict(
            flavor='notcmalloc'
        )
        assert install.get_flavor(config) == 'notcmalloc'

    def test_get_flavor_valgrind(self):
        config = dict(
            valgrind=True
        )
        assert install.get_flavor(config) == 'notcmalloc'

    def test_upgrade_is_downgrade(self):
        assert_ok_vals = [
	    ('9.0.0', '10.0.0'),
	    ('10.2.2-63-g8542898-1trusty', '10.2.2-64-gabcdef1-1trusty'),
	    ('11.0.0-918.g13c13c7', '11.0.0-2165.gabcdef1')
	]
	for t in assert_ok_vals:
            assert install._upgrade_is_downgrade(t[0], t[1]) == False

    @patch("teuthology.packaging.get_package_version")
    @patch("teuthology.misc.get_system_type")
    @patch("teuthology.task.install.verify_package_version")
{'fraction_non_alphanumeric': 0.12284087619415228, 'fraction_numerical': 0.01640451606677603, 'mean_word_length': 2.522773623385452, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '805140', 'n_tokens_mistral': 3439, 'n_tokens_neox': 3237, 'n_words': 497}