Esempio n. 1
0
File: run.py Progetto: jsjhb/checkmk
def run(check_info, check_manager, dataset, write=False):
    """Run all possible tests on 'dataset'"""
    print("START: %r" % (dataset,))
    checklist = checkhandler.get_applicables(dataset.checkname, check_info)
    assert checklist, "Found no check plugin for %r" % (dataset.checkname,)

    immu = Immutables()

    with optional_freeze_time(dataset):

        parsed = run_test_on_parse(check_manager, dataset, immu)

        # LOOP OVER ALL (SUB)CHECKS
        for sname in checklist:
            subcheck = (sname + '.').split('.')[1]
            check = check_manager.get_check(sname)

            info_arg = get_info_argument(dataset, subcheck, parsed)
            immu.test(' after get_info_argument ')
            immu.register(info_arg, 'info_arg')

            mock_is, mock_hec, mock_hecm = get_mock_values(dataset, subcheck)

            with value_store.context(CheckPluginName("test"), "unit-test"), \
                 MockItemState(mock_is), \
                 MockHostExtraConf(check, mock_hec), \
                 MockHostExtraConf(check, mock_hecm, "host_extra_conf_merged"):

                run_test_on_discovery(check, subcheck, dataset, info_arg, immu, write)

                run_test_on_checks(check, subcheck, dataset, info_arg, immu, write)

        immu.test(' at end of subcheck loop %r ' % (subcheck,))
Esempio n. 2
0
def test_df_discovery_groups_with_parse(inventory_df_rules, filesystem_groups,
                                        expected_result):
    check = Check('df')

    def mocked_host_extra_conf_merged(_hostname, ruleset):
        if ruleset is check.context.get("inventory_df_rules"):
            return inventory_df_rules
        raise AssertionError(
            "Unknown/unhandled ruleset 'inventory_df_rules' used in mock of host_extra_conf_merged"
        )

    def mocked_host_extra_conf(_hostname, ruleset):
        if ruleset is check.context.get("filesystem_groups"):
            return filesystem_groups
        raise AssertionError(
            "Unknown/unhandled ruleset 'filesystem_groups' used in mock of host_extra_conf"
        )

    with MockHostExtraConf(check, mocked_host_extra_conf_merged,
                           "host_extra_conf_merged"):
        with MockHostExtraConf(check, mocked_host_extra_conf,
                               "host_extra_conf"):
            raw_discovery_result = check.run_discovery(
                parse_df(info_df_groups))
            discovery_result = DiscoveryResult(raw_discovery_result)

    expected_result = DiscoveryResult(expected_result)
    assertDiscoveryResultsEqual(check, discovery_result, expected_result)
Esempio n. 3
0
def run(check_manager, dataset, write=False):
    """Run all possible tests on 'dataset'"""
    print("START: %r" % dataset)
    checklist = checkhandler.get_applicables(dataset.checkname)
    assert checklist, "Found no check plugin for %r" % dataset.checkname

    immu = Immutables()

    with optional_freeze_time(dataset):
        # test the parse function
        if hasattr(dataset, 'info'):
            immu.register(dataset.info, 'info')
        parsed = parse(check_manager, dataset)
        immu.test(' after parse function ')

        immu.register(parsed, 'parsed')

        # get the expected check results, if present
        checks_expected = getattr(dataset, 'checks', {})

        # LOOP OVER ALL (SUB)CHECKS
        for sname in checklist:
            subcheck = (sname + '.').split('.')[1]
            check = check_manager.get_check(sname)

            info_arg = get_info_argument(dataset, subcheck, parsed)
            immu.test(' after get_info_argument ')
            immu.register(info_arg, 'info_arg')

            mock_is, mock_hec, mock_hecm = get_mock_values(dataset, subcheck)

            with MockItemState(mock_is), \
                 MockHostExtraConf(check, mock_hec), \
                 MockHostExtraConf(check, mock_hecm, "host_extra_conf_merged"):
                # test discovery
                d_result = discovery(check, subcheck, dataset, info_arg, immu)
                if write:
                    dataset.discovery[subcheck] = [e.tuple for e in d_result]
                    # test checks
                for dr in d_result:
                    cdr = check_discovered_result(check, dr, info_arg, immu)
                    if write and cdr:
                        dataset.checks.setdefault(subcheck, []).append(cdr)
                if not write:
                    for entry in checks_expected.get(subcheck, []):

                        check_listed_result(check, entry, info_arg, immu)

        immu.test(' at end of subcheck loop %r ' % subcheck)
Esempio n. 4
0
def test_check_fileinfo_group_patterns_host_extra_conf(item, host_rulesets,
                                                       expected_result):
    fileinfo_groups_check = Check('fileinfo.groups')

    def mock_host_extra_conf(_hostname, _rulesets):
        return host_rulesets

    with MockHostExtraConf(fileinfo_groups_check, mock_host_extra_conf,
                           'host_extra_conf'):
        assert list(
            fileinfo_groups_check.run_check(
                item,
                {},
                {
                    'reftime': 1619516613,
                    'files': {
                        '/sms/checked/bla':
                        FileinfoItem(name='/sms/checked/bla',
                                     missing=False,
                                     failed=False,
                                     size=0,
                                     time=1619515730)
                    },
                },
            )) == expected_result
Esempio n. 5
0
def test_logwatch_inventory_group(check_manager, info, fwd_rule, inventory_groups, expected_result):
    parsed = check_manager.get_check('logwatch').run_parse(info)

    check = check_manager.get_check('logwatch.groups')

    mock_cfgs = [fwd_rule, inventory_groups]

    def mock_cfg(_hostname, _ruleset):
        return mock_cfgs.pop(0)

    with MockHostExtraConf(check, mock_cfg):
        actual_result = DiscoveryResult(check.run_discovery(parsed))
        assertDiscoveryResultsEqual(check, actual_result, DiscoveryResult(expected_result))
Esempio n. 6
0
def test_logwatch_ec_inventory_single(check_manager, info, fwd_rule,
                                      expected_result):
    parsed = check_manager.get_check('logwatch').run_parse(info)

    check = check_manager.get_check('logwatch.ec_single')

    def mock_cfg(_hostname, _ruleset):
        return fwd_rule

    with MockHostExtraConf(check, mock_cfg):
        actual_result = DiscoveryResult(check.run_discovery(parsed))
        assertDiscoveryResultsEqual(check, actual_result,
                                    DiscoveryResult(expected_result))
Esempio n. 7
0
def test_hr_ps_discovery(info, discovery_params, expected_discovery_result):
    '''Test that the hr_ps check returns the correct discovery results given different
    discovery parameters.
    '''
    check = Check('hr_ps')

    with MockHostExtraConf(check, discovery_params, 'host_extra_conf'):
        actual_discovery_result = check.run_discovery(check.run_parse(info))

    assertDiscoveryResultsEqual(
        check,
        DiscoveryResult(actual_discovery_result),
        DiscoveryResult(expected_discovery_result),
    )
Esempio n. 8
0
def test_df_discovery_with_parse(check_manager, info, expected_result, inventory_df_rules):
    check = check_manager.get_check("df")

    def mocked_host_extra_conf_merged(_hostname, ruleset):
        if ruleset is check.context.get("inventory_df_rules"):
            return inventory_df_rules
        raise AssertionError("Unknown/unhandled ruleset used in mock of host_extra_conf")

    with MockHostExtraConf(check, mocked_host_extra_conf_merged, "host_extra_conf_merged"):
        raw_discovery_result = check.run_discovery(check.run_parse(info))

    discovery_result = DiscoveryResult(raw_discovery_result)
    expected_result = DiscoveryResult(expected_result)
    assertDiscoveryResultsEqual(check, discovery_result, expected_result)