1
# Copyright (C) 2008, 2009, 2010 Canonical, Ltd.
3
# This program is free software: you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation, version 3 of the License.
7
# This program is distributed in the hope that it will be useful,
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
# GNU General Public License for more details.
12
# You should have received a copy of the GNU General Public License
13
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
"""Test the plugin application interfaces."""
17
from __future__ import absolute_import, unicode_literals
21
'ApplicationTestSetupMixin',
33
from contextlib import contextmanager
35
import computerjanitor
36
import computerjanitord.application
38
from computerjanitord.application import (
39
Application, MissingLandmarkError, NonDownloadableError)
43
def landmarks(*packages):
44
# Hack the module global list of known landmark packages.
45
old_landmarks = computerjanitord.application.LANDMARK_PACKAGES[:]
46
computerjanitord.application.LANDMARK_PACKAGES[:] = packages
48
computerjanitord.application.LANDMARK_PACKAGES[:] = old_landmarks
52
def __init__(self, name):
56
warnings.warn('.get_name() is deprecated; use .name',
61
class ApplicationTestSetupMixin:
62
"""Set up an `Application` instance with test data in its apt_cache."""
65
self.data_dir = os.path.abspath(
66
pkg_resources.resource_filename('computerjanitord.tests', 'data'))
67
# Make the test insensitive to the platform's architecture.
68
apt_pkg.Config.Set('APT::Architecture', 'i386')
69
self.cache = apt.Cache(rootdir=self.data_dir)
70
self.app = Application(self.cache)
74
cache_dir = os.path.join(self.data_dir, 'var', 'cache', 'apt')
75
for filename in os.listdir(cache_dir):
76
if filename.endswith('.bin'):
77
os.remove(os.path.join(cache_dir, filename))
80
class TestApplication(unittest.TestCase, ApplicationTestSetupMixin):
81
"""Test the `Application` interface."""
84
ApplicationTestSetupMixin.setUp(self)
87
ApplicationTestSetupMixin.tearDown(self)
89
def test_verify_apt_cache_good_path(self):
90
# All essential packages are in the cache by default.
91
self.assertEqual(self.app.verify_apt_cache(), None)
93
def test_verify_apt_cache_with_nondownloadable_landmark(self):
94
# Test that a missing landmark file causes an exception.
95
with landmarks('gzip', 'dash-nodownload'):
96
self.assertRaises(NonDownloadableError, self.app.verify_apt_cache)
98
def test_verify_apt_cache_with_missing_landmark(self):
99
with landmarks('gzip', 'dash', 'i-am-not-here'):
100
self.assertRaises(MissingLandmarkError, self.app.verify_apt_cache)
104
suite = unittest.TestSuite()
105
suite.addTests(unittest.makeSuite(TestApplication))