~barry/ubuntu/maverick/computer-janitor/bug-665740

« back to all changes in this revision

Viewing changes to computerjanitord/tests/test_application.py

  • Committer: Bazaar Package Importer
  • Author(s): Barry Warsaw
  • Date: 2010-05-21 16:55:46 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100521165546-53kn5xq1irmkjazx
Tags: 2.0.1-0ubuntu1
BumpingĀ forĀ Ubuntu.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008, 2009, 2010  Canonical, Ltd.
 
2
#
 
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.
 
6
#
 
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.
 
11
#
 
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/>.
 
14
 
 
15
"""Test the plugin application interfaces."""
 
16
 
 
17
from __future__ import absolute_import, unicode_literals
 
18
 
 
19
__metaclass__ = type
 
20
__all__ = [
 
21
    'ApplicationTestSetupMixin',
 
22
    'test_suite',
 
23
    ]
 
24
 
 
25
 
 
26
import os
 
27
import apt
 
28
import apt_pkg
 
29
import unittest
 
30
import warnings
 
31
import pkg_resources
 
32
 
 
33
from contextlib import contextmanager
 
34
 
 
35
import computerjanitor
 
36
import computerjanitord.application
 
37
 
 
38
from computerjanitord.application import (
 
39
    Application, MissingLandmarkError, NonDownloadableError)
 
40
 
 
41
 
 
42
@contextmanager
 
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
 
47
    yield
 
48
    computerjanitord.application.LANDMARK_PACKAGES[:] = old_landmarks
 
49
 
 
50
 
 
51
class MockCruft:
 
52
    def __init__(self, name):
 
53
        self.name = name
 
54
 
 
55
    def get_name(self):
 
56
        warnings.warn('.get_name() is deprecated; use .name',
 
57
                      DeprecationWarning)
 
58
        return self.name
 
59
 
 
60
 
 
61
class ApplicationTestSetupMixin:
 
62
    """Set up an `Application` instance with test data in its apt_cache."""
 
63
 
 
64
    def setUp(self):
 
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)
 
71
 
 
72
    def tearDown(self):
 
73
        # Clear the 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))
 
78
 
 
79
 
 
80
class TestApplication(unittest.TestCase, ApplicationTestSetupMixin):
 
81
    """Test the `Application` interface."""
 
82
 
 
83
    def setUp(self):
 
84
        ApplicationTestSetupMixin.setUp(self)
 
85
 
 
86
    def tearDown(self):
 
87
        ApplicationTestSetupMixin.tearDown(self)
 
88
 
 
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)
 
92
 
 
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)
 
97
 
 
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)
 
101
 
 
102
 
 
103
def test_suite():
 
104
    suite = unittest.TestSuite()
 
105
    suite.addTests(unittest.makeSuite(TestApplication))
 
106
    return suite