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 kubeconfig
= """apiVersion: v1
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
53 cluster: microk8s-cluster
56 current-context: microk8s
62 token: clhkRExRem5Xd1dCdnFEVXdvRGtDRGE5b1F3WnNrZk5qeHFCOU10bHBZRT0K
66 async def AsyncMockFunc():
67 await asyncio
.sleep(1)
70 class AsyncMock(MagicMock
):
71 async def __call__(self
, *args
, **kwargs
):
72 return super(AsyncMock
, self
).__call
__(*args
, **kwargs
)
75 class FakeN2VC(MagicMock
):
76 last_written_values
= None
78 async def write_app_status_to_db(
81 status
: N2VCDeploymentStatus
,
86 self
.last_written_values
= Dict(
88 "n2vc_status": status
,
89 "message": detailed_status
,
90 "vca_status": vca_status
,
91 "entity": entity_type
,
95 osm_status
= N2VCConnector
.osm_status
98 class FakeMachine(MagicMock
):
100 dns_name
= "FAKE ENDPOINT"
101 model_name
= "FAKE MODEL"
102 entity_type
= "machine"
103 safe_data
= {"instance-id": "myid"}
105 async def destroy(self
, force
):
109 class FakeManualMachine(MagicMock
):
111 dns_name
= "FAKE ENDPOINT"
112 model_name
= "FAKE MODEL"
113 entity_type
= "machine"
114 safe_data
= {"instance-id": "manual:myid"}
116 async def destroy(self
, force
):
120 class FakeWatcher(AsyncMock
):
122 delta_to_return
= None
124 async def Next(self
):
125 return Dict({"deltas": self
.delta_to_return
})
128 class FakeConnection(MagicMock
):
133 class FakeAction(MagicMock
):
139 def __init__(self
, applications
: dict = {}):
140 self
._applications
= applications
143 def applications(self
):
144 return self
._applications
147 class FakeUnit(MagicMock
):
148 async def is_leader_from_status(self
):
151 async def run_action(self
, action_name
):
155 class FakeApplication(AsyncMock
):
156 async def set_config(self
, config
):
159 async def add_unit(self
, to
):
162 async def get_actions(self
):
163 return ["existing_action"]
165 units
= [FakeUnit(), FakeUnit()]
169 def __init__(self
, content
: str = ""):
170 self
.content
= content
172 def read(self
, size
: int = -1):
176 class FakeFileWrapper
:
177 def __init__(self
, content
: str = ""):
178 self
.file = FakeFile(content
=content
)
183 def __exit__(self
, type, value
, traceback
):
187 FAKE_DELTA_MACHINE_PENDING
= Dict(
189 "deltas": ["machine", "change", {}],
194 "instance-id": "juju-1b5808-2",
195 "agent-status": {"current": "pending", "message": "", "version": ""},
196 "instance-status": {"current": "running", "message": "Running"},
200 FAKE_DELTA_MACHINE_STARTED
= Dict(
202 "deltas": ["machine", "change", {}],
207 "instance-id": "juju-1b5808-2",
208 "agent-status": {"current": "started", "message": "", "version": ""},
209 "instance-status": {"current": "running", "message": "Running"},
214 FAKE_DELTA_UNIT_PENDING
= Dict(
216 "deltas": ["unit", "change", {}],
221 "application": "git",
223 "workload-status": {"current": "waiting", "message": ""},
224 "agent-status": {"current": "idle", "message": ""},
229 FAKE_DELTA_UNIT_STARTED
= Dict(
231 "deltas": ["unit", "change", {}],
236 "application": "git",
238 "workload-status": {"current": "active", "message": ""},
239 "agent-status": {"current": "idle", "message": ""},
244 FAKE_DELTA_APPLICATION_MAINTENANCE
= Dict(
246 "deltas": ["application", "change", {}],
247 "entity": "application",
252 "current": "maintenance",
253 "message": "installing charm software",
259 FAKE_DELTA_APPLICATION_ACTIVE
= Dict(
261 "deltas": ["application", "change", {}],
262 "entity": "application",
264 "data": {"name": "git", "status": {"current": "active", "message": "Ready!"}},
268 FAKE_DELTA_ACTION_COMPLETED
= Dict(
270 "deltas": ["action", "change", {}],
274 "model-uuid": "af19cdd4-374a-4d9f-86b1-bfed7b1b5808",
278 "status": "completed",
287 "entity": Dict({"id": "2", "type": "machine"}),
288 "filter": Dict({"entity_id": "2", "entity_type": "machine"}),
289 "delta": FAKE_DELTA_MACHINE_PENDING
,
290 "entity_status": Dict(
291 {"status": "pending", "message": "Running", "vca_status": "running"}
298 "message": "Running",
300 "vca_status": "running",
301 "n2vc_status": N2VCDeploymentStatus
.PENDING
,
310 "entity": Dict({"id": "2", "type": "machine"}),
311 "filter": Dict({"entity_id": "1", "entity_type": "machine"}),
312 "delta": FAKE_DELTA_MACHINE_PENDING
,
313 "entity_status": Dict(
314 {"status": "pending", "message": "Running", "vca_status": "running"}
316 "db": Dict({"written": False, "data": None}),
321 "entity": Dict({"id": "2", "type": "machine"}),
322 "filter": Dict({"entity_id": "2", "entity_type": "machine"}),
323 "delta": FAKE_DELTA_MACHINE_STARTED
,
324 "entity_status": Dict(
325 {"status": "started", "message": "Running", "vca_status": "running"}
332 "message": "Running",
334 "vca_status": "running",
335 "n2vc_status": N2VCDeploymentStatus
.COMPLETED
,
344 "entity": Dict({"id": "2", "type": "machine"}),
345 "filter": Dict({"entity_id": "1", "entity_type": "machine"}),
346 "delta": FAKE_DELTA_MACHINE_STARTED
,
347 "entity_status": Dict(
348 {"status": "started", "message": "Running", "vca_status": "running"}
350 "db": Dict({"written": False, "data": None}),
355 "entity": Dict({"id": "git/0", "type": "unit"}),
356 "filter": Dict({"entity_id": "git", "entity_type": "application"}),
357 "delta": FAKE_DELTA_UNIT_PENDING
,
358 "entity_status": Dict(
359 {"status": "waiting", "message": "", "vca_status": "waiting"}
368 "vca_status": "waiting",
369 "n2vc_status": N2VCDeploymentStatus
.RUNNING
,
378 "entity": Dict({"id": "git/0", "type": "unit"}),
379 "filter": Dict({"entity_id": "2", "entity_type": "machine"}),
380 "delta": FAKE_DELTA_UNIT_PENDING
,
381 "entity_status": Dict(
382 {"status": "waiting", "message": "", "vca_status": "waiting"}
384 "db": Dict({"written": False, "data": None}),
389 "entity": Dict({"id": "git/0", "type": "unit"}),
390 "filter": Dict({"entity_id": "git", "entity_type": "application"}),
391 "delta": FAKE_DELTA_UNIT_STARTED
,
392 "entity_status": Dict(
393 {"status": "active", "message": "", "vca_status": "active"}
402 "vca_status": "active",
403 "n2vc_status": N2VCDeploymentStatus
.COMPLETED
,
412 "entity": Dict({"id": "git/0", "type": "unit"}),
413 "filter": Dict({"entity_id": "1", "entity_type": "action"}),
414 "delta": FAKE_DELTA_UNIT_STARTED
,
415 "entity_status": Dict(
416 {"status": "active", "message": "", "vca_status": "active"}
418 "db": Dict({"written": False, "data": None}),
423 "entity": Dict({"id": "git", "type": "application"}),
424 "filter": Dict({"entity_id": "git", "entity_type": "application"}),
425 "delta": FAKE_DELTA_APPLICATION_MAINTENANCE
,
426 "entity_status": Dict(
428 "status": "maintenance",
429 "message": "installing charm software",
430 "vca_status": "maintenance",
438 "message": "installing charm software",
439 "entity": "application",
440 "vca_status": "maintenance",
441 "n2vc_status": N2VCDeploymentStatus
.RUNNING
,
450 "entity": Dict({"id": "git", "type": "application"}),
451 "filter": Dict({"entity_id": "2", "entity_type": "machine"}),
452 "delta": FAKE_DELTA_APPLICATION_MAINTENANCE
,
453 "entity_status": Dict(
455 "status": "maintenance",
456 "message": "installing charm software",
457 "vca_status": "maintenance",
460 "db": Dict({"written": False, "data": None}),
465 "entity": Dict({"id": "git", "type": "application"}),
466 "filter": Dict({"entity_id": "git", "entity_type": "application"}),
467 "delta": FAKE_DELTA_APPLICATION_ACTIVE
,
468 "entity_status": Dict(
469 {"status": "active", "message": "Ready!", "vca_status": "active"}
477 "entity": "application",
478 "vca_status": "active",
479 "n2vc_status": N2VCDeploymentStatus
.COMPLETED
,
488 "entity": Dict({"id": "git", "type": "application"}),
489 "filter": Dict({"entity_id": "1", "entity_type": "action"}),
490 "delta": FAKE_DELTA_APPLICATION_ACTIVE
,
491 "entity_status": Dict(
492 {"status": "active", "message": "Ready!", "vca_status": "active"}
494 "db": Dict({"written": False, "data": None}),
499 "entity": Dict({"id": "1", "type": "action"}),
500 "filter": Dict({"entity_id": "1", "entity_type": "action"}),
501 "delta": FAKE_DELTA_ACTION_COMPLETED
,
502 "entity_status": Dict(
504 "status": "completed",
505 "message": "completed",
506 "vca_status": "completed",
514 "message": "completed",
516 "vca_status": "completed",
517 "n2vc_status": N2VCDeploymentStatus
.COMPLETED
,
526 "entity": Dict({"id": "git", "type": "action"}),
527 "filter": Dict({"entity_id": "1", "entity_type": "machine"}),
528 "delta": FAKE_DELTA_ACTION_COMPLETED
,
529 "entity_status": Dict(
531 "status": "completed",
532 "message": "completed",
533 "vca_status": "completed",
536 "db": Dict({"written": False, "data": None}),