Skip to content

Commit

Permalink
Unit/Integration test fixes for testflinger-agent-host-charm
Browse files Browse the repository at this point in the history
  • Loading branch information
plars committed Jan 8, 2025
1 parent d473d6e commit 734ff6a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 38 deletions.
2 changes: 1 addition & 1 deletion agent/charms/testflinger-agent-host-charm/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,14 @@ def on_start(self, _):

def on_config_changed(self, _):
self.unit.status = MaintenanceStatus("Handling config_changed hook")
self.copy_ssh_keys()
try:
self.update_config_files()
except ValueError:
self.unit.status = BlockedStatus(
"config-repo and config-dir must be set"
)
return
self.copy_ssh_keys()
self.write_supervisor_service_files()
self.supervisor_update()
self.restart_agents()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pathlib import Path
from unittest.mock import patch
import pytest
from pytest_operator.plugin import OpsTest

Expand All @@ -9,12 +8,12 @@
TEST_CONFIG_01 = {
"config-repo": "https://github.com/canonical/testflinger.git",
"config-dir": "agent/charms/testflinger-agent-host-charm/tests/integration/data/test01",
"config-branch": "write-supervisord-service-files",
"config-branch": "unified-agent-host-charm",
}
TEST_CONFIG_02 = {
"config-repo": "https://github.com/canonical/testflinger.git",
"config-dir": "agent/charms/testflinger-agent-host-charm/tests/integration/data/test02",
"config-branch": "write-supervisord-service-files",
"config-branch": "unified-agent-host-charm",
}


Expand Down Expand Up @@ -82,11 +81,13 @@ async def test_supervisord_files_written(ops_test: OpsTest):
# check that agent001.conf was written in /etc/supervisor/conf.d/
expected_contents = (
"[program:agent001]\n"
"environment=PYTHONIOENCODING=utf-8\n"
"redirect_stderr=true\n"
'environment=USER="ubuntu",HOME="/home/ubuntu",'
"PYTHONIOENCODING=utf-8\n"
"user=ubuntu\n"
"command=/srv/testflinger-venv/bin/testflinger-agent -c /srv/agent-configs/agent/charms/"
"testflinger-agent-host-charm/tests/integration/data/test01/agent001/"
"testflinger-agent.conf\n"
"command=/srv/testflinger-venv/bin/testflinger-agent -c "
"/srv/agent-configs/agent/charms/testflinger-agent-host-charm/tests/"
"integration/data/test01/agent001/testflinger-agent.conf\n"
)

conf_file = "/etc/supervisor/conf.d/agent001.conf"
Expand Down
63 changes: 34 additions & 29 deletions agent/charms/testflinger-agent-host-charm/tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
# Learn more about testing at: https://juju.is/docs/sdk/testing

import unittest
import os
import pytest
from unittest.mock import patch
import base64
from unittest.mock import patch, mock_open
from charm import TestflingerAgentHostCharm
from ops.testing import Harness

Expand All @@ -17,14 +16,24 @@ def setUp(self):
self.addCleanup(self.harness.cleanup)
self.harness.begin()

@patch("os.chown")
@patch("os.chmod")
@patch("shutil.move")
@patch("git.Repo.clone_from")
@patch("charm.TestflingerAgentHostCharm.write_file")
@patch("charm.TestflingerAgentHostCharm.restart_agents")
@patch("charm.TestflingerAgentHostCharm.supervisor_update")
@patch("charm.TestflingerAgentHostCharm.write_supervisor_service_files")
def test_copy_ssh_keys(
self, _, __, ___, mock_write_file, mock_clone_from, mock_move
self,
_,
__,
___,
mock_write_file,
mock_clone_from,
mock_move,
mock_chmod,
mock_chown,
):
"""
Test the copy_ssh_keys method
Expand All @@ -38,8 +47,12 @@ def test_copy_ssh_keys(
mock_move.return_value = None
self.harness.update_config(
{
"ssh-private-key": "ssh_private_key_content",
"ssh-public-key": "ssh_public_key_content",
"ssh-private-key": base64.b64encode(
b"ssh_private_key_content"
).decode(),
"ssh-public-key": base64.b64encode(
b"ssh_public_key_content"
).decode(),
"config-repo": "foo",
"config-dir": "bar",
}
Expand All @@ -50,37 +63,29 @@ def test_copy_ssh_keys(
mock_write_file.assert_any_call(
"/home/ubuntu/.ssh/id_rsa.pub", "ssh_public_key_content"
)
self.assertEqual(mock_write_file.call_count, 2)
self.assertEqual(mock_write_file.call_count, 3)

@patch("os.listdir")
@patch("shutil.copy")
@patch("os.chmod")
def test_update_tf_cmd_scripts(self, mock_chmod, mock_copy, mock_listdir):
@patch("builtins.open", new_callable=mock_open, read_data="test data")
@patch("pathlib.Path.write_text")
@patch("pathlib.Path.chmod")
def test_update_tf_cmd_scripts(
self,
mock_chmod,
mock_write_text,
mock_open,
mock_listdir,
):
"""Test the update_tf_cmd_scripts method"""
charm = self.harness.charm
tf_cmd_scripts_files = ["tf-script3", "tf-script4"]
tf_cmd_scripts_files = ["tf-setup"]

mock_listdir.side_effect = [tf_cmd_scripts_files]

charm.update_tf_cmd_scripts()
tf_cmd_dir = "src/tf-cmd-scripts/"
usr_local_bin = "/usr/local/bin/"
mock_copy.assert_any_call(
os.path.join(tf_cmd_dir, "tf-script3"),
usr_local_bin,
)
mock_copy.assert_any_call(
os.path.join(tf_cmd_dir, "tf-script4"),
usr_local_bin,
)
self.assertEqual(mock_copy.call_count, 2)
mock_chmod.assert_any_call(
os.path.join(usr_local_bin, "tf-script3"), 0o775
)
mock_chmod.assert_any_call(
os.path.join(usr_local_bin, "tf-script4"), 0o775
)
self.assertEqual(mock_chmod.call_count, 2)

# Ensure it tried to write the file correctly
mock_write_text.assert_any_call("test data")

def test_blocked_on_no_config_repo(self):
"""Test the on_config_changed method with no config-repo"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_clone_repo(mock_run_with_logged_errors, mock_clone_from):
f"{VIRTUAL_ENV_PATH}/bin/pip3",
"install",
"-I",
"/srv/testflinger/agent",
"/srv/testflinger/device-connectors",
]
)

Expand Down

0 comments on commit 734ff6a

Please sign in to comment.