diff --git a/ironic_python_agent/hardware_managers/container.py b/ironic_python_agent/hardware_managers/container.py index 162d3921e..5f304f709 100644 --- a/ironic_python_agent/hardware_managers/container.py +++ b/ironic_python_agent/hardware_managers/container.py @@ -16,6 +16,7 @@ from ironic_python_agent import utils from oslo_config import cfg from oslo_log import log +from functools import partial import yaml CONF = cfg.CONF @@ -41,6 +42,41 @@ class ContainerHardwareManager(hardware.HardwareManager): LOG.debug("Error loading steps from YAML file: %s", e) return [] + def container_clean_step(self, node, ports, container_url, + pull_options=None, run_options=None): + try: + pull_options = pull_options or CONF.container.pull_options + run_options = run_options or CONF.container.run_options + utils.execute(CONF.container.runner, "pull", + *pull_options, container_url) + utils.execute(CONF.container.runner, "run", + *run_options, container_url) + LOG.info("Container step completed for image: %s", container_url) + except Exception as e: + LOG.exception("Error during container operation: %s", e) + raise + + def _create_cleanup_method(self, container_url, pull_options=None, + run_options=None): + return partial(self.container_clean_step, container_url=container_url, + pull_options=pull_options, run_options=run_options) + + def _create_container_step(self): + return { + "step": "container_clean_step", + "priority": 0, # run only manual cleaning + "interface": "deploy", + "reboot_requested": False, + "abortable": True, + "argsinfo": { + "container_url": {"description": "Container image URL"}, + "pull_options": {"description": "Pull options", + "required": False}, + "run_options": {"description": "Run options", + "required": False}, + }, + } + def evaluate_hardware_support(self): """Determine if container runner exists and return support level.""" containers_runners = ["podman", "docker"] @@ -59,7 +95,7 @@ class ContainerHardwareManager(hardware.HardwareManager): """Dynamically generate cleaning steps.""" self.STEPS = self._load_steps_from_yaml( CONF.container['container_steps_file']) - steps = [] + steps = [self._create_container_step()] for step in self.STEPS: try: steps.append( @@ -82,6 +118,12 @@ class ContainerHardwareManager(hardware.HardwareManager): ) return steps + def get_service_steps(self, node, ports): + return self.get_clean_steps(node, ports) + + def get_deploy_steps(self, node, ports): + return self.get_clean_steps(node, ports) + def __getattr__(self, name): ALLOW_ARBITRARY_CONTAINERS = CONF.container ['allow_arbitrary_containers'] @@ -100,29 +142,11 @@ class ContainerHardwareManager(hardware.HardwareManager): ) continue - def run_container_steps(*args, **kwargs): - try: - utils.execute( - CONF.container.runner, - "pull", - *step.get("pull_options", - CONF.container.pull_options), - step.get("image"), - ) - LOG.info("Container image '%s' pulled", - step.get("image")) - utils.execute( - CONF.container.runner, - "run", - *step.get("run_options", - CONF.container.run_options), - step.get("image"), - ) - LOG.info("Container image '%s' completed", - step.get("image")) - except Exception as e: - LOG.exception("Error during cleanup: %s", e) - raise - return run_container_steps + return self._create_cleanup_method( + container_url=step.get('image'), + pull_options=step.get( + 'pull_options'), + run_options=step + .get('run_options')) raise AttributeError( "%s object has no attribute %s", self.__class__.__name__, name) diff --git a/ironic_python_agent/tests/unit/hardware_managers/test_container.py b/ironic_python_agent/tests/unit/hardware_managers/test_container.py new file mode 100644 index 000000000..6afdea4b0 --- /dev/null +++ b/ironic_python_agent/tests/unit/hardware_managers/test_container.py @@ -0,0 +1,115 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from unittest import mock + +from oslo_config import cfg + +from ironic_python_agent import hardware +from ironic_python_agent.hardware_managers import container +from ironic_python_agent.tests.unit import base + +CONF = cfg.CONF + + +class TestContainerHardwareManager(base.IronicAgentTest): + def setUp(self): + super(TestContainerHardwareManager, self).setUp() + self.hardware = container.ContainerHardwareManager() + self.config( + runner='podman', + pull_options=['--tls-verify=false'], + run_options=['--rm', '--network=host', '--tls-verify=false'], + container_steps_file='/tmp/steps.yaml', + allow_arbitrary_containers=False, + allowed_containers=[], + group='container' + ) + + def test_evaluate_hardware_support_docker_available(self): + with mock.patch('ironic_python_agent.utils.execute', + autospec=True) as mock_execute: + mock_execute.side_effect = [ + mock.Mock(side_effect=Exception('Podman not found')), + ('/usr/bin/docker', '') + ] + + support_level = self.hardware.evaluate_hardware_support() + mock_execute.assert_called_with('which', 'docker') + self.assertEqual(support_level, hardware.HardwareSupport.MAINLINE) + + def test_evaluate_hardware_support_podman_available(self): + with mock.patch('ironic_python_agent.utils.execute', + autospec=True) as mock_execute: + mock_execute.return_value = ('/usr/bin/podman', '') + support_level = self.hardware.evaluate_hardware_support() + mock_execute.assert_called_with('which', 'podman') + self.assertEqual(support_level, hardware.HardwareSupport.MAINLINE) + + def test_evaluate_hardware_support_no_runners(self): + with mock.patch('ironic_python_agent.utils.execute', + autospec=True) as mock_execute: + mock_execute.side_effect = Exception('Runner not found') + support_level = self.hardware.evaluate_hardware_support() + expected_calls = [ + mock.call('which', 'podman'), + mock.call('which', 'docker') + ] + mock_execute.assert_has_calls(expected_calls, any_order=True) + self.assertEqual(support_level, hardware.HardwareSupport.NONE) + + def test_container_runners_list(self): + expected_runners = ["podman", "docker"] + runners = getattr(self.hardware, 'CONTAINERS_RUNNERS', + ["podman", "docker"]) + self.assertEqual(runners, expected_runners) + + @mock.patch('ironic_python_agent.utils.execute', autospec=True) + def test_container_clean_step_with_custom_options(self, mock_execute): + node = mock.MagicMock() + ports = mock.MagicMock() + container_url = 'test-image:latest' + pull_options = ['--tls-verify=false', '-q'] + run_options = ['--rm', '--network=host', '--tls-verify=false', '-q'] + + self.hardware.container_clean_step( + node, + ports, + container_url, + pull_options=pull_options, + run_options=run_options + ) + mock_execute.assert_any_call( + CONF.container.runner, + "pull", + *pull_options, + container_url + ) + mock_execute.assert_any_call( + CONF.container.runner, + "run", + *run_options, + container_url + ) + + def test_create_container_step(self): + step = self.hardware._create_container_step() + + self.assertEqual(step['step'], 'container_clean_step') + self.assertEqual(step['priority'], 0) + self.assertEqual(step['interface'], 'deploy') + self.assertFalse(step['reboot_requested']) + self.assertTrue(step['abortable']) + + self.assertIn('container_url', step['argsinfo']) + self.assertIn('pull_options', step['argsinfo']) + self.assertIn('run_options', step['argsinfo'])