1 # Copyright 2020 Canonical Ltd.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 from n2vc
.utils
import Dict
, N2VCDeploymentStatus
18 from n2vc
.n2vc_conn
import N2VCConnector
19 from unittest
.mock
import MagicMock
22 async def AsyncMockFunc():
23 await asyncio
.sleep(1)
26 class AsyncMock(MagicMock
):
27 async def __call__(self
, *args
, **kwargs
):
28 return super(AsyncMock
, self
).__call
__(*args
, **kwargs
)
31 class FakeN2VC(MagicMock
):
32 last_written_values
= None
34 async def write_app_status_to_db(
37 status
: N2VCDeploymentStatus
,
42 self
.last_written_values
= Dict(
44 "n2vc_status": status
,
45 "message": detailed_status
,
46 "vca_status": vca_status
,
47 "entity": entity_type
,
51 osm_status
= N2VCConnector
.osm_status
54 class FakeMachine(MagicMock
):
56 dns_name
= "FAKE ENDPOINT"
57 model_name
= "FAKE MODEL"
58 entity_type
= "machine"
59 safe_data
= {"instance-id": "myid"}
61 async def destroy(self
, force
):
65 class FakeManualMachine(MagicMock
):
67 dns_name
= "FAKE ENDPOINT"
68 model_name
= "FAKE MODEL"
69 entity_type
= "machine"
70 safe_data
= {"instance-id": "manual:myid"}
72 async def destroy(self
, force
):
76 class FakeWatcher(AsyncMock
):
78 delta_to_return
= None
81 return Dict({"deltas": self
.delta_to_return
})
84 class FakeConnection(MagicMock
):
89 class FakeAction(MagicMock
):
94 class FakeUnit(MagicMock
):
95 async def is_leader_from_status(self
):
98 async def run_action(self
, action_name
):
102 class FakeApplication(AsyncMock
):
104 async def set_config(self
, config
):
107 async def add_unit(self
, to
):
110 async def get_actions(self
):
111 return ["existing_action"]
113 units
= [FakeUnit(), FakeUnit()]
116 FAKE_DELTA_MACHINE_PENDING
= Dict(
118 "deltas": ["machine", "change", {}],
123 "instance-id": "juju-1b5808-2",
124 "agent-status": {"current": "pending", "message": "", "version": ""},
125 "instance-status": {"current": "running", "message": "Running"},
129 FAKE_DELTA_MACHINE_STARTED
= Dict(
131 "deltas": ["machine", "change", {}],
136 "instance-id": "juju-1b5808-2",
137 "agent-status": {"current": "started", "message": "", "version": ""},
138 "instance-status": {"current": "running", "message": "Running"},
143 FAKE_DELTA_UNIT_PENDING
= Dict(
145 "deltas": ["unit", "change", {}],
150 "application": "git",
152 "workload-status": {"current": "waiting", "message": ""},
153 "agent-status": {"current": "idle", "message": ""},
158 FAKE_DELTA_UNIT_STARTED
= Dict(
160 "deltas": ["unit", "change", {}],
165 "application": "git",
167 "workload-status": {"current": "active", "message": ""},
168 "agent-status": {"current": "idle", "message": ""},
173 FAKE_DELTA_APPLICATION_MAINTENANCE
= Dict(
175 "deltas": ["application", "change", {}],
176 "entity": "application",
181 "current": "maintenance",
182 "message": "installing charm software",
188 FAKE_DELTA_APPLICATION_ACTIVE
= Dict(
190 "deltas": ["application", "change", {}],
191 "entity": "application",
193 "data": {"name": "git", "status": {"current": "active", "message": "Ready!"}},
197 FAKE_DELTA_ACTION_COMPLETED
= Dict(
199 "deltas": ["action", "change", {}],
203 "model-uuid": "af19cdd4-374a-4d9f-86b1-bfed7b1b5808",
207 "status": "completed",
216 "entity": Dict({"id": "2", "type": "machine"}),
217 "filter": Dict({"entity_id": "2", "entity_type": "machine"}),
218 "delta": FAKE_DELTA_MACHINE_PENDING
,
219 "entity_status": Dict(
220 {"status": "pending", "message": "Running", "vca_status": "running"}
227 "message": "Running",
229 "vca_status": "running",
230 "n2vc_status": N2VCDeploymentStatus
.PENDING
,
239 "entity": Dict({"id": "2", "type": "machine"}),
240 "filter": Dict({"entity_id": "1", "entity_type": "machine"}),
241 "delta": FAKE_DELTA_MACHINE_PENDING
,
242 "entity_status": Dict(
243 {"status": "pending", "message": "Running", "vca_status": "running"}
245 "db": Dict({"written": False, "data": None}),
250 "entity": Dict({"id": "2", "type": "machine"}),
251 "filter": Dict({"entity_id": "2", "entity_type": "machine"}),
252 "delta": FAKE_DELTA_MACHINE_STARTED
,
253 "entity_status": Dict(
254 {"status": "started", "message": "Running", "vca_status": "running"}
261 "message": "Running",
263 "vca_status": "running",
264 "n2vc_status": N2VCDeploymentStatus
.COMPLETED
,
273 "entity": Dict({"id": "2", "type": "machine"}),
274 "filter": Dict({"entity_id": "1", "entity_type": "machine"}),
275 "delta": FAKE_DELTA_MACHINE_STARTED
,
276 "entity_status": Dict(
277 {"status": "started", "message": "Running", "vca_status": "running"}
279 "db": Dict({"written": False, "data": None}),
284 "entity": Dict({"id": "git/0", "type": "unit"}),
285 "filter": Dict({"entity_id": "git", "entity_type": "application"}),
286 "delta": FAKE_DELTA_UNIT_PENDING
,
287 "entity_status": Dict(
288 {"status": "waiting", "message": "", "vca_status": "waiting"}
297 "vca_status": "waiting",
298 "n2vc_status": N2VCDeploymentStatus
.RUNNING
,
307 "entity": Dict({"id": "git/0", "type": "unit"}),
308 "filter": Dict({"entity_id": "2", "entity_type": "machine"}),
309 "delta": FAKE_DELTA_UNIT_PENDING
,
310 "entity_status": Dict(
311 {"status": "waiting", "message": "", "vca_status": "waiting"}
313 "db": Dict({"written": False, "data": None}),
318 "entity": Dict({"id": "git/0", "type": "unit"}),
319 "filter": Dict({"entity_id": "git", "entity_type": "application"}),
320 "delta": FAKE_DELTA_UNIT_STARTED
,
321 "entity_status": Dict(
322 {"status": "active", "message": "", "vca_status": "active"}
331 "vca_status": "active",
332 "n2vc_status": N2VCDeploymentStatus
.COMPLETED
,
341 "entity": Dict({"id": "git/0", "type": "unit"}),
342 "filter": Dict({"entity_id": "1", "entity_type": "action"}),
343 "delta": FAKE_DELTA_UNIT_STARTED
,
344 "entity_status": Dict(
345 {"status": "active", "message": "", "vca_status": "active"}
347 "db": Dict({"written": False, "data": None}),
352 "entity": Dict({"id": "git", "type": "application"}),
353 "filter": Dict({"entity_id": "git", "entity_type": "application"}),
354 "delta": FAKE_DELTA_APPLICATION_MAINTENANCE
,
355 "entity_status": Dict(
357 "status": "maintenance",
358 "message": "installing charm software",
359 "vca_status": "maintenance",
367 "message": "installing charm software",
368 "entity": "application",
369 "vca_status": "maintenance",
370 "n2vc_status": N2VCDeploymentStatus
.RUNNING
,
379 "entity": Dict({"id": "git", "type": "application"}),
380 "filter": Dict({"entity_id": "2", "entity_type": "machine"}),
381 "delta": FAKE_DELTA_APPLICATION_MAINTENANCE
,
382 "entity_status": Dict(
384 "status": "maintenance",
385 "message": "installing charm software",
386 "vca_status": "maintenance",
389 "db": Dict({"written": False, "data": None}),
394 "entity": Dict({"id": "git", "type": "application"}),
395 "filter": Dict({"entity_id": "git", "entity_type": "application"}),
396 "delta": FAKE_DELTA_APPLICATION_ACTIVE
,
397 "entity_status": Dict(
398 {"status": "active", "message": "Ready!", "vca_status": "active"}
406 "entity": "application",
407 "vca_status": "active",
408 "n2vc_status": N2VCDeploymentStatus
.COMPLETED
,
417 "entity": Dict({"id": "git", "type": "application"}),
418 "filter": Dict({"entity_id": "1", "entity_type": "action"}),
419 "delta": FAKE_DELTA_APPLICATION_ACTIVE
,
420 "entity_status": Dict(
421 {"status": "active", "message": "Ready!", "vca_status": "active"}
423 "db": Dict({"written": False, "data": None}),
428 "entity": Dict({"id": "1", "type": "action"}),
429 "filter": Dict({"entity_id": "1", "entity_type": "action"}),
430 "delta": FAKE_DELTA_ACTION_COMPLETED
,
431 "entity_status": Dict(
433 "status": "completed",
434 "message": "completed",
435 "vca_status": "completed",
443 "message": "completed",
445 "vca_status": "completed",
446 "n2vc_status": N2VCDeploymentStatus
.COMPLETED
,
455 "entity": Dict({"id": "git", "type": "action"}),
456 "filter": Dict({"entity_id": "1", "entity_type": "machine"}),
457 "delta": FAKE_DELTA_ACTION_COMPLETED
,
458 "entity_status": Dict(
460 "status": "completed",
461 "message": "completed",
462 "vca_status": "completed",
465 "db": Dict({"written": False, "data": None}),