Merge "Helper method to use dnf instead of yum on fedora >=22"

This commit is contained in:
Jenkins 2016-05-12 15:06:12 +00:00 committed by Gerrit Code Review
commit 91cafcb9d2
2 changed files with 44 additions and 6 deletions

View File

@ -42,9 +42,11 @@ class TestGetOsDistrib(testtools.TestCase):
class TestInstallPackages(testtools.TestCase):
@mock.patch('sahara.utils.ssh_remote._get_os_version')
@mock.patch('sahara.utils.ssh_remote._get_os_distrib')
@mock.patch('sahara.utils.ssh_remote._execute_command')
def test_install_packages(self, p_execute_command, p_get_os_distrib):
def test_install_packages(self, p_execute_command, p_get_os_distrib,
p_get_os_version):
packages = ('git', 'emacs', 'tree')
# test ubuntu
@ -60,13 +62,22 @@ class TestInstallPackages(testtools.TestCase):
'yum install -y git emacs tree',
run_as_root=True)
# test fedora
# test fedora < 22
p_get_os_distrib.return_value = 'fedora'
p_get_os_version.return_value = 20
ssh_remote._install_packages(packages)
p_execute_command.assert_called_with(
'yum install -y git emacs tree',
run_as_root=True)
# test fedora >=22
p_get_os_distrib.return_value = 'fedora'
p_get_os_version.return_value = 23
ssh_remote._install_packages(packages)
p_execute_command.assert_called_with(
'dnf install -y git emacs tree',
run_as_root=True)
# test redhat
p_get_os_distrib.return_value = 'redhat'
ssh_remote._install_packages(packages)
@ -84,9 +95,11 @@ class TestInstallPackages(testtools.TestCase):
class TestUpdateRepository(testtools.TestCase):
@mock.patch('sahara.utils.ssh_remote._get_os_version')
@mock.patch('sahara.utils.ssh_remote._get_os_distrib')
@mock.patch('sahara.utils.ssh_remote._execute_command')
def test_update_repository(self, p_execute_command, p_get_os_distrib):
def test_update_repository(self, p_execute_command, p_get_os_distrib,
p_get_os_version):
# test ubuntu
p_get_os_distrib.return_value = 'ubuntu'
ssh_remote._update_repository()
@ -99,12 +112,19 @@ class TestUpdateRepository(testtools.TestCase):
p_execute_command.assert_called_with(
'yum clean all', run_as_root=True)
# test fedora
# test fedora < 22
p_get_os_distrib.return_value = 'fedora'
p_get_os_version.return_value = 20
ssh_remote._update_repository()
p_execute_command.assert_called_with(
'yum clean all', run_as_root=True)
# test fedora >=22
p_get_os_distrib.return_value = 'fedora'
p_get_os_version.return_value = 23
ssh_remote._update_repository()
p_execute_command.assert_called_with(
'dnf clean all', run_as_root=True)
# test redhat
p_get_os_distrib.return_value = 'redhat'
ssh_remote._update_repository()

View File

@ -360,11 +360,23 @@ def _get_os_distrib():
run_as_root=False)[1].lower()
def _get_os_version():
return _execute_command(
('printf "import platform\nprint(platform.linux_distribution()[1])"'
' | python'), run_as_root=False)
def _install_packages(packages):
distrib = _get_os_distrib()
if distrib == 'ubuntu':
cmd = 'RUNLEVEL=1 apt-get install -y %(pkgs)s'
elif distrib in ('redhat', 'centos', 'fedora'):
elif distrib == 'fedora':
fversion = _get_os_version()
if fversion >= 22:
cmd = 'dnf install -y %(pkgs)s'
else:
cmd = 'yum install -y %(pkgs)s'
elif distrib in ('redhat', 'centos'):
cmd = 'yum install -y %(pkgs)s'
else:
raise ex.NotImplementedException(
@ -379,7 +391,13 @@ def _update_repository():
distrib = _get_os_distrib()
if distrib == 'ubuntu':
cmd = 'apt-get update'
elif distrib in ('redhat', 'centos', 'fedora'):
elif distrib == 'fedora':
fversion = _get_os_version()
if fversion >= 22:
cmd = 'dnf clean all'
else:
cmd = 'yum clean all'
elif distrib in ('redhat', 'centos'):
cmd = 'yum clean all'
else:
raise ex.NotImplementedException(