51d9b78cf8
OBS-URL: https://build.opensuse.org/package/show/systemsmanagement:saltstack/salt?expand=0&rev=156
182 lines
6.4 KiB
Diff
182 lines
6.4 KiB
Diff
From 9fc2217cd12d313f87d4db0e408e5b68b23c2330 Mon Sep 17 00:00:00 2001
|
|
From: Bo Maryniuk <bo@suse.de>
|
|
Date: Wed, 14 Nov 2018 17:36:23 +0100
|
|
Subject: [PATCH] Get os_arch also without RPM package installed
|
|
|
|
backport pkg.rpm test
|
|
|
|
Add pkg.rpm unit test case
|
|
|
|
Fix docstring
|
|
|
|
Add UT for getting OS architecture fallback, when no RPM found (initrd, e.g.)
|
|
|
|
Add UT for OS architecture detection on fallback, when no CPU arch can be determined
|
|
|
|
Add UT for OS arch detection when no CPU arch or machine can be determined
|
|
|
|
Remove unsupported testcase
|
|
---
|
|
salt/utils/pkg/rpm.py | 18 +++++---
|
|
tests/unit/utils/test_pkg.py | 105 ++++++++++++++++++++++++++-----------------
|
|
2 files changed, 77 insertions(+), 46 deletions(-)
|
|
|
|
diff --git a/salt/utils/pkg/rpm.py b/salt/utils/pkg/rpm.py
|
|
index 94e231da4b..bb8c3fb589 100644
|
|
--- a/salt/utils/pkg/rpm.py
|
|
+++ b/salt/utils/pkg/rpm.py
|
|
@@ -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
|
|
'''
|
|
- 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(list(filter(None, platform.uname()[-2:]))[-1:])
|
|
+
|
|
return salt.utils.stringutils.to_str(ret).strip() or 'unknown'
|
|
|
|
|
|
diff --git a/tests/unit/utils/test_pkg.py b/tests/unit/utils/test_pkg.py
|
|
index c293852058..361e0bf92f 100644
|
|
--- a/tests/unit/utils/test_pkg.py
|
|
+++ b/tests/unit/utils/test_pkg.py
|
|
@@ -1,47 +1,72 @@
|
|
# -*- 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 Mock, 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
|
|
+
|
|
+try:
|
|
+ import pytest
|
|
+except ImportError:
|
|
+ pytest = None
|
|
|
|
|
|
-class PkgUtilsTestCase(TestCase):
|
|
+@skipIf(NO_MOCK, NO_MOCK_REASON)
|
|
+@skipIf(pytest is None, 'PyTest is missing')
|
|
+class PkgRPMTestCase(TestCase):
|
|
'''
|
|
- TestCase for salt.utils.pkg module
|
|
+ Test case for pkg.rpm utils
|
|
'''
|
|
- test_parameters = [
|
|
- ("16.0.0.49153-0+f1", "", "16.0.0.49153-0+f1"),
|
|
- ("> 15.0.0", ">", "15.0.0"),
|
|
- ("< 15.0.0", "<", "15.0.0"),
|
|
- ("<< 15.0.0", "<<", "15.0.0"),
|
|
- (">> 15.0.0", ">>", "15.0.0"),
|
|
- (">= 15.0.0", ">=", "15.0.0"),
|
|
- ("<= 15.0.0", "<=", "15.0.0"),
|
|
- ("!= 15.0.0", "!=", "15.0.0"),
|
|
- ("<=> 15.0.0", "<=>", "15.0.0"),
|
|
- ("<> 15.0.0", "<>", "15.0.0"),
|
|
- ("= 15.0.0", "=", "15.0.0"),
|
|
- (">15.0.0", ">", "15.0.0"),
|
|
- ("<15.0.0", "<", "15.0.0"),
|
|
- ("<<15.0.0", "<<", "15.0.0"),
|
|
- (">>15.0.0", ">>", "15.0.0"),
|
|
- (">=15.0.0", ">=", "15.0.0"),
|
|
- ("<=15.0.0", "<=", "15.0.0"),
|
|
- ("!=15.0.0", "!=", "15.0.0"),
|
|
- ("<=>15.0.0", "<=>", "15.0.0"),
|
|
- ("<>15.0.0", "<>", "15.0.0"),
|
|
- ("=15.0.0", "=", "15.0.0"),
|
|
- ("", "", "")
|
|
- ]
|
|
-
|
|
- def test_split_comparison(self):
|
|
- '''
|
|
- Tests salt.utils.pkg.split_comparison
|
|
- '''
|
|
- for test_parameter in self.test_parameters:
|
|
- oper, verstr = salt.utils.pkg.split_comparison(test_parameter[0])
|
|
- self.assertEqual(test_parameter[1], oper)
|
|
- self.assertEqual(test_parameter[2], verstr)
|
|
+
|
|
+ @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'
|
|
--
|
|
2.16.4
|
|
|
|
|