Skip to content

Commit

Permalink
chore: Finalized integration tests for db migration
Browse files Browse the repository at this point in the history
  • Loading branch information
alithethird committed Sep 23, 2024
1 parent 43e58b4 commit 3e997e8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 110 deletions.
Binary file added tests/integration/mock_db
Binary file not shown.
163 changes: 53 additions & 110 deletions tests/integration/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# See LICENSE file for licensing details.
"""Discourse integration tests."""

import json
import logging
import re
from datetime import datetime, timedelta
Expand All @@ -18,7 +17,7 @@
from ops.model import ActiveStatus, WaitingStatus
from pytest_operator.plugin import Model, OpsTest

from charm import DISCOURSE_PATH, PROMETHEUS_PORT
from charm import PROMETHEUS_PORT

from . import types

Expand Down Expand Up @@ -81,8 +80,12 @@ async def test_setup_discourse(

@pytest.mark.asyncio
@pytest.mark.abort_on_fail
async def test_db_migration(model: Model, ops_test: OpsTest, run_action):

async def test_db_migration(model: Model, ops_test: OpsTest, pytestconfig: Config, run_action):
"""
arrange: preload postgres with a mock db
act: deploy and integrate with discourse
assert: discourse must be active idle
"""
postgres_app = await model.deploy(
"postgresql-k8s",
channel="14/stable",
Expand All @@ -92,128 +95,68 @@ async def test_db_migration(model: Model, ops_test: OpsTest, run_action):
)
async with ops_test.fast_forward():
await model.wait_for_idle(apps=[postgres_app.name], status="active")

redis_app = await model.deploy("redis-k8s", series="jammy", channel="latest/edge")
await model.wait_for_idle(apps=[redis_app.name], status="active")

await model.deploy("nginx-ingress-integrator", series="focal", trust=True)
app_name = "discourse-k8s"
discourse_app = await model.deploy(
app_name,
channel="latest/edge",
revision=162,
series="focal",
resources={"discourse-image": "152"},
)
await model.wait_for_idle(apps=[app_name], status="waiting")

logger.info("Deployed discourse ")
# configure postgres
await postgres_app.set_config(
{
"plugin_hstore_enable": "true",
"plugin_pg_trgm_enable": "true",
}
)
logger.info("Configured postgresql")
await model.wait_for_idle(apps=[postgres_app.name], status="active")
db_pass = await run_action(postgres_app.name, "get-password", username="operator")
db_pass = db_pass["password"]
return_code, _, _ = await ops_test.juju(
"scp",
"--container",
# postgres_app.name,
"postgresql",
"./mock_db",
f"{postgres_app.units[0].name}:.",
)
assert return_code == 0

logger.info("Checking discourse status")
# Add required relations
unit = model.applications[app_name].units[0]
assert unit.workload_status == WaitingStatus.name # type: ignore
logger.info("Adding relations")
await model.add_relation(app_name, "postgresql-k8s:database")
await model.add_relation(app_name, "redis-k8s")
await model.add_relation(app_name, "nginx-ingress-integrator")
logger.info("Added relations")
await model.wait_for_idle(apps=[app_name], status="active")
# import pdb; pdb.set_trace()
logger.info("Create mock database")

# Create mock database
# Get the api key first
return_code, api_key, _ = await ops_test.juju(
return_code, _, _ = await ops_test.juju(
"ssh",
"--container",
"discourse",
unit.name,
f"pebble exec --user=_daemon_ --context=discourse -w={DISCOURSE_PATH} -- {DISCOURSE_PATH}/bin/bundle exec rake api_key:create_master['api key description']",
"postgresql",
postgres_app.units[0].name,
"createdb -h localhost -U operator --password discourse",
stdin=str.encode(f"{db_pass}\n"),
)
api_key = api_key.strip()
# create admin user
user_1_pass = await run_action(app_name, "create-user", email="email@example.com", admin=True)
logger.info("email password: %s", user_1_pass)
logger.info("api_key: %s",api_key )
return_code, invite_output, _ = await ops_test.juju(
assert return_code == 0

return_code, _, _ = await ops_test.juju(
"ssh",
"--container",
"discourse",
unit.name,
f"pebble exec --user=_daemon_ --context=discourse -w={DISCOURSE_PATH} -- {DISCOURSE_PATH}/bin/bundle exec rake admin:invite['email2@example.com']",
"postgresql",
postgres_app.units[0].name,
"pg_restore -h localhost -U operator\
--password -d discourse\
--no-owner --clean --if-exists ./mock_db",
stdin=str.encode(f"{db_pass}\n"),
)
logger.info("invite output: %s", invite_output)

# import pdb; pdb.set_trace()
# await run_action(app_name, "create-user", email="email2@example.com", admin=True)
# create regular user
header = {"Api-Key": api_key, "Api-Username": "email", "Host": "discourse-k8s"}
# user_json = {
# "name": "email2",
# "email": "email2@example.com",
# "password": "Strong.Password",
# "username": "email2",
# "active": False,
# "approved": False,
# }
# user_result = requests.post("http://127.0.0.1/users.json", json=user_json, headers=header)
# logger.warning(user_result.text)
# user_id = json.loads(user_result.text)["user_id"]
# new_res = requests.put(f"http://127.0.0.1/admin/users/{user_id}/activate.json", json={"id": user_id}, headers=header)
# logger.info("new_res : %s",new_res.text )
# import pdb; pdb.set_trace()
# create a topic
example_topic = {
"title": "test topic for integration tests",
"category": 4,
"category_id": 4,
"raw": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"id": 12,
}
# header["Api-Username"] = "email2"
post_res = requests.post("http://127.0.0.1/posts.json", json=example_topic, headers=header)
post_json = json.loads(post_res.text)
logger.info(post_json)
# import pdb; pdb.set_trace()
reply_obj = {
"raw": "https://github.com/canonical/discourse-k8s-operator/pull/287",
"topic_id": post_json["topic_id"],
}
post_res = requests.post("http://127.0.0.1/posts.json", json=reply_obj, headers=header)
# import pdb ;pdb.set_trace()
header["Api-Username"] = "email2"
reply_obj = {
"raw": """"
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque eu vehicula eros. Aliquam id velit ac libero rutrum vulputate. Fusce at vehicula justo. Cras vulputate aliquet nisi in viverra. Nullam vitae libero tincidunt, feugiat mi ac, condimentum lacus. Nulla quam nisi, sodales quis tempor eu, blandit at justo. Proin semper finibus accumsan. Aliquam non nunc at dolor dapibus luctus id a leo.
Fusce mollis, augue quis pharetra porttitor, ante felis euismod ante, at dignissim enim felis ac sem. Praesent at purus faucibus, tincidunt arcu eget, dapibus lectus. Etiam scelerisque pulvinar enim ut blandit. Morbi sed nibh et nisi convallis fermentum. Nullam nec ultricies arcu, et imperdiet sapien. Aliquam mauris ipsum, blandit id semper in, tincidunt sed quam. Fusce sollicitudin sollicitudin elit et condimentum. Duis tempus ligula arcu, eu fringilla eros placerat sit amet. Donec tempus enim et finibus rutrum. Vestibulum ac erat ac dolor maximus accumsan. Nulla aliquam diam non felis feugiat dictum.
Cras hendrerit odio arcu, nec ultrices lectus tincidunt in. Nam gravida viverra mi eu accumsan. Sed luctus tellus posuere mi venenatis, ac elementum purus porttitor. Ut vel massa dui. Duis auctor sapien in aliquet ultricies. Maecenas quis nibh id metus porta tempor. Sed imperdiet erat massa, non consectetur velit aliquam a. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vestibulum tortor leo, vel congue ex ullamcorper venenatis. Donec ipsum ex, ultrices sed congue et, ornare non purus. Nulla ornare eget metus in suscipit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Donec aliquet ornare est, eu posuere ligula. Nunc at viverra turpis. Maecenas sed volutpat augue, ut mollis nunc. Nam quis felis ac enim blandit aliquet. Duis facilisis enim id erat vehicula, nec tempor est blandit. Quisque eu nulla non nibh consequat porta.
Aliquam in elementum tortor. Sed purus magna, vulputate eu lacus sit amet, dapibus gravida enim. Maecenas at orci vel erat accumsan feugiat sit amet non ante. Aliquam imperdiet, augue sed volutpat efficitur, massa arcu tempus sapien, eget iaculis ipsum lorem at mauris. Fusce ligula risus, condimentum quis odio ut, maximus viverra mi. Maecenas eget lectus porttitor dui commodo ultricies. Mauris aliquam, turpis ut facilisis mattis, orci felis aliquam erat, eget posuere nisl metus sed orci. Vivamus tellus dolor, sodales dapibus urna vitae, venenatis rutrum erat. Cras efficitur tempor tortor, non dictum dui facilisis eu. Morbi nec turpis eu neque aliquet bibendum id at erat. Nullam lacinia maximus consectetur. Fusce finibus arcu eu luctus auctor. Aenean porttitor in turpis dignissim auctor. Proin finibus non justo in efficitur.
""",
"topic_id": post_json["topic_id"],
}
post_res = requests.post("http://127.0.0.1/posts.json", json=reply_obj, headers=header)
assert return_code == 0
redis_app = await model.deploy("redis-k8s", series="jammy", channel="latest/edge")
await model.wait_for_idle(apps=[redis_app.name], status="active")

resources = {"discourse-image": "localhost:32000/discourse-rock:1.1"}
charm_path = await ops_test.build_charm(".")
await discourse_app.refresh(path=charm_path, resources=resources)
# import pdb; pdb.set_trace()
assert True, "TODO"
resources = {"discourse-image": pytestconfig.getoption("--discourse-image")}
charm = await ops_test.build_charm(".")
await model.deploy("nginx-ingress-integrator", series="focal", trust=True)
app_name = "discourse-k8s"
discourse_application = await model.deploy(
charm,
resources=resources,
application_name=app_name,
series="focal",
)
await model.wait_for_idle(apps=[app_name], status="waiting")
# Add required relations
unit = discourse_application.units[0]
assert unit.workload_status == WaitingStatus.name # type: ignore
await model.add_relation(app_name, "postgresql-k8s:database")
await model.add_relation(app_name, "redis-k8s")
await model.add_relation(app_name, "nginx-ingress-integrator")
await model.wait_for_idle(apps=[app_name], status="active", raise_on_error=True)


@pytest.mark.asyncio
Expand Down

0 comments on commit 3e997e8

Please sign in to comment.