X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FN2VC.git;a=blobdiff_plain;f=n2vc%2Ftests%2Funit%2Futils.py;h=2f107a71eebb39debf542b7ad1f73824f7b54c0f;hp=fe7362e7471bc54aeba5f21c9fba79e0c727fca0;hb=refs%2Fchanges%2F16%2F10616%2F9;hpb=7ff392f6b60850ac7408f96fd42ab16b005ec2bf diff --git a/n2vc/tests/unit/utils.py b/n2vc/tests/unit/utils.py index fe7362e..2f107a7 100644 --- a/n2vc/tests/unit/utils.py +++ b/n2vc/tests/unit/utils.py @@ -14,11 +14,55 @@ import asyncio -from n2vc.utils import Dict, EntityType, N2VCDeploymentStatus +from n2vc.utils import Dict, N2VCDeploymentStatus from n2vc.n2vc_conn import N2VCConnector from unittest.mock import MagicMock +kubeconfig = """apiVersion: v1 +clusters: +- cluster: + certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1\ + JSURBVENDQWVtZ0F3SUJBZ0lKQUxjMk9xVUpwcnVCTUEwR0NTcUdTSWIzRFFFQk\ + N3VUFNQmN4RlRBVEJnTlYKQkFNTURERXdMakUxTWk0eE9ETXVNVEFlRncweU1EQ\ + TVNVEV4TkRJeU16VmFGdzB6TURBNU1Ea3hOREl5TXpWYQpNQmN4RlRBVEJnTlZC\ + QU1NRERFd0xqRTFNaTR4T0RNdU1UQ0NBU0l3RFFZSktvWklodmNOQVFFQkJRQUR\ + nZ0VQCkFEQ0NBUW9DZ2dFQkFNV0tyQkdxWlJRT0VONDExR2RESmY2ckZWRDcvMU\ + xHNlZMWjNhd1BRdHBhRTRxdVdyNisKWjExTWwra2kwVEU1cGZFV3dKenVUZXlCU\ + XVkUEpnYm1QTjF1VWROdGRiNlpocHEzeC9oT0hCMVJLNC9iSlNFUgpiZ0dITmN6\ + MzR6SHRaZ1dwb2NPTXpPOW9oRUdhMTZUaDhmQWVxYU1CQTJRaklmeUFlaVp3VHJ\ + nZ3BrY2dBMUlOCjBvQkdqSURnSGVoSU5tbGZOOURkQ3hNN1FNTmtSbzRXdE13bF\ + JSRWZ4QnFiVkNpZGFjbVhhb1VPUjJPeFVmQWEKN1orSUU1TmN5ZFQ1TGovazdwd\ + XZCVkdIa0JQWnE0TmlBa3R4aXd5NVB5R29GTk9mT0NrV2I2VnBzVzNhTlNJeAo4\ + aXBITkc3enV3elc1TGQ5TkhQYWpRckZwdFZBSHpJNWNhRUNBd0VBQWFOUU1FNHd\ + IUVlEVlIwT0JCWUVGQ1dVCkFaTXNaeE13L1k1OGlXMGZJWVAzcDdTYk1COEdBMV\ + VkSXdRWU1CYUFGQ1dVQVpNc1p4TXcvWTU4aVcwZklZUDMKcDdTYk1Bd0dBMVVkR\ + XdRRk1BTUJBZjh3RFFZSktvWklodmNOQVFFTEJRQURnZ0VCQUJaMlYxMWowRzhh\ + Z1Z6Twp2YWtKTGt4UGZ0UE1NMFFOaVRzZmV6RzlicnBkdEVLSjFyalFCblNXYTN\ + WbThWRGZTYkhLQUNXaGh0OEhzcXhtCmNzdVQyOWUyaGZBNHVIOUxMdy9MVG5EdE\ + tJSjZ6aWFzaTM5RGh3UGwwaExuamJRMjk4VVo5TGovVlpnZGlqemIKWnVPdHlpT\ + nVOS0E2Nmd0dGxXcWZRQ2hkbnJ5MlZUbjBjblR5dU9UalByYWdOdXJMdlVwL3Nl\ + eURhZmsxNXJ4egozcmlYZldiQnRhUUk1dnM0ekFKU2xneUg2RnpiZStoTUhlUzF\ + mM2ppb3dJV0lRR2NNbHpGT1RpMm1xWFRybEJYCnh1WmpLZlpOcndjQVNGbk9qYV\ + BWeFQ1ODJ4WWhtTm8wR3J2MlZEck51bDlSYkgvK3lNS2J5NEhkOFRvVThMU2kKY\ + 3Uxajh3cz0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo= + server: https://192.168.0.22:16443 + name: microk8s-cluster +contexts: +- context: + cluster: microk8s-cluster + user: admin + name: microk8s +current-context: microk8s +kind: Config +preferences: {} +users: +- name: admin + user: + token: clhkRExRem5Xd1dCdnFEVXdvRGtDRGE5b1F3WnNrZk5qeHFCOU10bHBZRT0K +""" + + async def AsyncMockFunc(): await asyncio.sleep(1) @@ -38,7 +82,18 @@ class FakeN2VC(MagicMock): detailed_status: str, vca_status: str, entity_type: str, + vca_id: str = None, ): + """ + Write application status to database + + :param: db_dict: DB dictionary + :param: status: Status of the application + :param: detailed_status: Detailed status + :param: vca_status: VCA status + :param: entity_type: Entity type ("application", "machine, and "action") + :param: vca_id: Id of the VCA. If None, the default VCA will be used. + """ self.last_written_values = Dict( { "n2vc_status": status, @@ -55,7 +110,19 @@ class FakeMachine(MagicMock): entity_id = "2" dns_name = "FAKE ENDPOINT" model_name = "FAKE MODEL" - entity_type = EntityType.MACHINE + entity_type = "machine" + safe_data = {"instance-id": "myid"} + + async def destroy(self, force): + pass + + +class FakeManualMachine(MagicMock): + entity_id = "2" + dns_name = "FAKE ENDPOINT" + model_name = "FAKE MODEL" + entity_type = "machine" + safe_data = {"instance-id": "manual:myid"} async def destroy(self, force): pass @@ -79,6 +146,15 @@ class FakeAction(MagicMock): status = "ready" +class FakeModel: + def __init__(self, applications: dict = {}): + self._applications = applications + + @property + def applications(self): + return self._applications + + class FakeUnit(MagicMock): async def is_leader_from_status(self): return True @@ -88,7 +164,6 @@ class FakeUnit(MagicMock): class FakeApplication(AsyncMock): - async def set_config(self, config): pass @@ -98,9 +173,31 @@ class FakeApplication(AsyncMock): async def get_actions(self): return ["existing_action"] + async def get_config(self): + return ["app_config"] + units = [FakeUnit(), FakeUnit()] +class FakeFile: + def __init__(self, content: str = ""): + self.content = content + + def read(self, size: int = -1): + return self.content + + +class FakeFileWrapper: + def __init__(self, content: str = ""): + self.file = FakeFile(content=content) + + def __enter__(self): + return self.file + + def __exit__(self, type, value, traceback): + pass + + FAKE_DELTA_MACHINE_PENDING = Dict( { "deltas": ["machine", "change", {}], @@ -201,8 +298,8 @@ FAKE_DELTA_ACTION_COMPLETED = Dict( Deltas = [ Dict( { - "entity": Dict({"id": "2", "type": EntityType.MACHINE}), - "filter": Dict({"entity_id": "2", "entity_type": EntityType.MACHINE}), + "entity": Dict({"id": "2", "type": "machine"}), + "filter": Dict({"entity_id": "2", "entity_type": "machine"}), "delta": FAKE_DELTA_MACHINE_PENDING, "entity_status": Dict( {"status": "pending", "message": "Running", "vca_status": "running"} @@ -224,8 +321,8 @@ Deltas = [ ), Dict( { - "entity": Dict({"id": "2", "type": EntityType.MACHINE}), - "filter": Dict({"entity_id": "1", "entity_type": EntityType.MACHINE}), + "entity": Dict({"id": "2", "type": "machine"}), + "filter": Dict({"entity_id": "1", "entity_type": "machine"}), "delta": FAKE_DELTA_MACHINE_PENDING, "entity_status": Dict( {"status": "pending", "message": "Running", "vca_status": "running"} @@ -235,8 +332,8 @@ Deltas = [ ), Dict( { - "entity": Dict({"id": "2", "type": EntityType.MACHINE}), - "filter": Dict({"entity_id": "2", "entity_type": EntityType.MACHINE}), + "entity": Dict({"id": "2", "type": "machine"}), + "filter": Dict({"entity_id": "2", "entity_type": "machine"}), "delta": FAKE_DELTA_MACHINE_STARTED, "entity_status": Dict( {"status": "started", "message": "Running", "vca_status": "running"} @@ -258,8 +355,8 @@ Deltas = [ ), Dict( { - "entity": Dict({"id": "2", "type": EntityType.MACHINE}), - "filter": Dict({"entity_id": "1", "entity_type": EntityType.MACHINE}), + "entity": Dict({"id": "2", "type": "machine"}), + "filter": Dict({"entity_id": "1", "entity_type": "machine"}), "delta": FAKE_DELTA_MACHINE_STARTED, "entity_status": Dict( {"status": "started", "message": "Running", "vca_status": "running"} @@ -269,8 +366,8 @@ Deltas = [ ), Dict( { - "entity": Dict({"id": "git/0", "type": EntityType.UNIT}), - "filter": Dict({"entity_id": "git", "entity_type": EntityType.APPLICATION}), + "entity": Dict({"id": "git/0", "type": "unit"}), + "filter": Dict({"entity_id": "git", "entity_type": "application"}), "delta": FAKE_DELTA_UNIT_PENDING, "entity_status": Dict( {"status": "waiting", "message": "", "vca_status": "waiting"} @@ -292,8 +389,8 @@ Deltas = [ ), Dict( { - "entity": Dict({"id": "git/0", "type": EntityType.UNIT}), - "filter": Dict({"entity_id": "2", "entity_type": EntityType.MACHINE}), + "entity": Dict({"id": "git/0", "type": "unit"}), + "filter": Dict({"entity_id": "2", "entity_type": "machine"}), "delta": FAKE_DELTA_UNIT_PENDING, "entity_status": Dict( {"status": "waiting", "message": "", "vca_status": "waiting"} @@ -303,8 +400,8 @@ Deltas = [ ), Dict( { - "entity": Dict({"id": "git/0", "type": EntityType.UNIT}), - "filter": Dict({"entity_id": "git", "entity_type": EntityType.APPLICATION}), + "entity": Dict({"id": "git/0", "type": "unit"}), + "filter": Dict({"entity_id": "git", "entity_type": "application"}), "delta": FAKE_DELTA_UNIT_STARTED, "entity_status": Dict( {"status": "active", "message": "", "vca_status": "active"} @@ -326,8 +423,8 @@ Deltas = [ ), Dict( { - "entity": Dict({"id": "git/0", "type": EntityType.UNIT}), - "filter": Dict({"entity_id": "1", "entity_type": EntityType.ACTION}), + "entity": Dict({"id": "git/0", "type": "unit"}), + "filter": Dict({"entity_id": "1", "entity_type": "action"}), "delta": FAKE_DELTA_UNIT_STARTED, "entity_status": Dict( {"status": "active", "message": "", "vca_status": "active"} @@ -337,8 +434,8 @@ Deltas = [ ), Dict( { - "entity": Dict({"id": "git", "type": EntityType.APPLICATION}), - "filter": Dict({"entity_id": "git", "entity_type": EntityType.APPLICATION}), + "entity": Dict({"id": "git", "type": "application"}), + "filter": Dict({"entity_id": "git", "entity_type": "application"}), "delta": FAKE_DELTA_APPLICATION_MAINTENANCE, "entity_status": Dict( { @@ -364,8 +461,8 @@ Deltas = [ ), Dict( { - "entity": Dict({"id": "git", "type": EntityType.APPLICATION}), - "filter": Dict({"entity_id": "2", "entity_type": EntityType.MACHINE}), + "entity": Dict({"id": "git", "type": "application"}), + "filter": Dict({"entity_id": "2", "entity_type": "machine"}), "delta": FAKE_DELTA_APPLICATION_MAINTENANCE, "entity_status": Dict( { @@ -379,8 +476,8 @@ Deltas = [ ), Dict( { - "entity": Dict({"id": "git", "type": EntityType.APPLICATION}), - "filter": Dict({"entity_id": "git", "entity_type": EntityType.APPLICATION}), + "entity": Dict({"id": "git", "type": "application"}), + "filter": Dict({"entity_id": "git", "entity_type": "application"}), "delta": FAKE_DELTA_APPLICATION_ACTIVE, "entity_status": Dict( {"status": "active", "message": "Ready!", "vca_status": "active"} @@ -402,8 +499,8 @@ Deltas = [ ), Dict( { - "entity": Dict({"id": "git", "type": EntityType.APPLICATION}), - "filter": Dict({"entity_id": "1", "entity_type": EntityType.ACTION}), + "entity": Dict({"id": "git", "type": "application"}), + "filter": Dict({"entity_id": "1", "entity_type": "action"}), "delta": FAKE_DELTA_APPLICATION_ACTIVE, "entity_status": Dict( {"status": "active", "message": "Ready!", "vca_status": "active"} @@ -413,8 +510,8 @@ Deltas = [ ), Dict( { - "entity": Dict({"id": "1", "type": EntityType.ACTION}), - "filter": Dict({"entity_id": "1", "entity_type": EntityType.ACTION}), + "entity": Dict({"id": "1", "type": "action"}), + "filter": Dict({"entity_id": "1", "entity_type": "action"}), "delta": FAKE_DELTA_ACTION_COMPLETED, "entity_status": Dict( { @@ -440,8 +537,8 @@ Deltas = [ ), Dict( { - "entity": Dict({"id": "git", "type": EntityType.ACTION}), - "filter": Dict({"entity_id": "1", "entity_type": EntityType.MACHINE}), + "entity": Dict({"id": "git", "type": "action"}), + "filter": Dict({"entity_id": "1", "entity_type": "machine"}), "delta": FAKE_DELTA_ACTION_COMPLETED, "entity_status": Dict( {