def test_tablerow_instanciated(): table_row = TableRow( path=["software", "os"], key_columns={"foo": "bar"}, status_columns={"packages": 42}, inventory_columns={"vendor": "emmentaler"}, ) assert table_row.path == ["software", "os"] assert table_row.key_columns == {"foo": "bar"} assert table_row.status_columns == {"packages": 42} assert table_row.inventory_columns == {"vendor": "emmentaler"} assert ( repr(table_row) == "TableRow(path=['software', 'os'], key_columns={'foo': 'bar'}, inventory_columns={'vendor': 'emmentaler'}, status_columns={'packages': 42})" ) table_row2 = TableRow( path=["software", "os"], key_columns={"foo": "bar"}, status_columns={"packages": 42}, inventory_columns={"vendor": "gorgonzola"}, ) assert table_row == table_row assert table_row2 != table_row
def _generate_table_rows( local_status_data_tree: MockStructuredDataTree, local_inventory_tree: MockStructuredDataTree, ) -> Generator[TableRow, None, None]: for path in sorted(set(local_status_data_tree.tables) | set(local_inventory_tree.tables)): inv_table = local_inventory_tree.tables.get(path, []) status_table = local_status_data_tree.tables.get(path, []) common_inv_keys = {k for k in inv_table[0] if all(k in row for row in inv_table) } if inv_table else set() common_status_keys = {k for k in status_table[0] if all(k in row for row in status_table) } if status_table else set() for row in inv_table: keys = (common_inv_keys & common_status_keys) or common_inv_keys key_columns = {str(k): v for k, v in row.items() if k in keys} # key columns must not be empty. If the following construct is empty, somehting # quite weird is going on, and we skip the table rows. if key_columns: yield TableRow( path=list(path), key_columns=key_columns, inventory_columns={str(k): v for k, v in row.items() if k not in keys}, ) for row in status_table: keys = (common_inv_keys & common_status_keys) or common_status_keys key_columns = {str(k): v for k, v in row.items() if k in keys} if key_columns: yield TableRow( path=list(path), key_columns=key_columns, status_columns={str(k): v for k, v in row.items() if k not in keys}, )
def test__generate_table_rows_list(): local_status_data_tree = MockStructuredDataNode() local_inventory_tree = MockStructuredDataNode() def plugin_list(): node = local_inventory_tree.get_list("level0_1.level1_list:") for a, b in [("l", "L1"), ("l", "L2")]: node.append({a: b}) plugin_list() table_rows = list( _generate_table_rows(local_status_data_tree, local_inventory_tree)) assert table_rows == [ TableRow( path=["level0_1", "level1_list"], key_columns={"l": "L1"}, inventory_columns={}, status_columns={}, ), TableRow( path=["level0_1", "level1_list"], key_columns={"l": "L2"}, inventory_columns={}, status_columns={}, ), ]
def test_tablerow_conflicting_keys(): with pytest.raises(ValueError): _ = TableRow( path=["hardware"], key_columns={"foo": "bar"}, status_columns={"foo": "bar"}, )
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_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 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_integrate_table_row() -> None: inventory_items: List[TableRow] = [ TableRow( path=["a", "b", "c"], key_columns={"foo": "baz"}, inventory_columns={ "col1": "baz val1", "col2": "baz val2", "col3": "baz val3", }, ), TableRow( path=["a", "b", "c"], key_columns={"foo": "bar"}, inventory_columns={ "col1": "bar val1", "col2": "bar val2", }, ), TableRow( path=["a", "b", "c"], key_columns={"foo": "bar"}, inventory_columns={ "col1": "new bar val1", "col3": "bar val3", }, ), ] 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": {}, "Nodes": {}, "Table": { "KeyColumns": ["foo"], "Rows": [ { "col1": "baz " "val1", "col2": "baz " "val2", "col3": "baz " "val3", "foo": "baz", }, { "col1": "new " "bar " "val1", "col2": "bar " "val2", "col3": "bar " "val3", "foo": "bar", }, ], }, } }, "Table": {}, } }, "Table": {}, } }, "Table": {}, }
def test_tablerow_wrong_types(): with pytest.raises(TypeError): _ = TableRow(path=["hardware"], key_columns={23: 42}) # type: ignore[dict-item]
def test_tablerow_missing_key_columns(): with pytest.raises(TypeError): _ = TableRow(path=["hardware"], key_columns=None) # type: ignore[arg-type] _ = TableRow(path=["hardware"], key_columns={})
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={}), ]