-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Block upgrade if vault is in sealed status
Add pre-check step as first step before all the steps to make sure vault is not in sealed.
- Loading branch information
Showing
9 changed files
with
132 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Copyright 2024 Canonical Limited | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Functions for prereq steps relating to vault.""" | ||
import logging | ||
|
||
from cou.exceptions import ApplicationNotFound, VaultSealed | ||
from cou.utils.juju_utils import Model | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
async def check_vault_status(model: Model) -> None: | ||
"""Make sure vault is not in sealed status. | ||
:param model: juju model to work with | ||
:type model: Model | ||
:raises VaultSealed: if application in sealed status | ||
""" | ||
try: | ||
app_names = await model.get_application_names(charm_name="vault") | ||
for app_name in app_names: | ||
app = await model.get_application_status(app_name=app_name) | ||
if app.status.info == "Unit is sealed" and app.status.status == "blocked": | ||
raise VaultSealed( | ||
"Vault is in sealed, please follow the steps on " | ||
"https://charmhub.io/vault to unseal the vault manually before upgrade" | ||
) | ||
except ApplicationNotFound: | ||
logger.warning("Application vault not found, skip") | ||
logger.debug("Vault not in sealed status") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
tests/mocked_plans/sample_plans/018346c5-f95c-46df-a34e-9a78bdec0018.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
tests/mocked_plans/sample_plans/9eb9af6a-b919-4cf9-8f2f-9df16a1556be.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright 2024 Canonical Limited | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
|
||
from cou.exceptions import ApplicationNotFound, VaultSealed | ||
from cou.steps.vault import check_vault_status | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_check_vault_status_sealed(model) -> None: | ||
model.get_application_names.return_value = ["app1", "app2"] | ||
model.get_application_status.return_value = MagicMock() | ||
model.get_application_status.return_value.status = MagicMock() | ||
model.get_application_status.return_value.status.info = "Unit is sealed" | ||
model.get_application_status.return_value.status.status = "blocked" | ||
err_msg = ( | ||
"Vault is in sealed, please follow the steps on " | ||
"https://charmhub.io/vault to unseal the vault manually before upgrade" | ||
) | ||
with pytest.raises(VaultSealed, match=err_msg): | ||
await check_vault_status(model) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"case,info,status", | ||
[ | ||
["wrong info", "wrong info msg", "blocked"], | ||
["wrong status", "Unit is sealed", "wrong status"], | ||
], | ||
) | ||
@pytest.mark.asyncio | ||
async def test_check_vault_status_unseal(case, info, status, model) -> None: | ||
model.get_application_names.return_value = ["app1", "app2"] | ||
model.get_application_status.return_value = MagicMock() | ||
model.get_application_status.return_value.status = MagicMock() | ||
model.get_application_status.return_value.status.info = info | ||
model.get_application_status.return_value.status.status = status | ||
await check_vault_status(model) | ||
|
||
|
||
@pytest.mark.asyncio | ||
@patch("cou.steps.vault.logger") | ||
async def test_check_vault_status_vault_not_exists(mock_logger, model) -> None: | ||
model.get_application_names.side_effect = ApplicationNotFound | ||
await check_vault_status(model) | ||
mock_logger.warning.assert_called_once_with("Application vault not found, skip") |