Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix core grains when Debian OS detected as 'Debian GNU/Linux' #31418

Merged
merged 4 commits into from Feb 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions salt/grains/core.py
Expand Up @@ -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.
Expand All @@ -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',
Expand Down
36 changes: 27 additions & 9 deletions tests/runtests.py
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions tests/unit/grains/__init__.py
@@ -0,0 +1 @@
# -*- coding: utf-8 -*-
125 changes: 125 additions & 0 deletions tests/unit/grains/core_test.py
@@ -0,0 +1,125 @@
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Erik Johnson <erik@saltstack.com>`
'''

# 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)