| Dominik Fleischmann | 7ff392f | 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 | from unittest import TestCase |
| 16 | |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 17 | from n2vc.utils import ( |
| 18 | Dict, |
| 19 | EntityType, |
| 20 | JujuStatusToOSM, |
| 21 | N2VCDeploymentStatus, |
| 22 | get_ee_id_components, |
| 23 | ) |
| Dominik Fleischmann | 7ff392f | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 24 | from juju.machine import Machine |
| 25 | from juju.application import Application |
| 26 | from juju.action import Action |
| 27 | from juju.unit import Unit |
| 28 | |
| 29 | |
| 30 | class UtilsTest(TestCase): |
| 31 | def test_dict(self): |
| 32 | example = Dict({"key": "value"}) |
| 33 | self.assertEqual(example["key"], example.key) |
| 34 | |
| 35 | def test_entity_type(self): |
| 36 | self.assertFalse(EntityType.has_value("machine2")) |
| 37 | values = [Machine, Application, Action, Unit] |
| 38 | for value in values: |
| 39 | self.assertTrue(EntityType.has_value(value)) |
| 40 | |
| 41 | self.assertEqual(EntityType.MACHINE, EntityType.get_entity(Machine)) |
| 42 | self.assertEqual(EntityType.APPLICATION, EntityType.get_entity(Application)) |
| 43 | self.assertEqual(EntityType.UNIT, EntityType.get_entity(Unit)) |
| 44 | self.assertEqual(EntityType.ACTION, EntityType.get_entity(Action)) |
| 45 | |
| 46 | def test_juju_status_to_osm(self): |
| 47 | tests = [ |
| 48 | { |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 49 | "entity_type": "machine", |
| Dominik Fleischmann | 7ff392f | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 50 | "status": [ |
| 51 | {"juju": "pending", "osm": N2VCDeploymentStatus.PENDING}, |
| 52 | {"juju": "started", "osm": N2VCDeploymentStatus.COMPLETED}, |
| 53 | ], |
| 54 | }, |
| 55 | { |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 56 | "entity_type": "application", |
| Dominik Fleischmann | 7ff392f | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 57 | "status": [ |
| 58 | {"juju": "waiting", "osm": N2VCDeploymentStatus.RUNNING}, |
| 59 | {"juju": "maintenance", "osm": N2VCDeploymentStatus.RUNNING}, |
| 60 | {"juju": "blocked", "osm": N2VCDeploymentStatus.RUNNING}, |
| 61 | {"juju": "error", "osm": N2VCDeploymentStatus.FAILED}, |
| 62 | {"juju": "active", "osm": N2VCDeploymentStatus.COMPLETED}, |
| 63 | ], |
| 64 | }, |
| 65 | { |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 66 | "entity_type": "unit", |
| Dominik Fleischmann | 7ff392f | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 67 | "status": [ |
| 68 | {"juju": "waiting", "osm": N2VCDeploymentStatus.RUNNING}, |
| 69 | {"juju": "maintenance", "osm": N2VCDeploymentStatus.RUNNING}, |
| 70 | {"juju": "blocked", "osm": N2VCDeploymentStatus.RUNNING}, |
| 71 | {"juju": "error", "osm": N2VCDeploymentStatus.FAILED}, |
| 72 | {"juju": "active", "osm": N2VCDeploymentStatus.COMPLETED}, |
| 73 | ], |
| 74 | }, |
| 75 | { |
| David Garcia | c38a696 | 2020-09-16 13:31:33 +0200 | [diff] [blame] | 76 | "entity_type": "action", |
| Dominik Fleischmann | 7ff392f | 2020-07-07 13:11:19 +0200 | [diff] [blame] | 77 | "status": [ |
| 78 | {"juju": "running", "osm": N2VCDeploymentStatus.RUNNING}, |
| 79 | {"juju": "completed", "osm": N2VCDeploymentStatus.COMPLETED}, |
| 80 | ], |
| 81 | }, |
| 82 | ] |
| 83 | |
| 84 | for test in tests: |
| 85 | entity_type = test["entity_type"] |
| 86 | self.assertTrue(entity_type in JujuStatusToOSM) |
| 87 | |
| 88 | for status in test["status"]: |
| 89 | juju_status = status["juju"] |
| 90 | osm_status = status["osm"] |
| 91 | self.assertTrue(juju_status in JujuStatusToOSM[entity_type]) |
| 92 | self.assertEqual(osm_status, JujuStatusToOSM[entity_type][juju_status]) |
| David Garcia | 582b923 | 2021-10-26 12:30:44 +0200 | [diff] [blame] | 93 | |
| 94 | |
| 95 | class GetEEComponentTest(TestCase): |
| 96 | def test_valid(self): |
| 97 | model, application, machine = get_ee_id_components("model.application.machine") |
| 98 | self.assertEqual(model, "model") |
| 99 | self.assertEqual(application, "application") |
| 100 | self.assertEqual(machine, "machine") |
| 101 | |
| 102 | def test_invalid(self): |
| 103 | with self.assertRaises(Exception): |
| 104 | get_ee_id_components("model.application.machine.1") |
| 105 | with self.assertRaises(Exception): |
| 106 | get_ee_id_components("model.application") |