コード例 #1
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-3-Clause
# Copyright © 2017-2019, GoodData Corporation. All rights reserved.

import logging
import mock
import pytest
from testfixtures import log_capture

from _utils import _import
tool = _import('ipamanager.alerting', 'nsca')
errors = _import('ipamanager', 'errors')
modulename = 'ipamanager.alerting.nsca'


class TestAlerting(object):
    def setup_method(self, method):
        self.config = {
            'messages': {
                'ok': 'Everything OK.',
                'warn': 'Some warnings:',
                'err': 'Some errors:'
            },
            'command': '/dev/null/send_nsca',
            'service': 'ipamanager-push'
        }
        if not method.func_name.startswith('test_init'):
            self.plugin = tool.NscaAlertingPlugin(self.config)
            if method.func_name.startswith('test_dispatch'):
                self.plugin._run_dispatch = mock.Mock()
コード例 #2
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-3-Clause
# Copyright © 2017-2019, GoodData Corporation. All rights reserved.

import logging
import os.path
import pytest
from testfixtures import log_capture

from _utils import _import
tool = _import('ipamanager', 'config_loader')
entities = _import('ipamanager', 'entities')
utils = _import('ipamanager', 'utils')
modulename = 'ipamanager.config_loader'
testpath = os.path.dirname(os.path.abspath(__file__))

CONFIG_CORRECT = os.path.join(testpath, 'freeipa-manager-config/correct')
CONFIG_INVALID = os.path.join(testpath, 'freeipa-manager-config/invalid')
NUMBERS = ['one', 'three', 'two']


class TestConfigLoader(object):
    def setup_method(self, method):
        if method.func_name.startswith('test_load'):
            ignored = {
                'user': ['firstname.lastname$'],
                'group': ['group-one-users'],
                'hostgroup': ['some-hostgroup']
            }
        else:
コード例 #3
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-3-Clause
# Copyright © 2017-2019, GoodData Corporation. All rights reserved.
import logging
import mock
import os
import pytest
import yaml

from _utils import _import
from testfixtures import log_capture

tool = _import('ipamanager', 'template')
errors = _import('ipamanager', 'errors')


class TestTemplate(object):
    def setup_method(self, method):
        self.data = {
            'datacenters': {
                'xx': [42, 666],
                'yy': [19],
                'zz': [15]
            },
            'include_params': {
                'all': {
                    'description': 'all description'
                },
                'rules': {
                    'all': {
コード例 #4
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-3-Clause
# Copyright © 2017-2019, GoodData Corporation. All rights reserved.

import logging
import mock
import os
import pytest
import socket
import sys
from testfixtures import log_capture, LogCapture, StringComparison

from _utils import _import
sys.modules['ipalib'] = mock.Mock()
tool = _import('ipamanager', 'freeipa_manager')
errors = _import('ipamanager', 'errors')
entities = _import('ipamanager', 'entities')
utils = _import('ipamanager', 'utils')
ipa_connector = _import('ipamanager', 'ipa_connector')
modulename = 'ipamanager.freeipa_manager'
SETTINGS = os.path.join(os.path.dirname(__file__),
                        'freeipa-manager-config/settings.yaml')
SETTINGS_ALERTING = os.path.join(
    os.path.dirname(__file__), 'freeipa-manager-config/settings_alerting.yaml')
SETTINGS_INCLUDE = os.path.join(
    os.path.dirname(__file__), 'freeipa-manager-config/settings_include.yaml')
SETTINGS_MERGE_INCLUDE = os.path.join(
    os.path.dirname(__file__), 'freeipa-manager-config/settings_merge.yaml')
SETTINGS_INVALID = os.path.join(
    os.path.dirname(__file__), 'freeipa-manager-config/settings_invalid.yaml')
コード例 #5
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-3-Clause
# Copyright © 2017-2019, GoodData Corporation. All rights reserved.

import logging
import mock
import os.path
import pytest
from testfixtures import log_capture

from _utils import _import
tool = _import('ipamanager', 'integrity_checker')
testpath = os.path.dirname(os.path.abspath(__file__))


class TestIntegrityChecker(object):
    def _create_checker(self, entities):
        settings = {'user-group-pattern': '^role-.+|.+-users$'}
        self.checker = tool.IntegrityChecker(entities, settings)
        self.checker.errs = dict()

    def test_build_dict(self):
        self._create_checker(self._sample_entities_correct())
        entity_dict = self.checker.entity_dict
        assert sorted(entity_dict.keys()) == [
            'group', 'hbacrule', 'hostgroup', 'permission', 'privilege',
            'role', 'service', 'sudorule', 'user'
        ]
        assert isinstance(entity_dict['hostgroup']['group-one-hosts'],
                          tool.entities.FreeIPAHostGroup)
コード例 #6
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-3-Clause
# Copyright © 2017-2019, GoodData Corporation. All rights reserved.

import logging
import os.path
import pytest
from testfixtures import log_capture

from _utils import _import
tool = _import('ipamanager', 'difference')
testpath = os.path.dirname(os.path.abspath(__file__))

PROD_PATH = os.path.join(testpath, 'freeipa-manager-config/diff/prod')
INT_PATH = os.path.join(testpath, 'freeipa-manager-config/diff/int')


class TestDiff(object):
    def test_incorrect(self):
        with pytest.raises(tool.IntegrityError) as exc:
            tool.FreeIPADifference(INT_PATH, PROD_PATH).run()
        assert exc.value[0] == 'The ADDITIONAL entities are : additional.yaml'

    @log_capture('FreeIPADifference', level=logging.INFO)
    def test_correct(self, captured_log):
        tool.FreeIPADifference(PROD_PATH, INT_PATH).run()
        captured_log.check(
            ('FreeIPADifference', 'INFO', 'There are no ADDITIONAL entites'), )
コード例 #7
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-3-Clause
# Copyright © 2017-2019, GoodData Corporation. All rights reserved.

import logging
import mock
import pytest
import yaml
from testfixtures import LogCapture

from _utils import _import, _mock_dump

tool = _import('ipamanager', 'entities')
modulename = 'ipamanager.entities'

USER_GROUP_REGEX = r'^role-.+|.+-users$'


class TestFreeIPAEntity(object):
    def test_create_entity(self):
        with pytest.raises(TypeError) as exc:
            tool.FreeIPAEntity('sample.entity', {}, 'path')
        assert exc.value[0] == (
            "Can't instantiate abstract class FreeIPAEntity "
            "with abstract methods managed_attributes_push, validation_schema")

    def test_equality(self):
        user1 = tool.FreeIPAUser('user1', {
            'firstName': 'Some',
            'lastName': 'Name'
コード例 #8
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-3-Clause
# Copyright © 2017-2019, GoodData Corporation. All rights reserved.

import json
import mock
import os.path
import requests_mock
from testfixtures import LogCapture, StringComparison

from _utils import _import
tool = _import('ipamanager', 'okta_loader')
entities = _import('ipamanager', 'entities')
utils = _import('ipamanager', 'utils')
modulename = 'ipamanager.config_loader'
testpath = os.path.dirname(os.path.abspath(__file__))


class TestOktaLoader(object):
    def setup_method(self, method):
        settings = {
            'okta': {
                'auth': {
                    'org': 'testoktaorg',
                    'token_path': '/test/okta.token'
                },
                'ignore': ['admin', 'user1'],
                'attributes':
                ['email', 'firstName', 'lastName', 'initials', 'githubLogin'],
                'user_id_regex':
コード例 #9
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-3-Clause
# Copyright © 2017-2019, GoodData Corporation. All rights reserved.

import logging
import pytest
from testfixtures import log_capture

from _utils import _import
tool = _import('ipamanager', 'alerting')
modulename = 'ipamanager.alerting'


class DummyAlertingPlugin(tool.AlertingPlugin):
    def dispatch(self):
        self.lg.debug('Sent %d messages to /dev/null', len(self.messages))


class TestAlerting(object):
    def setup_method(self, method):
        if not method.func_name.endswith('abstract'):
            self.plugin = DummyAlertingPlugin(logging.WARNING)

    def test_init_abstract(self):
        with pytest.raises(TypeError) as exc:
            tool.AlertingPlugin(logging.WARNING)
        assert exc.value[0] == (
            "Can't instantiate abstract class AlertingPlugin "
            "with abstract methods dispatch")