Skip to content

Commit

Permalink
Fix unit tests caused by internal python changes
Browse files Browse the repository at this point in the history
Subprocesses have changed where exception messages are stored.
From stdout exception messages are moved to stderr and we need to adapt.

Additionally, we use `called_once`` for one test for a mock where we
need to migrate to `assert_called_once_with` as for python 12
called_once is gone: python/cpython#100690

Signed-off-by: Martin Vrachev <martin.vrachev@broadcom.com>
  • Loading branch information
MVrachev committed May 16, 2024
1 parent b1ca958 commit 36c180a
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 54 deletions.
10 changes: 5 additions & 5 deletions tests/unit/cli/admin/test_import_artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ def test_import_artifacts_no_api_server_config_no_param(
result = client.invoke(
import_artifacts.import_artifacts, options, obj=test_context
)
assert result.exit_code == 1, result.output
assert "Requires '--api-server' " in result.output
assert result.exit_code == 1, result.stderr
assert "Requires '--api-server' " in result.stderr

def test_import_artifacts_skip_publish_artifacts(
self, client, test_context
Expand Down Expand Up @@ -474,7 +474,7 @@ def test_import_artifacts_bootstrap_check_failed(
import_artifacts.import_artifacts, options, obj=test_context
)
assert result.exit_code == 1
assert "Server ERROR" in result.output, result.output
assert "Server ERROR" in result.stderr, result.stderr

def test_import_artifacts_without_bootstrap(self, client, test_context):
test_context["settings"].SERVER = "fake-server"
Expand All @@ -496,10 +496,10 @@ def test_import_artifacts_without_bootstrap(self, client, test_context):
result = client.invoke(
import_artifacts.import_artifacts, options, obj=test_context
)
assert result.exit_code == 1, result.output
assert result.exit_code == 1, result.stderr
assert (
"import-artifacts` requires bootstrap process done."
in result.output
in result.stderr
)
assert import_artifacts.bootstrap_status.calls == [
pretend.call(test_context["settings"])
Expand Down
20 changes: 10 additions & 10 deletions tests/unit/cli/admin_legacy/test_ceremony.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_ceremony_start_no(self, client, test_context, test_inputs):
obj=test_context,
catch_exceptions=False,
)
assert "Ceremony aborted." in test_result.output
assert "Ceremony aborted." in test_result.stderr
assert test_result.exit_code == 1

def test_ceremony_start_not_ready_load_the_keys(
Expand All @@ -83,7 +83,7 @@ def test_ceremony_start_not_ready_load_the_keys(
obj=test_context,
catch_exceptions=False,
)
assert "Ceremony aborted." in test_result.output
assert "Ceremony aborted." in test_result.stderr
assert test_result.exit_code == 1

def test_ceremony_problem_loading_priv_key_fix_and_continue(
Expand Down Expand Up @@ -674,8 +674,8 @@ def test_ceremony_option_save_OSError(
catch_exceptions=False,
)

assert test_result.exit_code == 1, test_result.output
assert "permission denied" in test_result.output
assert test_result.exit_code == 1, test_result.stderr
assert "permission denied" in test_result.stderr

def test_ceremony_option_bootstrap(
self, client, test_context, test_inputs, test_setup
Expand Down Expand Up @@ -753,8 +753,8 @@ def test_ceremony_option_bootstrap_server_already_bootstrap(
catch_exceptions=False,
)

assert test_result.exit_code == 1, test_result.output
assert "System LOCKED for bootstrap" in test_result.output
assert test_result.exit_code == 1, test_result.stderr
assert "System LOCKED for bootstrap" in test_result.stderr
assert ceremony.bootstrap_status.calls == [
pretend.call(test_context["settings"])
]
Expand Down Expand Up @@ -814,8 +814,8 @@ def test_ceremony_option_bootstrap_upload_missing_api_server(
catch_exceptions=False,
)

assert test_result.exit_code == 1, test_result.output
assert "Requires '--api-server'" in test_result.output
assert test_result.exit_code == 1, test_result.stderr
assert "Requires '--api-server'" in test_result.stderr

def test_ceremony_option_upload_missing_bootstrap(
self, client, test_context, test_inputs, test_setup
Expand All @@ -836,5 +836,5 @@ def test_ceremony_option_upload_missing_bootstrap(
catch_exceptions=False,
)

assert test_result.exit_code == 1, test_result.output
assert "Requires '-b/--bootstrap' option." in test_result.output
assert test_result.exit_code == 1, test_result.stderr
assert "Requires '-b/--bootstrap' option." in test_result.stderr
22 changes: 11 additions & 11 deletions tests/unit/cli/admin_legacy/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ def test_metadata_update_send_payload_to_no_api_server_missing_param(
)
finish_msg = "Requires '--api-server' when using '--upload/-u'."
assert result.exit_code == 1
assert finish_msg in result.output
assert finish_msg in result.stderr

def test_metadata_update_passing_current_root(
self, client, test_context, md_update_input, tmp_update_payload_path
Expand Down Expand Up @@ -825,8 +825,8 @@ def test_metadata_sign_api_unavailable(
input="\n".join(input_step),
obj=test_context,
)
assert test_result.exit_code == 1, test_result.output
assert "Interal Server Error" in test_result.output
assert test_result.exit_code == 1, test_result.stderr
assert "Interal Server Error" in test_result.stderr
assert metadata.request_server.calls == [
pretend.call(
"http://127.0.0.1",
Expand Down Expand Up @@ -854,8 +854,8 @@ def test_metadata_sign_invalid_data(
input="\n".join(input_step),
obj=test_context,
)
assert test_result.exit_code == 1, test_result.output
assert "No data for you" in test_result.output
assert test_result.exit_code == 1, test_result.stderr
assert "No data for you" in test_result.stderr
assert metadata.request_server.calls == [
pretend.call(
"http://127.0.0.1",
Expand All @@ -882,8 +882,8 @@ def test_metadata_sign_no_signing_available(
input="\n".join(input_step),
obj=test_context,
)
assert test_result.exit_code == 1, test_result.output
assert "No metadata available for signing" in test_result.output
assert test_result.exit_code == 1, test_result.stderr
assert "No metadata available for signing" in test_result.stderr
assert metadata.request_server.calls == [
pretend.call(
"http://127.0.0.1",
Expand Down Expand Up @@ -1093,8 +1093,8 @@ def test_metadata_sign_load_invalid_key(
input="\n".join(input_step),
obj=test_context,
)
assert test_result.exit_code == 1, test_result.output
assert "Loaded key is not 'Jimi Hendrix'" in test_result.output
assert test_result.exit_code == 1, test_result.stderr
assert "Loaded key is not 'Jimi Hendrix'" in test_result.stderr
assert metadata.request_server.calls == [
pretend.call(
"http://127.0.0.1",
Expand Down Expand Up @@ -1133,8 +1133,8 @@ def test_metadata_sign_fails_during_signing(
input="\n".join(input_step),
obj=test_context,
)
assert test_result.exit_code == 1, test_result.output
assert "Problem signing the metadata" in test_result.output
assert test_result.exit_code == 1, test_result.stderr
assert "Problem signing the metadata" in test_result.stderr
assert metadata.request_server.calls == [
pretend.call(
"http://127.0.0.1",
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/cli/artifact/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,5 @@ def test_add_without_api_server(self, client, test_context):

result = client.invoke(add.add, input, obj=test_context)

assert result.exit_code == 1, result.output
assert "Requires '--api-server'" in result.output
assert result.exit_code == 1, result.stderr
assert "Requires '--api-server'" in result.stderr
23 changes: 11 additions & 12 deletions tests/unit/cli/artifact/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_download_command_missing_metadata_url(
obj=test_context,
)

assert "Please specify metadata url" in test_result.output
assert "Please specify metadata url" in test_result.stderr
assert test_result.exit_code == 1

def test_download_command_missing_artifacts_url(
Expand All @@ -61,7 +61,7 @@ def test_download_command_missing_artifacts_url(
obj=test_context,
)

assert "Please specify artifacts url" in test_result.output
assert "Please specify artifacts url" in test_result.stderr
assert test_result.exit_code == 1

def test_download_command_using_tofu(
Expand Down Expand Up @@ -362,7 +362,7 @@ def refresh(self):
)
assert f"Using trusted root in {metadata_dir}" in test_result.output
err_msg = f"Failed to download artifact {ARTIFACT_NAME}"
assert err_msg in test_result.output
assert err_msg in test_result.stderr
assert fake_build_metadata_dir.calls == [
pretend.call(METADATA_URL),
]
Expand Down Expand Up @@ -397,9 +397,8 @@ def test_download_command_with_failing_tofu(
)

assert "Using 'tofu' to Trust-On-First-Use" in test_result.output
assert "Failed to download initial root from" in test_result.output
assert "`tofu` was not successful" in test_result.output
assert test_result.exit_code == 1
assert "Failed to download initial root from" in test_result.stderr
assert "`tofu` was not successful" in test_result.stderr
assert len(fake_is_file.calls) == 2
assert pretend.call("foo_dir/root.json") in fake_is_file.calls

Expand Down Expand Up @@ -435,7 +434,7 @@ def test_download_command_no_current_repo(
[ARTIFACT_NAME],
obj=test_context,
)
assert "Please specify current repository" in test_result.output
assert "Please specify current repository" in test_result.stderr

def test_download_command_no_repos_listed(
self, client, test_context, test_setup, monkeypatch
Expand All @@ -456,8 +455,9 @@ def test_download_command_no_repos_listed(
[ARTIFACT_NAME],
obj=test_context,
)
assert test_result.exit_code == 1
assert (
"No reposotiroes listed in the config file" in test_result.output
"No reposotiroes listed in the config file" in test_result.stderr
)

def test_download_command_and_no_root_param(
Expand Down Expand Up @@ -518,8 +518,9 @@ def test_download_command_repo_is_missing(
[ARTIFACT_NAME],
obj=test_context,
)
assert test_result.exit_code == 1
err_msg = "Repository r1_expected is missing in the configuration file"
assert err_msg in test_result.output
assert err_msg in test_result.stderr

def test_download_command_no_trusted_root(
self, client, test_context, test_setup, monkeypatch
Expand All @@ -546,10 +547,8 @@ def test_download_command_no_trusted_root(
[ARTIFACT_NAME],
obj=test_context,
)
assert "Trusted root is not cofigured." in test_result.output
msg = "You should either add it to your config file,"
assert msg in test_result.output
assert test_result.exit_code == 1
assert "Trusted root is not cofigured." in test_result.stderr


class TestDownloadArtifactOptions:
Expand Down
20 changes: 10 additions & 10 deletions tests/unit/cli/artifact/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def test_repository_show_wrong_data_type(
)
assert (
"Repository wrong_root_type has incorrect configuration."
in test_result.output
in test_result.stderr
)
assert test_result.exit_code == 1

Expand All @@ -150,7 +150,7 @@ def test_repository_show_no_repos(self, client, test_context, test_setup):
repository.show,
obj=test_context,
)
assert "There are no configured repositories" in test_result.output
assert "There are no configured repositories" in test_result.stderr
assert test_result.exit_code == 1

test_result = client.invoke(
Expand All @@ -160,7 +160,7 @@ def test_repository_show_no_repos(self, client, test_context, test_setup):
)
assert (
"Repository r1 is missing in your configuration"
in test_result.output
in test_result.stderr
)
assert test_result.exit_code == 1

Expand Down Expand Up @@ -248,8 +248,8 @@ def test_repository_add_one_param_only(
)

assert test_result.exit_code == 2
assert "Try 'add --help' for help" in test_result.output
assert "Missing option '-m' / '--metadata-url'." in test_result.output
assert "Try 'add --help' for help" in test_result.stderr
assert "Missing option '-m' / '--metadata-url'." in test_result.stderr

def test_repository_add_all_params(
self, client, test_context, test_setup, monkeypatch
Expand Down Expand Up @@ -512,7 +512,7 @@ def test_repository_update_no_repos(

assert (
"There are no configured repositories to update"
in test_result.output
in test_result.stderr
)
assert test_result.exit_code == 1

Expand Down Expand Up @@ -690,9 +690,9 @@ def test_repository_update_non_existing(

assert (
"Repository non_existing not available in config. "
in test_result.output
in test_result.stderr
)
assert "You can create it instead" in test_result.output
assert "You can create it instead" in test_result.stderr
assert test_result.exit_code == 1

def test_repository_delete_no_repos(
Expand Down Expand Up @@ -720,7 +720,7 @@ def test_repository_delete_no_repos(

assert (
"There are no configured repositories. Nothing to delete"
in test_result.output
in test_result.stderr
)
assert test_result.exit_code == 1

Expand Down Expand Up @@ -762,7 +762,7 @@ def test_repository_delete_non_existing(

assert (
"Repository non_existing not available. Nothing to delete"
in test_result.output
in test_result.stderr
)
assert test_result.exit_code == 1

Expand Down
8 changes: 5 additions & 3 deletions tests/unit/cli/key/test_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_generate_key_types(self, key_type: str, client) -> None:

assert (
"Please select one of the available options"
not in test_result.output
not in test_result.stderr
)

assert test_result.exit_code == 1
Expand Down Expand Up @@ -86,7 +86,9 @@ def test_generate_types_generation(self, key_type, client) -> None:
catch_exceptions=False,
)

mock_keypair.called_once()
mock_keypair.assert_called_once_with(
password=password, filepath=filename
)
assert test_result.exit_code == 0
assert generate.load_key.calls == [
pretend.call(filename, key_type, password, "")
Expand Down Expand Up @@ -176,7 +178,7 @@ def test_generate_file_overwrite(self, client) -> None:
in test_result.output
)

assert "Key creation aborted." in test_result.output
assert "Key creation aborted." in test_result.stderr

# password not shown in output
assert password not in test_result.output
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/cli/task/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ def test_info_no_api_server_missing_param(self, client, test_context):
)

assert result.exit_code == 1
assert output_message in result.output
assert output_message in result.stderr

0 comments on commit 36c180a

Please sign in to comment.