diff --git a/agent/charms/testflinger-agent-host-charm/src/charm.py b/agent/charms/testflinger-agent-host-charm/src/charm.py index d1a4d0b0..5ae3ae30 100755 --- a/agent/charms/testflinger-agent-host-charm/src/charm.py +++ b/agent/charms/testflinger-agent-host-charm/src/charm.py @@ -255,7 +255,6 @@ 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: @@ -263,6 +262,7 @@ def on_config_changed(self, _): "config-repo and config-dir must be set" ) return + self.copy_ssh_keys() self.write_supervisor_service_files() self.supervisor_update() self.restart_agents() diff --git a/agent/charms/testflinger-agent-host-charm/tests/integration/test_charm_integration.py b/agent/charms/testflinger-agent-host-charm/tests/integration/test_charm_integration.py index 9224bfa8..c6dec575 100644 --- a/agent/charms/testflinger-agent-host-charm/tests/integration/test_charm_integration.py +++ b/agent/charms/testflinger-agent-host-charm/tests/integration/test_charm_integration.py @@ -1,5 +1,4 @@ from pathlib import Path -from unittest.mock import patch import pytest from pytest_operator.plugin import OpsTest @@ -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", } @@ -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" diff --git a/agent/charms/testflinger-agent-host-charm/tests/unit/test_charm.py b/agent/charms/testflinger-agent-host-charm/tests/unit/test_charm.py index 83250a4c..03a0a4d3 100644 --- a/agent/charms/testflinger-agent-host-charm/tests/unit/test_charm.py +++ b/agent/charms/testflinger-agent-host-charm/tests/unit/test_charm.py @@ -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 @@ -17,6 +16,8 @@ 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") @@ -24,7 +25,15 @@ def setUp(self): @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 @@ -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", } @@ -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""" diff --git a/agent/charms/testflinger-agent-host-charm/tests/unit/test_testflinger_source.py b/agent/charms/testflinger-agent-host-charm/tests/unit/test_testflinger_source.py index 40ce52cb..e0083b9e 100644 --- a/agent/charms/testflinger-agent-host-charm/tests/unit/test_testflinger_source.py +++ b/agent/charms/testflinger-agent-host-charm/tests/unit/test_testflinger_source.py @@ -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", ] )