Skip to content

Commit

Permalink
HHT-1174: resource termination integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
ktatarnikov committed Aug 15, 2024
1 parent c2cba1a commit 77bbc30
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- resource:
prefix: cluster
id: kube-system.coredns-787d4945fb-l85r5.coredns
statusValue: Running
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- resource:
prefix: cluster
id: glaciation-test-master01
statusValue: Ready
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- resource:
prefix: cluster
id: kube-system.coredns-787d4945fb-l85r5
statusValue: running
4 changes: 3 additions & 1 deletion app/core/builder/test_kg_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

Expand Down
15 changes: 6 additions & 9 deletions app/core/kg/kg_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
34 changes: 34 additions & 0 deletions app/core/test_snapshot_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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=[])

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions app/test_kgexporter_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 77bbc30

Please sign in to comment.