Blame tests/test_config_manager.py

Packit 3a9065
# Copyright (C) 2015  Red Hat, Inc.
Packit 3a9065
#
Packit 3a9065
# This copyrighted material is made available to anyone wishing to use,
Packit 3a9065
# modify, copy, or redistribute it subject to the terms and conditions of
Packit 3a9065
# the GNU General Public License v.2, or (at your option) any later version.
Packit 3a9065
# This program is distributed in the hope that it will be useful, but WITHOUT
Packit 3a9065
# ANY WARRANTY expressed or implied, including the implied warranties of
Packit 3a9065
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit 3a9065
# Public License for more details.  You should have received a copy of the
Packit 3a9065
# GNU General Public License along with this program; if not, write to the
Packit 3a9065
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 3a9065
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
Packit 3a9065
# source code or documentation are not subject to the GNU General Public
Packit 3a9065
# License and may only be used or replicated with the express permission of
Packit 3a9065
# Red Hat, Inc.
Packit 3a9065
#
Packit 3a9065
Packit 3a9065
from __future__ import absolute_import
Packit 3a9065
from __future__ import unicode_literals
Packit 3a9065
from tests.support import command_run, mock
Packit 3a9065
Packit 3a9065
import config_manager
Packit 3a9065
import dnf
Packit 3a9065
import filecmp
Packit 3a9065
import os
Packit 3a9065
import shutil
Packit 3a9065
import tempfile
Packit 3a9065
import unittest
Packit 3a9065
Packit 3a9065
Packit 3a9065
REPOLABEL = "testrepo"
Packit 3a9065
REPOCONTENT = """[testrepo]
Packit 3a9065
name=TestRepo
Packit 3a9065
baseurl=file:///tmp
Packit 3a9065
enabled=1
Packit 3a9065
"""
Packit 3a9065
Packit 3a9065
class ConfigManagerBase(mock.MagicMock, dnf.base.Base):
Packit 3a9065
      conf = dnf.conf.Conf()
Packit 3a9065
Packit 3a9065
class ConfigManagerCommandTest(unittest.TestCase):
Packit 3a9065
Packit 3a9065
    def setUp(self):
Packit 3a9065
        cli = dnf.cli.cli.Cli(ConfigManagerBase())
Packit 3a9065
        self.cmd = config_manager.ConfigManagerCommand(cli)
Packit 3a9065
        self.cmd.base.conf.reposdir = [tempfile.mkdtemp()]
Packit 3a9065
Packit 3a9065
        self.addCleanup(shutil.rmtree, self.cmd.base.conf.reposdir[0])
Packit 3a9065
Packit 3a9065
    def test_add_from_repofile(self):
Packit 3a9065
        tempfile_kwargs = {'mode': 'w', 'suffix': '.repo', 'delete': False}
Packit 3a9065
        if dnf.pycomp.PY3:
Packit 3a9065
            tempfile_kwargs['encoding'] = 'utf8'
Packit 3a9065
Packit 3a9065
        repofile = tempfile.NamedTemporaryFile(**tempfile_kwargs)
Packit 3a9065
        dnf.pycomp.write_to_file(repofile, REPOCONTENT)
Packit 3a9065
        repofile.close()
Packit 3a9065
Packit 3a9065
        command_run(self.cmd, ['--add-repo', repofile.name])
Packit 3a9065
Packit 3a9065
        installed_repofile = os.path.join(self.cmd.base.conf.reposdir[0],
Packit 3a9065
                                          os.path.basename(repofile.name))
Packit 3a9065
        with open(installed_repofile) as f:
Packit 3a9065
            added = f.read()
Packit 3a9065
Packit 3a9065
        self.assertMultiLineEqual(REPOCONTENT, added)
Packit 3a9065
Packit 3a9065
        def get_matching(x):
Packit 3a9065
            repo = dnf.repo.Repo(x, self.cmd.base.conf)
Packit 3a9065
            repo.repofile = installed_repofile
Packit 3a9065
            return [repo]
Packit 3a9065
        self.cmd.base.repos.get_matching = get_matching
Packit 3a9065
        self.cmd.base.output = dnf.cli.output.Output(self.cmd.base, self.cmd.base.conf)
Packit 3a9065
Packit 3a9065
        self.subtest_disable(REPOLABEL, installed_repofile)
Packit 3a9065
        self.subtest_enable(REPOLABEL, installed_repofile)
Packit 3a9065
Packit 3a9065
        os.unlink(repofile.name)
Packit 3a9065
Packit 3a9065
    def test_add_from_repourl(self):
Packit 3a9065
        long_url = 'file:///tmp/%s' %  ('/'.join([str(x) for x in range(1,100)]))
Packit 3a9065
        name = 'tmp_1_2_3_4_5_6_7_8_9_10_11_12_13_14_15_16_17_18_19_20_21' \
Packit 3a9065
               + '_22_23_24_25_26_27_28_29_30_31_32_33_34_35_36_37_38_39' \
Packit 3a9065
               + '_40_41_42_43_44_45_46_47_48_49_50_51_52_53_54_55_56_57' \
Packit 3a9065
               + '_58_59_60_61_62_63__b3085fd1c13941e151ce4afe9d8670774d' \
Packit 3a9065
               + 'c8e19395f2a785bc1d210eccc0869b'
Packit 3a9065
        repofile = name + '.repo'
Packit 3a9065
Packit 3a9065
        repocontent = "[%s]\nname=created by dnf config-manager from %s\n" \
Packit 3a9065
                      "baseurl=%s\nenabled=1\n" % (name, long_url, long_url)
Packit 3a9065
        command_run(self.cmd, ['--add-repo', long_url])
Packit 3a9065
        with open(os.path.join(self.cmd.base.conf.reposdir[0], repofile)) as f:
Packit 3a9065
            added = f.read()
Packit 3a9065
Packit 3a9065
        self.assertMultiLineEqual(repocontent, added)
Packit 3a9065
Packit 3a9065
    def subtest_disable(self, label, fname):
Packit 3a9065
        command_run(self.cmd, ['--set-disabled', label])
Packit 3a9065
Packit 3a9065
        with open(fname) as f:
Packit 3a9065
            added = f.read()
Packit 3a9065
        disabled = REPOCONTENT.replace("enabled=1", "enabled=0")
Packit 3a9065
        self.assertMultiLineEqual(disabled, added)
Packit 3a9065
Packit 3a9065
    def subtest_enable(self, label, fname):
Packit 3a9065
        command_run(self.cmd, ['--set-enabled', label])
Packit 3a9065
Packit 3a9065
        with open(fname) as f:
Packit 3a9065
            added = f.read()
Packit 3a9065
        self.assertMultiLineEqual(REPOCONTENT, added)