Feature 10239: Distributed VCA
[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 kubeconfig = """apiVersion: v1
23 clusters:
24 - cluster:
25 certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1\
26 JSURBVENDQWVtZ0F3SUJBZ0lKQUxjMk9xVUpwcnVCTUEwR0NTcUdTSWIzRFFFQk\
27 N3VUFNQmN4RlRBVEJnTlYKQkFNTURERXdMakUxTWk0eE9ETXVNVEFlRncweU1EQ\
28 TVNVEV4TkRJeU16VmFGdzB6TURBNU1Ea3hOREl5TXpWYQpNQmN4RlRBVEJnTlZC\
29 QU1NRERFd0xqRTFNaTR4T0RNdU1UQ0NBU0l3RFFZSktvWklodmNOQVFFQkJRQUR\
30 nZ0VQCkFEQ0NBUW9DZ2dFQkFNV0tyQkdxWlJRT0VONDExR2RESmY2ckZWRDcvMU\
31 xHNlZMWjNhd1BRdHBhRTRxdVdyNisKWjExTWwra2kwVEU1cGZFV3dKenVUZXlCU\
32 XVkUEpnYm1QTjF1VWROdGRiNlpocHEzeC9oT0hCMVJLNC9iSlNFUgpiZ0dITmN6\
33 MzR6SHRaZ1dwb2NPTXpPOW9oRUdhMTZUaDhmQWVxYU1CQTJRaklmeUFlaVp3VHJ\
34 nZ3BrY2dBMUlOCjBvQkdqSURnSGVoSU5tbGZOOURkQ3hNN1FNTmtSbzRXdE13bF\
35 JSRWZ4QnFiVkNpZGFjbVhhb1VPUjJPeFVmQWEKN1orSUU1TmN5ZFQ1TGovazdwd\
36 XZCVkdIa0JQWnE0TmlBa3R4aXd5NVB5R29GTk9mT0NrV2I2VnBzVzNhTlNJeAo4\
37 aXBITkc3enV3elc1TGQ5TkhQYWpRckZwdFZBSHpJNWNhRUNBd0VBQWFOUU1FNHd\
38 IUVlEVlIwT0JCWUVGQ1dVCkFaTXNaeE13L1k1OGlXMGZJWVAzcDdTYk1COEdBMV\
39 VkSXdRWU1CYUFGQ1dVQVpNc1p4TXcvWTU4aVcwZklZUDMKcDdTYk1Bd0dBMVVkR\
40 XdRRk1BTUJBZjh3RFFZSktvWklodmNOQVFFTEJRQURnZ0VCQUJaMlYxMWowRzhh\
41 Z1Z6Twp2YWtKTGt4UGZ0UE1NMFFOaVRzZmV6RzlicnBkdEVLSjFyalFCblNXYTN\
42 WbThWRGZTYkhLQUNXaGh0OEhzcXhtCmNzdVQyOWUyaGZBNHVIOUxMdy9MVG5EdE\
43 tJSjZ6aWFzaTM5RGh3UGwwaExuamJRMjk4VVo5TGovVlpnZGlqemIKWnVPdHlpT\
44 nVOS0E2Nmd0dGxXcWZRQ2hkbnJ5MlZUbjBjblR5dU9UalByYWdOdXJMdlVwL3Nl\
45 eURhZmsxNXJ4egozcmlYZldiQnRhUUk1dnM0ekFKU2xneUg2RnpiZStoTUhlUzF\
46 mM2ppb3dJV0lRR2NNbHpGT1RpMm1xWFRybEJYCnh1WmpLZlpOcndjQVNGbk9qYV\
47 BWeFQ1ODJ4WWhtTm8wR3J2MlZEck51bDlSYkgvK3lNS2J5NEhkOFRvVThMU2kKY\
48 3Uxajh3cz0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
49 server: https://192.168.0.22:16443
50 name: microk8s-cluster
51 contexts:
52 - context:
53 cluster: microk8s-cluster
54 user: admin
55 name: microk8s
56 current-context: microk8s
57 kind: Config
58 preferences: {}
59 users:
60 - name: admin
61 user:
62 token: clhkRExRem5Xd1dCdnFEVXdvRGtDRGE5b1F3WnNrZk5qeHFCOU10bHBZRT0K
63 """
64
65
66 async def AsyncMockFunc():
67 await asyncio.sleep(1)
68
69
70 class AsyncMock(MagicMock):
71 async def __call__(self, *args, **kwargs):
72 return super(AsyncMock, self).__call__(*args, **kwargs)
73
74
75 class FakeN2VC(MagicMock):
76 last_written_values = None
77
78 async def write_app_status_to_db(
79 self,
80 db_dict: dict,
81 status: N2VCDeploymentStatus,
82 detailed_status: str,
83 vca_status: str,
84 entity_type: str,
85 vca_id: str = None,
86 ):
87 """
88 Write application status to database
89
90 :param: db_dict: DB dictionary
91 :param: status: Status of the application
92 :param: detailed_status: Detailed status
93 :param: vca_status: VCA status
94 :param: entity_type: Entity type ("application", "machine, and "action")
95 :param: vca_id: Id of the VCA. If None, the default VCA will be used.
96 """
97 self.last_written_values = Dict(
98 {
99 "n2vc_status": status,
100 "message": detailed_status,
101 "vca_status": vca_status,
102 "entity": entity_type,
103 }
104 )
105
106 osm_status = N2VCConnector.osm_status
107
108
109 class FakeMachine(MagicMock):
110 entity_id = "2"
111 dns_name = "FAKE ENDPOINT"
112 model_name = "FAKE MODEL"
113 entity_type = "machine"
114 safe_data = {"instance-id": "myid"}
115
116 async def destroy(self, force):
117 pass
118
119
120 class FakeManualMachine(MagicMock):
121 entity_id = "2"
122 dns_name = "FAKE ENDPOINT"
123 model_name = "FAKE MODEL"
124 entity_type = "machine"
125 safe_data = {"instance-id": "manual:myid"}
126
127 async def destroy(self, force):
128 pass
129
130
131 class FakeWatcher(AsyncMock):
132
133 delta_to_return = None
134
135 async def Next(self):
136 return Dict({"deltas": self.delta_to_return})
137
138
139 class FakeConnection(MagicMock):
140 endpoint = None
141 is_open = False
142
143
144 class FakeAction(MagicMock):
145 entity_id = "id"
146 status = "ready"
147
148
149 class FakeModel:
150 def __init__(self, applications: dict = {}):
151 self._applications = applications
152
153 @property
154 def applications(self):
155 return self._applications
156
157
158 class FakeUnit(MagicMock):
159 async def is_leader_from_status(self):
160 return True
161
162 async def run_action(self, action_name):
163 return FakeAction()
164
165
166 class FakeApplication(AsyncMock):
167 async def set_config(self, config):
168 pass
169
170 async def add_unit(self, to):
171 pass
172
173 async def get_actions(self):
174 return ["existing_action"]
175
176 async def get_config(self):
177 return ["app_config"]
178
179 units = [FakeUnit(), FakeUnit()]
180
181
182 class FakeFile:
183 def __init__(self, content: str = ""):
184 self.content = content
185
186 def read(self, size: int = -1):
187 return self.content
188
189
190 class FakeFileWrapper:
191 def __init__(self, content: str = ""):
192 self.file = FakeFile(content=content)
193
194 def __enter__(self):
195 return self.file
196
197 def __exit__(self, type, value, traceback):
198 pass
199
200
201 FAKE_DELTA_MACHINE_PENDING = Dict(
202 {
203 "deltas": ["machine", "change", {}],
204 "entity": "machine",
205 "type": "change",
206 "data": {
207 "id": "2",
208 "instance-id": "juju-1b5808-2",
209 "agent-status": {"current": "pending", "message": "", "version": ""},
210 "instance-status": {"current": "running", "message": "Running"},
211 },
212 }
213 )
214 FAKE_DELTA_MACHINE_STARTED = Dict(
215 {
216 "deltas": ["machine", "change", {}],
217 "entity": "machine",
218 "type": "change",
219 "data": {
220 "id": "2",
221 "instance-id": "juju-1b5808-2",
222 "agent-status": {"current": "started", "message": "", "version": ""},
223 "instance-status": {"current": "running", "message": "Running"},
224 },
225 }
226 )
227
228 FAKE_DELTA_UNIT_PENDING = Dict(
229 {
230 "deltas": ["unit", "change", {}],
231 "entity": "unit",
232 "type": "change",
233 "data": {
234 "name": "git/0",
235 "application": "git",
236 "machine-id": "6",
237 "workload-status": {"current": "waiting", "message": ""},
238 "agent-status": {"current": "idle", "message": ""},
239 },
240 }
241 )
242
243 FAKE_DELTA_UNIT_STARTED = Dict(
244 {
245 "deltas": ["unit", "change", {}],
246 "entity": "unit",
247 "type": "change",
248 "data": {
249 "name": "git/0",
250 "application": "git",
251 "machine-id": "6",
252 "workload-status": {"current": "active", "message": ""},
253 "agent-status": {"current": "idle", "message": ""},
254 },
255 }
256 )
257
258 FAKE_DELTA_APPLICATION_MAINTENANCE = Dict(
259 {
260 "deltas": ["application", "change", {}],
261 "entity": "application",
262 "type": "change",
263 "data": {
264 "name": "git",
265 "status": {
266 "current": "maintenance",
267 "message": "installing charm software",
268 },
269 },
270 }
271 )
272
273 FAKE_DELTA_APPLICATION_ACTIVE = Dict(
274 {
275 "deltas": ["application", "change", {}],
276 "entity": "application",
277 "type": "change",
278 "data": {"name": "git", "status": {"current": "active", "message": "Ready!"}},
279 }
280 )
281
282 FAKE_DELTA_ACTION_COMPLETED = Dict(
283 {
284 "deltas": ["action", "change", {}],
285 "entity": "action",
286 "type": "change",
287 "data": {
288 "model-uuid": "af19cdd4-374a-4d9f-86b1-bfed7b1b5808",
289 "id": "1",
290 "receiver": "git/0",
291 "name": "add-repo",
292 "status": "completed",
293 "message": "",
294 },
295 }
296 )
297
298 Deltas = [
299 Dict(
300 {
301 "entity": Dict({"id": "2", "type": "machine"}),
302 "filter": Dict({"entity_id": "2", "entity_type": "machine"}),
303 "delta": FAKE_DELTA_MACHINE_PENDING,
304 "entity_status": Dict(
305 {"status": "pending", "message": "Running", "vca_status": "running"}
306 ),
307 "db": Dict(
308 {
309 "written": True,
310 "data": Dict(
311 {
312 "message": "Running",
313 "entity": "machine",
314 "vca_status": "running",
315 "n2vc_status": N2VCDeploymentStatus.PENDING,
316 }
317 ),
318 }
319 ),
320 }
321 ),
322 Dict(
323 {
324 "entity": Dict({"id": "2", "type": "machine"}),
325 "filter": Dict({"entity_id": "1", "entity_type": "machine"}),
326 "delta": FAKE_DELTA_MACHINE_PENDING,
327 "entity_status": Dict(
328 {"status": "pending", "message": "Running", "vca_status": "running"}
329 ),
330 "db": Dict({"written": False, "data": None}),
331 }
332 ),
333 Dict(
334 {
335 "entity": Dict({"id": "2", "type": "machine"}),
336 "filter": Dict({"entity_id": "2", "entity_type": "machine"}),
337 "delta": FAKE_DELTA_MACHINE_STARTED,
338 "entity_status": Dict(
339 {"status": "started", "message": "Running", "vca_status": "running"}
340 ),
341 "db": Dict(
342 {
343 "written": True,
344 "data": Dict(
345 {
346 "message": "Running",
347 "entity": "machine",
348 "vca_status": "running",
349 "n2vc_status": N2VCDeploymentStatus.COMPLETED,
350 }
351 ),
352 }
353 ),
354 }
355 ),
356 Dict(
357 {
358 "entity": Dict({"id": "2", "type": "machine"}),
359 "filter": Dict({"entity_id": "1", "entity_type": "machine"}),
360 "delta": FAKE_DELTA_MACHINE_STARTED,
361 "entity_status": Dict(
362 {"status": "started", "message": "Running", "vca_status": "running"}
363 ),
364 "db": Dict({"written": False, "data": None}),
365 }
366 ),
367 Dict(
368 {
369 "entity": Dict({"id": "git/0", "type": "unit"}),
370 "filter": Dict({"entity_id": "git", "entity_type": "application"}),
371 "delta": FAKE_DELTA_UNIT_PENDING,
372 "entity_status": Dict(
373 {"status": "waiting", "message": "", "vca_status": "waiting"}
374 ),
375 "db": Dict(
376 {
377 "written": True,
378 "data": Dict(
379 {
380 "message": "",
381 "entity": "unit",
382 "vca_status": "waiting",
383 "n2vc_status": N2VCDeploymentStatus.RUNNING,
384 }
385 ),
386 }
387 ),
388 }
389 ),
390 Dict(
391 {
392 "entity": Dict({"id": "git/0", "type": "unit"}),
393 "filter": Dict({"entity_id": "2", "entity_type": "machine"}),
394 "delta": FAKE_DELTA_UNIT_PENDING,
395 "entity_status": Dict(
396 {"status": "waiting", "message": "", "vca_status": "waiting"}
397 ),
398 "db": Dict({"written": False, "data": None}),
399 }
400 ),
401 Dict(
402 {
403 "entity": Dict({"id": "git/0", "type": "unit"}),
404 "filter": Dict({"entity_id": "git", "entity_type": "application"}),
405 "delta": FAKE_DELTA_UNIT_STARTED,
406 "entity_status": Dict(
407 {"status": "active", "message": "", "vca_status": "active"}
408 ),
409 "db": Dict(
410 {
411 "written": True,
412 "data": Dict(
413 {
414 "message": "",
415 "entity": "unit",
416 "vca_status": "active",
417 "n2vc_status": N2VCDeploymentStatus.COMPLETED,
418 }
419 ),
420 }
421 ),
422 }
423 ),
424 Dict(
425 {
426 "entity": Dict({"id": "git/0", "type": "unit"}),
427 "filter": Dict({"entity_id": "1", "entity_type": "action"}),
428 "delta": FAKE_DELTA_UNIT_STARTED,
429 "entity_status": Dict(
430 {"status": "active", "message": "", "vca_status": "active"}
431 ),
432 "db": Dict({"written": False, "data": None}),
433 }
434 ),
435 Dict(
436 {
437 "entity": Dict({"id": "git", "type": "application"}),
438 "filter": Dict({"entity_id": "git", "entity_type": "application"}),
439 "delta": FAKE_DELTA_APPLICATION_MAINTENANCE,
440 "entity_status": Dict(
441 {
442 "status": "maintenance",
443 "message": "installing charm software",
444 "vca_status": "maintenance",
445 }
446 ),
447 "db": Dict(
448 {
449 "written": True,
450 "data": Dict(
451 {
452 "message": "installing charm software",
453 "entity": "application",
454 "vca_status": "maintenance",
455 "n2vc_status": N2VCDeploymentStatus.RUNNING,
456 }
457 ),
458 }
459 ),
460 }
461 ),
462 Dict(
463 {
464 "entity": Dict({"id": "git", "type": "application"}),
465 "filter": Dict({"entity_id": "2", "entity_type": "machine"}),
466 "delta": FAKE_DELTA_APPLICATION_MAINTENANCE,
467 "entity_status": Dict(
468 {
469 "status": "maintenance",
470 "message": "installing charm software",
471 "vca_status": "maintenance",
472 }
473 ),
474 "db": Dict({"written": False, "data": None}),
475 }
476 ),
477 Dict(
478 {
479 "entity": Dict({"id": "git", "type": "application"}),
480 "filter": Dict({"entity_id": "git", "entity_type": "application"}),
481 "delta": FAKE_DELTA_APPLICATION_ACTIVE,
482 "entity_status": Dict(
483 {"status": "active", "message": "Ready!", "vca_status": "active"}
484 ),
485 "db": Dict(
486 {
487 "written": True,
488 "data": Dict(
489 {
490 "message": "Ready!",
491 "entity": "application",
492 "vca_status": "active",
493 "n2vc_status": N2VCDeploymentStatus.COMPLETED,
494 }
495 ),
496 }
497 ),
498 }
499 ),
500 Dict(
501 {
502 "entity": Dict({"id": "git", "type": "application"}),
503 "filter": Dict({"entity_id": "1", "entity_type": "action"}),
504 "delta": FAKE_DELTA_APPLICATION_ACTIVE,
505 "entity_status": Dict(
506 {"status": "active", "message": "Ready!", "vca_status": "active"}
507 ),
508 "db": Dict({"written": False, "data": None}),
509 }
510 ),
511 Dict(
512 {
513 "entity": Dict({"id": "1", "type": "action"}),
514 "filter": Dict({"entity_id": "1", "entity_type": "action"}),
515 "delta": FAKE_DELTA_ACTION_COMPLETED,
516 "entity_status": Dict(
517 {
518 "status": "completed",
519 "message": "completed",
520 "vca_status": "completed",
521 }
522 ),
523 "db": Dict(
524 {
525 "written": True,
526 "data": Dict(
527 {
528 "message": "completed",
529 "entity": "action",
530 "vca_status": "completed",
531 "n2vc_status": N2VCDeploymentStatus.COMPLETED,
532 }
533 ),
534 }
535 ),
536 }
537 ),
538 Dict(
539 {
540 "entity": Dict({"id": "git", "type": "action"}),
541 "filter": Dict({"entity_id": "1", "entity_type": "machine"}),
542 "delta": FAKE_DELTA_ACTION_COMPLETED,
543 "entity_status": Dict(
544 {
545 "status": "completed",
546 "message": "completed",
547 "vca_status": "completed",
548 }
549 ),
550 "db": Dict({"written": False, "data": None}),
551 }
552 ),
553 ]