Feature-9904: Enhancing NG-UI to enable Juju operational view dashboard
[osm/N2VC.git] / n2vc / tests / unit / utils.py
1 # Copyright 2020 Canonical Ltd.
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import asyncio
16
17 from n2vc.utils import Dict, N2VCDeploymentStatus
18 from n2vc.n2vc_conn import N2VCConnector
19 from unittest.mock import MagicMock
20
21
22 async def AsyncMockFunc():
23 await asyncio.sleep(1)
24
25
26 class AsyncMock(MagicMock):
27 async def __call__(self, *args, **kwargs):
28 return super(AsyncMock, self).__call__(*args, **kwargs)
29
30
31 class FakeN2VC(MagicMock):
32 last_written_values = None
33
34 async def write_app_status_to_db(
35 self,
36 db_dict: dict,
37 status: N2VCDeploymentStatus,
38 detailed_status: str,
39 vca_status: str,
40 entity_type: str,
41 ):
42 self.last_written_values = Dict(
43 {
44 "n2vc_status": status,
45 "message": detailed_status,
46 "vca_status": vca_status,
47 "entity": entity_type,
48 }
49 )
50
51 osm_status = N2VCConnector.osm_status
52
53
54 class FakeMachine(MagicMock):
55 entity_id = "2"
56 dns_name = "FAKE ENDPOINT"
57 model_name = "FAKE MODEL"
58 entity_type = "machine"
59 safe_data = {"instance-id": "myid"}
60
61 async def destroy(self, force):
62 pass
63
64
65 class FakeManualMachine(MagicMock):
66 entity_id = "2"
67 dns_name = "FAKE ENDPOINT"
68 model_name = "FAKE MODEL"
69 entity_type = "machine"
70 safe_data = {"instance-id": "manual:myid"}
71
72 async def destroy(self, force):
73 pass
74
75
76 class FakeWatcher(AsyncMock):
77
78 delta_to_return = None
79
80 async def Next(self):
81 return Dict({"deltas": self.delta_to_return})
82
83
84 class FakeConnection(MagicMock):
85 endpoint = None
86 is_open = False
87
88
89 class FakeAction(MagicMock):
90 entity_id = "id"
91 status = "ready"
92
93
94 class FakeUnit(MagicMock):
95 async def is_leader_from_status(self):
96 return True
97
98 async def run_action(self, action_name):
99 return FakeAction()
100
101
102 class FakeApplication(AsyncMock):
103
104 async def set_config(self, config):
105 pass
106
107 async def add_unit(self, to):
108 pass
109
110 async def get_actions(self):
111 return ["existing_action"]
112
113 async def get_config(self):
114 return ["app_config"]
115
116 units = [FakeUnit(), FakeUnit()]
117
118
119 FAKE_DELTA_MACHINE_PENDING = Dict(
120 {
121 "deltas": ["machine", "change", {}],
122 "entity": "machine",
123 "type": "change",
124 "data": {
125 "id": "2",
126 "instance-id": "juju-1b5808-2",
127 "agent-status": {"current": "pending", "message": "", "version": ""},
128 "instance-status": {"current": "running", "message": "Running"},
129 },
130 }
131 )
132 FAKE_DELTA_MACHINE_STARTED = Dict(
133 {
134 "deltas": ["machine", "change", {}],
135 "entity": "machine",
136 "type": "change",
137 "data": {
138 "id": "2",
139 "instance-id": "juju-1b5808-2",
140 "agent-status": {"current": "started", "message": "", "version": ""},
141 "instance-status": {"current": "running", "message": "Running"},
142 },
143 }
144 )
145
146 FAKE_DELTA_UNIT_PENDING = Dict(
147 {
148 "deltas": ["unit", "change", {}],
149 "entity": "unit",
150 "type": "change",
151 "data": {
152 "name": "git/0",
153 "application": "git",
154 "machine-id": "6",
155 "workload-status": {"current": "waiting", "message": ""},
156 "agent-status": {"current": "idle", "message": ""},
157 },
158 }
159 )
160
161 FAKE_DELTA_UNIT_STARTED = Dict(
162 {
163 "deltas": ["unit", "change", {}],
164 "entity": "unit",
165 "type": "change",
166 "data": {
167 "name": "git/0",
168 "application": "git",
169 "machine-id": "6",
170 "workload-status": {"current": "active", "message": ""},
171 "agent-status": {"current": "idle", "message": ""},
172 },
173 }
174 )
175
176 FAKE_DELTA_APPLICATION_MAINTENANCE = Dict(
177 {
178 "deltas": ["application", "change", {}],
179 "entity": "application",
180 "type": "change",
181 "data": {
182 "name": "git",
183 "status": {
184 "current": "maintenance",
185 "message": "installing charm software",
186 },
187 },
188 }
189 )
190
191 FAKE_DELTA_APPLICATION_ACTIVE = Dict(
192 {
193 "deltas": ["application", "change", {}],
194 "entity": "application",
195 "type": "change",
196 "data": {"name": "git", "status": {"current": "active", "message": "Ready!"}},
197 }
198 )
199
200 FAKE_DELTA_ACTION_COMPLETED = Dict(
201 {
202 "deltas": ["action", "change", {}],
203 "entity": "action",
204 "type": "change",
205 "data": {
206 "model-uuid": "af19cdd4-374a-4d9f-86b1-bfed7b1b5808",
207 "id": "1",
208 "receiver": "git/0",
209 "name": "add-repo",
210 "status": "completed",
211 "message": "",
212 },
213 }
214 )
215
216 Deltas = [
217 Dict(
218 {
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"}
224 ),
225 "db": Dict(
226 {
227 "written": True,
228 "data": Dict(
229 {
230 "message": "Running",
231 "entity": "machine",
232 "vca_status": "running",
233 "n2vc_status": N2VCDeploymentStatus.PENDING,
234 }
235 ),
236 }
237 ),
238 }
239 ),
240 Dict(
241 {
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"}
247 ),
248 "db": Dict({"written": False, "data": None}),
249 }
250 ),
251 Dict(
252 {
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"}
258 ),
259 "db": Dict(
260 {
261 "written": True,
262 "data": Dict(
263 {
264 "message": "Running",
265 "entity": "machine",
266 "vca_status": "running",
267 "n2vc_status": N2VCDeploymentStatus.COMPLETED,
268 }
269 ),
270 }
271 ),
272 }
273 ),
274 Dict(
275 {
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"}
281 ),
282 "db": Dict({"written": False, "data": None}),
283 }
284 ),
285 Dict(
286 {
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"}
292 ),
293 "db": Dict(
294 {
295 "written": True,
296 "data": Dict(
297 {
298 "message": "",
299 "entity": "unit",
300 "vca_status": "waiting",
301 "n2vc_status": N2VCDeploymentStatus.RUNNING,
302 }
303 ),
304 }
305 ),
306 }
307 ),
308 Dict(
309 {
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"}
315 ),
316 "db": Dict({"written": False, "data": None}),
317 }
318 ),
319 Dict(
320 {
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"}
326 ),
327 "db": Dict(
328 {
329 "written": True,
330 "data": Dict(
331 {
332 "message": "",
333 "entity": "unit",
334 "vca_status": "active",
335 "n2vc_status": N2VCDeploymentStatus.COMPLETED,
336 }
337 ),
338 }
339 ),
340 }
341 ),
342 Dict(
343 {
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"}
349 ),
350 "db": Dict({"written": False, "data": None}),
351 }
352 ),
353 Dict(
354 {
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(
359 {
360 "status": "maintenance",
361 "message": "installing charm software",
362 "vca_status": "maintenance",
363 }
364 ),
365 "db": Dict(
366 {
367 "written": True,
368 "data": Dict(
369 {
370 "message": "installing charm software",
371 "entity": "application",
372 "vca_status": "maintenance",
373 "n2vc_status": N2VCDeploymentStatus.RUNNING,
374 }
375 ),
376 }
377 ),
378 }
379 ),
380 Dict(
381 {
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(
386 {
387 "status": "maintenance",
388 "message": "installing charm software",
389 "vca_status": "maintenance",
390 }
391 ),
392 "db": Dict({"written": False, "data": None}),
393 }
394 ),
395 Dict(
396 {
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"}
402 ),
403 "db": Dict(
404 {
405 "written": True,
406 "data": Dict(
407 {
408 "message": "Ready!",
409 "entity": "application",
410 "vca_status": "active",
411 "n2vc_status": N2VCDeploymentStatus.COMPLETED,
412 }
413 ),
414 }
415 ),
416 }
417 ),
418 Dict(
419 {
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"}
425 ),
426 "db": Dict({"written": False, "data": None}),
427 }
428 ),
429 Dict(
430 {
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(
435 {
436 "status": "completed",
437 "message": "completed",
438 "vca_status": "completed",
439 }
440 ),
441 "db": Dict(
442 {
443 "written": True,
444 "data": Dict(
445 {
446 "message": "completed",
447 "entity": "action",
448 "vca_status": "completed",
449 "n2vc_status": N2VCDeploymentStatus.COMPLETED,
450 }
451 ),
452 }
453 ),
454 }
455 ),
456 Dict(
457 {
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(
462 {
463 "status": "completed",
464 "message": "completed",
465 "vca_status": "completed",
466 }
467 ),
468 "db": Dict({"written": False, "data": None}),
469 }
470 ),
471 ]