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
, EntityType
, 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
= EntityType
.MACHINE
60 async def destroy(self
, force
):
64 class FakeWatcher(AsyncMock
):
66 delta_to_return
= None
69 return Dict({"deltas": self
.delta_to_return
})
72 class FakeConnection(MagicMock
):
77 class FakeAction(MagicMock
):
82 class FakeUnit(MagicMock
):
83 async def is_leader_from_status(self
):
86 async def run_action(self
, action_name
):
90 class FakeApplication(AsyncMock
):
92 async def set_config(self
, config
):
95 async def add_unit(self
, to
):
98 async def get_actions(self
):
99 return ["existing_action"]
101 units
= [FakeUnit(), FakeUnit()]
104 FAKE_DELTA_MACHINE_PENDING
= Dict(
106 "deltas": ["machine", "change", {}],
111 "instance-id": "juju-1b5808-2",
112 "agent-status": {"current": "pending", "message": "", "version": ""},
113 "instance-status": {"current": "running", "message": "Running"},
117 FAKE_DELTA_MACHINE_STARTED
= Dict(
119 "deltas": ["machine", "change", {}],
124 "instance-id": "juju-1b5808-2",
125 "agent-status": {"current": "started", "message": "", "version": ""},
126 "instance-status": {"current": "running", "message": "Running"},
131 FAKE_DELTA_UNIT_PENDING
= Dict(
133 "deltas": ["unit", "change", {}],
138 "application": "git",
140 "workload-status": {"current": "waiting", "message": ""},
141 "agent-status": {"current": "idle", "message": ""},
146 FAKE_DELTA_UNIT_STARTED
= Dict(
148 "deltas": ["unit", "change", {}],
153 "application": "git",
155 "workload-status": {"current": "active", "message": ""},
156 "agent-status": {"current": "idle", "message": ""},
161 FAKE_DELTA_APPLICATION_MAINTENANCE
= Dict(
163 "deltas": ["application", "change", {}],
164 "entity": "application",
169 "current": "maintenance",
170 "message": "installing charm software",
176 FAKE_DELTA_APPLICATION_ACTIVE
= Dict(
178 "deltas": ["application", "change", {}],
179 "entity": "application",
181 "data": {"name": "git", "status": {"current": "active", "message": "Ready!"}},
185 FAKE_DELTA_ACTION_COMPLETED
= Dict(
187 "deltas": ["action", "change", {}],
191 "model-uuid": "af19cdd4-374a-4d9f-86b1-bfed7b1b5808",
195 "status": "completed",
204 "entity": Dict({"id": "2", "type": EntityType
.MACHINE
}),
205 "filter": Dict({"entity_id": "2", "entity_type": EntityType
.MACHINE
}),
206 "delta": FAKE_DELTA_MACHINE_PENDING
,
207 "entity_status": Dict(
208 {"status": "pending", "message": "Running", "vca_status": "running"}
215 "message": "Running",
217 "vca_status": "running",
218 "n2vc_status": N2VCDeploymentStatus
.PENDING
,
227 "entity": Dict({"id": "2", "type": EntityType
.MACHINE
}),
228 "filter": Dict({"entity_id": "1", "entity_type": EntityType
.MACHINE
}),
229 "delta": FAKE_DELTA_MACHINE_PENDING
,
230 "entity_status": Dict(
231 {"status": "pending", "message": "Running", "vca_status": "running"}
233 "db": Dict({"written": False, "data": None}),
238 "entity": Dict({"id": "2", "type": EntityType
.MACHINE
}),
239 "filter": Dict({"entity_id": "2", "entity_type": EntityType
.MACHINE
}),
240 "delta": FAKE_DELTA_MACHINE_STARTED
,
241 "entity_status": Dict(
242 {"status": "started", "message": "Running", "vca_status": "running"}
249 "message": "Running",
251 "vca_status": "running",
252 "n2vc_status": N2VCDeploymentStatus
.COMPLETED
,
261 "entity": Dict({"id": "2", "type": EntityType
.MACHINE
}),
262 "filter": Dict({"entity_id": "1", "entity_type": EntityType
.MACHINE
}),
263 "delta": FAKE_DELTA_MACHINE_STARTED
,
264 "entity_status": Dict(
265 {"status": "started", "message": "Running", "vca_status": "running"}
267 "db": Dict({"written": False, "data": None}),
272 "entity": Dict({"id": "git/0", "type": EntityType
.UNIT
}),
273 "filter": Dict({"entity_id": "git", "entity_type": EntityType
.APPLICATION
}),
274 "delta": FAKE_DELTA_UNIT_PENDING
,
275 "entity_status": Dict(
276 {"status": "waiting", "message": "", "vca_status": "waiting"}
285 "vca_status": "waiting",
286 "n2vc_status": N2VCDeploymentStatus
.RUNNING
,
295 "entity": Dict({"id": "git/0", "type": EntityType
.UNIT
}),
296 "filter": Dict({"entity_id": "2", "entity_type": EntityType
.MACHINE
}),
297 "delta": FAKE_DELTA_UNIT_PENDING
,
298 "entity_status": Dict(
299 {"status": "waiting", "message": "", "vca_status": "waiting"}
301 "db": Dict({"written": False, "data": None}),
306 "entity": Dict({"id": "git/0", "type": EntityType
.UNIT
}),
307 "filter": Dict({"entity_id": "git", "entity_type": EntityType
.APPLICATION
}),
308 "delta": FAKE_DELTA_UNIT_STARTED
,
309 "entity_status": Dict(
310 {"status": "active", "message": "", "vca_status": "active"}
319 "vca_status": "active",
320 "n2vc_status": N2VCDeploymentStatus
.COMPLETED
,
329 "entity": Dict({"id": "git/0", "type": EntityType
.UNIT
}),
330 "filter": Dict({"entity_id": "1", "entity_type": EntityType
.ACTION
}),
331 "delta": FAKE_DELTA_UNIT_STARTED
,
332 "entity_status": Dict(
333 {"status": "active", "message": "", "vca_status": "active"}
335 "db": Dict({"written": False, "data": None}),
340 "entity": Dict({"id": "git", "type": EntityType
.APPLICATION
}),
341 "filter": Dict({"entity_id": "git", "entity_type": EntityType
.APPLICATION
}),
342 "delta": FAKE_DELTA_APPLICATION_MAINTENANCE
,
343 "entity_status": Dict(
345 "status": "maintenance",
346 "message": "installing charm software",
347 "vca_status": "maintenance",
355 "message": "installing charm software",
356 "entity": "application",
357 "vca_status": "maintenance",
358 "n2vc_status": N2VCDeploymentStatus
.RUNNING
,
367 "entity": Dict({"id": "git", "type": EntityType
.APPLICATION
}),
368 "filter": Dict({"entity_id": "2", "entity_type": EntityType
.MACHINE
}),
369 "delta": FAKE_DELTA_APPLICATION_MAINTENANCE
,
370 "entity_status": Dict(
372 "status": "maintenance",
373 "message": "installing charm software",
374 "vca_status": "maintenance",
377 "db": Dict({"written": False, "data": None}),
382 "entity": Dict({"id": "git", "type": EntityType
.APPLICATION
}),
383 "filter": Dict({"entity_id": "git", "entity_type": EntityType
.APPLICATION
}),
384 "delta": FAKE_DELTA_APPLICATION_ACTIVE
,
385 "entity_status": Dict(
386 {"status": "active", "message": "Ready!", "vca_status": "active"}
394 "entity": "application",
395 "vca_status": "active",
396 "n2vc_status": N2VCDeploymentStatus
.COMPLETED
,
405 "entity": Dict({"id": "git", "type": EntityType
.APPLICATION
}),
406 "filter": Dict({"entity_id": "1", "entity_type": EntityType
.ACTION
}),
407 "delta": FAKE_DELTA_APPLICATION_ACTIVE
,
408 "entity_status": Dict(
409 {"status": "active", "message": "Ready!", "vca_status": "active"}
411 "db": Dict({"written": False, "data": None}),
416 "entity": Dict({"id": "1", "type": EntityType
.ACTION
}),
417 "filter": Dict({"entity_id": "1", "entity_type": EntityType
.ACTION
}),
418 "delta": FAKE_DELTA_ACTION_COMPLETED
,
419 "entity_status": Dict(
421 "status": "completed",
422 "message": "completed",
423 "vca_status": "completed",
431 "message": "completed",
433 "vca_status": "completed",
434 "n2vc_status": N2VCDeploymentStatus
.COMPLETED
,
443 "entity": Dict({"id": "git", "type": EntityType
.ACTION
}),
444 "filter": Dict({"entity_id": "1", "entity_type": EntityType
.MACHINE
}),
445 "delta": FAKE_DELTA_ACTION_COMPLETED
,
446 "entity_status": Dict(
448 "status": "completed",
449 "message": "completed",
450 "vca_status": "completed",
453 "db": Dict({"written": False, "data": None}),