diff --git a/app/core/__fixture__/snapshot/minimal/ms_glaciation-test-master01_80.query.containers.yaml b/app/core/__fixture__/snapshot/minimal/ms_glaciation-test-master01_80.query.containers.yaml new file mode 100644 index 0000000..34a8e06 --- /dev/null +++ b/app/core/__fixture__/snapshot/minimal/ms_glaciation-test-master01_80.query.containers.yaml @@ -0,0 +1,4 @@ +- resource: + prefix: cluster + id: kube-system.coredns-787d4945fb-l85r5.coredns + statusValue: Running diff --git a/app/core/__fixture__/snapshot/minimal/ms_glaciation-test-master01_80.query.nodes.yaml b/app/core/__fixture__/snapshot/minimal/ms_glaciation-test-master01_80.query.nodes.yaml new file mode 100644 index 0000000..aa6fc31 --- /dev/null +++ b/app/core/__fixture__/snapshot/minimal/ms_glaciation-test-master01_80.query.nodes.yaml @@ -0,0 +1,4 @@ +- resource: + prefix: cluster + id: glaciation-test-master01 + statusValue: Ready diff --git a/app/core/__fixture__/snapshot/minimal/ms_glaciation-test-master01_80.query.pods.yaml b/app/core/__fixture__/snapshot/minimal/ms_glaciation-test-master01_80.query.pods.yaml new file mode 100644 index 0000000..6163e70 --- /dev/null +++ b/app/core/__fixture__/snapshot/minimal/ms_glaciation-test-master01_80.query.pods.yaml @@ -0,0 +1,4 @@ +- resource: + prefix: cluster + id: kube-system.coredns-787d4945fb-l85r5 + statusValue: running diff --git a/app/core/builder/test_kg_builder.py b/app/core/builder/test_kg_builder.py index e22b3ee..e2375b5 100644 --- a/app/core/builder/test_kg_builder.py +++ b/app/core/builder/test_kg_builder.py @@ -48,8 +48,10 @@ def setUp(self) -> None: ) def test_build_minimal(self) -> None: + slice_id = KGSliceId("glaciation-test-master01", 80) self.mock_inputs( "minimal", + [slice_id], self.k8s_client, self.metric_client, self.client, @@ -62,7 +64,7 @@ def test_build_minimal(self) -> None: slice = self.wait_for_slice(2) self.assertEqual(slice.timestamp, 1000) - self.assertEqual(slice.slice_id, KGSliceId("glaciation-test-master01", 80)) + self.assertEqual(slice.slice_id, slice_id) self.assertNotEqual(slice.graph, InMemoryGraph()) self.assert_graph(slice.graph, "minimal", slice.slice_id) diff --git a/app/core/kg/kg_repository.py b/app/core/kg/kg_repository.py index 441f385..c0cd4e3 100644 --- a/app/core/kg/kg_repository.py +++ b/app/core/kg/kg_repository.py @@ -17,23 +17,20 @@ class KGRepository: - node_query: ResourceStatusQuery - pod_query: ResourceStatusQuery - container_query: ResourceStatusQuery + NODE_QUERY = ResourceStatusQuery(ResourceType.NODE) + POD_QUERY = ResourceStatusQuery(ResourceType.POD) + CONTAINER_QUERY = ResourceStatusQuery(ResourceType.CONTAINER) metadata_client: MetadataServiceClient def __init__(self, metadata_client: MetadataServiceClient): self.metadata_client = metadata_client - self.node_query = ResourceStatusQuery(ResourceType.NODE) - self.pod_query = ResourceStatusQuery(ResourceType.POD) - self.container_query = ResourceStatusQuery(ResourceType.CONTAINER) async def query_snapshot(self, slice_id: KGSliceId) -> KGSnapshot: (nodes, pods, containers) = await asyncio.gather( - self.query(slice_id, self.node_query), - self.query(slice_id, self.pod_query), - self.query(slice_id, self.container_query), + self.query(slice_id, self.NODE_QUERY), + self.query(slice_id, self.POD_QUERY), + self.query(slice_id, self.CONTAINER_QUERY), ) return KGSnapshot(nodes, pods, containers) diff --git a/app/core/test_snapshot_base.py b/app/core/test_snapshot_base.py index 6aee303..5f298a0 100644 --- a/app/core/test_snapshot_base.py +++ b/app/core/test_snapshot_base.py @@ -13,12 +13,15 @@ MockMetadataServiceClient, ) from app.clients.prometheus.mock_prometheus_client import MockPrometheusClient +from app.core.kg.kg_repository import KGRepository from app.core.kg.kg_snapshot import KGSnapshot from app.core.repository.query_settings import QuerySettings from app.core.repository.types import MetricQuery from app.core.types import KGSliceId, MetricSnapshot, MetricValue, SliceInputs from app.kg.graph import Graph from app.kg.id_base import IdBase +from app.kg.iri import IRI +from app.kg.literal import Literal from app.serialize.jsonld_configuration import JsonLDConfiguration from app.serialize.jsonld_serializer import JsonLDSerialializer from app.transform.upper_ontology_base import UpperOntologyBase @@ -30,6 +33,7 @@ class SnapshotTestBase: def mock_inputs( self, identity: str, + slices: List[KGSliceId], k8s_client: MockK8SClient, metric_client: MockPrometheusClient, metadata_service_client: MockMetadataServiceClient, @@ -55,6 +59,21 @@ def mock_inputs( metric_client.mock_query(query.query, [value]) settings.pod_queries.append(query) + for slice_id in slices: + host_id = slice_id.get_host_port() + nodes = self.load_kg_statuses(identity, slice_id, "nodes") + pods = self.load_kg_statuses(identity, slice_id, "pods") + containers = self.load_kg_statuses(identity, slice_id, "containers") + metadata_service_client.mock_query( + host_id, KGRepository.NODE_QUERY.get_query(), nodes + ) + metadata_service_client.mock_query( + host_id, KGRepository.POD_QUERY.get_query(), pods + ) + metadata_service_client.mock_query( + host_id, KGRepository.CONTAINER_QUERY.get_query(), containers + ) + def get_existing_metadata(self, identity: str) -> KGSnapshot: return KGSnapshot(nodes=[], pods=[], containers=[]) @@ -122,6 +141,21 @@ def dataclass_from_dict(self, klass, d): except Exception: return d # Not a dataclass field + def load_kg_statuses( + self, snapshot_id: str, slice_id: KGSliceId, file_id: str + ) -> List[Dict[str, Any]]: + file_path = f"{self.SNAPSHOT_ROOT}/{snapshot_id}/ms_{slice_id.node_ip}_{slice_id.port}.query.{file_id}.yaml" + if not os.path.exists(file_path): + return [] + results = [] + for status_object in self.safe_load_yaml(file_path): + resource = IRI( + status_object["resource"]["prefix"], status_object["resource"]["id"] + ) + status_value = Literal(status_object["statusValue"], Literal.TYPE_STRING) + results.append({"resource": resource, "statusValue": status_value}) + return results + def assert_graph(self, graph: Graph, snapshot_id: str, slice_id: KGSliceId) -> None: file_path = f"{self.SNAPSHOT_ROOT}/{snapshot_id}/slice_{slice_id.node_ip}_{slice_id.port}.jsonld" node_jsonld = self.load_jsonld(file_path) diff --git a/app/test_kgexporter_context.py b/app/test_kgexporter_context.py index 3c5d97e..455fc37 100644 --- a/app/test_kgexporter_context.py +++ b/app/test_kgexporter_context.py @@ -50,6 +50,7 @@ def setUp(self) -> None: def test_end_to_end_minimal(self) -> None: self.mock_inputs( "minimal", + [KGSliceId("glaciation-test-master01", 80)], self.k8s_client, self.metric_client, self.metadata_client, @@ -67,6 +68,10 @@ def test_end_to_end_multinode(self) -> None: self.settings.prometheus.endpoint_port = 8081 self.mock_inputs( "multinode", + [ + KGSliceId("glaciation-test-master01", 80), + KGSliceId("glaciation-test-worker01", 80), + ], self.k8s_client, self.metric_client, self.metadata_client,