def _generate_attributes( local_status_data_tree: MockStructuredDataTree, local_inventory_tree: MockStructuredDataTree, ) -> Generator[Attributes, None, None]: for path in sorted( set(local_status_data_tree.attributes) | set(local_inventory_tree.attributes)): status_attributes = { str(k): v for k, v in local_status_data_tree.attributes.get(path, {}).items() } inventory_attributes = { str(k): v for k, v in local_inventory_tree.attributes.get(path, {}).items() if str(k) not in status_attributes } # Bypass the validation of the Attributes class: # Legacy plugins may put other types than strings in the attributes, # and we keep it that way, as this will trigger painter functions # in the inventory view. attr = Attributes(path=list(path)) yield attr._replace( inventory_attributes=inventory_attributes, status_attributes=status_attributes, )
def test_attributes_duplicate_keys(): with pytest.raises(ValueError): _ = Attributes( path=["software", "os"], inventory_attributes={"version": "42"}, status_attributes={"version": "42"}, )
def test_atrributes_wrong_types(): with pytest.raises(TypeError): _ = Attributes( path=["software", "os"], inventory_attributes={"version": (42, 23)}, # type: ignore[dict-item] )
def test_attributes_instanciated(): attr = Attributes( path=["software", "os"], status_attributes={"vendor": "emmentaler"}, inventory_attributes={"version": "42"}, ) assert attr.path == ["software", "os"] assert attr.status_attributes == {"vendor": "emmentaler"} assert attr.inventory_attributes == {"version": "42"} assert repr( attr ) == "Attributes(path=['software', 'os'], inventory_attributes={'version': '42'}, status_attributes={'vendor': 'emmentaler'})" attr2 = Attributes( path=["software", "os"], status_attributes={"vendor": "camembert"}, inventory_attributes={"version": "42"}, ) assert attr == attr assert attr2 != attr
def test_aggregator_raises_collision(): inventory_items: List[Union[Attributes, TableRow]] = [ Attributes(path=["a", "b", "c"], status_attributes={"foo": "bar"}), TableRow(path=["a", "b", "c"], key_columns={"foo": "bar"}), ] result = inventory._TreeAggregator().aggregate_results(inventory_items) assert isinstance(result, TypeError) assert str(result) == ( "Cannot create TableRow at path ['a', 'b', 'c']: this is a Attributes node." )
def test_integrate_attributes(): inventory_items: List[Attributes] = [ Attributes( path=["a", "b", "c"], inventory_attributes={ "foo0": "bar0", "foo1": "bar1", }, ), ] tree_aggr = inventory.TreeAggregator() tree_aggr.aggregate_results( inventory_generator=inventory_items, retentions_tracker=RetentionsTracker([]), raw_cache_info=None, is_legacy_plugin=False, ) assert tree_aggr.trees.inventory.serialize() == { "Attributes": {}, "Nodes": { "a": { "Attributes": {}, "Nodes": { "b": { "Attributes": {}, "Nodes": { "c": { "Attributes": { "Pairs": { "foo0": "bar0", "foo1": "bar1" }, }, "Nodes": {}, "Table": {}, } }, "Table": {}, } }, "Table": {}, } }, "Table": {}, }
def test_aggregator_raises_collision(): inventory_items: List[Union[Attributes, TableRow]] = [ Attributes(path=["a", "b", "c"], status_attributes={"foo": "bar"}), TableRow(path=["a", "b", "c"], key_columns={"foo": "bar"}), ] result = inventory.TreeAggregator().aggregate_results( inventory_generator=inventory_items, retentions_tracker=RetentionsTracker([]), raw_cache_info=None, is_legacy_plugin=False, ) assert isinstance(result, TypeError) assert str(result) == ( "Cannot create TableRow at path ['a', 'b', 'c']: this is a Attributes node." )
def _generate_attributes( local_status_data_tree: MockStructuredDataTree, local_inventory_tree: MockStructuredDataTree, ) -> Generator[Attributes, None, None]: for path in sorted( set(local_status_data_tree.attributes) | set(local_inventory_tree.attributes)): status_attributes = { str(k): str(v) for k, v in local_status_data_tree.attributes.get(path, {}).items() } inventory_attributes = { str(k): str(v) for k, v in local_inventory_tree.attributes.get(path, {}).items() if str(k) not in status_attributes } yield Attributes( path=list(path), inventory_attributes=inventory_attributes, status_attributes=status_attributes, )
def test_aggregator_raises_collision() -> None: inventory_items: List[Union[Attributes, TableRow]] = [ Attributes(path=["a", "b", "c"], status_attributes={"foo": "bar"}), TableRow(path=["a", "b", "c"], key_columns={"foo": "bar"}), ] # For some reason, the callee raises instead of returning the exception if # it runs in debug mode. So let us explicitly disable that here. cmk.utils.debug.disable() result = inventory.TreeAggregator().aggregate_results( inventory_generator=inventory_items, retentions_tracker=RetentionsTracker([]), raw_cache_info=None, is_legacy_plugin=False, ) assert isinstance(result, TypeError) assert str(result) == ( "Cannot create TableRow at path ['a', 'b', 'c']: this is a Attributes node." )
def test_inventory_checkmk(): yielded_inventory = list(inv_checkmk.generate_inventory(MERGED_SECTION_ENTERPRISE)) assert yielded_inventory == [ TableRow( path=["software", "applications", "check_mk", "sites"], key_columns={"site": "cisco"}, inventory_columns={"autostart": False, "used_version": "1.6.0p13.cee"}, status_columns={ "apache": "stopped", "cmc": "stopped", "crontab": "stopped", "dcd": "stopped", "liveproxyd": "stopped", "mkeventd": "stopped", "mknotifyd": "stopped", "rrdcached": "stopped", "stunnel": "not existent", "xinetd": "not existent", }, ), TableRow( path=["software", "applications", "check_mk", "sites"], key_columns={"site": "heute"}, inventory_columns={"autostart": False, "used_version": "2020.08.20.cee"}, status_columns={ "apache": "running", "check_helper_usage": 6.34573e-12, "check_mk_helper_usage": 0.172541, "fetcher_helper_usage": 0.172541, "checker_helper_usage": 0.172541, "cmc": "running", "crontab": "running", "dcd": "running", "liveproxyd": "running", "livestatus_usage": 0.0, "mkeventd": "running", "mknotifyd": "running", "num_hosts": "1", "num_services": "48", "rrdcached": "running", "stunnel": "not existent", "xinetd": "not existent", }, ), TableRow( path=["software", "applications", "check_mk", "sites"], key_columns={"site": "stable"}, inventory_columns={"autostart": False, "used_version": "1.6.0-2020.08.18.cee"}, status_columns={ "apache": "running", "check_helper_usage": 3.46e-321, "check_mk_helper_usage": 0.377173, "fetcher_helper_usage": 0.377173, "checker_helper_usage": 0.377173, "cmc": "running", "crontab": "running", "dcd": "running", "liveproxyd": "running", "livestatus_usage": 0.0, "mkeventd": "running", "mknotifyd": "running", "num_hosts": "2", "num_services": "103", "rrdcached": "running", "stunnel": "not existent", "xinetd": "not existent", }, ), TableRow( path=["software", "applications", "check_mk", "versions"], key_columns={"version": "1.6.0-2020.08.18.cee"}, inventory_columns={ "demo": False, "edition": "cee", "num_sites": 1, "number": "1.6.0-2020.08.18", }, status_columns={}, ), TableRow( path=["software", "applications", "check_mk", "versions"], key_columns={"version": "1.6.0p12.cee"}, inventory_columns={ "demo": False, "edition": "cee", "num_sites": 0, "number": "1.6.0p12", }, status_columns={}, ), TableRow( path=["software", "applications", "check_mk", "versions"], key_columns={"version": "1.6.0p13.cee"}, inventory_columns={ "demo": False, "edition": "cee", "num_sites": 1, "number": "1.6.0p13", }, status_columns={}, ), TableRow( path=["software", "applications", "check_mk", "versions"], key_columns={"version": "2020.08.13.cee"}, inventory_columns={ "demo": False, "edition": "cee", "num_sites": 0, "number": "2020.08.13", }, status_columns={}, ), TableRow( path=["software", "applications", "check_mk", "versions"], key_columns={"version": "2020.08.20.cee"}, inventory_columns={ "demo": False, "edition": "cee", "num_sites": 1, "number": "2020.08.20", }, status_columns={}, ), Attributes( path=["software", "applications", "check_mk"], inventory_attributes={"num_versions": 5, "num_sites": 3}, status_attributes={}, ), ]
def test_common_kwarg_only(): with pytest.raises(TypeError): _ = Attributes(["a"]) # type: ignore[misc] with pytest.raises(TypeError): _ = TableRow(["a"], key_columns={"ding": "dong"}) # type: ignore[misc]
def test_inventory_checkmk(): yielded_inventory = list( inv_checkmk.generate_inventory(MERGED_SECTION_ENTERPRISE)) assert yielded_inventory == [ TableRow(path=['software', 'applications', 'check_mk', 'sites'], key_columns={'site': 'cisco'}, inventory_columns={ 'autostart': False, 'used_version': '1.6.0p13.cee' }, status_columns={ 'apache': 'stopped', 'cmc': 'stopped', 'crontab': 'stopped', 'dcd': 'stopped', 'liveproxyd': 'stopped', 'mkeventd': 'stopped', 'mknotifyd': 'stopped', 'rrdcached': 'stopped', 'stunnel': 'not existent', 'xinetd': 'not existent' }), TableRow(path=['software', 'applications', 'check_mk', 'sites'], key_columns={'site': 'heute'}, inventory_columns={ 'autostart': False, 'used_version': '2020.08.20.cee' }, status_columns={ 'apache': 'running', 'check_helper_usage': 6.34573e-12, 'check_mk_helper_usage': 0.172541, 'fetcher_helper_usage': 0.172541, 'checker_helper_usage': 0.172541, 'cmc': 'running', 'crontab': 'running', 'dcd': 'running', 'liveproxyd': 'running', 'livestatus_usage': 0.0, 'mkeventd': 'running', 'mknotifyd': 'running', 'num_hosts': '1', 'num_services': '48', 'rrdcached': 'running', 'stunnel': 'not existent', 'xinetd': 'not existent' }), TableRow(path=['software', 'applications', 'check_mk', 'sites'], key_columns={'site': 'stable'}, inventory_columns={ 'autostart': False, 'used_version': '1.6.0-2020.08.18.cee' }, status_columns={ 'apache': 'running', 'check_helper_usage': 3.46e-321, 'check_mk_helper_usage': 0.377173, 'fetcher_helper_usage': 0.377173, 'checker_helper_usage': 0.377173, 'cmc': 'running', 'crontab': 'running', 'dcd': 'running', 'liveproxyd': 'running', 'livestatus_usage': 0.0, 'mkeventd': 'running', 'mknotifyd': 'running', 'num_hosts': '2', 'num_services': '103', 'rrdcached': 'running', 'stunnel': 'not existent', 'xinetd': 'not existent' }), TableRow(path=['software', 'applications', 'check_mk', 'versions'], key_columns={'version': '1.6.0-2020.08.18.cee'}, inventory_columns={ 'demo': False, 'edition': 'cee', 'num_sites': 1, 'number': '1.6.0-2020.08.18' }, status_columns={}), TableRow(path=['software', 'applications', 'check_mk', 'versions'], key_columns={'version': '1.6.0p12.cee'}, inventory_columns={ 'demo': False, 'edition': 'cee', 'num_sites': 0, 'number': '1.6.0p12' }, status_columns={}), TableRow(path=['software', 'applications', 'check_mk', 'versions'], key_columns={'version': '1.6.0p13.cee'}, inventory_columns={ 'demo': False, 'edition': 'cee', 'num_sites': 1, 'number': '1.6.0p13' }, status_columns={}), TableRow(path=['software', 'applications', 'check_mk', 'versions'], key_columns={'version': '2020.08.13.cee'}, inventory_columns={ 'demo': False, 'edition': 'cee', 'num_sites': 0, 'number': '2020.08.13' }, status_columns={}), TableRow(path=['software', 'applications', 'check_mk', 'versions'], key_columns={'version': '2020.08.20.cee'}, inventory_columns={ 'demo': False, 'edition': 'cee', 'num_sites': 1, 'number': '2020.08.20' }, status_columns={}), Attributes(path=['software', 'applications', 'check_mk'], inventory_attributes={ 'num_versions': 5, 'num_sites': 3 }, status_attributes={}), ]