Skip to content
This repository has been archived by the owner on Dec 27, 2021. It is now read-only.

Make aptitude role faster #136

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion provy/more/debian/package/aptitude.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
28 changes: 22 additions & 6 deletions tests/unit/more/debian/package/test_aptitude.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down