blob: bffbc299fa8b46f9de7ba69acbf8f74c13ff3a7c [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 Garciaeb8943a2021-04-12 12:07:37 +020017from n2vc.utils import Dict, EntityType, JujuStatusToOSM, N2VCDeploymentStatus
Dominik Fleischmann7ff392f2020-07-07 13:11:19 +020018from juju.machine import Machine
19from juju.application import Application
20from juju.action import Action
21from juju.unit import Unit
22
23
24class 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 {
David Garciac38a6962020-09-16 13:31:33 +020043 "entity_type": "machine",
Dominik Fleischmann7ff392f2020-07-07 13:11:19 +020044 "status": [
45 {"juju": "pending", "osm": N2VCDeploymentStatus.PENDING},
46 {"juju": "started", "osm": N2VCDeploymentStatus.COMPLETED},
47 ],
48 },
49 {
David Garciac38a6962020-09-16 13:31:33 +020050 "entity_type": "application",
Dominik Fleischmann7ff392f2020-07-07 13:11:19 +020051 "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 {
David Garciac38a6962020-09-16 13:31:33 +020060 "entity_type": "unit",
Dominik Fleischmann7ff392f2020-07-07 13:11:19 +020061 "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 {
David Garciac38a6962020-09-16 13:31:33 +020070 "entity_type": "action",
Dominik Fleischmann7ff392f2020-07-07 13:11:19 +020071 "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])