forked from gnuoy/snap-openstack
-
Notifications
You must be signed in to change notification settings - Fork 36
fix: grant access for observability model to other nodes #794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
139 changes: 139 additions & 0 deletions
139
sunbeam-python/tests/unit/sunbeam/provider/local/test_commands.py
This file contains hidden or 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,139 @@ | ||
| # SPDX-FileCopyrightText: 2023 - Canonical Ltd | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from unittest.mock import Mock, patch | ||
|
|
||
| import pytest | ||
| from click.testing import CliRunner | ||
|
|
||
| from sunbeam.core.common import ResultType | ||
| from sunbeam.provider.local.commands import add | ||
| from sunbeam.steps.juju import JujuGrantModelAccessStep | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def run_preflight(): | ||
| with patch("sunbeam.provider.local.commands.run_preflight_checks") as p: | ||
| yield p | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def daemon_group_check(): | ||
| with patch("sunbeam.provider.local.commands.DaemonGroupCheck") as p: | ||
| yield p | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def verify_fqdn_check(): | ||
| with patch("sunbeam.provider.local.commands.VerifyFQDNCheck") as p: | ||
| yield p | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def run_plan_cmd(): | ||
| with patch("sunbeam.provider.local.commands.run_plan") as p: | ||
| yield p | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def juju_helper_cmd(): | ||
| with patch("sunbeam.provider.local.commands.JujuHelper") as p: | ||
| yield p | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def get_step_result_cmd(): | ||
| with patch("sunbeam.provider.local.commands.get_step_result") as p: | ||
| yield p | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def get_step_message_cmd(): | ||
| with patch("sunbeam.provider.local.commands.get_step_message") as p: | ||
| yield p | ||
|
|
||
|
|
||
| class TestAddNodeGrantModels: | ||
| """Test that adding a node grants access to all Juju models dynamically.""" | ||
|
|
||
| def test_add_grants_access_to_all_models( | ||
| self, | ||
| daemon_group_check, | ||
| verify_fqdn_check, | ||
| run_preflight, | ||
| run_plan_cmd, | ||
| juju_helper_cmd, | ||
| get_step_result_cmd, | ||
| get_step_message_cmd, | ||
| ): | ||
| """run_plan receives JujuGrantModelAccessStep for every model.""" | ||
| jhelper_instance = juju_helper_cmd.return_value | ||
| jhelper_instance.models.return_value = [ | ||
| {"short-name": "openstack-machines"}, | ||
| {"short-name": "openstack"}, | ||
| {"short-name": "observability"}, | ||
| ] | ||
|
|
||
| add_node_result = Mock(result_type=ResultType.COMPLETED, message="test-token") | ||
| create_user_result = Mock(result_type=ResultType.COMPLETED) | ||
| get_step_result_cmd.side_effect = [add_node_result, create_user_result] | ||
| get_step_message_cmd.return_value = "user-token" | ||
|
|
||
| deployment = Mock() | ||
| runner = CliRunner() | ||
| result = runner.invoke(add, ["new-node.domain"], obj=deployment) | ||
|
|
||
| assert result.exit_code == 0, result.output | ||
|
|
||
| # Second call to run_plan is plan_access | ||
| plan_access = run_plan_cmd.call_args_list[1][0][0] | ||
| grant_steps = [ | ||
| s for s in plan_access if isinstance(s, JujuGrantModelAccessStep) | ||
| ] | ||
| assert len(grant_steps) == 3 | ||
| assert [s.model for s in grant_steps] == [ | ||
| "openstack-machines", | ||
| "openstack", | ||
| "observability", | ||
| ] | ||
| for step in grant_steps: | ||
| assert step.username == "new-node.domain" | ||
|
|
||
| def test_add_skips_models_with_empty_short_name( | ||
| self, | ||
| daemon_group_check, | ||
| verify_fqdn_check, | ||
| run_preflight, | ||
| run_plan_cmd, | ||
| juju_helper_cmd, | ||
| get_step_result_cmd, | ||
| get_step_message_cmd, | ||
| ): | ||
| """Models with empty short-name are excluded from grant steps.""" | ||
| jhelper_instance = juju_helper_cmd.return_value | ||
| jhelper_instance.models.return_value = [ | ||
| {"short-name": "openstack"}, | ||
| {"short-name": "observability"}, | ||
| {"short-name": ""}, | ||
| ] | ||
|
|
||
| add_node_result = Mock(result_type=ResultType.COMPLETED, message="test-token") | ||
| create_user_result = Mock(result_type=ResultType.COMPLETED) | ||
| get_step_result_cmd.side_effect = [add_node_result, create_user_result] | ||
| get_step_message_cmd.return_value = "user-token" | ||
|
|
||
| deployment = Mock() | ||
| runner = CliRunner() | ||
| result = runner.invoke(add, ["new-node.domain"], obj=deployment) | ||
|
|
||
| assert result.exit_code == 0, result.output | ||
|
|
||
| plan_access = run_plan_cmd.call_args_list[1][0][0] | ||
| grant_steps = [ | ||
| s for s in plan_access if isinstance(s, JujuGrantModelAccessStep) | ||
| ] | ||
| assert len(grant_steps) == 2 | ||
| model_names = [s.model for s in grant_steps] | ||
| assert "openstack" in model_names | ||
| assert "observability" in model_names | ||
| assert "" not in model_names |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed once #791 is merged.