From e3d549d376bd8220298c98ee5e1f1e910815794c Mon Sep 17 00:00:00 2001 From: Erik Johnson Date: Mon, 22 Feb 2016 10:48:01 -0600 Subject: [PATCH 1/4] Fix debian grains setup This was broken in 885e00b because the updated regex now removes the slash in 'Debian GNU/Linux'. This issue did not make it into an official release, it exists only in the head of the 2015.8 branch. --- salt/grains/core.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/salt/grains/core.py b/salt/grains/core.py index e464cc8936f2..e0047323d4fa 100644 --- a/salt/grains/core.py +++ b/salt/grains/core.py @@ -908,7 +908,7 @@ def id_(): ''' return {'id': __opts__.get('id', '')} -_REPLACE_LINUX_RE = re.compile(r'\Wlinux', re.IGNORECASE) +_REPLACE_LINUX_RE = re.compile(r'\W(?:gnu/)?linux', re.IGNORECASE) # This maps (at most) the first ten characters (no spaces, lowercased) of # 'osfullname' to the 'os' grain that Salt traditionally uses. @@ -920,8 +920,7 @@ def id_(): 'archarm': 'Arch ARM', 'arch': 'Arch', 'debian': 'Debian', - 'debiangnu/': 'Debian', - 'raspbiangn': 'Raspbian', + 'raspbian': 'Raspbian', 'fedoraremi': 'Fedora', 'amazonami': 'Amazon', 'alt': 'ALT', From d3cd1b596dcd06a3c9fd95c388e16952769a93c0 Mon Sep 17 00:00:00 2001 From: Erik Johnson Date: Mon, 22 Feb 2016 16:35:53 -0600 Subject: [PATCH 2/4] Add unit test for core grains --- tests/unit/grains/__init__.py | 1 + tests/unit/grains/core.py | 125 ++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 tests/unit/grains/__init__.py create mode 100644 tests/unit/grains/core.py diff --git a/tests/unit/grains/__init__.py b/tests/unit/grains/__init__.py new file mode 100644 index 000000000000..40a96afc6ff0 --- /dev/null +++ b/tests/unit/grains/__init__.py @@ -0,0 +1 @@ +# -*- coding: utf-8 -*- diff --git a/tests/unit/grains/core.py b/tests/unit/grains/core.py new file mode 100644 index 000000000000..68e9e53a99e2 --- /dev/null +++ b/tests/unit/grains/core.py @@ -0,0 +1,125 @@ +# -*- coding: utf-8 -*- +''' + :codeauthor: :email:`Erik Johnson ` +''' + +# Import Python libs +from __future__ import absolute_import +import os +import platform + +# Import Salt Testing Libs +from salttesting import TestCase, skipIf +from salttesting.helpers import ensure_in_syspath +from salttesting.mock import ( + MagicMock, + patch, + NO_MOCK, + NO_MOCK_REASON +) + +ensure_in_syspath('../../') + +# Import Salt Libs +import salt.utils +from salt.grains import core + +# Globals +core.__salt__ = {} + + +@skipIf(NO_MOCK, NO_MOCK_REASON) +class CoreGrainsTestCase(TestCase): + ''' + Test cases for core grains + ''' + @skipIf(not salt.utils.is_linux(), 'System is not Linux') + def test_gnu_slash_linux_in_os_name(self): + ''' + Test to return a list of all enabled services + ''' + _path_exists_map = { + '/proc/1/cmdline': False + } + _path_isfile_map = {} + _cmd_run_map = { + 'dpkg --print-architecture': 'amd64' + } + + path_exists_mock = MagicMock(side_effect=lambda x: _path_exists_map[x]) + path_isfile_mock = MagicMock( + side_effect=lambda x: _path_isfile_map.get(x, False) + ) + cmd_run_mock = MagicMock( + side_effect=lambda x: _cmd_run_map[x] + ) + empty_mock = MagicMock(return_value={}) + + orig_import = __import__ + + def _import_mock(name, *args): + if name == 'lsb_release': + raise ImportError('No module named lsb_release') + return orig_import(name, *args) + + # Skip the first if statement + with patch.object(salt.utils, 'is_proxy', + MagicMock(return_value=False)): + # Skip the selinux/systemd stuff (not pertinent) + with patch.object(core, '_linux_bin_exists', + MagicMock(return_value=False)): + # Skip the init grain compilation (not pertinent) + with patch.object(os.path, 'exists', path_exists_mock): + # Ensure that lsb_release fails to import + with patch('__builtin__.__import__', + side_effect=_import_mock): + # Skip all the /etc/*-release stuff (not pertinent) + with patch.object(os.path, 'isfile', path_isfile_mock): + # Mock platform.linux_distribution to give us the + # OS name that we want. + distro_mock = MagicMock( + return_value=('Debian GNU/Linux', '8.3', '') + ) + with patch.object( + platform, + 'linux_distribution', + distro_mock): + # Make a bunch of functions return empty dicts, + # we don't care about these grains for the + # purposes of this test. + with patch.object( + core, + '_linux_cpudata', + empty_mock): + with patch.object( + core, + '_linux_gpu_data', + empty_mock): + with patch.object( + core, + '_memdata', + empty_mock): + with patch.object( + core, + '_hw_data', + empty_mock): + with patch.object( + core, + '_virtual', + empty_mock): + with patch.object( + core, + '_ps', + empty_mock): + # Mock the osarch + with patch.dict( + core.__salt__, + {'cmd.run': cmd_run_mock}): + os_grains = core.os_data() + + self.assertEqual(os_grains.get('os_family'), 'Debian') + + +if __name__ == '__main__': + from integration import run_tests + run_tests(CoreGrainsTestCase, needs_daemon=False) From 0e0cd17160ddab11f2894d44b54fea52c4589b4d Mon Sep 17 00:00:00 2001 From: Erik Johnson Date: Mon, 22 Feb 2016 21:54:49 -0600 Subject: [PATCH 3/4] Rename core.py to core_test.py --- tests/unit/grains/{core.py => core_test.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/unit/grains/{core.py => core_test.py} (100%) diff --git a/tests/unit/grains/core.py b/tests/unit/grains/core_test.py similarity index 100% rename from tests/unit/grains/core.py rename to tests/unit/grains/core_test.py From 5c833efc01aca4a05604eb173aff2604b3673faf Mon Sep 17 00:00:00 2001 From: Erik Johnson Date: Mon, 22 Feb 2016 21:55:32 -0600 Subject: [PATCH 4/4] Support running grains tests --- tests/runtests.py | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/tests/runtests.py b/tests/runtests.py index f8daf90fedd9..1e477fc26805 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -105,6 +105,15 @@ def setup_additional_options(self): action='store_true', help='Run tests for client' ) + self.test_selection_group.add_option( + '-G', + '--grains', + '--grains-tests', + dest='grains', + default=False, + action='store_true', + help='Run tests for grains' + ) self.test_selection_group.add_option( '-s', '--shell', @@ -207,6 +216,7 @@ def validate_options(self): self.options.module, self.options.cli, self.options.client, + self.options.grains, self.options.shell, self.options.unit, self.options.state, @@ -228,15 +238,17 @@ def validate_options(self): ) # Set test suite defaults if no specific suite options are provided - if not any((self.options.module, self.options.client, self.options.cli, - self.options.shell, self.options.unit, self.options.state, - self.options.runners, self.options.loader, self.options.name, + if not any((self.options.module, self.options.cli, self.options.client, + self.options.grains, self.options.shell, self.options.unit, + self.options.state, self.options.runners, + self.options.loader, self.options.name, self.options.outputter, self.options.cloud_provider_tests, - self.options.fileserver, self.options.wheel, self.options.api, - self.options.renderers)): + self.options.fileserver, self.options.wheel, + self.options.api, self.options.renderers)): self.options.module = True self.options.cli = True self.options.client = True + self.options.grains = True self.options.shell = True self.options.unit = True self.options.runners = True @@ -364,6 +376,7 @@ def run_integration_tests(self): self.options.module or self.options.cli or self.options.client or + self.options.grains or self.options.loader or self.options.outputter or self.options.fileserver or @@ -388,10 +401,12 @@ def run_integration_tests(self): print_header(' * Setting up Salt daemons to execute tests', top=False) status = [] - if not any([self.options.cli, self.options.client, self.options.module, - self.options.runners, self.options.shell, self.options.state, - self.options.loader, self.options.outputter, self.options.name, - self.options.cloud_provider_tests, self.options.api, self.options.renderers, + if not any([self.options.cli, self.options.client, self.options.grains, + self.options.module, self.options.runners, + self.options.shell, self.options.state, + self.options.loader, self.options.outputter, + self.options.name, self.options.cloud_provider_tests, + self.options.api, self.options.renderers, self.options.fileserver, self.options.wheel]): return status @@ -414,6 +429,9 @@ def run_integration_tests(self): status.append(self.run_integration_suite('cli', 'CLI')) if self.options.client: status.append(self.run_integration_suite('client', 'Client')) + # No grains integration tests at this time, uncomment if we add any + #if self.options.grains: + # status.append(self.run_integration_suite('grains', 'Grains')) if self.options.shell: status.append(self.run_integration_suite('shell', 'Shell')) if self.options.outputter: