Cleanup all direct config.service_extra_conf calls
[check_mk.git] / tests / unit / cmk_base / test_core_config.py
blobb6c31a88bd702134c112269047b3f0ec3c34f898
1 # encoding: utf-8
2 import pytest # type: ignore
4 from cmk.utils.exceptions import MKGeneralException
5 import cmk_base.config as config
6 import cmk_base.core_config as core_config
9 def test_active_check_arguments(mocker):
10 with pytest.raises(MKGeneralException):
11 core_config.active_check_arguments("bla", "blub", 1)
13 with pytest.raises(MKGeneralException):
14 core_config.active_check_arguments("bla", "blub", (1, 2))
16 prepare_check_command = mocker.patch.object(config, "prepare_check_command")
17 core_config.active_check_arguments("bla", "blub", u"args 123 -x 1 -y 2")
18 assert prepare_check_command.called_once()
21 def test_get_host_attributes(monkeypatch):
22 monkeypatch.setattr(config, "all_hosts", ["test-host|abc"])
23 monkeypatch.setattr(config, "host_paths", {"test-host": "/"})
24 monkeypatch.setattr(config, "host_tags", {
25 "test-host": {
26 "tag_group": "abc",
29 monkeypatch.setattr(config, "host_labels", {
30 "test-host": {
31 "ding": "dong",
35 config_cache = config.get_config_cache()
36 config_cache.initialize()
38 attrs = core_config.get_host_attributes("test-host", config_cache)
39 assert attrs == {
40 '_ADDRESS_4': '0.0.0.0',
41 '_ADDRESS_6': '',
42 '_ADDRESS_FAMILY': '4',
43 '_FILENAME': '/',
44 '_TAGS': 'abc',
45 '__TAG_tag_group': 'abc',
46 '__LABEL_ding': 'dong',
47 '__LABELSOURCE_ding': 'explicit',
48 'address': '0.0.0.0',
49 'alias': 'test-host',
53 @pytest.mark.parametrize("tag_groups,result", [({
54 "tg1": "val1",
55 "tg2": "val1",
56 }, {
57 u"__TAG_tg1": u"val1",
58 u"__TAG_tg2": u"val1",
59 }), ({
60 u"täg-113232_eybc": u"äbcdef"
61 }, {
62 u"__TAG_täg-113232_eybc": u"äbcdef",
63 }), ({
64 "a.d B/E u-f N_A": "a.d B/E u-f N_A"
65 }, {
66 u"__TAG_a.d B/E u-f N_A": "a.d B/E u-f N_A",
67 })])
68 def test_get_tag_attributes(tag_groups, result):
69 attributes = core_config._get_tag_attributes(tag_groups, "TAG")
70 assert attributes == result
71 for k, v in attributes.items():
72 assert isinstance(k, unicode)
73 assert isinstance(v, unicode)
76 def test_custom_service_attributes_of(monkeypatch):
77 config_cache = config.get_config_cache()
78 attributes = core_config._custom_service_attributes_of(config_cache, "luluhost", "laladescr")
79 assert attributes == {}
81 monkeypatch.setattr(config, "all_hosts", ["luluhost"])
82 monkeypatch.setattr(config, "host_paths", {"luluhost": "/"})
83 monkeypatch.setattr(config, "custom_service_attributes", [
84 ([('deng', '1')], [], config.ALL_HOSTS, config.ALL_SERVICES, {}),
85 ([('ding', '2'), ('ding', '2a'),
86 ('dong', '3')], [], config.ALL_HOSTS, config.ALL_SERVICES, {}),
88 config_cache.initialize()
90 attributes = core_config._custom_service_attributes_of(config_cache, "luluhost", "laladescr")
91 assert attributes == {
92 "deng": "1",
93 "ding": "2a",
94 "dong": "3",