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 async def get_config(self
):
114 return ["app_config"]
116 units
= [FakeUnit(), FakeUnit()]
119 FAKE_DELTA_MACHINE_PENDING
= Dict(
121 "deltas": ["machine", "change", {}],
126 "instance-id": "juju-1b5808-2",
127 "agent-status": {"current": "pending", "message": "", "version": ""},
128 "instance-status": {"current": "running", "message": "Running"},
132 FAKE_DELTA_MACHINE_STARTED
= Dict(
134 "deltas": ["machine", "change", {}],
139 "instance-id": "juju-1b5808-2",
140 "agent-status": {"current": "started", "message": "", "version": ""},
141 "instance-status": {"current": "running", "message": "Running"},
146 FAKE_DELTA_UNIT_PENDING
= Dict(
148 "deltas": ["unit", "change", {}],
153 "application": "git",
155 "workload-status": {"current": "waiting", "message": ""},
156 "agent-status": {"current": "idle", "message": ""},
161 FAKE_DELTA_UNIT_STARTED
= Dict(
163 "deltas": ["unit", "change", {}],
168 "application": "git",
170 "workload-status": {"current": "active", "message": ""},
171 "agent-status": {"current": "idle", "message": ""},
176 FAKE_DELTA_APPLICATION_MAINTENANCE
= Dict(
178 "deltas": ["application", "change", {}],
179 "entity": "application",
184 "current": "maintenance",
185 "message": "installing charm software",
191 FAKE_DELTA_APPLICATION_ACTIVE
= Dict(
193 "deltas": ["application", "change", {}],
194 "entity": "application",
196 "data": {"name": "git", "status": {"current": "active", "message": "Ready!"}},
200 FAKE_DELTA_ACTION_COMPLETED
= Dict(
202 "deltas": ["action", "change", {}],
206 "model-uuid": "af19cdd4-374a-4d9f-86b1-bfed7b1b5808",
210 "status": "completed",
219 "entity": Dict({"id": "2", "type": "machine"}),
220 "filter": Dict({"entity_id": "2", "entity_type": "machine"}),
221 "delta": FAKE_DELTA_MACHINE_PENDING
,
222 "entity_status": Dict(
223 {"status": "pending", "message": "Running", "vca_status": "running"}
230 "message": "Running",
232 "vca_status": "running",
233 "n2vc_status": N2VCDeploymentStatus
.PENDING
,
242 "entity": Dict({"id": "2", "type": "machine"}),
243 "filter": Dict({"entity_id": "1", "entity_type": "machine"}),
244 "delta": FAKE_DELTA_MACHINE_PENDING
,
245 "entity_status": Dict(
246 {"status": "pending", "message": "Running", "vca_status": "running"}
248 "db": Dict({"written": False, "data": None}),
253 "entity": Dict({"id": "2", "type": "machine"}),
254 "filter": Dict({"entity_id": "2", "entity_type": "machine"}),
255 "delta": FAKE_DELTA_MACHINE_STARTED
,
256 "entity_status": Dict(
257 {"status": "started", "message": "Running", "vca_status": "running"}
264 "message": "Running",
266 "vca_status": "running",
267 "n2vc_status": N2VCDeploymentStatus
.COMPLETED
,
276 "entity": Dict({"id": "2", "type": "machine"}),
277 "filter": Dict({"entity_id": "1", "entity_type": "machine"}),
278 "delta": FAKE_DELTA_MACHINE_STARTED
,
279 "entity_status": Dict(
280 {"status": "started", "message": "Running", "vca_status": "running"}
282 "db": Dict({"written": False, "data": None}),
287 "entity": Dict({"id": "git/0", "type": "unit"}),
288 "filter": Dict({"entity_id": "git", "entity_type": "application"}),
289 "delta": FAKE_DELTA_UNIT_PENDING
,
290 "entity_status": Dict(
291 {"status": "waiting", "message": "", "vca_status": "waiting"}
300 "vca_status": "waiting",
301 "n2vc_status": N2VCDeploymentStatus
.RUNNING
,
310 "entity": Dict({"id": "git/0", "type": "unit"}),
311 "filter": Dict({"entity_id": "2", "entity_type": "machine"}),
312 "delta": FAKE_DELTA_UNIT_PENDING
,
313 "entity_status": Dict(
314 {"status": "waiting", "message": "", "vca_status": "waiting"}
316 "db": Dict({"written": False, "data": None}),
321 "entity": Dict({"id": "git/0", "type": "unit"}),
322 "filter": Dict({"entity_id": "git", "entity_type": "application"}),
323 "delta": FAKE_DELTA_UNIT_STARTED
,
324 "entity_status": Dict(
325 {"status": "active", "message": "", "vca_status": "active"}
334 "vca_status": "active",
335 "n2vc_status": N2VCDeploymentStatus
.COMPLETED
,
344 "entity": Dict({"id": "git/0", "type": "unit"}),
345 "filter": Dict({"entity_id": "1", "entity_type": "action"}),
346 "delta": FAKE_DELTA_UNIT_STARTED
,
347 "entity_status": Dict(
348 {"status": "active", "message": "", "vca_status": "active"}
350 "db": Dict({"written": False, "data": None}),
355 "entity": Dict({"id": "git", "type": "application"}),
356 "filter": Dict({"entity_id": "git", "entity_type": "application"}),
357 "delta": FAKE_DELTA_APPLICATION_MAINTENANCE
,
358 "entity_status": Dict(
360 "status": "maintenance",
361 "message": "installing charm software",
362 "vca_status": "maintenance",
370 "message": "installing charm software",
371 "entity": "application",
372 "vca_status": "maintenance",
373 "n2vc_status": N2VCDeploymentStatus
.RUNNING
,
382 "entity": Dict({"id": "git", "type": "application"}),
383 "filter": Dict({"entity_id": "2", "entity_type": "machine"}),
384 "delta": FAKE_DELTA_APPLICATION_MAINTENANCE
,
385 "entity_status": Dict(
387 "status": "maintenance",
388 "message": "installing charm software",
389 "vca_status": "maintenance",
392 "db": Dict({"written": False, "data": None}),
397 "entity": Dict({"id": "git", "type": "application"}),
398 "filter": Dict({"entity_id": "git", "entity_type": "application"}),
399 "delta": FAKE_DELTA_APPLICATION_ACTIVE
,
400 "entity_status": Dict(
401 {"status": "active", "message": "Ready!", "vca_status": "active"}
409 "entity": "application",
410 "vca_status": "active",
411 "n2vc_status": N2VCDeploymentStatus
.COMPLETED
,
420 "entity": Dict({"id": "git", "type": "application"}),
421 "filter": Dict({"entity_id": "1", "entity_type": "action"}),
422 "delta": FAKE_DELTA_APPLICATION_ACTIVE
,
423 "entity_status": Dict(
424 {"status": "active", "message": "Ready!", "vca_status": "active"}
426 "db": Dict({"written": False, "data": None}),
431 "entity": Dict({"id": "1", "type": "action"}),
432 "filter": Dict({"entity_id": "1", "entity_type": "action"}),
433 "delta": FAKE_DELTA_ACTION_COMPLETED
,
434 "entity_status": Dict(
436 "status": "completed",
437 "message": "completed",
438 "vca_status": "completed",
446 "message": "completed",
448 "vca_status": "completed",
449 "n2vc_status": N2VCDeploymentStatus
.COMPLETED
,
458 "entity": Dict({"id": "git", "type": "action"}),
459 "filter": Dict({"entity_id": "1", "entity_type": "machine"}),
460 "delta": FAKE_DELTA_ACTION_COMPLETED
,
461 "entity_status": Dict(
463 "status": "completed",
464 "message": "completed",
465 "vca_status": "completed",
468 "db": Dict({"written": False, "data": None}),