1 # Copyright 2022 Canonical Ltd.
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
16 from unittest
.mock
import Mock
, patch
18 from ops
.model
import ActiveStatus
, BlockedStatus
, MaintenanceStatus
19 from ops
.testing
import Harness
21 from charm
import UpgradeDBCharm
24 class TestCharm(unittest
.TestCase
):
26 self
.harness
= Harness(UpgradeDBCharm
)
27 self
.addCleanup(self
.harness
.cleanup
)
30 def test_initial_config(self
):
31 self
.assertEqual(self
.harness
.model
.unit
.status
, MaintenanceStatus(""))
33 def test_config_changed(self
):
34 self
.harness
.update_config({"mongodb-uri": "foo"})
35 self
.assertEqual(self
.harness
.model
.unit
.status
, ActiveStatus())
37 def test_config_changed_blocked(self
):
38 self
.harness
.update_config({"log-level": "DEBUG"})
40 self
.harness
.model
.unit
.status
,
41 BlockedStatus("mongodb-uri and/or mysql-uri must be set"),
44 def test_update_db_fail_only_params(self
):
53 self
.harness
.charm
._on
_update
_db
_action
(action_event
)
55 action_event
.fail
.call_args
,
56 [("Failed DB Upgrade: cannot set both mysql-only and mongodb-only options to True",)],
59 @patch("charm.MongoUpgrade")
60 @patch("charm.MysqlUpgrade")
61 def test_update_db_mysql(self
, mock_mysql_upgrade
, mock_mongo_upgrade
):
62 self
.harness
.update_config({"mysql-uri": "foo"})
68 "mongodb-only": False,
71 self
.harness
.charm
._on
_update
_db
_action
(action_event
)
72 mock_mysql_upgrade().upgrade
.assert_called_once()
73 mock_mongo_upgrade
.assert_not_called()
75 @patch("charm.MongoUpgrade")
76 @patch("charm.MysqlUpgrade")
77 def test_update_db_mongo(self
, mock_mysql_upgrade
, mock_mongo_upgrade
):
78 self
.harness
.update_config({"mongodb-uri": "foo"})
87 self
.harness
.charm
._on
_update
_db
_action
(action_event
)
88 mock_mongo_upgrade().upgrade
.assert_called_once()
89 mock_mysql_upgrade
.assert_not_called()
91 @patch("charm.MongoUpgrade")
92 def test_update_db_not_configured_mongo_fail(self
, mock_mongo_upgrade
):
101 self
.harness
.charm
._on
_update
_db
_action
(action_event
)
102 mock_mongo_upgrade
.assert_not_called()
104 action_event
.fail
.call_args
,
105 [("Failed DB Upgrade: mongo-uri not set",)],
108 @patch("charm.MysqlUpgrade")
109 def test_update_db_not_configured_mysql_fail(self
, mock_mysql_upgrade
):
112 "current-version": 7,
113 "target-version": 10,
115 "mongodb-only": False,
118 self
.harness
.charm
._on
_update
_db
_action
(action_event
)
119 mock_mysql_upgrade
.assert_not_called()
121 action_event
.fail
.call_args
,
122 [("Failed DB Upgrade: mysql-uri not set",)],
125 @patch("charm.MongoUpgrade")
126 @patch("charm.MysqlUpgrade")
127 def test_update_db_mongodb_and_mysql(self
, mock_mysql_upgrade
, mock_mongo_upgrade
):
128 self
.harness
.update_config({"mongodb-uri": "foo"})
129 self
.harness
.update_config({"mysql-uri": "foo"})
132 "current-version": 7,
133 "target-version": 10,
135 "mongodb-only": False,
138 self
.harness
.charm
._on
_update
_db
_action
(action_event
)
139 mock_mysql_upgrade().upgrade
.assert_called_once()
140 mock_mongo_upgrade().upgrade
.assert_called_once()
142 @patch("charm.MongoUpgrade")
143 def test_apply_patch(self
, mock_mongo_upgrade
):
144 self
.harness
.update_config({"mongodb-uri": "foo"})
150 self
.harness
.charm
._on
_apply
_patch
_action
(action_event
)
151 mock_mongo_upgrade().apply_patch
.assert_called_once()
153 @patch("charm.MongoUpgrade")
154 def test_apply_patch_fail(self
, mock_mongo_upgrade
):
160 self
.harness
.charm
._on
_apply
_patch
_action
(action_event
)
161 mock_mongo_upgrade
.assert_not_called()
163 action_event
.fail
.call_args
,
164 [("Failed Patch Application: mongo-uri not set",)],