def test_parse_sections_unsuperseded(monkeypatch: MonkeyPatch) -> None: assert (ParsedSectionsResolver( section_plugins=(SECTION_ONE, SECTION_THREE), ).resolve(_get_parser(), ParsedSectionName("parsed")) is not None)
def test_get_section_kwargs( required_sections: List[str], expected_result: Dict[str, Dict[str, str]]) -> None: node_sections = AgentHostSections( sections={ SectionName("one"): NODE_1, SectionName("two"): NODE_1, SectionName("three"): NODE_1, }) host_key = HostKey(HostName("node1"), HostAddress("127.0.0.1"), SourceType.HOST) parsed_sections_broker = ParsedSectionsBroker({ host_key: ( ParsedSectionsResolver(section_plugins=[ SECTION_ONE, SECTION_TWO, SECTION_THREE, SECTION_FOUR ]), SectionsParser(host_sections=node_sections), ), }) kwargs = get_section_kwargs( parsed_sections_broker, host_key, [ParsedSectionName(n) for n in required_sections], ) assert expected_result == kwargs
def test_get_section_cluster_kwargs(required_sections: Sequence[str], expected_result: Dict[str, Any]) -> None: node1_sections = HostSections[AgentRawDataSection]( sections={ SectionName("one"): NODE_1, SectionName("two"): NODE_1, SectionName("three"): NODE_1, }) node2_sections = HostSections[AgentRawDataSection]( sections={ SectionName("two"): NODE_2, SectionName("three"): NODE_2, }) parsed_sections_broker = ParsedSectionsBroker({ HostKey(HostName("node1"), HostAddress("127.0.0.1"), SourceType.HOST): ( ParsedSectionsResolver(section_plugins=[ SECTION_ONE, SECTION_TWO, SECTION_THREE, SECTION_FOUR ], ), SectionsParser(host_sections=node1_sections, host_name=HostName("node1")), ), HostKey(HostName("node2"), HostAddress("127.0.0.1"), SourceType.HOST): ( ParsedSectionsResolver(section_plugins=[ SECTION_ONE, SECTION_TWO, SECTION_THREE, SECTION_FOUR ], ), SectionsParser(host_sections=node2_sections, host_name=HostName("node2")), ), }) kwargs = get_section_cluster_kwargs( parsed_sections_broker, [ HostKey(HostName("node1"), HostAddress("127.0.0.1"), SourceType.HOST), HostKey(HostName("node2"), HostAddress("127.0.0.1"), SourceType.HOST), ], [ParsedSectionName(n) for n in required_sections], ) assert expected_result == kwargs
def make_provider( section_plugins: Sequence[SectionPlugin], ) -> Tuple[ParsedSectionsResolver, _FakeParser]: return ( ParsedSectionsResolver(section_plugins=section_plugins, ), _FakeParser({ "section_one": ParsingResult(data=1, cache_info=None), "section_two": ParsingResult(data=2, cache_info=None), "section_thr": ParsingResult(data=3, cache_info=None), }), )
def test_get_section_content(hostname, host_entries, cluster_node_keys, expected_result): parsed_sections_broker = ParsedSectionsBroker({ HostKey(nodename, "127.0.0.1", SourceType.HOST): ( ParsedSectionsResolver( # NOTE: this tests the legacy functionality. The "proper" ParsedSectionsBroker # methods are bypassed, so these will not matter at all: section_plugins=[], ), SectionsParser(host_sections=AgentHostSections( sections={ SectionName("section_plugin_name"): node_section_content })), ) for nodename, node_section_content in host_entries }) mhs = _MultiHostSections(parsed_sections_broker) section_content = mhs.get_section_content( HostKey(hostname, "127.0.0.1", SourceType.HOST), HOST_ONLY, "section_plugin_name", False, cluster_node_keys=cluster_node_keys, check_legacy_info= {}, # only for parse_function lookup, not needed in this test ) assert expected_result == section_content section_content = mhs.get_section_content( HostKey(hostname, "127.0.0.1", SourceType.HOST), HOST_PRECEDENCE, "section_plugin_name", False, cluster_node_keys=cluster_node_keys, check_legacy_info= {}, # only for parse_function lookup, not needed in this test ) assert expected_result == section_content section_content = mhs.get_section_content( HostKey(hostname, "127.0.0.1", SourceType.MANAGEMENT), MGMT_ONLY, "section_plugin_name", False, cluster_node_keys=None if cluster_node_keys is None else [ HostKey(hn, ip, SourceType.MANAGEMENT) for (hn, ip, _st) in cluster_node_keys ], check_legacy_info= {}, # only for parse_function lookup, not needed in this test ) assert section_content is None
def test_get_section_cluster_kwargs(required_sections, expected_result): node1_sections = AgentHostSections( sections={ SectionName("one"): NODE_1, SectionName("two"): NODE_1, SectionName("three"): NODE_1 }) node2_sections = AgentHostSections(sections={ SectionName("two"): NODE_2, SectionName("three"): NODE_2, }) parsed_sections_broker = ParsedSectionsBroker({ HostKey("node1", "127.0.0.1", SourceType.HOST): ( ParsedSectionsResolver(section_plugins=[ SECTION_ONE, SECTION_TWO, SECTION_THREE, SECTION_FOUR ], ), SectionsParser(host_sections=node1_sections), ), HostKey("node2", "127.0.0.1", SourceType.HOST): ( ParsedSectionsResolver(section_plugins=[ SECTION_ONE, SECTION_TWO, SECTION_THREE, SECTION_FOUR ], ), SectionsParser(host_sections=node2_sections), ), }) kwargs = get_section_cluster_kwargs( parsed_sections_broker, [ HostKey("node1", "127.0.0.1", SourceType.HOST), HostKey("node2", "127.0.0.1", SourceType.HOST), ], [ParsedSectionName(n) for n in required_sections], ) assert expected_result == kwargs
def test_get_parsed_section(node_sections, expected_result): parsed_sections_broker = ParsedSectionsBroker({ HostKey("node1", "127.0.0.1", SourceType.HOST): ( ParsedSectionsResolver(section_plugins=[ SECTION_ONE, SECTION_TWO, SECTION_THREE, SECTION_FOUR ], ), SectionsParser(host_sections=node_sections), ), }) content = parsed_sections_broker.get_parsed_section( HostKey("node1", "127.0.0.1", SourceType.HOST), ParsedSectionName("parsed"), ) assert expected_result == content
def test_get_parsed_section(node_sections: HostSections[AgentRawDataSection], expected_result: Mapping) -> None: parsed_sections_broker = ParsedSectionsBroker({ HostKey(HostName("node1"), HostAddress("127.0.0.1"), SourceType.HOST): ( ParsedSectionsResolver(section_plugins=[ SECTION_ONE, SECTION_TWO, SECTION_THREE, SECTION_FOUR ], ), SectionsParser(host_sections=node_sections), ), }) content = parsed_sections_broker.get_parsed_section( HostKey(HostName("node1"), HostAddress("127.0.0.1"), SourceType.HOST), ParsedSectionName("parsed"), ) assert expected_result == content
def test_parse_sections_superseded(monkeypatch): assert ParsedSectionsResolver( section_plugins=(SECTION_ONE, SECTION_THREE, SECTION_FOUR), ).resolve( _get_parser(), ParsedSectionName("parsed")) is None