From 31e80d0a873346c94e4bf8d2e6d514cc0437185a Mon Sep 17 00:00:00 2001 From: Oleksandr Havryliak Date: Mon, 9 Sep 2024 13:49:05 +0300 Subject: [PATCH] PSMDB-1482 add the test with unshardCollection --- pbm-functional/pytest/example_cluster.py | 7 +- pbm-functional/pytest/test_PBM-1386.py | 82 ++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 pbm-functional/pytest/test_PBM-1386.py diff --git a/pbm-functional/pytest/example_cluster.py b/pbm-functional/pytest/example_cluster.py index 58408e3b..fe728846 100644 --- a/pbm-functional/pytest/example_cluster.py +++ b/pbm-functional/pytest/example_cluster.py @@ -11,7 +11,10 @@ {"_id": "rs2", "members": [{"host": "rs201"}, {"host": "rs202"}, {"host": "rs203"}]} ]} -cluster = Cluster(config) +#mongod_extra_args = " --setParameter=perconaTelemetryGracePeriod=2 --setParameter=perconaTelemetryScrapeInterval=5" +mongod_extra_args = " " + +cluster = Cluster(config,mongod_extra_args=mongod_extra_args) def handler(signum,frame): cluster.destroy() @@ -19,7 +22,7 @@ def handler(signum,frame): cluster.destroy() cluster.create() -cluster.setup_pbm() +#cluster.setup_pbm() signal.signal(signal.SIGINT,handler) print("\nCluster is prepared and ready to use") diff --git a/pbm-functional/pytest/test_PBM-1386.py b/pbm-functional/pytest/test_PBM-1386.py new file mode 100644 index 00000000..ada4dcf0 --- /dev/null +++ b/pbm-functional/pytest/test_PBM-1386.py @@ -0,0 +1,82 @@ +import pytest +import pymongo +import bson +import testinfra +import time +import os +import docker + +from datetime import datetime +from cluster import Cluster +from packaging import version + + +@pytest.fixture(scope="package") +def mongod_version(): + return docker.from_env().containers.run( + image='replica_member/local', + remove=True, + command='bash -c \'mongod --version | head -n1 | sed "s/db version v//"\'' + ).decode("utf-8", errors="replace") + +@pytest.fixture(scope="package") +def config(mongod_version): + if version.parse(mongod_version) < version.parse("8.0.0"): + pytest.skip("Unsupported version for unshardCollection") + else: + return { "mongos": "mongos", + "configserver": + {"_id": "rscfg", "members": [{"host":"rscfg01"},{"host": "rscfg02"},{"host": "rscfg03" }]}, + "shards":[ + {"_id": "rs1", "members": [{"host":"rs101"},{"host": "rs102"},{"host": "rs103" }]}, + {"_id": "rs2", "members": [{"host":"rs201"},{"host": "rs202"},{"host": "rs203" }]} + ]} + +@pytest.fixture(scope="package") +def cluster(config): + return Cluster(config) + +@pytest.fixture(scope="function") +def start_cluster(cluster,request): + try: + cluster.destroy() + os.chmod("/backups",0o777) + os.system("rm -rf /backups/*") + cluster.create() + client=pymongo.MongoClient(cluster.connection) + client.admin.command("enableSharding", "test") + client.admin.command("shardCollection", "test.test", key={"_id": "hashed"}) + cluster.setup_pbm() + result = cluster.exec_pbm_cli("config --set storage.type=filesystem --set storage.filesystem.path=/backups --set backup.compression=none") + assert result.rc == 0 + Cluster.log("Setup PBM with fs storage:\n" + result.stdout) + yield True + finally: + if request.config.getoption("--verbose"): + cluster.get_logs() + cluster.destroy(cleanup_backups=True) + +@pytest.mark.timeout(900,func_only=True) +def test_logical_PBM_T264(start_cluster,cluster): + cluster.check_pbm_status() + client=pymongo.MongoClient(cluster.connection) + for i in range(600): + client['test']['test'].insert_one({"doc":i}) + + cluster.make_backup('logical') + cluster.enable_pitr(pitr_extra_args="--set pitr.oplogSpanMin=0.5") + time.sleep(10) + Cluster.log("Start unsharding collection test.test") + result=client.admin.command({'unshardCollection': "test.test", 'toShard': "rs2"}) + Cluster.log(result) + time.sleep(10) + assert not pymongo.MongoClient(cluster.connection)["test"].command("collstats", "test").get("sharded", False) + pitr = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S") + Cluster.log("Time for PITR is: " + pitr) + pitr_backup="--time=" + pitr + time.sleep(60) + pymongo.MongoClient(cluster.connection).drop_database('test') + cluster.make_restore(pitr_backup,check_pbm_status=True,make_resync=False) + assert pymongo.MongoClient(cluster.connection)["test"]["test"].count_documents({}) == 600 + assert not pymongo.MongoClient(cluster.connection)["test"].command("collstats", "test").get("sharded", False) +