Fix bug 1467
[osm/N2VC.git] / n2vc / tests / unit / test_utils.py
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
17 from n2vc.utils import Dict, EntityType, JujuStatusToOSM, N2VCDeploymentStatus, DB_DATA
18 from juju.machine import Machine
19 from juju.application import Application
20 from juju.action import Action
21 from juju.unit import Unit
22
23
24 class UtilsTest(TestCase):
25 def test_dict(self):
26 example = Dict({"key": "value"})
27 self.assertEqual(example["key"], example.key)
28
29 def test_entity_type(self):
30 self.assertFalse(EntityType.has_value("machine2"))
31 values = [Machine, Application, Action, Unit]
32 for value in values:
33 self.assertTrue(EntityType.has_value(value))
34
35 self.assertEqual(EntityType.MACHINE, EntityType.get_entity(Machine))
36 self.assertEqual(EntityType.APPLICATION, EntityType.get_entity(Application))
37 self.assertEqual(EntityType.UNIT, EntityType.get_entity(Unit))
38 self.assertEqual(EntityType.ACTION, EntityType.get_entity(Action))
39
40 def test_juju_status_to_osm(self):
41 tests = [
42 {
43 "entity_type": "machine",
44 "status": [
45 {"juju": "pending", "osm": N2VCDeploymentStatus.PENDING},
46 {"juju": "started", "osm": N2VCDeploymentStatus.COMPLETED},
47 ],
48 },
49 {
50 "entity_type": "application",
51 "status": [
52 {"juju": "waiting", "osm": N2VCDeploymentStatus.RUNNING},
53 {"juju": "maintenance", "osm": N2VCDeploymentStatus.RUNNING},
54 {"juju": "blocked", "osm": N2VCDeploymentStatus.RUNNING},
55 {"juju": "error", "osm": N2VCDeploymentStatus.FAILED},
56 {"juju": "active", "osm": N2VCDeploymentStatus.COMPLETED},
57 ],
58 },
59 {
60 "entity_type": "unit",
61 "status": [
62 {"juju": "waiting", "osm": N2VCDeploymentStatus.RUNNING},
63 {"juju": "maintenance", "osm": N2VCDeploymentStatus.RUNNING},
64 {"juju": "blocked", "osm": N2VCDeploymentStatus.RUNNING},
65 {"juju": "error", "osm": N2VCDeploymentStatus.FAILED},
66 {"juju": "active", "osm": N2VCDeploymentStatus.COMPLETED},
67 ],
68 },
69 {
70 "entity_type": "action",
71 "status": [
72 {"juju": "running", "osm": N2VCDeploymentStatus.RUNNING},
73 {"juju": "completed", "osm": N2VCDeploymentStatus.COMPLETED},
74 ],
75 },
76 ]
77
78 for test in tests:
79 entity_type = test["entity_type"]
80 self.assertTrue(entity_type in JujuStatusToOSM)
81
82 for status in test["status"]:
83 juju_status = status["juju"]
84 osm_status = status["osm"]
85 self.assertTrue(juju_status in JujuStatusToOSM[entity_type])
86 self.assertEqual(osm_status, JujuStatusToOSM[entity_type][juju_status])
87
88 def test_db_data(self):
89 self.assertEqual(DB_DATA.api_endpoints.table, "admin")
90 self.assertEqual(DB_DATA.api_endpoints.filter, {"_id": "juju"})
91 self.assertEqual(DB_DATA.api_endpoints.key, "api_endpoints")