Skip to content
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

Fix/linting #33

Merged
merged 13 commits into from
Feb 2, 2023
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ copyright-regexp = "Copyright\\s\\d{4}([-,]\\d{4})*\\s+%(author)s"
ignore_missing_imports = true
explicit_package_bases = true
namespace_packages = true

[tool.pylint.SIMILARITIES]
min-similarity-lines=10
merkata marked this conversation as resolved.
Show resolved Hide resolved
21 changes: 13 additions & 8 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ def __init__(self, *args):
self.framework.observe(self.on.discourse_pebble_ready, self._config_changed)
self.framework.observe(self.on.config_changed, self._config_changed)

self.db = pgsql.PostgreSQLClient(self, "db")
self.db_client = pgsql.PostgreSQLClient(self, "db")
self.framework.observe(
self.db.on.database_relation_joined, self._on_database_relation_joined
self.db_client.on.database_relation_joined, self._on_database_relation_joined
)
self.framework.observe(self.db.on.master_changed, self._on_database_changed)
self.framework.observe(self.db_client.on.master_changed, self._on_database_changed)
self.framework.observe(self.on.add_admin_user_action, self._on_add_admin_user_action)

self.redis = RedisRequires(self, self._stored)
Expand Down Expand Up @@ -162,7 +162,9 @@ def _get_saml_config(self) -> Dict[str, Any]:
Dictionary with the SAML configuration settings..
"""
saml_fingerprints = {
"https://login.ubuntu.com/+saml": "32:15:20:9F:A4:3C:8E:3E:8E:47:72:62:9A:86:8D:0E:E6:CF:45:D5"
"https://login.ubuntu.com/+saml": (
"32:15:20:9F:A4:3C:8E:3E:8E:47:" "72:62:9A:86:8D:0E:E6:CF:45:D5"
)
}
saml_config = {}

Expand Down Expand Up @@ -309,7 +311,9 @@ def _create_layer_config(self) -> Dict[str, Any]:
return layer_config

def _should_run_setup(self, current_plan: Dict, s3info: Optional[S3Info]) -> bool:
"""Determine if the setup script is to be run based on the current plan and the new S3 settings.
"""Determine if the setup script is to be run.

This is based on the current plan and the new S3 settings.

Args:
current_plan: Dictionary containing the current plan.
Expand Down Expand Up @@ -397,8 +401,8 @@ def _config_changed(self, event: HookEvent) -> None:
try:
stdout, _ = process.wait_output()
logger.debug("%s stdout: %s", script, stdout)
except ExecError as e:
logger.exception("%s command exited with code %d.", script, e.exit_code)
except ExecError as cmd_err:
logger.exception("%s command exited with code %d.", script, cmd_err.exit_code)
raise

# Then start the service
Expand Down Expand Up @@ -461,7 +465,8 @@ def _on_add_admin_user_action(self, event: ActionEvent) -> None:
[
"bash",
"-c",
f"./bin/bundle exec rake admin:create <<< $'{email}\n{password}\n{password}\nY'",
"./bin/bundle exec rake admin:create",
f"<<< $'{email}\n{password}\n{password}\nY'",
],
user="discourse",
working_dir=DISCOURSE_PATH,
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Copyright 2022 Canonical Ltd.
# See LICENSE file for licensing details.
"""Module for test customizations."""


def pytest_addoption(parser):
"""Adds parser switches."""
parser.addoption("--discourse-image", action="store")
parser.addoption("--s3-url", action="store")
18 changes: 10 additions & 8 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2022 Canonical Ltd.
# See LICENSE file for licensing details.
"""Discourse integration tests fixtures."""

import asyncio
import logging
Expand All @@ -15,20 +16,20 @@
logger = logging.getLogger(__name__)


@fixture(scope="module")
def metadata():
@fixture(scope="module", name="metadata")
def fixture_metadata():
"""Provides charm metadata."""
yield yaml.safe_load(Path("./metadata.yaml").read_text())
yield yaml.safe_load(Path("./metadata.yaml").read_text(encoding="UTF-8"))


@fixture(scope="module")
def app_name(metadata):
@fixture(scope="module", name="app_name")
def fixture_app_name(metadata):
"""Provides app name from the metadata."""
yield metadata["name"]


@fixture(scope="module")
def app_config():
@fixture(scope="module", name="app_config")
def fixture_app_config():
"""Provides app config."""
yield {
"developer_emails": "noreply@canonical.com",
Expand Down Expand Up @@ -76,7 +77,8 @@ async def app(ops_test: OpsTest, app_name: str, app_config: Dict[str, str], pyte
await ops_test.model.wait_for_idle()

# Add required relations
assert ops_test.model.applications[app_name].units[0].workload_status == WaitingStatus.name # type: ignore
unit = ops_test.model.applications[app_name].units[0]
assert unit.workload_status == WaitingStatus.name # type: ignore
await asyncio.gather(
ops_test.model.add_relation(app_name, "postgresql-k8s:db-admin"),
ops_test.model.add_relation(app_name, "redis-k8s"),
Expand Down
1 change: 1 addition & 0 deletions tests/integration/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
# Copyright 2022 Canonical Ltd.
# See LICENSE file for licensing details.
"""Helper functions for Discourse charm tests."""

import itertools
from collections import namedtuple
Expand Down
142 changes: 85 additions & 57 deletions tests/integration/test_charm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
# Copyright 2022 Canonical Ltd.
# See LICENSE file for licensing details.
"""Discourse integration tests."""

import json
import logging
Expand Down Expand Up @@ -107,27 +108,27 @@ async def test_setup_discourse(

# Get the form info
assert parsed_registration.body
utf8 = parsed_registration.body.find("input", attrs={"name": "utf8"}).get("value") # type: ignore
authenticity_token = parsed_registration.body.find("input", attrs={"name": "authenticity_token"}).get("value") # type: ignore
form_fields = {
"utf8": utf8,
"authenticity_token": authenticity_token,
"username": "admin",
"email": app_config["developer_emails"],
"password": "MyLovelySecurePassword2022!",
}

form_headers = {
"Host": f"{app_config['external_hostname']}",
"Content-Type": "application/x-www-form-urlencoded",
}

logger.info("Submitting registration form")

response = session.post(
f"{discourse_url}/finish-installation/register",
headers=form_headers,
data=urlencode(form_fields),
headers={
"Host": f"{app_config['external_hostname']}",
"Content-Type": "application/x-www-form-urlencoded",
},
data=urlencode(
{
"utf8": parsed_registration.body.find(
"input", attrs={"name": "utf8"} # type: ignore
).get("value"),
"authenticity_token": parsed_registration.body.find(
"input", attrs={"name": "authenticity_token"} # type: ignore
).get("value"),
"username": "admin",
"email": app_config["developer_emails"],
"password": "MyLovelySecurePassword2022!",
}
),
allow_redirects=False,
timeout=requests_timeout,
)
Expand Down Expand Up @@ -170,25 +171,28 @@ async def test_setup_discourse(
)

assert response.status_code == 200
parsed_challenge = response.json()

assert parsed_validation.body
authenticity_token = parsed_validation.body.find("input", attrs={"name": "authenticity_token"}).get("value") # type: ignore
form_fields = {
"_method": "put",
"authenticity_token": authenticity_token,
"password_confirmation": parsed_challenge["value"],
"authenticity_token": parsed_validation.body.find(
"input", attrs={"name": "authenticity_token"} # type: ignore
).get("value"),
"password_confirmation": response.json()["value"],
# The challenge string is reversed see
# https://github.com/discourse/discourse/blob/main/app/assets/javascripts/discourse/scripts/activate-account.js
"challenge": parsed_challenge["challenge"][::-1],
"challenge": response.json()["challenge"][::-1],
}

logger.info("Submitting account validation form")

# Submit the activation of the account
response = session.post(
f"{discourse_url}/u/activate-account/{email_token}",
headers=form_headers,
headers={
"Host": f"{app_config['external_hostname']}",
"Content-Type": "application/x-www-form-urlencoded",
},
data=urlencode(form_fields),
allow_redirects=False,
timeout=requests_timeout,
Expand All @@ -212,13 +216,13 @@ async def test_setup_discourse(
# Extract the CSRF token
parsed_admin: BeautifulSoup = BeautifulSoup(response.content, features="html.parser")
assert parsed_admin.head
csrf_token = parsed_admin.head.find("meta", attrs={"name": "csrf-token"}).get("content") # type: ignore
csrf_token = parsed_admin.head.find("meta", attrs={"name": "csrf-token"}).get( # type: ignore
"content"
)
merkata marked this conversation as resolved.
Show resolved Hide resolved

logger.info("Getting admin API key")

# Finally create an API Key, which will be used on the next integration tests
merkata marked this conversation as resolved.
Show resolved Hide resolved
api_key_payload = {"key": {"description": "Key to The Batmobile", "username": "admin"}}

response = session.post(
f"{discourse_url}/admin/api/keys",
headers={
Expand All @@ -227,14 +231,13 @@ async def test_setup_discourse(
"X-CSRF-Token": csrf_token, # type: ignore
"Content-Type": "application/json",
},
data=json.dumps(api_key_payload),
data=json.dumps({"key": {"description": "Key to The Batmobile", "username": "admin"}}),
timeout=requests_timeout,
)

assert response.status_code == 200

parsed_key = response.json()
logger.info("Admin API Key: %s", {parsed_key["key"]["key"]})
logger.info("Admin API Key: %s", {response.json()["key"]["key"]})


@pytest.mark.asyncio
Expand All @@ -246,28 +249,15 @@ async def test_s3_conf(ops_test: OpsTest, app: Application, s3_url: str):
This test requires a localstack deployed
"""

# Localstack doesn't require any specific value there, any random string will work
config_s3_bucket = {"access-key": "my-lovely-key", "secret-key": "this-is-very-secret"}

# Localstack enforce to use this domain and it resolves to localhost
s3_domain = "localhost.localstack.cloud"
s3_bucket = "tests"
s3_region = "us-east-1"

# Parse URL to get the IP address and the port, and compose the required variables
parsed_s3_url = urlparse(s3_url)
s3_ip_address = parsed_s3_url.hostname
s3_endpoint = f"{parsed_s3_url.scheme}://{s3_domain}"
if parsed_s3_url:
s3_endpoint = f"{s3_endpoint}:{parsed_s3_url.port}"
s3_conf: Dict = generate_s3_config(s3_url)

logger.info("Updating discourse hosts")

# Discourse S3 client uses subdomain bucket routing,
# I need to inject subdomain in the DNS (not needed if everything runs localhost)
# Application actually does have units
action = await app.units[0].run( # type: ignore
f'echo "{s3_ip_address} {s3_bucket}.{s3_domain}" >> /etc/hosts'
f'echo "{s3_conf["ip_address"]} {s3_conf["bucket"]}.{s3_conf["domain"]}" >> /etc/hosts'
)
result = await action.wait()
assert result.results.get("return-code") == 0, "Can't inject S3 IP in Discourse hosts"
Expand All @@ -279,12 +269,12 @@ async def test_s3_conf(ops_test: OpsTest, app: Application, s3_url: str):
{
"s3_enabled": "true",
# The final URL is computed by discourse, we need to pass the main URL
"s3_endpoint": s3_endpoint,
"s3_bucket": s3_bucket,
"s3_secret_access_key": config_s3_bucket["secret-key"],
"s3_access_key_id": config_s3_bucket["access-key"],
"s3_endpoint": s3_conf["endpoint"],
"s3_bucket": s3_conf["bucket"],
"s3_secret_access_key": s3_conf["credentials"]["secret-key"],
"s3_access_key_id": s3_conf["credentials"]["access-key"],
# Default localstack region
"s3_region": s3_region,
"s3_region": s3_conf["region"],
}
)
assert ops_test.model
Expand All @@ -294,14 +284,14 @@ async def test_s3_conf(ops_test: OpsTest, app: Application, s3_url: str):

# Configuration for boto client
s3_client_config = Config(
region_name=s3_region,
region_name=s3_conf["region"],
s3={
"addressing_style": "virtual",
},
)

# Trick to use when localstack is deployed on another location than locally
if s3_ip_address != "127.0.0.1":
if s3_conf["ip_address"] != "127.0.0.1":
proxy_definition = {
"http": s3_url,
}
Expand All @@ -314,10 +304,10 @@ async def test_s3_conf(ops_test: OpsTest, app: Application, s3_url: str):
# Configure the boto client
s3_client = client(
"s3",
s3_region,
aws_access_key_id=config_s3_bucket["access-key"],
aws_secret_access_key=config_s3_bucket["secret-key"],
endpoint_url=s3_endpoint,
s3_conf["region"],
aws_access_key_id=s3_conf["credentials"]["access-key"],
aws_secret_access_key=s3_conf["credentials"]["secret-key"],
endpoint_url=s3_conf["endpoint"],
use_ssl=False,
config=s3_client_config,
)
Expand All @@ -326,10 +316,48 @@ async def test_s3_conf(ops_test: OpsTest, app: Application, s3_url: str):
response = s3_client.list_buckets()
bucket_list = [*map(lambda a: a["Name"], response["Buckets"])]

assert s3_bucket in bucket_list
assert s3_conf["bucket"] in bucket_list

# Check content has been uploaded in the bucket
response = s3_client.list_objects(Bucket=s3_bucket)
response = s3_client.list_objects(Bucket=s3_conf["bucket"])
object_count = sum(1 for _ in response["Contents"])

assert object_count > 0

# Cleanup
await app.set_config( # type: ignore
{
"s3_enabled": "false",
# The final URL is computed by discourse, we need to pass the main URL
"s3_endpoint": "",
"s3_bucket": "",
"s3_secret_access_key": "",
"s3_access_key_id": "",
# Default localstack region
"s3_region": "",
}
)
assert ops_test.model
await ops_test.model.wait_for_idle(status="active")


def generate_s3_config(s3_url: str) -> Dict:
"""Generate an S3 config for localstack based test."""
s3_config: Dict = {
# Localstack doesn't require any specific value there, any random string will work
"credentials": {"access-key": "my-lovely-key", "secret-key": "this-is-very-secret"},
# Localstack enforce to use this domain and it resolves to localhost
"domain": "localhost.localstack.cloud",
"bucket": "tests",
"region": "us-east-1",
}

# Parse URL to get the IP address and the port, and compose the required variables
parsed_s3_url = urlparse(s3_url)
s3_ip_address = parsed_s3_url.hostname
s3_endpoint = f"{parsed_s3_url.scheme}://{s3_config['domain']}"
if parsed_s3_url:
s3_endpoint = f"{s3_endpoint}:{parsed_s3_url.port}"
s3_config["ip_address"] = s3_ip_address
s3_config["endpoint"] = s3_endpoint
return s3_config
5 changes: 1 addition & 4 deletions tests/unit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# Copyright 2022 Canonical Ltd.
# See LICENSE file for licensing details.

import ops.testing

ops.testing.SIMULATE_CAN_CONNECT = True
"""Ops testing settings."""
Loading