From ec995109973ffb3d68721ac4efa4b0d5174ba832 Mon Sep 17 00:00:00 2001 From: Laurent Sibilla Date: Sun, 29 Nov 2020 21:27:48 +0100 Subject: [PATCH] Docker pull runner --- runci/engine/core.py | 1 + runci/engine/runner/docker_pull.py | 25 +++++++++++++++++ tests/test_runner_docker_pull.py | 45 ++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 runci/engine/runner/docker_pull.py create mode 100644 tests/test_runner_docker_pull.py diff --git a/runci/engine/core.py b/runci/engine/core.py index cac1bbc..81d71cf 100644 --- a/runci/engine/core.py +++ b/runci/engine/core.py @@ -113,6 +113,7 @@ def status(self): "runci.engine.runner.compose_build", "runci.engine.runner.compose_run", "runci.engine.runner.docker_build", + "runci.engine.runner.docker_pull", "runci.engine.runner.target_run", "runci.engine.listener.terminal", ] diff --git a/runci/engine/runner/docker_pull.py b/runci/engine/runner/docker_pull.py new file mode 100644 index 0000000..d6d7c6b --- /dev/null +++ b/runci/engine/runner/docker_pull.py @@ -0,0 +1,25 @@ +import asyncio + +from .base import RunnerBase +from runci.entities.context import Context + + +class DockerPullRunner(RunnerBase): + _selector = 'docker-pull' + + async def run_internal(self, context: Context): + images = self.spec.get("image", + self.spec.get("_", None)) + + if images is None: + raise Exception("Image name should be specified for docker-pull step") + + base_args = ['docker', 'pull'] + + tasks = [] + for image in images.split(' '): + args = list(base_args) + args.append(image) + tasks.append(asyncio.create_task(self._run_process(args))) + + await asyncio.wait(tasks) diff --git a/tests/test_runner_docker_pull.py b/tests/test_runner_docker_pull.py new file mode 100644 index 0000000..c983a01 --- /dev/null +++ b/tests/test_runner_docker_pull.py @@ -0,0 +1,45 @@ +import asyncio +import unittest + +from runci.entities.config import Project, Target, Step +from runci.entities.parameters import Parameters +from runci.engine.core import create_context, DependencyTree +from runci.engine.runner.docker_pull import DockerPullRunner +from unittest.mock import patch, call + + +class test_runner_docker_build(unittest.TestCase): + step = Step("test", "docker-pull", { + "image": "busybox alpine" + }) + + project = Project( + services=[], + targets=[Target( + name="target", + dependencies=[], + steps=[step] + )]) + parameters = Parameters(dataconnection="runci.yml", targets=["target"], verbosity=0) + + @patch('runci.engine.runner.docker_pull.DockerPullRunner._run_process') + def test_command_line_args(self, mock): + async def run(): + runner = DockerPullRunner(lambda e: None, self.step.spec) + context = create_context(self.project, self.parameters) + await runner.run(context) + + asyncio.run(run()) + mock.assert_has_calls([ + call('docker pull busybox'.split(' ')), + call('docker pull alpine'.split(' '))]) + + @patch('runci.engine.runner.docker_pull.DockerPullRunner.run') + def test_integration(self, mock): + context = create_context(self.project, self.parameters) + DependencyTree(context).run() + mock.assert_called_once() + + +if __name__ == '__main__': + unittest.main()