|
| 1 | +import pytest |
| 2 | +import pymongo |
| 3 | +import bson |
| 4 | +import testinfra |
| 5 | +import time |
| 6 | +import os |
| 7 | +import docker |
| 8 | + |
| 9 | +from datetime import datetime |
| 10 | +from cluster import Cluster |
| 11 | +from packaging import version |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(scope="package") |
| 15 | +def mongod_version(): |
| 16 | + return docker.from_env().containers.run( |
| 17 | + image='replica_member/local', |
| 18 | + remove=True, |
| 19 | + command='bash -c \'mongod --version | head -n1 | sed "s/db version v//"\'' |
| 20 | + ).decode("utf-8", errors="replace") |
| 21 | + |
| 22 | +@pytest.fixture(scope="package") |
| 23 | +def config(mongod_version): |
| 24 | + if version.parse(mongod_version) < version.parse("8.0.0"): |
| 25 | + pytest.skip("Unsupported version for unshardCollection") |
| 26 | + else: |
| 27 | + return { "mongos": "mongos", |
| 28 | + "configserver": |
| 29 | + {"_id": "rscfg", "members": [{"host":"rscfg01"},{"host": "rscfg02"},{"host": "rscfg03" }]}, |
| 30 | + "shards":[ |
| 31 | + {"_id": "rs1", "members": [{"host":"rs101"},{"host": "rs102"},{"host": "rs103" }]}, |
| 32 | + {"_id": "rs2", "members": [{"host":"rs201"},{"host": "rs202"},{"host": "rs203" }]} |
| 33 | + ]} |
| 34 | + |
| 35 | +@pytest.fixture(scope="package") |
| 36 | +def cluster(config): |
| 37 | + return Cluster(config) |
| 38 | + |
| 39 | +@pytest.fixture(scope="function") |
| 40 | +def start_cluster(cluster,request): |
| 41 | + try: |
| 42 | + cluster.destroy() |
| 43 | + os.chmod("/backups",0o777) |
| 44 | + os.system("rm -rf /backups/*") |
| 45 | + cluster.create() |
| 46 | + client=pymongo.MongoClient(cluster.connection) |
| 47 | + client.admin.command("enableSharding", "test") |
| 48 | + client.admin.command("shardCollection", "test.test", key={"_id": "hashed"}) |
| 49 | + cluster.setup_pbm() |
| 50 | + result = cluster.exec_pbm_cli("config --set storage.type=filesystem --set storage.filesystem.path=/backups --set backup.compression=none") |
| 51 | + assert result.rc == 0 |
| 52 | + Cluster.log("Setup PBM with fs storage:\n" + result.stdout) |
| 53 | + yield True |
| 54 | + finally: |
| 55 | + if request.config.getoption("--verbose"): |
| 56 | + cluster.get_logs() |
| 57 | + cluster.destroy(cleanup_backups=True) |
| 58 | + |
| 59 | +@pytest.mark.timeout(900,func_only=True) |
| 60 | +def test_logical_PBM_T264(start_cluster,cluster): |
| 61 | + cluster.check_pbm_status() |
| 62 | + client=pymongo.MongoClient(cluster.connection) |
| 63 | + for i in range(600): |
| 64 | + client['test']['test'].insert_one({"doc":i}) |
| 65 | + |
| 66 | + cluster.make_backup('logical') |
| 67 | + cluster.enable_pitr(pitr_extra_args="--set pitr.oplogSpanMin=0.5") |
| 68 | + time.sleep(10) |
| 69 | + Cluster.log("Start unsharding collection test.test") |
| 70 | + result=client.admin.command({'unshardCollection': "test.test", 'toShard': "rs2"}) |
| 71 | + Cluster.log(result) |
| 72 | + time.sleep(10) |
| 73 | + assert not pymongo.MongoClient(cluster.connection)["test"].command("collstats", "test").get("sharded", False) |
| 74 | + pitr = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S") |
| 75 | + Cluster.log("Time for PITR is: " + pitr) |
| 76 | + pitr_backup="--time=" + pitr |
| 77 | + time.sleep(60) |
| 78 | + pymongo.MongoClient(cluster.connection).drop_database('test') |
| 79 | + cluster.make_restore(pitr_backup,check_pbm_status=True,make_resync=False) |
| 80 | + assert pymongo.MongoClient(cluster.connection)["test"]["test"].count_documents({}) == 600 |
| 81 | + assert not pymongo.MongoClient(cluster.connection)["test"].command("collstats", "test").get("sharded", False) |
| 82 | + |
0 commit comments