blob: 3896b2f3120fb8e72bf7288a3ae76c21974dae76 [file] [log] [blame]
Dominik Fleischmann7ff392f2020-07-07 13:11:19 +02001# 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
15from unittest import TestCase
16
David Garcia582b9232021-10-26 12:30:44 +020017from n2vc.utils import (
18 Dict,
19 EntityType,
20 JujuStatusToOSM,
21 N2VCDeploymentStatus,
22 get_ee_id_components,
23)
Dominik Fleischmann7ff392f2020-07-07 13:11:19 +020024from juju.machine import Machine
25from juju.application import Application
26from juju.action import Action
27from juju.unit import Unit
28
29
30class 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 Garciac38a6962020-09-16 13:31:33 +020049 "entity_type": "machine",
Dominik Fleischmann7ff392f2020-07-07 13:11:19 +020050 "status": [
51 {"juju": "pending", "osm": N2VCDeploymentStatus.PENDING},
52 {"juju": "started", "osm": N2VCDeploymentStatus.COMPLETED},
53 ],
54 },
55 {
David Garciac38a6962020-09-16 13:31:33 +020056 "entity_type": "application",
Dominik Fleischmann7ff392f2020-07-07 13:11:19 +020057 "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 Garciac38a6962020-09-16 13:31:33 +020066 "entity_type": "unit",
Dominik Fleischmann7ff392f2020-07-07 13:11:19 +020067 "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 Garciac38a6962020-09-16 13:31:33 +020076 "entity_type": "action",
Dominik Fleischmann7ff392f2020-07-07 13:11:19 +020077 "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 Garcia582b9232021-10-26 12:30:44 +020093
94
95class 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")