Merge pull request #54605 from garethgreenaway/2019_2_1_port_50523

[master] Porting #50523 to master
This commit is contained in:
Daniel Wozniak 2019-11-13 03:06:07 -07:00 committed by GitHub
commit 1f15aad1f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 11 deletions

View File

@ -9,7 +9,9 @@ import collections
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
@ -42,12 +44,16 @@ def get_osarch():
'''
Get the os architecture using rpm --eval
'''
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'

View File

@ -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):
@ -45,3 +45,61 @@ class PkgUtilsTestCase(TestCase):
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'