1
# -*- coding: utf-8 -*-
3
# Authors: Natalia B. Bidart <nataliabidart@canonical.com>
5
# Copyright 2010 Canonical Ltd.
7
# This program is free software: you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License version 3, as published
9
# by the Free Software Foundation.
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
14
# PURPOSE. See the GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License along
17
# with this program. If not, see <http://www.gnu.org/licenses/>.
19
"""Tests for the package manager service."""
24
# Unable to import 'defer', pylint: disable=F0401,E0611,W0404
25
from aptdaemon import defer
27
# Unable to import 'defer', pylint: disable=F0401,E0611,W0404
30
from twisted.internet.defer import inlineCallbacks
32
from ubuntuone.controlpanel.gui.gtk import package_manager
33
from ubuntuone.controlpanel.tests import TestCase
37
SUCCESS = package_manager.aptdaemon.enums.EXIT_SUCCESS
38
FAILURE = package_manager.aptdaemon.enums.EXIT_FAILED
41
class FakedPackage(object):
44
def __init__(self, name=None, is_installed=False):
46
self.is_installed = is_installed
49
class FakedTransaction(object):
50
"""Fake a transaction."""
54
def __init__(self, packages, cache=None):
55
self._signals = collections.defaultdict(list)
58
self.packages = packages
59
self.connect = lambda sig, f: self._signals[sig].append(f)
65
if self._cache is not None:
66
for package in self.packages:
67
FAKED_CACHE[package].is_installed = True
69
if self.failure is None:
74
for listener in self._signals['finished']:
82
class FakedClient(object):
83
"""Fake an apt client."""
85
def install_packages(self, packages_names, **kwargs):
86
"""Install packages listed in 'package_names'.
88
package_names - a list of package names
89
wait - if True run the transaction immediately and return its exit
90
state instead of the transaction itself.
91
reply_handler - callback function. If specified in combination with
92
error_handler the method will be called asynchrounsouly.
93
error_handler - in case of an error the given callback gets the
94
corresponding DBus exception instance
97
if kwargs.get('wait', False):
98
return package_manager.aptdaemon.enums.EXIT_FAILED
100
d.callback(FakedTransaction(packages_names, cache=FAKED_CACHE))
104
class PackageManagerTestCase(TestCase):
105
"""Test for the package manager."""
111
yield super(PackageManagerTestCase, self).setUp()
112
FAKED_CACHE.clear() # clean cache
113
self.patch(package_manager.apt, 'Cache', lambda: FAKED_CACHE)
114
self.patch(package_manager.aptdaemon.client, 'AptClient', FakedClient)
115
self.obj = package_manager.PackageManager()
117
def test_is_installed(self):
118
"""Check that a package is installed."""
120
FAKED_CACHE[name] = FakedPackage(name, is_installed=True)
122
result = self.obj.is_installed(name)
124
self.assertTrue(result, 'must be installed')
126
def test_is_not_installed(self):
127
"""Check if a package is installed."""
129
FAKED_CACHE[name] = FakedPackage(name) # not installed by default
131
result = self.obj.is_installed(name)
133
self.assertFalse(result, 'must not be installed')
135
def test_is_not_installed_if_key_error(self):
136
"""Check if a package is installed when cache raises KeyError."""
137
name = 'test' # is not in the cache
138
result = self.obj.is_installed(name)
140
self.assertFalse(result, 'must not be installed')
142
def test_progress_bar(self):
143
"""The progress bar class is correct."""
144
self.assertIsInstance(package_manager.PackageManagerProgressBar(),
145
package_manager.AptProgressBar)
147
@package_manager.inline_callbacks
148
def test_install(self):
149
"""Install is correct."""
151
FAKED_CACHE[name] = FakedPackage(name) # not installed by default
153
result = yield self.obj.install(name)
155
self.assertIsInstance(result, FakedTransaction)
156
self.assertFalse(result.was_run, 'transaction must not be run')
158
@package_manager.inline_callbacks
159
def test_transaction_install(self):
160
"""Install is correct."""
162
FAKED_CACHE[name] = FakedPackage(name) # not installed by default
164
trans = yield self.obj.install(name)
166
trans.connect('finished', self._set_called)
169
self.assertEqual(self._called, ((trans, SUCCESS), {}))
170
self.assertTrue(trans.was_run, 'transaction must be run')
171
self.assertTrue(self.obj.is_installed(name))
173
@package_manager.inline_callbacks
174
def test_install_if_installed(self):
175
"""Install does nothing is package is already installed."""
177
FAKED_CACHE[name] = FakedPackage(name, is_installed=True)
179
result = yield self.obj.install(name)
181
self.assertEqual(result, SUCCESS)