| Dominik Fleischmann | b78b3e0 | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 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 | |
| David Garcia | aded583 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 17 | from n2vc.utils import Dict, N2VCDeploymentStatus |
| Dominik Fleischmann | b78b3e0 | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 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" |
| David Garcia | aded583 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 58 | entity_type = "machine" |
| David Garcia | 4a8ed1c | 2020-09-29 19:48:13 +0200 | [diff] [blame^] | 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"} |
| Dominik Fleischmann | b78b3e0 | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 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 | units = [FakeUnit(), FakeUnit()] |
| 114 | |
| 115 | |
| 116 | FAKE_DELTA_MACHINE_PENDING = Dict( |
| 117 | { |
| 118 | "deltas": ["machine", "change", {}], |
| 119 | "entity": "machine", |
| 120 | "type": "change", |
| 121 | "data": { |
| 122 | "id": "2", |
| 123 | "instance-id": "juju-1b5808-2", |
| 124 | "agent-status": {"current": "pending", "message": "", "version": ""}, |
| 125 | "instance-status": {"current": "running", "message": "Running"}, |
| 126 | }, |
| 127 | } |
| 128 | ) |
| 129 | FAKE_DELTA_MACHINE_STARTED = Dict( |
| 130 | { |
| 131 | "deltas": ["machine", "change", {}], |
| 132 | "entity": "machine", |
| 133 | "type": "change", |
| 134 | "data": { |
| 135 | "id": "2", |
| 136 | "instance-id": "juju-1b5808-2", |
| 137 | "agent-status": {"current": "started", "message": "", "version": ""}, |
| 138 | "instance-status": {"current": "running", "message": "Running"}, |
| 139 | }, |
| 140 | } |
| 141 | ) |
| 142 | |
| 143 | FAKE_DELTA_UNIT_PENDING = Dict( |
| 144 | { |
| 145 | "deltas": ["unit", "change", {}], |
| 146 | "entity": "unit", |
| 147 | "type": "change", |
| 148 | "data": { |
| 149 | "name": "git/0", |
| 150 | "application": "git", |
| 151 | "machine-id": "6", |
| 152 | "workload-status": {"current": "waiting", "message": ""}, |
| 153 | "agent-status": {"current": "idle", "message": ""}, |
| 154 | }, |
| 155 | } |
| 156 | ) |
| 157 | |
| 158 | FAKE_DELTA_UNIT_STARTED = Dict( |
| 159 | { |
| 160 | "deltas": ["unit", "change", {}], |
| 161 | "entity": "unit", |
| 162 | "type": "change", |
| 163 | "data": { |
| 164 | "name": "git/0", |
| 165 | "application": "git", |
| 166 | "machine-id": "6", |
| 167 | "workload-status": {"current": "active", "message": ""}, |
| 168 | "agent-status": {"current": "idle", "message": ""}, |
| 169 | }, |
| 170 | } |
| 171 | ) |
| 172 | |
| 173 | FAKE_DELTA_APPLICATION_MAINTENANCE = Dict( |
| 174 | { |
| 175 | "deltas": ["application", "change", {}], |
| 176 | "entity": "application", |
| 177 | "type": "change", |
| 178 | "data": { |
| 179 | "name": "git", |
| 180 | "status": { |
| 181 | "current": "maintenance", |
| 182 | "message": "installing charm software", |
| 183 | }, |
| 184 | }, |
| 185 | } |
| 186 | ) |
| 187 | |
| 188 | FAKE_DELTA_APPLICATION_ACTIVE = Dict( |
| 189 | { |
| 190 | "deltas": ["application", "change", {}], |
| 191 | "entity": "application", |
| 192 | "type": "change", |
| 193 | "data": {"name": "git", "status": {"current": "active", "message": "Ready!"}}, |
| 194 | } |
| 195 | ) |
| 196 | |
| 197 | FAKE_DELTA_ACTION_COMPLETED = Dict( |
| 198 | { |
| 199 | "deltas": ["action", "change", {}], |
| 200 | "entity": "action", |
| 201 | "type": "change", |
| 202 | "data": { |
| 203 | "model-uuid": "af19cdd4-374a-4d9f-86b1-bfed7b1b5808", |
| 204 | "id": "1", |
| 205 | "receiver": "git/0", |
| 206 | "name": "add-repo", |
| 207 | "status": "completed", |
| 208 | "message": "", |
| 209 | }, |
| 210 | } |
| 211 | ) |
| 212 | |
| 213 | Deltas = [ |
| 214 | Dict( |
| 215 | { |
| David Garcia | aded583 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 216 | "entity": Dict({"id": "2", "type": "machine"}), |
| 217 | "filter": Dict({"entity_id": "2", "entity_type": "machine"}), |
| Dominik Fleischmann | b78b3e0 | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 218 | "delta": FAKE_DELTA_MACHINE_PENDING, |
| 219 | "entity_status": Dict( |
| 220 | {"status": "pending", "message": "Running", "vca_status": "running"} |
| 221 | ), |
| 222 | "db": Dict( |
| 223 | { |
| 224 | "written": True, |
| 225 | "data": Dict( |
| 226 | { |
| 227 | "message": "Running", |
| 228 | "entity": "machine", |
| 229 | "vca_status": "running", |
| 230 | "n2vc_status": N2VCDeploymentStatus.PENDING, |
| 231 | } |
| 232 | ), |
| 233 | } |
| 234 | ), |
| 235 | } |
| 236 | ), |
| 237 | Dict( |
| 238 | { |
| David Garcia | aded583 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 239 | "entity": Dict({"id": "2", "type": "machine"}), |
| 240 | "filter": Dict({"entity_id": "1", "entity_type": "machine"}), |
| Dominik Fleischmann | b78b3e0 | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 241 | "delta": FAKE_DELTA_MACHINE_PENDING, |
| 242 | "entity_status": Dict( |
| 243 | {"status": "pending", "message": "Running", "vca_status": "running"} |
| 244 | ), |
| 245 | "db": Dict({"written": False, "data": None}), |
| 246 | } |
| 247 | ), |
| 248 | Dict( |
| 249 | { |
| David Garcia | aded583 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 250 | "entity": Dict({"id": "2", "type": "machine"}), |
| 251 | "filter": Dict({"entity_id": "2", "entity_type": "machine"}), |
| Dominik Fleischmann | b78b3e0 | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 252 | "delta": FAKE_DELTA_MACHINE_STARTED, |
| 253 | "entity_status": Dict( |
| 254 | {"status": "started", "message": "Running", "vca_status": "running"} |
| 255 | ), |
| 256 | "db": Dict( |
| 257 | { |
| 258 | "written": True, |
| 259 | "data": Dict( |
| 260 | { |
| 261 | "message": "Running", |
| 262 | "entity": "machine", |
| 263 | "vca_status": "running", |
| 264 | "n2vc_status": N2VCDeploymentStatus.COMPLETED, |
| 265 | } |
| 266 | ), |
| 267 | } |
| 268 | ), |
| 269 | } |
| 270 | ), |
| 271 | Dict( |
| 272 | { |
| David Garcia | aded583 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 273 | "entity": Dict({"id": "2", "type": "machine"}), |
| 274 | "filter": Dict({"entity_id": "1", "entity_type": "machine"}), |
| Dominik Fleischmann | b78b3e0 | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 275 | "delta": FAKE_DELTA_MACHINE_STARTED, |
| 276 | "entity_status": Dict( |
| 277 | {"status": "started", "message": "Running", "vca_status": "running"} |
| 278 | ), |
| 279 | "db": Dict({"written": False, "data": None}), |
| 280 | } |
| 281 | ), |
| 282 | Dict( |
| 283 | { |
| David Garcia | aded583 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 284 | "entity": Dict({"id": "git/0", "type": "unit"}), |
| 285 | "filter": Dict({"entity_id": "git", "entity_type": "application"}), |
| Dominik Fleischmann | b78b3e0 | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 286 | "delta": FAKE_DELTA_UNIT_PENDING, |
| 287 | "entity_status": Dict( |
| 288 | {"status": "waiting", "message": "", "vca_status": "waiting"} |
| 289 | ), |
| 290 | "db": Dict( |
| 291 | { |
| 292 | "written": True, |
| 293 | "data": Dict( |
| 294 | { |
| 295 | "message": "", |
| 296 | "entity": "unit", |
| 297 | "vca_status": "waiting", |
| 298 | "n2vc_status": N2VCDeploymentStatus.RUNNING, |
| 299 | } |
| 300 | ), |
| 301 | } |
| 302 | ), |
| 303 | } |
| 304 | ), |
| 305 | Dict( |
| 306 | { |
| David Garcia | aded583 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 307 | "entity": Dict({"id": "git/0", "type": "unit"}), |
| 308 | "filter": Dict({"entity_id": "2", "entity_type": "machine"}), |
| Dominik Fleischmann | b78b3e0 | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 309 | "delta": FAKE_DELTA_UNIT_PENDING, |
| 310 | "entity_status": Dict( |
| 311 | {"status": "waiting", "message": "", "vca_status": "waiting"} |
| 312 | ), |
| 313 | "db": Dict({"written": False, "data": None}), |
| 314 | } |
| 315 | ), |
| 316 | Dict( |
| 317 | { |
| David Garcia | aded583 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 318 | "entity": Dict({"id": "git/0", "type": "unit"}), |
| 319 | "filter": Dict({"entity_id": "git", "entity_type": "application"}), |
| Dominik Fleischmann | b78b3e0 | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 320 | "delta": FAKE_DELTA_UNIT_STARTED, |
| 321 | "entity_status": Dict( |
| 322 | {"status": "active", "message": "", "vca_status": "active"} |
| 323 | ), |
| 324 | "db": Dict( |
| 325 | { |
| 326 | "written": True, |
| 327 | "data": Dict( |
| 328 | { |
| 329 | "message": "", |
| 330 | "entity": "unit", |
| 331 | "vca_status": "active", |
| 332 | "n2vc_status": N2VCDeploymentStatus.COMPLETED, |
| 333 | } |
| 334 | ), |
| 335 | } |
| 336 | ), |
| 337 | } |
| 338 | ), |
| 339 | Dict( |
| 340 | { |
| David Garcia | aded583 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 341 | "entity": Dict({"id": "git/0", "type": "unit"}), |
| 342 | "filter": Dict({"entity_id": "1", "entity_type": "action"}), |
| Dominik Fleischmann | b78b3e0 | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 343 | "delta": FAKE_DELTA_UNIT_STARTED, |
| 344 | "entity_status": Dict( |
| 345 | {"status": "active", "message": "", "vca_status": "active"} |
| 346 | ), |
| 347 | "db": Dict({"written": False, "data": None}), |
| 348 | } |
| 349 | ), |
| 350 | Dict( |
| 351 | { |
| David Garcia | aded583 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 352 | "entity": Dict({"id": "git", "type": "application"}), |
| 353 | "filter": Dict({"entity_id": "git", "entity_type": "application"}), |
| Dominik Fleischmann | b78b3e0 | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 354 | "delta": FAKE_DELTA_APPLICATION_MAINTENANCE, |
| 355 | "entity_status": Dict( |
| 356 | { |
| 357 | "status": "maintenance", |
| 358 | "message": "installing charm software", |
| 359 | "vca_status": "maintenance", |
| 360 | } |
| 361 | ), |
| 362 | "db": Dict( |
| 363 | { |
| 364 | "written": True, |
| 365 | "data": Dict( |
| 366 | { |
| 367 | "message": "installing charm software", |
| 368 | "entity": "application", |
| 369 | "vca_status": "maintenance", |
| 370 | "n2vc_status": N2VCDeploymentStatus.RUNNING, |
| 371 | } |
| 372 | ), |
| 373 | } |
| 374 | ), |
| 375 | } |
| 376 | ), |
| 377 | Dict( |
| 378 | { |
| David Garcia | aded583 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 379 | "entity": Dict({"id": "git", "type": "application"}), |
| 380 | "filter": Dict({"entity_id": "2", "entity_type": "machine"}), |
| Dominik Fleischmann | b78b3e0 | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 381 | "delta": FAKE_DELTA_APPLICATION_MAINTENANCE, |
| 382 | "entity_status": Dict( |
| 383 | { |
| 384 | "status": "maintenance", |
| 385 | "message": "installing charm software", |
| 386 | "vca_status": "maintenance", |
| 387 | } |
| 388 | ), |
| 389 | "db": Dict({"written": False, "data": None}), |
| 390 | } |
| 391 | ), |
| 392 | Dict( |
| 393 | { |
| David Garcia | aded583 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 394 | "entity": Dict({"id": "git", "type": "application"}), |
| 395 | "filter": Dict({"entity_id": "git", "entity_type": "application"}), |
| Dominik Fleischmann | b78b3e0 | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 396 | "delta": FAKE_DELTA_APPLICATION_ACTIVE, |
| 397 | "entity_status": Dict( |
| 398 | {"status": "active", "message": "Ready!", "vca_status": "active"} |
| 399 | ), |
| 400 | "db": Dict( |
| 401 | { |
| 402 | "written": True, |
| 403 | "data": Dict( |
| 404 | { |
| 405 | "message": "Ready!", |
| 406 | "entity": "application", |
| 407 | "vca_status": "active", |
| 408 | "n2vc_status": N2VCDeploymentStatus.COMPLETED, |
| 409 | } |
| 410 | ), |
| 411 | } |
| 412 | ), |
| 413 | } |
| 414 | ), |
| 415 | Dict( |
| 416 | { |
| David Garcia | aded583 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 417 | "entity": Dict({"id": "git", "type": "application"}), |
| 418 | "filter": Dict({"entity_id": "1", "entity_type": "action"}), |
| Dominik Fleischmann | b78b3e0 | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 419 | "delta": FAKE_DELTA_APPLICATION_ACTIVE, |
| 420 | "entity_status": Dict( |
| 421 | {"status": "active", "message": "Ready!", "vca_status": "active"} |
| 422 | ), |
| 423 | "db": Dict({"written": False, "data": None}), |
| 424 | } |
| 425 | ), |
| 426 | Dict( |
| 427 | { |
| David Garcia | aded583 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 428 | "entity": Dict({"id": "1", "type": "action"}), |
| 429 | "filter": Dict({"entity_id": "1", "entity_type": "action"}), |
| Dominik Fleischmann | b78b3e0 | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 430 | "delta": FAKE_DELTA_ACTION_COMPLETED, |
| 431 | "entity_status": Dict( |
| 432 | { |
| 433 | "status": "completed", |
| 434 | "message": "completed", |
| 435 | "vca_status": "completed", |
| 436 | } |
| 437 | ), |
| 438 | "db": Dict( |
| 439 | { |
| 440 | "written": True, |
| 441 | "data": Dict( |
| 442 | { |
| 443 | "message": "completed", |
| 444 | "entity": "action", |
| 445 | "vca_status": "completed", |
| 446 | "n2vc_status": N2VCDeploymentStatus.COMPLETED, |
| 447 | } |
| 448 | ), |
| 449 | } |
| 450 | ), |
| 451 | } |
| 452 | ), |
| 453 | Dict( |
| 454 | { |
| David Garcia | aded583 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 455 | "entity": Dict({"id": "git", "type": "action"}), |
| 456 | "filter": Dict({"entity_id": "1", "entity_type": "machine"}), |
| Dominik Fleischmann | b78b3e0 | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 457 | "delta": FAKE_DELTA_ACTION_COMPLETED, |
| 458 | "entity_status": Dict( |
| 459 | { |
| 460 | "status": "completed", |
| 461 | "message": "completed", |
| 462 | "vca_status": "completed", |
| 463 | } |
| 464 | ), |
| 465 | "db": Dict({"written": False, "data": None}), |
| 466 | } |
| 467 | ), |
| 468 | ] |