1# -*- coding: utf-8 -*-
2
3# Copyright (C) 2012-2018 Red Hat, Inc.
4#
5# This copyrighted material is made available to anyone wishing to use,
6# modify, copy, or redistribute it subject to the terms and conditions of
7# the GNU General Public License v.2, or (at your option) any later version.
8# This program is distributed in the hope that it will be useful, but WITHOUT
9# ANY WARRANTY expressed or implied, including the implied warranties of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11# Public License for more details.  You should have received a copy of the
12# GNU General Public License along with this program; if not, write to the
13# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
14# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
15# source code or documentation are not subject to the GNU General Public
16# License and may only be used or replicated with the express permission of
17# Red Hat, Inc.
18#
19
20from __future__ import absolute_import
21from __future__ import unicode_literals
22
23import dnf.exceptions
24import dnf.repo
25import dnf.sack
26
27import tests.support
28from tests.support import mock
29
30
31class SackTest(tests.support.DnfBaseTestCase):
32
33    REPOS = []
34
35    def test_rpmdb_version(self):
36        version = self.sack._rpmdb_version()
37        self.assertIsNotNone(version)
38        expected = "%s:%s" % (tests.support.TOTAL_RPMDB_COUNT, tests.support.RPMDB_CHECKSUM)
39        self.assertEqual(version, expected)
40
41    def test_excludepkgs(self):
42        self.base.conf.excludepkgs = ['pepper']
43        self.base._setup_excludes_includes()
44        peppers = self.base.sack.query().filter(name='pepper').run()
45        self.assertLength(peppers, 0)
46
47    def test_exclude(self):
48        self.base.conf.exclude = ['pepper']
49        self.base._setup_excludes_includes()
50        peppers = self.base.sack.query().filter(name='pepper').run()
51        self.assertLength(peppers, 0)
52
53    def test_disable_excludes(self):
54        self.base.conf.disable_excludes = ['all']
55        self.base.conf.excludepkgs = ['pepper']
56        self.base._setup_excludes_includes()
57        peppers = self.base.sack.query().filter(name='pepper').run()
58        self.assertLength(peppers, 1)
59
60    def test_excludepkgs_glob(self):
61        # override base with custom repos
62        self.base = tests.support.MockBase('main')
63        self.base.repos['main'].excludepkgs = ['pepp*']
64        self.base._setup_excludes_includes()
65        peppers = self.base.sack.query().filter(name='pepper', reponame='main')
66        self.assertLength(peppers, 0)
67
68    def test_excludepkgs_includepkgs(self):
69        self.base.conf.excludepkgs = ['*.i?86']
70        self.base.conf.includepkgs = ['lib*']
71        self.base._setup_excludes_includes()
72        peppers = self.base.sack.query().run()
73        self.assertLength(peppers, 1)
74        self.assertEqual(str(peppers[0]), "librita-1-1.x86_64")
75
76    @mock.patch('dnf.sack._build_sack', lambda x: mock.Mock())
77    @mock.patch('dnf.goal.Goal', lambda x: mock.Mock())
78    def test_fill_sack(self):
79        def raiser():
80            raise dnf.exceptions.RepoError()
81
82        r = tests.support.MockRepo('bag', self.base.conf)
83        r.enable()
84        self.base._repos.add(r)
85        r.load = mock.Mock(side_effect=raiser)
86        r.skip_if_unavailable = False
87        self.assertRaises(dnf.exceptions.RepoError,
88                          self.base.fill_sack, load_system_repo=False)
89        self.assertTrue(r.enabled)
90        self.assertTrue(r._check_config_file_age)
91