Skip to content

Commit

Permalink
Merge pull request #50523 from isbm/isbm-inird-no-rpm-platform-fix
Browse files Browse the repository at this point in the history
Bugfix: OS arch fall-back when no RPM installed on inird image
  • Loading branch information
Mike Place authored Nov 20, 2018
2 parents ee9573c + f8deae2 commit 56d8a7e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 11 deletions.
18 changes: 12 additions & 6 deletions salt/utils/pkg/rpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import datetime
import logging
import subprocess
import platform
import salt.utils.stringutils
import salt.utils.path

# Import 3rd-party libs
from salt.ext import six
Expand Down Expand Up @@ -45,12 +47,16 @@ def get_osarch():
'''
Get the os architecture using rpm --eval
'''
ret = subprocess.Popen(
'rpm --eval "%{_host_cpu}"',
shell=True,
close_fds=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()[0]
if salt.utils.path.which('rpm'):
ret = subprocess.Popen(
'rpm --eval "%{_host_cpu}"',
shell=True,
close_fds=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()[0]
else:
ret = ''.join([x for x in platform.uname()[-2:] if x][-1:])

return salt.utils.stringutils.to_str(ret).strip() or 'unknown'


Expand Down
68 changes: 63 additions & 5 deletions tests/unit/utils/test_pkg.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# -*- coding: utf-8 -*-

# Import Python libs
from __future__ import absolute_import
# Import Salt Libs
from __future__ import absolute_import, unicode_literals, print_function

from tests.support.unit import TestCase, skipIf
from tests.support.mock import MagicMock, patch, NO_MOCK, NO_MOCK_REASON
import salt.utils.pkg
# Import Salt Testing Libs
from tests.support.unit import TestCase
from salt.utils.pkg import rpm


class PkgUtilsTestCase(TestCase):
Expand Down Expand Up @@ -45,3 +45,61 @@ def test_split_comparison(self):
oper, verstr = salt.utils.pkg.split_comparison(test_parameter[0])
self.assertEqual(test_parameter[1], oper)
self.assertEqual(test_parameter[2], verstr)


@skipIf(NO_MOCK, NO_MOCK_REASON)
class PkgRPMTestCase(TestCase):
'''
Test case for pkg.rpm utils
'''

@patch('salt.utils.path.which', MagicMock(return_value=True))
def test_get_osarch_by_rpm(self):
'''
Get os_arch if RPM package is installed.
:return:
'''
subprocess_mock = MagicMock()
subprocess_mock.Popen = MagicMock()
subprocess_mock.Popen().communicate = MagicMock(return_value=['Z80'])
with patch('salt.utils.pkg.rpm.subprocess', subprocess_mock):
assert rpm.get_osarch() == 'Z80'
assert subprocess_mock.Popen.call_count == 2 # One within the mock
assert subprocess_mock.Popen.call_args[1]['close_fds']
assert subprocess_mock.Popen.call_args[1]['shell']
assert len(subprocess_mock.Popen.call_args_list) == 2
assert subprocess_mock.Popen.call_args[0][0] == 'rpm --eval "%{_host_cpu}"'

@patch('salt.utils.path.which', MagicMock(return_value=False))
@patch('salt.utils.pkg.rpm.subprocess', MagicMock(return_value=False))
@patch('salt.utils.pkg.rpm.platform.uname', MagicMock(
return_value=('Sinclair BASIC', 'motophone', '1982 Sinclair Research Ltd', '1.0', 'ZX81', 'Z80')))
def test_get_osarch_by_platform(self):
'''
Get os_arch if RPM package is not installed (inird image, for example).
:return:
'''
assert rpm.get_osarch() == 'Z80'

@patch('salt.utils.path.which', MagicMock(return_value=False))
@patch('salt.utils.pkg.rpm.subprocess', MagicMock(return_value=False))
@patch('salt.utils.pkg.rpm.platform.uname', MagicMock(
return_value=('Sinclair BASIC', 'motophone', '1982 Sinclair Research Ltd', '1.0', 'ZX81', '')))
def test_get_osarch_by_platform_no_cpu_arch(self):
'''
Get os_arch if RPM package is not installed (inird image, for example) but cpu arch cannot be determined.
:return:
'''
assert rpm.get_osarch() == 'ZX81'

@patch('salt.utils.path.which', MagicMock(return_value=False))
@patch('salt.utils.pkg.rpm.subprocess', MagicMock(return_value=False))
@patch('salt.utils.pkg.rpm.platform.uname', MagicMock(
return_value=('Sinclair BASIC', 'motophone', '1982 Sinclair Research Ltd', '1.0', '', '')))
def test_get_osarch_by_platform_no_cpu_arch_no_machine(self):
'''
Get os_arch if RPM package is not installed (inird image, for example)
where both cpu arch and machine cannot be determined.
:return:
'''
assert rpm.get_osarch() == 'unknown'

0 comments on commit 56d8a7e

Please sign in to comment.