-
Notifications
You must be signed in to change notification settings - Fork 14
[DPE-2777] Basic sharding int test #279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7cc9ae2
sainity int test for sharding
MiaAltieri b7d9e9c
fmt +lint
MiaAltieri 144904a
restructure tests
MiaAltieri 998766e
decrease load on CI and increase timeout
MiaAltieri aa80378
do not proceed if relation fails
MiaAltieri 6750d59
Merge branch '6/edge' into basic-sharding-int-test
MiaAltieri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright 2023 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
from urllib.parse import quote_plus | ||
|
||
from pymongo import MongoClient | ||
from pytest_operator.plugin import OpsTest | ||
|
||
from ..helpers import get_password | ||
|
||
MONGOS_PORT = 27018 | ||
MONGOD_PORT = 27017 | ||
|
||
|
||
async def generate_mongodb_client(ops_test: OpsTest, app_name: str, mongos: bool): | ||
"""Returns a MongoDB client for mongos/mongod.""" | ||
hosts = [unit.public_address for unit in ops_test.model.applications[app_name].units] | ||
password = await get_password(ops_test, app_name) | ||
port = MONGOS_PORT if mongos else MONGOD_PORT | ||
hosts = [f"{host}:{port}" for host in hosts] | ||
hosts = ",".join(hosts) | ||
auth_source = "" | ||
database = "admin" | ||
|
||
return MongoClient( | ||
f"mongodb://operator:" | ||
f"{quote_plus(password)}@" | ||
f"{hosts}/{quote_plus(database)}?" | ||
f"{auth_source}" | ||
) | ||
|
||
|
||
def write_data_to_mongodb(client, db_name, coll_name, content) -> None: | ||
"""Writes data to the provided collection and database.""" | ||
db = client[db_name] | ||
horses_collection = db[coll_name] | ||
horses_collection.insert_one(content) | ||
|
||
|
||
def verify_data_mongodb(client, db_name, coll_name, key, value) -> bool: | ||
"""Checks a key/value pair for a provided collection and database.""" | ||
db = client[db_name] | ||
test_collection = db[coll_name] | ||
query = test_collection.find({}, {key: 1}) | ||
return query[0][key] == value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright 2023 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
import pytest | ||
from pytest_operator.plugin import OpsTest | ||
|
||
from .helpers import generate_mongodb_client, verify_data_mongodb, write_data_to_mongodb | ||
|
||
SHARD_ONE_APP_NAME = "shard-one" | ||
SHARD_TWO_APP_NAME = "shard-two" | ||
CONFIG_SERVER_APP_NAME = "config-server-one" | ||
SHARD_REL_NAME = "sharding" | ||
CONFIG_SERVER_REL_NAME = "config-server" | ||
MONGODB_KEYFILE_PATH = "/var/snap/charmed-mongodb/current/etc/mongod/keyFile" | ||
TIMEOUT = 15 * 60 | ||
|
||
|
||
@pytest.mark.abort_on_fail | ||
async def test_build_and_deploy(ops_test: OpsTest) -> None: | ||
"""Build and deploy a sharded cluster.""" | ||
my_charm = await ops_test.build_charm(".") | ||
await ops_test.model.deploy( | ||
my_charm, | ||
num_units=2, | ||
config={"role": "config-server"}, | ||
application_name=CONFIG_SERVER_APP_NAME, | ||
) | ||
await ops_test.model.deploy( | ||
my_charm, num_units=2, config={"role": "shard"}, application_name=SHARD_ONE_APP_NAME | ||
) | ||
await ops_test.model.deploy( | ||
my_charm, num_units=2, config={"role": "shard"}, application_name=SHARD_TWO_APP_NAME | ||
) | ||
|
||
async with ops_test.fast_forward(): | ||
await ops_test.model.wait_for_idle( | ||
apps=[CONFIG_SERVER_APP_NAME, SHARD_ONE_APP_NAME, SHARD_TWO_APP_NAME], | ||
idle_period=20, | ||
raise_on_blocked=False, | ||
timeout=TIMEOUT, | ||
) | ||
|
||
# TODO Future PR: assert that CONFIG_SERVER_APP_NAME, SHARD_ONE_APP_NAME, SHARD_TWO_APP_NAME | ||
# are blocked waiting for relaitons | ||
|
||
|
||
@pytest.mark.abort_on_fail | ||
async def test_cluster_active(ops_test: OpsTest) -> None: | ||
"""Tests the integration of cluster components works without error.""" | ||
await ops_test.model.integrate( | ||
f"{SHARD_ONE_APP_NAME}:{SHARD_REL_NAME}", | ||
f"{CONFIG_SERVER_APP_NAME}:{CONFIG_SERVER_REL_NAME}", | ||
) | ||
await ops_test.model.integrate( | ||
f"{SHARD_TWO_APP_NAME}:{SHARD_REL_NAME}", | ||
f"{CONFIG_SERVER_APP_NAME}:{CONFIG_SERVER_REL_NAME}", | ||
) | ||
|
||
async with ops_test.fast_forward(): | ||
await ops_test.model.wait_for_idle( | ||
apps=[CONFIG_SERVER_APP_NAME, SHARD_ONE_APP_NAME, SHARD_TWO_APP_NAME], | ||
idle_period=20, | ||
status="active", | ||
timeout=TIMEOUT, | ||
) | ||
|
||
# TODO Future PR: assert that CONFIG_SERVER_APP_NAME, SHARD_ONE_APP_NAME, SHARD_TWO_APP_NAME | ||
# have the correct active statuses. | ||
|
||
|
||
async def test_sharding(ops_test: OpsTest) -> None: | ||
"""Tests writing data to mongos gets propagated to shards.""" | ||
# write data to mongos on both shards. | ||
mongos_client = await generate_mongodb_client( | ||
ops_test, app_name=CONFIG_SERVER_APP_NAME, mongos=True | ||
) | ||
|
||
# write data to shard one | ||
write_data_to_mongodb( | ||
mongos_client, | ||
db_name="animals_database_1", | ||
coll_name="horses", | ||
content={"horse-breed": "unicorn", "real": True}, | ||
) | ||
mongos_client.admin.command("movePrimary", "animals_database_1", to=SHARD_ONE_APP_NAME) | ||
|
||
# write data to shard two | ||
write_data_to_mongodb( | ||
mongos_client, | ||
db_name="animals_database_2", | ||
coll_name="horses", | ||
content={"horse-breed": "pegasus", "real": True}, | ||
) | ||
mongos_client.admin.command("movePrimary", "animals_database_2", to=SHARD_TWO_APP_NAME) | ||
|
||
# log into shard 1 verify data | ||
shard_one_client = await generate_mongodb_client( | ||
ops_test, app_name=SHARD_ONE_APP_NAME, mongos=False | ||
) | ||
has_correct_data = verify_data_mongodb( | ||
shard_one_client, | ||
db_name="animals_database_1", | ||
coll_name="horses", | ||
key="horse-breed", | ||
value="unicorn", | ||
) | ||
assert has_correct_data, "data not written to shard-one" | ||
|
||
# log into shard 2 verify data | ||
shard_two_client = await generate_mongodb_client( | ||
ops_test, app_name=SHARD_TWO_APP_NAME, mongos=False | ||
) | ||
has_correct_data = verify_data_mongodb( | ||
shard_two_client, | ||
db_name="animals_database_2", | ||
coll_name="horses", | ||
key="horse-breed", | ||
value="pegasus", | ||
) | ||
assert has_correct_data, "data not written to shard-two" |
84 changes: 0 additions & 84 deletions
84
tests/integration/sharding_tests/test_sharding_components.py
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.