6
0
mirror of https://github.com/ChristianLight/tutor.git synced 2024-12-13 06:37:46 +00:00
tutor/tests/commands/test_images.py
Régis Behmo 15b219e235 feat: migrate to plugins.v1 with filters & actions
This is a very large refactoring which aims at making Tutor both more
extendable and more generic. Historically, the Tutor plugin system was
designed as an ad-hoc solution to allow developers to modify their own
Open edX platforms without having to fork Tutor. The plugin API was
simple, but limited, because of its ad-hoc nature. As a consequence,
there were many things that plugin developers could not do, such as
extending different parts of the CLI or adding custom template filters.

Here, we refactor the whole codebase to make use of a generic plugin
system. This system was inspired by the Wordpress plugin API and the
Open edX "hooks and filters" API. The various components are added to a
small core thanks to a set of actions and filters. Actions are callback
functions that can be triggered at different points of the application
lifecycle. Filters are functions that modify some data. Both actions and
filters are collectively named as "hooks". Hooks can optionally be
created within a certain context, which makes it easier to keep track of
which application created which callback.

This new hooks system allows us to provide a Python API that developers
can use to extend their applications. The API reference is added to the
documentation, along with a new plugin development tutorial.

The plugin v0 API remains supported for backward compatibility of
existing plugins.

Done:
- Do not load commands from plugins which are not enabled.
- Load enabled plugins once on start.
- Implement contexts for actions and filters, which allow us to keep track of
  the source of every hook.
- Migrate patches
- Migrate commands
- Migrate plugin detection
- Migrate templates_root
- Migrate config
- Migrate template environment globals and filters
- Migrate hooks to tasks
- Generate hook documentation
- Generate patch reference documentation
- Add the concept of action priority

Close #499.
2022-04-15 15:30:54 +02:00

146 lines
5.2 KiB
Python

from unittest.mock import Mock, patch
from tests.helpers import PluginsTestCase, temporary_root
from tutor import images, plugins
from tutor.__about__ import __version__
from tutor.commands.images import ImageNotFoundError
from .base import TestCommandMixin
class ImagesTests(PluginsTestCase, TestCommandMixin):
def test_images_help(self) -> None:
result = self.invoke(["images", "--help"])
self.assertIsNone(result.exception)
self.assertEqual(0, result.exit_code)
def test_images_pull_image(self) -> None:
result = self.invoke(["images", "pull"])
self.assertIsNone(result.exception)
self.assertEqual(0, result.exit_code)
def test_images_pull_plugin_invalid_plugin_should_throw_error(self) -> None:
result = self.invoke(["images", "pull", "plugin"])
self.assertEqual(1, result.exit_code)
self.assertEqual(ImageNotFoundError, type(result.exception))
@patch.object(images, "pull", return_value=None)
def test_images_pull_plugin(self, image_pull: Mock) -> None:
plugins.v0.DictPlugin(
{
"name": "plugin1",
"hooks": {
"remote-image": {
"service1": "service1:1.0.0",
"service2": "service2:2.0.0",
}
},
}
)
plugins.load("plugin1")
result = self.invoke(["images", "pull", "service1"])
self.assertIsNone(result.exception)
self.assertEqual(0, result.exit_code)
image_pull.assert_called_once_with("service1:1.0.0")
@patch.object(images, "pull", return_value=None)
def test_images_pull_all_vendor_images(self, image_pull: Mock) -> None:
result = self.invoke(["images", "pull", "mysql"])
self.assertIsNone(result.exception)
self.assertEqual(0, result.exit_code)
# Note: we should update this tag whenever the mysql image is updated
image_pull.assert_called_once_with("docker.io/mysql:5.7.35")
def test_images_printtag_image(self) -> None:
result = self.invoke(["images", "printtag", "openedx"])
self.assertIsNone(result.exception)
self.assertEqual(0, result.exit_code)
self.assertRegex(
result.output, rf"docker.io/overhangio/openedx:{__version__}\n"
)
def test_images_printtag_plugin(self) -> None:
plugins.v0.DictPlugin(
{
"name": "plugin1",
"hooks": {
"build-image": {
"service1": "service1:1.0.0",
"service2": "service2:2.0.0",
}
},
}
)
plugins.load("plugin1")
result = self.invoke(["images", "printtag", "service1"])
self.assertIsNone(result.exception)
self.assertEqual(0, result.exit_code, result)
self.assertEqual(result.output, "service1:1.0.0\n")
@patch.object(images, "build", return_value=None)
def test_images_build_plugin(self, mock_image_build: Mock) -> None:
plugins.v0.DictPlugin(
{
"name": "plugin1",
"hooks": {
"build-image": {
"service1": "service1:1.0.0",
"service2": "service2:2.0.0",
}
},
}
)
plugins.load("plugin1")
with temporary_root() as root:
self.invoke_in_root(root, ["config", "save"])
result = self.invoke_in_root(root, ["images", "build", "service1"])
self.assertIsNone(result.exception)
self.assertEqual(0, result.exit_code)
mock_image_build.assert_called()
self.assertIn("service1:1.0.0", mock_image_build.call_args[0])
@patch.object(images, "build", return_value=None)
def test_images_build_plugin_with_args(self, image_build: Mock) -> None:
plugins.v0.DictPlugin(
{
"name": "plugin1",
"hooks": {
"build-image": {
"service1": "service1:1.0.0",
"service2": "service2:2.0.0",
}
},
}
)
plugins.load("plugin1")
build_args = [
"images",
"build",
"--no-cache",
"-a",
"myarg=value",
"--add-host",
"host",
"--target",
"target",
"-d",
"docker_args",
"service1",
]
with temporary_root() as root:
self.invoke_in_root(root, ["config", "save"])
result = self.invoke_in_root(root, build_args)
self.assertIsNone(result.exception)
self.assertEqual(0, result.exit_code)
image_build.assert_called()
self.assertIn("service1:1.0.0", image_build.call_args[0])
for arg in image_build.call_args[0][2:]:
# The only extra args are `--build-arg`
if arg != "--build-arg":
self.assertIn(arg, build_args)
def test_images_push(self) -> None:
result = self.invoke(["images", "push"])
self.assertIsNone(result.exception)
self.assertEqual(0, result.exit_code)