-
Notifications
You must be signed in to change notification settings - Fork 2
[DPE-10062] Unit tests and tweaks #176
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
38 commits
Select commit
Hold shift + click to select a range
78043a1
Redo
dragomirp 2ade056
Merge branch '16/edge' into dpe-10062-cluster2
dragomirp 6192b0b
Conditional password
dragomirp b78a78e
Merge branch '16/edge' into dpe-10062-cluster2
dragomirp 6e45739
Port changes WIP
dragomirp f9f251c
Comment out raft logic
dragomirp b850820
Attr loop
dragomirp 35c3581
Merge branch '16/edge' into dpe-10062-cluster2
dragomirp 4f9840d
Don't pull psycopg2
dragomirp 2c319f5
None lists and touching wrong path
dragomirp 2aa5ce6
Wrong bootstrap paths
dragomirp 2427b69
Versioned volume paths
dragomirp 86f336d
Fix unit tests
dragomirp c17fe2f
Wait for switchover (K8s)
dragomirp 187f9a9
Snaplib conditional import
dragomirp 87a139a
K8s paths
dragomirp 4c9ce4f
Move exceptions
dragomirp db9d422
Don't pull client class in managers
dragomirp 245d2b6
Spaces ips
dragomirp 80f5fb6
Exception
dragomirp bec12eb
Running members diff
dragomirp d4dd47b
Ensure slots retval
dragomirp f9b8789
Workload status check
dragomirp b7d76ff
Lazy load container
dragomirp b3133ac
Reduce update sync nodes retry timeout
dragomirp b079cb1
Revert container change
dragomirp 512f51a
Reduce timeout
dragomirp 1497c9a
[DPE-10062] Single kernel changes
dragomirp 8f2d954
Merge branch 'dpe-10062-kernel-changes' into dpe-10062-cluster2
dragomirp ad37c17
[DPE-10062] Single kernel changes
dragomirp f6e8789
Merge branch 'dpe-10062-kernel-changes' into dpe-10062-cluster2
dragomirp 60e7623
Merge branch '16/edge' into dpe-10062-cluster2
dragomirp fcfbcb7
K8s passes unit names
dragomirp 1bc58e4
Dead code
dragomirp f1583b3
Unit tests and tweaks
dragomirp b776240
Merge branch '16/edge' into dpe-10062-tests
dragomirp e97be7e
VM tests
dragomirp 93d1c46
K8s test
dragomirp 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 |
|---|---|---|
|
|
@@ -12,14 +12,14 @@ | |
| import logging | ||
| import os | ||
| import re | ||
| import subprocess | ||
| from contextlib import suppress | ||
| from functools import cached_property | ||
| from typing import Any, Literal, TypedDict | ||
|
|
||
| # Platform specific imports | ||
| with suppress(ImportError): | ||
| from charmlibs import snap | ||
| from charmlibs.systemd import daemon_reload | ||
| import psycopg2 | ||
| import psycopg2.extras | ||
| import requests | ||
|
|
@@ -412,6 +412,8 @@ def member_inactive(self) -> bool: | |
| True if services is not running, starting or restarting. Retries over a period of 60 | ||
| seconds times to allow server time to start up. | ||
| """ | ||
| if not self.workload.is_patroni_running(): | ||
| return True | ||
| try: | ||
| response = self.cached_patroni_health | ||
| except RetryError: | ||
|
|
@@ -828,7 +830,7 @@ def update_patroni_restart_condition(self, new_condition: str) -> None: | |
| logger.debug(f"new patroni service file: {new_patroni_service}") | ||
| with open(VM_PATRONI_SERVICE_DEFAULT_PATH, "w") as patroni_service_file: | ||
| patroni_service_file.write(new_patroni_service) | ||
| subprocess.run(["/bin/systemctl", "daemon-reload"]) | ||
| daemon_reload() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missed from the VM implementation. |
||
|
|
||
| @property | ||
| def primary_endpoint_ready(self) -> bool: | ||
|
|
||
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,231 @@ | ||
| # Copyright 2021 Canonical Ltd. | ||
| # See LICENSE file for licensing details. | ||
| from unittest.mock import Mock, PropertyMock, mock_open, patch, sentinel | ||
|
|
||
| import pytest | ||
| from single_kernel_postgresql.config.enums import Substrates | ||
| from single_kernel_postgresql.core.state import CharmState | ||
| from single_kernel_postgresql.managers.config import ConfigManager | ||
| from single_kernel_postgresql.workload.k8s import K8sWorkload | ||
| from single_kernel_postgresql.workload.vm import VMWorkload | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def config(substrate): | ||
| mock_charm = Mock() | ||
| mock_container = Mock() | ||
| workload = VMWorkload(".") if substrate == Substrates.VM else K8sWorkload(".", mock_container) | ||
| with patch( | ||
| "single_kernel_postgresql.workload.base.BaseWorkload.get_postgresql_version", | ||
| return_value="16.6", | ||
| ): | ||
| config = ConfigManager( | ||
| state=CharmState(charm=mock_charm, substrate=substrate), workload=workload | ||
| ) | ||
| yield config | ||
|
|
||
|
|
||
| def test_dict_to_hba_string(config): | ||
| mock_data = { | ||
| "ldapbasedn": "dc=example,dc=net", | ||
| "ldapbinddn": "cn=serviceuser,dc=example,dc=net", | ||
| "ldapbindpasswd": "password", | ||
| "ldaptls": False, | ||
| "ldapurl": "ldap://0.0.0.0:3893", | ||
| } | ||
|
|
||
| assert config._dict_to_hba_string(mock_data) == ( | ||
| 'ldapbasedn="dc=example,dc=net" ' | ||
| 'ldapbinddn="cn=serviceuser,dc=example,dc=net" ' | ||
| 'ldapbindpasswd="password" ' | ||
| "ldaptls=0 " | ||
| 'ldapurl="ldap://0.0.0.0:3893"' | ||
| ) | ||
|
|
||
|
|
||
| def test_render_patroni_yml_file(substrate, config): | ||
| with ( | ||
| patch( | ||
| "single_kernel_postgresql.workload.base.BaseWorkload.get_postgresql_version", | ||
| return_value="16.6", | ||
| ), | ||
| patch("single_kernel_postgresql.managers.config.render_file") as _render_file, | ||
| patch("single_kernel_postgresql.managers.config.Template") as _template, | ||
| patch( | ||
| "single_kernel_postgresql.core.state.CharmState.config", new_callable=PropertyMock | ||
| ) as _config, | ||
| patch( | ||
| "single_kernel_postgresql.core.state.CharmState.endpoint", | ||
| new_callable=PropertyMock, | ||
| return_value=sentinel.endpoint, | ||
| ), | ||
| patch( | ||
| "single_kernel_postgresql.core.state.CharmState.model_name", | ||
| new_callable=PropertyMock, | ||
| return_value=sentinel.model_name, | ||
| ), | ||
| patch( | ||
| "single_kernel_postgresql.core.state.CharmState.listen_ips", | ||
| new_callable=PropertyMock, | ||
| return_value=sentinel.listen_ips, | ||
| ), | ||
| patch( | ||
| "single_kernel_postgresql.core.state.CharmState.unit_ip", | ||
| new_callable=PropertyMock, | ||
| return_value=sentinel.unit_ip, | ||
| ), | ||
| patch( | ||
| "single_kernel_postgresql.core.peer_relation.PostgreSQLApplication.patroni_password", | ||
| new_callable=PropertyMock, | ||
| return_value=sentinel.patroni_pass, | ||
| ), | ||
| patch( | ||
| "single_kernel_postgresql.core.peer_relation.PostgreSQLApplication.user_password", | ||
| new_callable=PropertyMock, | ||
| return_value=sentinel.user_pass, | ||
| ), | ||
| patch( | ||
| "single_kernel_postgresql.core.peer_relation.PostgreSQLApplication.replication_password", | ||
| new_callable=PropertyMock, | ||
| return_value=sentinel.replication_pass, | ||
| ), | ||
| patch( | ||
| "single_kernel_postgresql.core.peer_relation.PostgreSQLApplication.rewind_password", | ||
| new_callable=PropertyMock, | ||
| return_value=sentinel.rewind_pass, | ||
| ), | ||
| patch( | ||
| "single_kernel_postgresql.core.peer_relation.PostgreSQLApplication.raft_password", | ||
| new_callable=PropertyMock, | ||
| return_value=sentinel.raft_pass, | ||
| ), | ||
| patch( | ||
| "single_kernel_postgresql.core.peer_relation.PostgreSQLApplication.planned_units", | ||
| new_callable=PropertyMock, | ||
| return_value=1, | ||
| ), | ||
| patch( | ||
| "single_kernel_postgresql.core.peer_relation.PostgreSQLApplication.cluster_name", | ||
| new_callable=PropertyMock, | ||
| return_value=sentinel.cluster_name, | ||
| ), | ||
| patch( | ||
| "single_kernel_postgresql.core.peer_relation.PostgreSQLApplication.endpoints", | ||
| new_callable=PropertyMock, | ||
| return_value=["endpoint1", "endpoint2", "endpoint3"], | ||
| ), | ||
| patch( | ||
| "single_kernel_postgresql.core.peer_relation.PostgreSQLApplication.members_ips", | ||
| new_callable=PropertyMock, | ||
| return_value=["ip1", "ip2", "ip3"], | ||
| ), | ||
| patch( | ||
| "single_kernel_postgresql.core.peer_relation.PostgreSQLPeer.member_name", | ||
| new_callable=PropertyMock, | ||
| return_value=sentinel.member_name, | ||
| ), | ||
| patch("builtins.open", mock_open(read_data="template")), | ||
| ): | ||
| _config.return_value.synchronous_node_count = 1 | ||
| _config.return_value.durability_maximum_lag_on_failover = ( | ||
| sentinel.durability_maximum_lag_on_failover | ||
| ) | ||
| _config.return_value.instance_password_encryption = sentinel.instance_password_encryption | ||
| _template.return_value.render.return_value = sentinel.template_output | ||
|
|
||
| config.render_patroni_yml_file() | ||
|
|
||
| _template.assert_called_once_with("template") | ||
| if substrate == Substrates.K8S: | ||
| _template.return_value.render.assert_called_once_with( | ||
| connectivity=False, | ||
| enable_ldap=False, | ||
| enable_tls=False, | ||
| member_name=sentinel.member_name, | ||
| superuser="operator", | ||
| superuser_password=sentinel.user_pass, | ||
| rewind_user="rewind", | ||
| rewind_password=sentinel.rewind_pass, | ||
| replication_password=sentinel.replication_pass, | ||
| enable_pgbackrest_archiving=False, | ||
| stanza=None, | ||
| restore_stanza=None, | ||
| restoring_backup=False, | ||
| backup_id=None, | ||
| pitr_target=None, | ||
| restore_timeline=None, | ||
| restore_to_latest=False, | ||
| is_creating_backup=False, | ||
| version="16", | ||
| synchronous_node_count=0, | ||
| maximum_lag_on_failover=sentinel.durability_maximum_lag_on_failover, | ||
| pg_parameters=None, | ||
| primary_cluster_endpoint=None, | ||
| ldap_parameters="", | ||
| patroni_password=sentinel.patroni_pass, | ||
| user_databases_map=None, | ||
| slots={}, | ||
| instance_password_encryption=sentinel.instance_password_encryption, | ||
| extra_replication_endpoints=[], | ||
| endpoint=sentinel.endpoint, | ||
| endpoints=["endpoint1", "endpoint2", "endpoint3"], | ||
| is_no_sync_member=False, | ||
| namespace=sentinel.model_name, | ||
| storage_path="/var/lib/pg/data", | ||
| logs_storage_path="/var/lib/pg/logs", | ||
| pgdata_path="/var/lib/pg/data/16/main", | ||
| ) | ||
| _render_file.assert_called_once_with( | ||
| substrate, "/var/lib/pg/data/patroni.yaml", sentinel.template_output, 0o644 | ||
| ) | ||
| else: | ||
| _template.return_value.render.assert_called_once_with( | ||
| connectivity=False, | ||
| enable_ldap=False, | ||
| enable_tls=False, | ||
| member_name=sentinel.member_name, | ||
| superuser="operator", | ||
| superuser_password=sentinel.user_pass, | ||
| rewind_user="rewind", | ||
| rewind_password=sentinel.rewind_pass, | ||
| replication_password=sentinel.replication_pass, | ||
| enable_pgbackrest_archiving=False, | ||
| stanza=None, | ||
| restore_stanza=None, | ||
| restoring_backup=False, | ||
| backup_id=None, | ||
| pitr_target=None, | ||
| restore_timeline=None, | ||
| restore_to_latest=False, | ||
| is_creating_backup=False, | ||
| version="16", | ||
| synchronous_node_count=0, | ||
| maximum_lag_on_failover=sentinel.durability_maximum_lag_on_failover, | ||
| pg_parameters=None, | ||
| primary_cluster_endpoint=None, | ||
| ldap_parameters="", | ||
| patroni_password=sentinel.patroni_pass, | ||
| user_databases_map=None, | ||
| slots={}, | ||
| instance_password_encryption=sentinel.instance_password_encryption, | ||
| extra_replication_endpoints=[], | ||
| conf_path="/var/snap/charmed-postgresql/current/etc/patroni", | ||
| log_path="/var/snap/charmed-postgresql/common/var/log/patroni", | ||
| postgresql_log_path="/var/snap/charmed-postgresql/common/var/log/postgresql", | ||
| data_path="/var/snap/charmed-postgresql/common/var/lib/postgresql/16/main", | ||
| wal_dir="/var/snap/charmed-postgresql/common/data/logs/16/main", | ||
| partner_addrs=[], | ||
| peers_ips=["ip1", "ip2", "ip3"], | ||
| pgbackrest_configuration_file="--config=/var/snap/charmed-postgresql/current/etc/pgbackrest/pgbackrest.conf", | ||
| scope=sentinel.cluster_name, | ||
| self_ip=sentinel.unit_ip, | ||
| listen_ips=sentinel.listen_ips, | ||
| raft_password=sentinel.raft_pass, | ||
| watcher=None, | ||
| ) | ||
| _render_file.assert_called_once_with( | ||
| substrate, | ||
| "/var/snap/charmed-postgresql/current/etc/patroni/patroni.yaml", | ||
| sentinel.template_output, | ||
| 0o600, | ||
| ) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed from the VM implementation.