Skip to content

Commit

Permalink
updated service test (#43)
Browse files Browse the repository at this point in the history
## Proposal
Minor cleanup to pgbouncer start-service test. 

## Commits
* updated service test

* lint

* fixed unit import
  • Loading branch information
Will Fitch authored Dec 16, 2022
1 parent b3db153 commit afbab16
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ User=postgres
ExecStart=/sbin/pgbouncer -R /var/lib/postgresql/pgbouncer/instance_%i/pgbouncer.ini
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGINT
PIDFile=/var/lib/postgresql/pgbouncer/instance_%i/pgbouncer.pid
Restart=on-failure
RestartSec=5s

Expand Down
10 changes: 4 additions & 6 deletions tests/integration/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import yaml
from charms.pgbouncer_k8s.v0 import pgb
from juju.unit import Unit
from pytest_operator.plugin import OpsTest
from tenacity import RetryError, Retrying, stop_after_delay, wait_fixed

Expand Down Expand Up @@ -72,7 +73,7 @@ async def get_unit_cores(unit: str) -> int:
raise Exception(get_cores_from_unit.results)


async def get_running_instances(unit: str, service: str) -> int:
async def get_running_instances(unit: Unit, service: str) -> int:
"""Returns the number of running instances of the given service.
Uses `ps` to find the number of instances of a given service.
Expand All @@ -84,11 +85,8 @@ async def get_running_instances(unit: str, service: str) -> int:
Returns:
an integer defining the number of running instances.
"""
get_running_instances = await unit.run(f"ps aux | grep {service}")
ps_output = get_running_instances.results.get("Stdout")
num_of_ps_lines = len(ps_output.split("\n"))
# one extra for grep process, and one for a blank line at the end
return num_of_ps_lines - 2
get_running_instances = await unit.run(f"pgrep -c {service}")
return int(get_running_instances.results.get("Stdout"))


async def get_unit_info(ops_test: OpsTest, unit_name: str) -> Dict:
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ async def test_systemd_restarts_pgbouncer_processes(ops_test: OpsTest):
assert await helpers.get_running_instances(unit, "pgbouncer") == expected_processes

# Kill pgbouncer process and wait for it to restart
await unit.run("kill $(ps aux | grep pgbouncer | awk '{print $2}')")
await unit.run("kill $(pgrep pgbouncer)")
async with ops_test.fast_forward():
await ops_test.model.wait_for_idle(apps=[PGB], status="blocked", timeout=300)
await ops_test.model.wait_for_idle(apps=[PGB], status="blocked", timeout=(3 * 60))
assert ops_test.model.units[f"{PGB}/0"].workload_status_message == WAIT_MSG

# verify all processes start again
Expand Down

0 comments on commit afbab16

Please sign in to comment.