Skip to content

Commit

Permalink
Fix integ tests
Browse files Browse the repository at this point in the history
  • Loading branch information
javierdelapuente committed Dec 5, 2024
1 parent c514428 commit 053b57d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
9 changes: 8 additions & 1 deletion tests/integration/helpers/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,21 @@ class InstanceHelper(typing.Protocol):
"""Helper for running commands in instances."""

async def run_in_instance(
self, unit: Unit, command: str, timeout: int | None = None
self,
unit: Unit,
command: str,
timeout: int | None = None,
assert_on_failure: bool = False,
assert_msg: str | None = None,
) -> tuple[int, str | None, str | None]:
"""Run command in instance.
Args:
unit: Juju unit to execute the command in.
command: Command to execute.
timeout: Amount of time to wait for the execution.
assert_on_failure: Perform assertion on non-zero exit code.
assert_msg: Message for the failure assertion.
"""
...

Expand Down
9 changes: 8 additions & 1 deletion tests/integration/helpers/lxd.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@ class LXDInstanceHelper(InstanceHelper):
"""Helper class to interact with LXD instances."""

async def run_in_instance(
self, unit: Unit, command: str, timeout: int | None = None
self,
unit: Unit,
command: str,
timeout: int | None = None,
assert_on_failure: bool = False,
assert_msg: str | None = None,
) -> tuple[int, str | None, str | None]:
"""Run command in LXD instance.
Args:
unit: Juju unit to execute the command in.
command: Command to execute.
timeout: Amount of time to wait for the execution.
assert_on_failure: Not used in lxd
assert_msg: Not used in lxd
Returns:
Tuple of return code, stdout and stderr.
Expand Down
14 changes: 6 additions & 8 deletions tests/integration/test_charm_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"""Integration tests for charm upgrades."""

import functools
import logging
import pathlib

import pytest
Expand Down Expand Up @@ -38,9 +37,9 @@ async def test_charm_upgrade(
app_name: str,
path: str,
token: str,
http_proxy: str,
https_proxy: str,
no_proxy: str,
openstack_http_proxy: str,
openstack_https_proxy: str,
openstack_no_proxy: str,
tmp_path: pathlib.Path,
clouds_yaml_contents: str,
network_name: str,
Expand Down Expand Up @@ -70,7 +69,6 @@ async def test_charm_upgrade(
)
assert retcode == 0, f"failed to download charm, {stdout} {stderr}"

logging.info("proxy value %s", http_proxy)
# deploy latest stable version of the charm
application = await deploy_github_runner_charm(
model=model,
Expand All @@ -79,9 +77,9 @@ async def test_charm_upgrade(
path=path,
token=token,
runner_storage="juju-storage",
http_proxy=http_proxy,
https_proxy=https_proxy,
no_proxy=no_proxy,
http_proxy=openstack_http_proxy,
https_proxy=openstack_https_proxy,
no_proxy=openstack_no_proxy,
reconcile_interval=5,
# override default virtual_machines=0 config.
config={
Expand Down
34 changes: 33 additions & 1 deletion tests/integration/test_debug_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

"""Integration tests for github-runner charm with ssh-debug integration."""
import logging
import socket
import subprocess

import pytest
from github.Branch import Branch
Expand All @@ -11,7 +13,7 @@
from juju.model import Model

from charm_state import DENYLIST_CONFIG_NAME
from tests.integration.helpers.common import dispatch_workflow, get_job_logs
from tests.integration.helpers.common import InstanceHelper, dispatch_workflow, get_job_logs
from tests.status_name import ACTIVE

logger = logging.getLogger(__name__)
Expand All @@ -27,13 +29,21 @@ async def test_ssh_debug(
github_repository: Repository,
test_github_branch: Branch,
tmate_ssh_server_unit_ip: str,
instance_helper: InstanceHelper,
):
"""
arrange: given an integrated GitHub-Runner charm and tmate-ssh-server charm with a denylist \
covering ip ranges of tmate-ssh-server.
act: when canonical/action-tmate is triggered.
assert: the ssh connection info from action-log and tmate-ssh-server matches.
"""
# As the tmate_ssh_server runs in lxd under this host, we need all the connection
# that arrive to this host to port 10022 to go to the tmate_ssh_server unit.
subprocess.run(["sudo", "bash", "-c", "echo 1 > /proc/sys/net/ipv4/ip_forward"], check=True)
# This maybe problematic if we run the test twice in the same machine.
dnat_command_in_test_machine = f"sudo iptables -t nat -A PREROUTING -p tcp --dport 10022 -j DNAT --to-destination {tmate_ssh_server_unit_ip}:10022"
subprocess.run(dnat_command_in_test_machine.split(), check=True)

await app_no_wait_openstack.set_config(
{
DENYLIST_CONFIG_NAME: (
Expand All @@ -46,6 +56,17 @@ async def test_ssh_debug(
)
await model.wait_for_idle(status=ACTIVE, timeout=60 * 120)

unit = app_no_wait_openstack.units[0]
# We need the runner to connect to the current machine, instead of the tmate_ssh_server unit,
# as the tmate_ssh_server is not routable.
this_host_ip = _get_current_ip()
dnat_comman_in_runner = f"sudo iptables -t nat -A OUTPUT -p tcp --dport 10022 -j DNAT --to-destination {this_host_ip}:10022"
ret_code, stdout, stderr = await instance_helper.run_in_instance(
unit,
dnat_comman_in_runner,
assert_on_failure=True,
)

# trigger tmate action
logger.info("Dispatching workflow_dispatch_ssh_debug.yaml workflow.")

Expand All @@ -64,3 +85,14 @@ async def test_ssh_debug(
logger.info("Logs: %s", logs)
assert tmate_ssh_server_unit_ip in logs, "Tmate ssh server IP not found in action logs."
assert "10022" in logs, "Tmate ssh server connection port not found in action logs."


def _get_current_ip():
"""Get the IP for the current machine."""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0)
try:
s.connect(("2.2.2.2", 1))
return s.getsockname()[0]
finally:
s.close()

0 comments on commit 053b57d

Please sign in to comment.