diff --git a/provy/more/debian/package/aptitude.py b/provy/more/debian/package/aptitude.py index 6d97423..a039a91 100644 --- a/provy/more/debian/package/aptitude.py +++ b/provy/more/debian/package/aptitude.py @@ -51,9 +51,9 @@ def provision(self): ''' if not self.is_package_installed('aptitude'): + self.execute('apt-get update', stdout=False, sudo=True) self.execute('apt-get install aptitude -y', stdout=False, sudo=True) - self.ensure_up_to_date() self.ensure_package_installed('curl') def ensure_gpg_key(self, url): @@ -289,6 +289,7 @@ def provision(self): ''' if not self.is_package_installed(package_name): + self.ensure_up_to_date() self.__check_before_install(package_name) self.log('%s is not installed (via aptitude)! Installing...' % package_name) self.execute('%s install -y %s' % (self.aptitude, package_name), stdout=stdout, sudo=sudo) @@ -321,6 +322,7 @@ def provision(self): role.package_exists('nginx') # True ''' try: + self.ensure_up_to_date() return bool(self.execute('%s show %s' % (self.aptitude, package), stdout=False)) except SystemExit: return False diff --git a/tests/unit/more/debian/package/test_aptitude.py b/tests/unit/more/debian/package/test_aptitude.py index 2ed263d..cf6e97b 100644 --- a/tests/unit/more/debian/package/test_aptitude.py +++ b/tests/unit/more/debian/package/test_aptitude.py @@ -2,7 +2,7 @@ import sys from base64 import b64encode -from mock import MagicMock, patch +from mock import MagicMock, patch, call from nose.tools import istest from provy.more.debian import AptitudeRole, PackageNotFound @@ -23,7 +23,8 @@ def checks_that_a_package_exists(self): @istest def checks_that_a_package_doesnt_exist(self): - with self.execute_mock() as execute: + with self.mock_role_methods('execute', 'ensure_up_to_date'): + execute = self.role.execute execute.return_value = False self.assertFalse(self.role.package_exists('phyton')) execute.assert_called_with('aptitude show phyton', stdout=False) @@ -71,6 +72,18 @@ def checks_if_a_package_exists_before_installing(self): self.assertTrue(package_exists.called) execute.assert_called_with('aptitude install -y python', stdout=False, sudo=True) + @istest + def updates_sources_before_install(self): + with self.execute_mock() as execute, self.mock_role_methods('package_exists', 'ensure_up_to_date'): + package_exists = self.role.package_exists + package_exists.return_value = True + + result = self.role.ensure_package_installed('python') + self.role.ensure_up_to_date.assert_called_once_with() + self.assertTrue(result) + + execute.assert_called_with('aptitude install -y python', stdout=False, sudo=True) + @istest def fails_to_install_package_if_it_doesnt_exist(self): with self.execute_mock(), self.mock_role_method('package_exists') as package_exists: @@ -81,11 +94,13 @@ def fails_to_install_package_if_it_doesnt_exist(self): @istest def doesnt_install_package_if_already_installed(self): - with self.mock_role_method('is_package_installed'): + with self.mock_role_methods('is_package_installed', 'ensure_up_to_date'): self.role.is_package_installed.return_value = True + self.assertFalse(self.role.ensure_up_to_date.called, "We should update only iff we install package") result = self.role.ensure_package_installed('python') + self.assertFalse(result) @istest @@ -116,8 +131,10 @@ def installs_necessary_packages_to_provision(self): self.role.provision() self.role.is_package_installed.assert_called_once_with('aptitude') - self.role.execute.assert_called_once_with('apt-get install aptitude -y', stdout=False, sudo=True) - self.role.ensure_up_to_date.assert_called_once_with() + self.assertEqual(self.role.execute.call_args_list, [ + call('apt-get update', stdout=False, sudo=True), + call('apt-get install aptitude -y', stdout=False, sudo=True), + ]) self.role.ensure_package_installed.assert_called_once_with('curl') @istest @@ -129,7 +146,6 @@ def doesnt_install_aptitude_if_already_installed_when_provisioning(self): self.role.is_package_installed.assert_called_once_with('aptitude') self.assertFalse(self.role.execute.called) - self.role.ensure_up_to_date.assert_called_once_with() self.role.ensure_package_installed.assert_called_once_with('curl') @istest