2016-06-13 16:17:28 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-06-29 13:22:10 +00:00
|
|
|
#
|
|
|
|
# Copyright 2016 SUSE LLC
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2016-06-13 16:17:28 +00:00
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Bo Maryniuk <bo@suse.de>`
|
|
|
|
'''
|
|
|
|
# Import Python Libs
|
|
|
|
from __future__ import absolute_import
|
2016-06-13 22:25:08 +00:00
|
|
|
import os
|
2016-06-13 16:17:28 +00:00
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
|
|
|
from tests.support.mock import (
|
2016-06-13 16:17:28 +00:00
|
|
|
MagicMock,
|
|
|
|
patch,
|
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON
|
|
|
|
)
|
|
|
|
|
2017-02-27 15:59:04 +00:00
|
|
|
# Import salt libs
|
2016-06-13 16:17:28 +00:00
|
|
|
from salt.modules.inspectlib.collector import Inspector
|
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
|
|
|
class InspectorCollectorTestCase(TestCase):
|
|
|
|
'''
|
|
|
|
Test inspectlib:collector:Inspector
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
def setUp(self):
|
|
|
|
patcher = patch("os.mkdir", MagicMock())
|
|
|
|
patcher.start()
|
|
|
|
self.addCleanup(patcher.stop)
|
|
|
|
|
2016-06-13 16:17:28 +00:00
|
|
|
def test_env_loader(self):
|
|
|
|
'''
|
|
|
|
Get packages on the different distros.
|
|
|
|
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
inspector = Inspector(cachedir='/foo/cache', piddir='/foo/pid', pidfilename='bar.pid')
|
|
|
|
self.assertEqual(inspector.dbfile, '/foo/cache/_minion_collector.db')
|
|
|
|
self.assertEqual(inspector.pidfile, '/foo/pid/bar.pid')
|
|
|
|
|
2016-06-13 22:25:08 +00:00
|
|
|
def test_file_tree(self):
|
|
|
|
'''
|
|
|
|
Test file tree.
|
|
|
|
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
|
|
|
|
inspector = Inspector(cachedir='/test', piddir='/test', pidfilename='bar.pid')
|
|
|
|
tree_root = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'inspectlib', 'tree_test')
|
|
|
|
expected_tree = (['/a/a/dummy.a', '/a/b/dummy.b', '/b/b.1', '/b/b.2', '/b/b.3'],
|
|
|
|
['/a', '/a/a', '/a/b', '/a/c', '/b', '/c'],
|
|
|
|
['/a/a/dummy.ln.a', '/a/b/dummy.ln.b', '/a/c/b.1', '/b/b.4',
|
|
|
|
'/b/b.5', '/c/b.1', '/c/b.2', '/c/b.3'])
|
|
|
|
tree_result = []
|
|
|
|
for chunk in inspector._get_all_files(tree_root):
|
|
|
|
buff = []
|
|
|
|
for pth in chunk:
|
|
|
|
buff.append(pth.replace(tree_root, ''))
|
|
|
|
tree_result.append(buff)
|
|
|
|
tree_result = tuple(tree_result)
|
|
|
|
self.assertEqual(expected_tree, tree_result)
|
|
|
|
|
2016-06-13 22:05:34 +00:00
|
|
|
def test_get_unmanaged_files(self):
|
|
|
|
'''
|
|
|
|
Test get_unmanaged_files.
|
|
|
|
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
inspector = Inspector(cachedir='/test', piddir='/test', pidfilename='bar.pid')
|
|
|
|
managed = (
|
|
|
|
['a', 'b', 'c'],
|
|
|
|
['d', 'e', 'f'],
|
|
|
|
['g', 'h', 'i'],
|
|
|
|
)
|
|
|
|
system_all = (
|
|
|
|
['a', 'b', 'c'],
|
|
|
|
['d', 'E', 'f'],
|
|
|
|
['G', 'H', 'i'],
|
|
|
|
)
|
|
|
|
self.assertEqual(inspector._get_unmanaged_files(managed=managed, system_all=system_all),
|
|
|
|
([], ['E'], ['G', 'H']))
|
|
|
|
|
2016-06-13 16:17:28 +00:00
|
|
|
def test_pkg_get(self):
|
|
|
|
'''
|
|
|
|
Test if grains switching the pkg get method.
|
|
|
|
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
debian_list = """
|
|
|
|
g++
|
|
|
|
g++-4.9
|
|
|
|
g++-5
|
|
|
|
gawk
|
|
|
|
gcc
|
|
|
|
gcc-4.9
|
|
|
|
gcc-4.9-base:amd64
|
|
|
|
gcc-4.9-base:i386
|
|
|
|
gcc-5
|
|
|
|
gcc-5-base:amd64
|
|
|
|
gcc-5-base:i386
|
|
|
|
gcc-6-base:amd64
|
|
|
|
gcc-6-base:i386
|
|
|
|
"""
|
|
|
|
inspector = Inspector(cachedir='/test', piddir='/test', pidfilename='bar.pid')
|
|
|
|
inspector.grains_core = MagicMock()
|
|
|
|
inspector.grains_core.os_data = MagicMock()
|
|
|
|
inspector.grains_core.os_data.get = MagicMock(return_value='Debian')
|
|
|
|
with patch.object(inspector, '_Inspector__get_cfg_pkgs_dpkg', MagicMock(return_value='dpkg')):
|
|
|
|
with patch.object(inspector, '_Inspector__get_cfg_pkgs_rpm', MagicMock(return_value='rpm')):
|
|
|
|
inspector.grains_core = MagicMock()
|
|
|
|
inspector.grains_core.os_data = MagicMock()
|
|
|
|
inspector.grains_core.os_data().get = MagicMock(return_value='Debian')
|
|
|
|
self.assertEqual(inspector._get_cfg_pkgs(), 'dpkg')
|
2016-06-17 16:56:42 +00:00
|
|
|
inspector.grains_core.os_data().get = MagicMock(return_value='SUSE')
|
2016-06-13 16:17:28 +00:00
|
|
|
self.assertEqual(inspector._get_cfg_pkgs(), 'rpm')
|
|
|
|
inspector.grains_core.os_data().get = MagicMock(return_value='redhat')
|
|
|
|
self.assertEqual(inspector._get_cfg_pkgs(), 'rpm')
|