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