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

[DPE-6053] Fix ports #803

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
433 changes: 0 additions & 433 deletions lib/charms/observability_libs/v1/kubernetes_service_patch.py

This file was deleted.

5 changes: 1 addition & 4 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from charms.data_platform_libs.v0.data_models import TypedCharmBase
from charms.grafana_k8s.v0.grafana_dashboard import GrafanaDashboardProvider
from charms.loki_k8s.v1.loki_push_api import LogProxyConsumer
from charms.observability_libs.v1.kubernetes_service_patch import KubernetesServicePatch
from charms.postgresql_k8s.v0.postgresql import (
REQUIRED_PLUGINS,
PostgreSQL,
Expand Down Expand Up @@ -242,9 +241,7 @@ def __init__(self, *args):
relation_name="logging",
)

postgresql_db_port = ServicePort(5432, name="database")
patroni_api_port = ServicePort(8008, name="api")
self.service_patcher = KubernetesServicePatch(self, [postgresql_db_port, patroni_api_port])
self.unit.set_ports(*[5432, 8008])
self.tracing = TracingEndpointRequirer(
self, relation_name=TRACING_RELATION_NAME, protocols=[TRACING_PROTOCOL]
)
Expand Down
28 changes: 13 additions & 15 deletions tests/unit/test_async_replication.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,27 @@

@pytest.fixture(autouse=True)
def harness():
with patch("charm.KubernetesServicePatch", lambda x, y: None):
harness = Harness(PostgresqlOperatorCharm)
harness = Harness(PostgresqlOperatorCharm)

# Set up the initial relation and hooks.
harness.set_leader(True)
harness.begin()
# Set up the initial relation and hooks.
harness.set_leader(True)
harness.begin()

yield harness
harness.cleanup()
yield harness
harness.cleanup()


@pytest.fixture(autouse=True)
def standby():
with patch("charm.KubernetesServicePatch", lambda x, y: None):
harness = Harness(PostgresqlOperatorCharm)
harness.set_model_name("standby")
harness = Harness(PostgresqlOperatorCharm)
harness.set_model_name("standby")

# Set up the initial relation and hooks.
harness.set_leader(True)
harness.begin()
# Set up the initial relation and hooks.
harness.set_leader(True)
harness.begin()

yield harness
harness.cleanup()
yield harness
harness.cleanup()


@pytest.mark.parametrize("relation_name", RELATION_NAMES)
Expand Down
25 changes: 12 additions & 13 deletions tests/unit/test_backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,18 @@

@pytest.fixture(autouse=True)
def harness():
with patch("charm.KubernetesServicePatch", lambda x, y: None):
# Mock generic sync client to avoid search to ~/.kube/config.
patcher = patch("lightkube.core.client.GenericSyncClient")
patcher.start()

harness = Harness(PostgresqlOperatorCharm)

# Set up the initial relation and hooks.
peer_rel_id = harness.add_relation(PEER, "postgresql-k8s")
harness.add_relation_unit(peer_rel_id, "postgresql-k8s/0")
harness.begin()
yield harness
harness.cleanup()
# Mock generic sync client to avoid search to ~/.kube/config.
patcher = patch("lightkube.core.client.GenericSyncClient")
patcher.start()

harness = Harness(PostgresqlOperatorCharm)

# Set up the initial relation and hooks.
peer_rel_id = harness.add_relation(PEER, "postgresql-k8s")
harness.add_relation_unit(peer_rel_id, "postgresql-k8s/0")
harness.begin()
yield harness
harness.cleanup()


def test_stanza_name(harness):
Expand Down
17 changes: 8 additions & 9 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,14 @@

@pytest.fixture(autouse=True)
def harness():
with patch("charm.KubernetesServicePatch", lambda x, y: None):
harness = Harness(PostgresqlOperatorCharm)
harness.handle_exec("postgresql", ["locale", "-a"], result="C")

harness.add_relation(PEER, "postgresql-k8s")
harness.begin()
harness.add_relation("restart", harness.charm.app.name)
yield harness
harness.cleanup()
harness = Harness(PostgresqlOperatorCharm)
harness.handle_exec("postgresql", ["locale", "-a"], result="C")

harness.add_relation(PEER, "postgresql-k8s")
harness.begin()
harness.add_relation("restart", harness.charm.app.name)
yield harness
harness.cleanup()


def test_on_leader_elected(harness):
Expand Down
132 changes: 65 additions & 67 deletions tests/unit/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,25 @@

@pytest.fixture(autouse=True)
def harness():
with patch("charm.KubernetesServicePatch", lambda x, y: None):
harness = Harness(PostgresqlOperatorCharm)

# Set up the initial relation and hooks.
harness.set_leader(True)
harness.begin()

# Define some relations.
rel_id = harness.add_relation(RELATION_NAME, "application")
harness.add_relation_unit(rel_id, "application/0")
peer_rel_id = harness.add_relation(PEER, harness.charm.app.name)
harness.add_relation_unit(peer_rel_id, f"{harness.charm.app.name}/1")
harness.add_relation_unit(peer_rel_id, harness.charm.unit.name)
harness.update_relation_data(
peer_rel_id,
harness.charm.app.name,
{"cluster_initialised": "True"},
)
yield harness
harness.cleanup()
harness = Harness(PostgresqlOperatorCharm)

# Set up the initial relation and hooks.
harness.set_leader(True)
harness.begin()

# Define some relations.
rel_id = harness.add_relation(RELATION_NAME, "application")
harness.add_relation_unit(rel_id, "application/0")
peer_rel_id = harness.add_relation(PEER, harness.charm.app.name)
harness.add_relation_unit(peer_rel_id, f"{harness.charm.app.name}/1")
harness.add_relation_unit(peer_rel_id, harness.charm.unit.name)
harness.update_relation_data(
peer_rel_id,
harness.charm.app.name,
{"cluster_initialised": "True"},
)
yield harness
harness.cleanup()


def clear_relation_data(_harness):
Expand Down Expand Up @@ -132,57 +131,56 @@ def test_on_relation_changed(harness):


def test_get_extensions(harness):
with patch("charm.KubernetesServicePatch", lambda x, y: None):
# Test when there are no extensions in the relation databags.
rel_id = harness.model.get_relation(RELATION_NAME).id
relation = harness.model.get_relation(RELATION_NAME, rel_id)
assert harness.charm.legacy_db_relation._get_extensions(relation) == ([], set())

# Test when there are extensions in the application relation databag.
extensions = ["", "citext:public", "debversion"]
with harness.hooks_disabled():
harness.update_relation_data(
rel_id,
"application",
{"extensions": ",".join(extensions)},
)
assert harness.charm.legacy_db_relation._get_extensions(relation) == (
[extensions[1], extensions[2]],
{extensions[1].split(":")[0], extensions[2]},
# Test when there are no extensions in the relation databags.
rel_id = harness.model.get_relation(RELATION_NAME).id
relation = harness.model.get_relation(RELATION_NAME, rel_id)
assert harness.charm.legacy_db_relation._get_extensions(relation) == ([], set())

# Test when there are extensions in the application relation databag.
extensions = ["", "citext:public", "debversion"]
with harness.hooks_disabled():
harness.update_relation_data(
rel_id,
"application",
{"extensions": ",".join(extensions)},
)
assert harness.charm.legacy_db_relation._get_extensions(relation) == (
[extensions[1], extensions[2]],
{extensions[1].split(":")[0], extensions[2]},
)

# Test when there are extensions in the unit relation databag.
with harness.hooks_disabled():
harness.update_relation_data(
rel_id,
"application",
{"extensions": ""},
)
harness.update_relation_data(
rel_id,
"application/0",
{"extensions": ",".join(extensions)},
)
assert harness.charm.legacy_db_relation._get_extensions(relation) == (
[extensions[1], extensions[2]],
{extensions[1].split(":")[0], extensions[2]},
# Test when there are extensions in the unit relation databag.
with harness.hooks_disabled():
harness.update_relation_data(
rel_id,
"application",
{"extensions": ""},
)

# Test when one of the plugins/extensions is enabled.
config = """options:
plugin_citext_enable:
default: true
type: boolean
plugin_debversion_enable:
default: false
type: boolean"""
harness = Harness(PostgresqlOperatorCharm, config=config)
harness.cleanup()
harness.begin()
assert harness.charm.legacy_db_relation._get_extensions(relation) == (
[extensions[1], extensions[2]],
{extensions[2]},
harness.update_relation_data(
rel_id,
"application/0",
{"extensions": ",".join(extensions)},
)
assert harness.charm.legacy_db_relation._get_extensions(relation) == (
[extensions[1], extensions[2]],
{extensions[1].split(":")[0], extensions[2]},
)

# Test when one of the plugins/extensions is enabled.
config = """options:
plugin_citext_enable:
default: true
type: boolean
plugin_debversion_enable:
default: false
type: boolean"""
harness = Harness(PostgresqlOperatorCharm, config=config)
harness.cleanup()
harness.begin()
assert harness.charm.legacy_db_relation._get_extensions(relation) == (
[extensions[1], extensions[2]],
{extensions[2]},
)


def test_set_up_relation(harness):
Expand Down
44 changes: 21 additions & 23 deletions tests/unit/test_patroni.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,32 @@

@pytest.fixture(autouse=True)
def harness():
with patch("charm.KubernetesServicePatch", lambda x, y: None):
harness = Harness(PostgresqlOperatorCharm)
harness.begin()
yield harness
harness.cleanup()
harness = Harness(PostgresqlOperatorCharm)
harness.begin()
yield harness
harness.cleanup()


@pytest.fixture(autouse=True)
def patroni(harness):
with patch("charm.KubernetesServicePatch", lambda x, y: None):
# Setup Patroni wrapper.
patroni = Patroni(
harness.charm,
"postgresql-k8s-0",
["postgresql-k8s-0", "postgresql-k8s-1", "postgresql-k8s-2"],
"postgresql-k8s-primary.dev.svc.cluster.local",
"test-model",
STORAGE_PATH,
"superuser-password",
"replication-password",
"rewind-password",
False,
"patroni-password",
)
root = harness.get_filesystem_root("postgresql")
(root / "var" / "log" / "postgresql").mkdir(parents=True, exist_ok=True)
# Setup Patroni wrapper.
patroni = Patroni(
harness.charm,
"postgresql-k8s-0",
["postgresql-k8s-0", "postgresql-k8s-1", "postgresql-k8s-2"],
"postgresql-k8s-primary.dev.svc.cluster.local",
"test-model",
STORAGE_PATH,
"superuser-password",
"replication-password",
"rewind-password",
False,
"patroni-password",
)
root = harness.get_filesystem_root("postgresql")
(root / "var" / "log" / "postgresql").mkdir(parents=True, exist_ok=True)

yield patroni
yield patroni


# This method will be used by the mock to replace requests.get
Expand Down
15 changes: 7 additions & 8 deletions tests/unit/test_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@

@pytest.fixture(autouse=True)
def harness():
with patch("charm.KubernetesServicePatch", lambda x, y: None):
harness = Harness(PostgresqlOperatorCharm)
harness = Harness(PostgresqlOperatorCharm)

# Set up the initial relation and hooks.
peer_rel_id = harness.add_relation(PEER, "postgresql-k8s")
harness.add_relation_unit(peer_rel_id, "postgresql-k8s/0")
harness.begin()
yield harness
harness.cleanup()
# Set up the initial relation and hooks.
peer_rel_id = harness.add_relation(PEER, "postgresql-k8s")
harness.add_relation_unit(peer_rel_id, "postgresql-k8s/0")
harness.begin()
yield harness
harness.cleanup()


def test_create_database(harness):
Expand Down
37 changes: 18 additions & 19 deletions tests/unit/test_postgresql_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,24 @@

@pytest.fixture(autouse=True)
def harness():
with patch("charm.KubernetesServicePatch", lambda x, y: None):
harness = Harness(PostgresqlOperatorCharm)

# Set up the initial relation and hooks.
harness.set_leader(True)
harness.begin()

# Define some relations.
rel_id = harness.add_relation(RELATION_NAME, "application")
harness.add_relation_unit(rel_id, "application/0")
peer_rel_id = harness.add_relation(PEER, harness.charm.app.name)
harness.add_relation_unit(peer_rel_id, harness.charm.unit.name)
harness.update_relation_data(
peer_rel_id,
harness.charm.app.name,
{"cluster_initialised": "True"},
)
yield harness
harness.cleanup()
harness = Harness(PostgresqlOperatorCharm)

# Set up the initial relation and hooks.
harness.set_leader(True)
harness.begin()

# Define some relations.
rel_id = harness.add_relation(RELATION_NAME, "application")
harness.add_relation_unit(rel_id, "application/0")
peer_rel_id = harness.add_relation(PEER, harness.charm.app.name)
harness.add_relation_unit(peer_rel_id, harness.charm.unit.name)
harness.update_relation_data(
peer_rel_id,
harness.charm.app.name,
{"cluster_initialised": "True"},
)
yield harness
harness.cleanup()


def request_database(_harness):
Expand Down
Loading
Loading