diff --git a/pubtools/iib/iib_ops.py b/pubtools/iib/iib_ops.py index 130cadf..1fd3371 100644 --- a/pubtools/iib/iib_ops.py +++ b/pubtools/iib/iib_ops.py @@ -101,10 +101,24 @@ }, ("--overwrite-from-index",): { "group": "IIB service", - "help": "overwrite from_index_image as output", + "help": ( + "overwrite from_index_image as output. If this is true," + " overwrite-from-index-token should also be specified." + ), "required": False, "type": bool, }, + ("--overwrite-from-index-token",): { + "group": "IIB service", + "help": ( + "destination repo token to overwrite from_index_image" + "If this is specified, overwrite-from-index must be set to True." + "Or set the OVERWRITE_FROM_INDEX_TOKEN environment variable." + ), + "required": False, + "type": str, + "env_variable": "OVERWRITE_FROM_INDEX_TOKEN", + }, ("--skip-pulp",): { "group": "IIB service", "help": "Skip operations on pulp", @@ -205,8 +219,14 @@ def _iib_op_main(args, operation=None, items_final_state="PUSHED"): extra_args = {"cnr_token": args.iib_cnr_token} if args.iib_legacy_org: extra_args["organization"] = args.iib_legacy_org - if args.overwrite_from_index: + if args.overwrite_from_index and args.overwrite_from_index_token: extra_args["overwrite_from_index"] = args.overwrite_from_index + extra_args["overwrite_from_index_token"] = args.overwrite_from_index_token + else: + raise ValueError( + "Either both or neither of overwrite-from-index and " + "overwrite-from-index-token should be specified." + ) build_details = bundle_op( args.index_image, diff --git a/tests/test_iib_ops.py b/tests/test_iib_ops.py index bc6b073..e016648 100644 --- a/tests/test_iib_ops.py +++ b/tests/test_iib_ops.py @@ -162,6 +162,7 @@ def add_bundles_mock_calls_tester( cnr_token="cnr_token", organization="legacy-org", overwrite_from_index=True, + overwrite_from_index_token="overwrite_from_index_token", ) fixture_iib_client.assert_called_once_with( "iib-server", auth=fixture_iib_krb_auth.return_value, ssl_verify=False @@ -185,6 +186,7 @@ def add_bundles_mock_calls_tester_not_called( cnr_token="cnr_token", organization="legacy-org", overwrite_from_index=True, + overwrite_from_index_token="overwrite_from_index_token", ) fixture_iib_client.assert_called_once_with( "iib-server", auth=fixture_iib_krb_auth.return_value, ssl_verify=False @@ -203,7 +205,12 @@ def remove_operators_mock_calls_tester_not_called( "iib-server", auth=fixture_iib_krb_auth.return_value, ssl_verify=False ) fixture_iib_client.return_value.remove_operators.assert_called_once_with( - "index-image", "binary-image", ["1"], ["arch"], overwrite_from_index=True + "index-image", + "binary-image", + ["1"], + ["arch"], + overwrite_from_index=True, + overwrite_from_index_token="overwrite_from_index_token", ) fixture_pulplib_repo_sync.assert_not_called() fixture_pulplib_repo_publish.assert_not_called() @@ -219,7 +226,12 @@ def remove_operators_mock_calls_tester( "iib-server", auth=fixture_iib_krb_auth.return_value, ssl_verify=False ) fixture_iib_client.return_value.remove_operators.assert_called_once_with( - "index-image", "binary-image", ["1"], ["arch"], overwrite_from_index=True + "index-image", + "binary-image", + ["1"], + ["arch"], + overwrite_from_index=True, + overwrite_from_index_token="overwrite_from_index_token", ) fixture_pulplib_repo_sync.assert_called_once() assert fixture_pulplib_repo_sync.mock_calls[0].args[0].feed == "https://feed.com" @@ -266,7 +278,11 @@ def test_add_bundles_cli( fixture_common_iib_op_args + ["--bundle", "bundle1", "--iib-legacy-org", "legacy-org"] + extra_args, - {"PULP_PASSWORD": "pulp-password", "CNR_TOKEN": "cnr_token"}, + { + "PULP_PASSWORD": "pulp-password", + "CNR_TOKEN": "cnr_token", + "OVERWRITE_FROM_INDEX_TOKEN": "overwrite_from_index_token", + }, ) as entry_func: entry_func() assert fixture_pushcollector.items == push_items @@ -303,7 +319,11 @@ def test_add_bundles_cli_error( ("pubtools_iib", "console_scripts", "pubtools-iib-add-bundles"), "pubtools-iib-add-bundle", fixture_common_iib_op_args + ["--bundle", "bundle1"], - {"PULP_PASSWORD": "pulp-password", "CNR_TOKEN": "cnr_token"}, + { + "PULP_PASSWORD": "pulp-password", + "CNR_TOKEN": "cnr_token", + "OVERWRITE_FROM_INDEX_TOKEN": "overwrite_from_index_token", + }, ) as entry_func: try: entry_func() @@ -326,13 +346,16 @@ def test_add_bundles_py( fixture_container_image_repo, fixture_common_iib_op_args, ): - repo = fixture_container_image_repo fixture_pulp_client.return_value.search_repository.return_value = [repo] fixture_pulp_client.return_value.get_repository.return_value = repo with setup_entry_point_py( ("pubtools_iib", "console_scripts", "pubtools-iib-add-bundles"), - {"PULP_PASSWORD": "pulp-password", "CNR_TOKEN": "cnr_token"}, + { + "PULP_PASSWORD": "pulp-password", + "CNR_TOKEN": "cnr_token", + "OVERWRITE_FROM_INDEX_TOKEN": "overwrite_from_index_token", + }, ) as entry_func: retval = entry_func( ["cmd"] + fixture_common_iib_op_args + ["--bundle", "bundle1"] @@ -350,6 +373,7 @@ def test_add_bundles_py( ["arch"], cnr_token="cnr_token", overwrite_from_index=True, + overwrite_from_index_token="overwrite_from_index_token", ) fixture_pulplib_repo_sync.assert_called_once() assert fixture_pulplib_repo_sync.mock_calls[0].args[0].feed == "https://feed.com" @@ -372,7 +396,11 @@ def test_add_bundles_py_multiple_bundles( fixture_pulp_client.return_value.get_repository.return_value = repo with setup_entry_point_py( ("pubtools_iib", "console_scripts", "pubtools-iib-add-bundles"), - {"PULP_PASSWORD": "pulp-password", "CNR_TOKEN": "cnr_token"}, + { + "PULP_PASSWORD": "pulp-password", + "CNR_TOKEN": "cnr_token", + "OVERWRITE_FROM_INDEX_TOKEN": "overwrite_from_index_token", + }, ) as entry_func: retval = entry_func( ["cmd"] @@ -392,6 +420,7 @@ def test_add_bundles_py_multiple_bundles( ["arch"], cnr_token="cnr_token", overwrite_from_index=True, + overwrite_from_index_token="overwrite_from_index_token", ) fixture_pulplib_repo_sync.assert_called_once() assert fixture_pulplib_repo_sync.mock_calls[0].args[0].feed == "https://feed.com" @@ -438,7 +467,10 @@ def test_remove_operators_cli( ("pubtools_iib", "console_scripts", "pubtools-iib-remove-operators"), "pubtools-iib-remove-operators", fixture_common_iib_op_args + ["--operator", "1"] + extra_args, - {"PULP_PASSWORD": "pulp-password"}, + { + "PULP_PASSWORD": "pulp-password", + "OVERWRITE_FROM_INDEX_TOKEN": "overwrite_from_index_token", + }, ) as entry_func: entry_func() assert fixture_pushcollector.items == push_items @@ -477,7 +509,10 @@ def test_remove_operators_cli_error( ("pubtools_iib", "console_scripts", "pubtools-iib-remove-operators"), "pubtools-iib-remove-operators", fixture_common_iib_op_args + ["--operator", "1"], - {"PULP_PASSWORD": "pulp-password"}, + { + "PULP_PASSWORD": "pulp-password", + "OVERWRITE_FROM_INDEX_TOKEN": "overwrite_from_index_token", + }, ) as entry_func: try: entry_func() @@ -506,7 +541,10 @@ def test_remove_operators_py( fixture_pulp_client.return_value.get_repository.return_value = repo with setup_entry_point_py( ("pubtools_iib", "console_scripts", "pubtools-iib-remove-operators"), - {"PULP_PASSWORD": "pulp-password"}, + { + "PULP_PASSWORD": "pulp-password", + "OVERWRITE_FROM_INDEX_TOKEN": "overwrite_from_index_token", + }, ) as entry_func: retval = entry_func(["cmd"] + fixture_common_iib_op_args + ["--operator", "1"]) @@ -526,3 +564,33 @@ def test_invalid_op(fixture_common_iib_op_args): assert False, "Should have raised" except ValueError: pass + + +def test_overwrite_from_index_token_missing( + fixture_iib_client, + fixture_pulp_client, + fixture_iib_krb_auth, + fixture_pulplib_repo_publish, + fixture_pulplib_repo_sync, + fixture_container_image_repo, + fixture_common_iib_op_args, +): + error_msg = ( + "Either both or neither of overwrite-from-index " + "and overwrite-from-index-token should be specified." + ) + repo = fixture_container_image_repo + fixture_pulp_client.return_value.search_repository.return_value = [repo] + fixture_pulp_client.return_value.get_repository.return_value = repo + with pytest.raises(ValueError, match=error_msg): + with setup_entry_point_py( + ("pubtools_iib", "console_scripts", "pubtools-iib-add-bundles"), + { + "PULP_PASSWORD": "pulp-password", + "CNR_TOKEN": "cnr_token", + "OVERWRITE_FROM_INDEX_TOKEN": "", + }, + ) as entry_func: + retval = entry_func( + ["cmd"] + fixture_common_iib_op_args + ["--bundle", "bundle1"] + ) diff --git a/tests/utils.py b/tests/utils.py index 072e7d7..9c127bc 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -21,6 +21,7 @@ def setup_task( cnr_token=None, organization=None, overwrite_from_index=False, + overwrite_from_index_token=None, state_seq=("in_progress", "finished"), op_type="add", ):