diff --git a/ocs_ci/ocs/bucket_utils.py b/ocs_ci/ocs/bucket_utils.py index 534a0efefd4d..b10389b77886 100644 --- a/ocs_ci/ocs/bucket_utils.py +++ b/ocs_ci/ocs/bucket_utils.py @@ -1890,3 +1890,37 @@ def finalizer(): logger.info("Skipping creation of first.bucket as it already exists") request.addfinalizer(finalizer) + + +def get_nb_bucket_stores(mcg_obj, bucket_name): + """ + Query the noobaa-db for the backingstores/namespacestores + that a given bucket is using for its data placement + + Args: + mcg_obj: MCG object + bucket_name: name of the bucket + + Returns: + list: list of backingstores/namespacestores names + + """ + stores = set() + bucket_data = bucket_read_api(mcg_obj, bucket_name) + + # Namespacestore bucket + if "namespace" in bucket_data: + read_srcs_list = [ + d["resource"] for d in bucket_data["namespace"]["read_resources"] + ] + write_src = bucket_data["namespace"]["write_resource"]["resource"] + stores.update(read_srcs_list + [write_src]) + + # Data bucket + else: + tiers = [d["tier"] for d in bucket_data["tiering"]["tiers"]] + for tier in tiers: + tier_data = mcg_obj.send_rpc_query("tier_api", "read_tier", {"name": tier}) + stores.update(tier_data["reply"]["attached_pools"]) + + return list(stores) diff --git a/tests/manage/mcg/test_default_backingstore_override.py b/tests/manage/mcg/test_default_backingstore_override.py index 19267b7914c2..66c597eb6034 100644 --- a/tests/manage/mcg/test_default_backingstore_override.py +++ b/tests/manage/mcg/test_default_backingstore_override.py @@ -5,36 +5,30 @@ from ocs_ci.framework import config from ocs_ci.framework.testlib import MCGTest from ocs_ci.ocs import constants -from ocs_ci.ocs.bucket_utils import delete_all_noobaa_buckets +from ocs_ci.ocs.bucket_utils import get_nb_bucket_stores from ocs_ci.ocs.ocp import OCP logger = logging.getLogger(__name__) -class TestDefaultBackingstoreOverride(MCGTest): +@pytest.fixture(scope="class") +def allow_default_backingstore_override(request): """ - Test overriding the default noobaa backingstore + Modify the noobaa CR to allow overriding the default backingstore + """ - @pytest.fixture(scope="function") - def override_nb_default_backingstore_fixture( - self, request, mcg_obj_session, backingstore_factory - ): - """ """ + nb_ocp_obj = OCP( + kind="noobaa", + namespace=config.ENV_DATA["cluster_namespace"], + resource_name="noobaa", + ) - nb_ocp_obj = OCP( - kind="noobaa", - namespace=config.ENV_DATA["cluster_namespace"], - resource_name="noobaa", - ) - - bucketclass_ocp_obj = OCP( - kind=constants.BUCKETCLASS, - namespace=config.ENV_DATA["cluster_namespace"], - resource_name=constants.DEFAULT_NOOBAA_BUCKETCLASS, - ) + def patch_allow_manual_default_backingstore(): + """ + Patch "manualDefaultBackingStore: true" to the noobaa CR - # Add manualDefaultBackingStore: true to the noobaa CR + """ add_op = [ {"op": "add", "path": "/spec/manualDefaultBackingStore", "value": True} ] @@ -44,16 +38,59 @@ def override_nb_default_backingstore_fixture( format_type="json", ) + def finalizer(): + """ + Remove "manualDefaultBackingStore: true" from the noobaa CR + + """ + remove_op = [ + { + "op": "remove", + "path": "/spec/manualDefaultBackingStore", + } + ] + nb_ocp_obj.patch( + resource_name=constants.NOOBAA_RESOURCE_NAME, + params=json.dumps(remove_op), + format_type="json", + ) + + request.addfinalizer(finalizer) + patch_allow_manual_default_backingstore() + + +@pytest.mark.usefixtures(allow_default_backingstore_override.__name__) +class TestDefaultBackingstoreOverride(MCGTest): + """ + Test overriding the default noobaa backingstore + """ + + @pytest.fixture(scope="function") + def override_nb_default_backingstore(self, request, mcg_obj_session): + """ + Override the default noobaa backingstore to the given alternative backingstore + + """ + + bucketclass_ocp_obj = OCP( + kind=constants.BUCKETCLASS, + namespace=config.ENV_DATA["cluster_namespace"], + resource_name=constants.DEFAULT_NOOBAA_BUCKETCLASS, + ) + def override_nb_default_backingstore_implementation( mcg_obj, alternative_backingstore_name ): - """ """ + """ + 1. Update the new default resource of the admin account + 2. Patch the default bucketclass to use the new default backingstore - # Delete all the noobaa buckets - delete_all_noobaa_buckets(mcg_obj, request) + Args: + mcg_obj (MCG): An MCG object + alternative_backingstore_name (str): The name of the alternative backingstore + """ # Update the new default resource of the admin account - mcg_obj.exec_mcg_cmd( "".join( ( @@ -78,54 +115,58 @@ def override_nb_default_backingstore_implementation( ) def finalizer(): + """ + Change the default backingstore back to the original + + """ override_nb_default_backingstore_implementation( mcg_obj_session, constants.DEFAULT_NOOBAA_BACKINGSTORE ) - # Remove manualDefaultBackingStore: true to the noobaa CR - remove_op = [ - { - "op": "remove", - "path": "/spec/manualDefaultBackingStore", - } - ] - nb_ocp_obj.patch( - resource_name=constants.NOOBAA_RESOURCE_NAME, - params=json.dumps(remove_op), - format_type="json", - ) - request.addfinalizer(finalizer) return override_nb_default_backingstore_implementation - def test_default_mcg_cli_buckets_use_new_backingstore( + def test_default_buckets_backingstore( self, mcg_obj_session, backingstore_factory, bucket_factory, - override_nb_default_backingstore_fixture, + override_nb_default_backingstore, ): + """ + 1. Override the default noobaa backingstore + 2. Create a new bucket using the mcg-cli with the default config + 3. Create a new OBC using oc and yamls without specifying the bucketclass + 4. Verify the buckets' backingstore is the new default backingstore + + """ + + # Override the default noobaa backingstore alternative_backingstore = backingstore_factory( *("oc", {"aws": [(1, "eu-central-1")]}) )[0] - override_nb_default_backingstore_fixture( - mcg_obj_session, alternative_backingstore.name - ) + override_nb_default_backingstore(mcg_obj_session, alternative_backingstore.name) + # Create a new bucket using the mcg-cli with the default backingstore default_cli_bucket = bucket_factory(amount=1, interface="cli")[0] - # TODO assert that default_cli_bucket is using the new backingstore - assert default_cli_bucket + # Create a new OBC using oc and yamls without specifying the bucketclass + default_obc_bucket = bucket_factory(amount=1, interface="oc")[0] - def test_default_obcs_use_new_backingstore( - self, override_nb_default_backingstore_fixture - ): - pass + # Verify the bucket's backingstore is the new default backingstore + assert ( + get_nb_bucket_stores(mcg_obj_session, default_cli_bucket.name)[0] + == alternative_backingstore.name + ), "The default mcg-cli bucket does not use the new default backingstore!" + assert ( + get_nb_bucket_stores(mcg_obj_session, default_obc_bucket.name)[0] + == alternative_backingstore.name + ), "The default OC bucket does not use the new default backingstore!" def test_default_backingstore_override_post_upgrade(self): pass def test_bucketclass_replication_after_default_backingstore_override( - self, override_nb_default_backingstore_fixture + self, override_nb_default_backingstore ): pass