2021-11-23 08:25:09 +00:00
|
|
|
import json
|
|
|
|
import os
|
2019-05-11 22:11:44 +00:00
|
|
|
import unittest
|
2021-02-25 08:09:14 +00:00
|
|
|
from unittest.mock import Mock, patch
|
2019-05-11 22:11:44 +00:00
|
|
|
|
2021-11-08 13:46:38 +00:00
|
|
|
import click
|
|
|
|
|
2022-02-07 17:11:43 +00:00
|
|
|
from tests.helpers import PluginsTestCase, temporary_root
|
2019-06-03 22:44:12 +00:00
|
|
|
from tutor import config as tutor_config
|
feat: strongly typed hooks
Now that the mypy bugs have been resolved, we are able to define more precisely
and cleanly the types of Actions and Filters.
Moreover, can now strongly type named actions and hooks (in consts.py). With
such a strong typing, we get early alerts of hooks called with incorrect
arguments, which is nothing short of awesome :)
This change breaks the hooks API by removing the `context=...` argument. The
reason for that is that we cannot insert arbitrary arguments between `P.args,
P.kwargs`: https://peps.python.org/pep-0612/#the-components-of-a-paramspec
> A function declared as def inner(a: A, b: B, *args: P.args, **kwargs:
> P.kwargs) -> R has type Callable[Concatenate[A, B, P], R]. Placing
> keyword-only parameters between the *args and **kwargs is forbidden.
Getting the documentation to build in nitpicky mode is quite difficult... We
need to add `nitpick_ignore` to the docs conf.py, otherwise sphinx complains
about many missing class references. This, despite upgrading almost all doc
requirements (except docutils).
2022-10-06 10:05:01 +00:00
|
|
|
from tutor import fmt, hooks, interactive, utils
|
2021-11-23 08:25:09 +00:00
|
|
|
from tutor.types import Config, get_typed
|
2019-05-11 22:11:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConfigTests(unittest.TestCase):
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_version(self) -> None:
|
2022-02-07 17:11:43 +00:00
|
|
|
defaults = tutor_config.get_defaults()
|
2019-05-20 17:09:58 +00:00
|
|
|
self.assertNotIn("TUTOR_VERSION", defaults)
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_merge(self) -> None:
|
2021-04-06 10:09:00 +00:00
|
|
|
config1: Config = {"x": "y"}
|
|
|
|
config2: Config = {"x": "z"}
|
2019-05-29 09:14:06 +00:00
|
|
|
tutor_config.merge(config1, config2)
|
|
|
|
self.assertEqual({"x": "y"}, config1)
|
|
|
|
|
2021-11-08 13:46:38 +00:00
|
|
|
def test_merge_not_render(self) -> None:
|
2021-04-06 10:09:00 +00:00
|
|
|
config: Config = {}
|
2022-02-07 17:11:43 +00:00
|
|
|
base = tutor_config.get_base()
|
feat: strongly typed hooks
Now that the mypy bugs have been resolved, we are able to define more precisely
and cleanly the types of Actions and Filters.
Moreover, can now strongly type named actions and hooks (in consts.py). With
such a strong typing, we get early alerts of hooks called with incorrect
arguments, which is nothing short of awesome :)
This change breaks the hooks API by removing the `context=...` argument. The
reason for that is that we cannot insert arbitrary arguments between `P.args,
P.kwargs`: https://peps.python.org/pep-0612/#the-components-of-a-paramspec
> A function declared as def inner(a: A, b: B, *args: P.args, **kwargs:
> P.kwargs) -> R has type Callable[Concatenate[A, B, P], R]. Placing
> keyword-only parameters between the *args and **kwargs is forbidden.
Getting the documentation to build in nitpicky mode is quite difficult... We
need to add `nitpick_ignore` to the docs conf.py, otherwise sphinx complains
about many missing class references. This, despite upgrading almost all doc
requirements (except docutils).
2022-10-06 10:05:01 +00:00
|
|
|
with patch.object(utils, "random_string", return_value="abcd"):
|
2021-11-08 13:46:38 +00:00
|
|
|
tutor_config.merge(config, base)
|
2019-05-11 22:11:44 +00:00
|
|
|
|
2021-11-08 13:46:38 +00:00
|
|
|
# Check that merge does not perform a rendering
|
|
|
|
self.assertNotEqual("abcd", config["MYSQL_ROOT_PASSWORD"])
|
2019-05-29 09:20:37 +00:00
|
|
|
|
feat: strongly typed hooks
Now that the mypy bugs have been resolved, we are able to define more precisely
and cleanly the types of Actions and Filters.
Moreover, can now strongly type named actions and hooks (in consts.py). With
such a strong typing, we get early alerts of hooks called with incorrect
arguments, which is nothing short of awesome :)
This change breaks the hooks API by removing the `context=...` argument. The
reason for that is that we cannot insert arbitrary arguments between `P.args,
P.kwargs`: https://peps.python.org/pep-0612/#the-components-of-a-paramspec
> A function declared as def inner(a: A, b: B, *args: P.args, **kwargs:
> P.kwargs) -> R has type Callable[Concatenate[A, B, P], R]. Placing
> keyword-only parameters between the *args and **kwargs is forbidden.
Getting the documentation to build in nitpicky mode is quite difficult... We
need to add `nitpick_ignore` to the docs conf.py, otherwise sphinx complains
about many missing class references. This, despite upgrading almost all doc
requirements (except docutils).
2022-10-06 10:05:01 +00:00
|
|
|
@patch.object(fmt, "echo")
|
2021-11-23 08:25:09 +00:00
|
|
|
def test_update_twice_should_return_same_config(self, _: Mock) -> None:
|
|
|
|
with temporary_root() as root:
|
2021-11-08 13:46:38 +00:00
|
|
|
config1 = tutor_config.load_minimal(root)
|
|
|
|
tutor_config.save_config_file(root, config1)
|
|
|
|
config2 = tutor_config.load_minimal(root)
|
2019-05-11 22:11:44 +00:00
|
|
|
|
|
|
|
self.assertEqual(config1, config2)
|
|
|
|
|
2021-11-08 13:46:38 +00:00
|
|
|
def test_interactive(self) -> None:
|
|
|
|
def mock_prompt(*_args: None, **kwargs: str) -> str:
|
|
|
|
return kwargs["default"]
|
|
|
|
|
2021-11-23 08:25:09 +00:00
|
|
|
with temporary_root() as rootdir:
|
2021-11-08 13:46:38 +00:00
|
|
|
with patch.object(click, "prompt", new=mock_prompt):
|
|
|
|
with patch.object(click, "confirm", new=mock_prompt):
|
2022-04-18 22:33:55 +00:00
|
|
|
config = tutor_config.load_minimal(rootdir)
|
|
|
|
interactive.ask_questions(config)
|
2019-06-24 10:36:19 +00:00
|
|
|
|
|
|
|
self.assertIn("MYSQL_ROOT_PASSWORD", config)
|
2021-04-06 10:09:00 +00:00
|
|
|
self.assertEqual(8, len(get_typed(config, "MYSQL_ROOT_PASSWORD", str)))
|
2021-11-08 13:46:38 +00:00
|
|
|
self.assertEqual("www.myopenedx.com", config["LMS_HOST"])
|
|
|
|
self.assertEqual("studio.www.myopenedx.com", config["CMS_HOST"])
|
Improve job running in local and k8s
Running jobs was previously done with "exec". This was because it
allowed us to avoid copying too much container specification information
from the docker-compose/deployments files to the jobs files. However,
this was limiting:
- In order to run a job, the corresponding container had to be running.
This was particularly painful in Kubernetes, where containers are
crashing as long as migrations are not correctly run.
- Containers in which we need to run jobs needed to be present in the
docker-compose/deployments files. This is unnecessary, for example when
mysql is disabled, or in the case of the certbot container.
Now, we create dedicated jobs files, both for local and k8s deployment.
This introduces a little redundancy, but not too much. Note that
dependent containers are not listed in the docker-compose.jobs.yml file,
so an actual platform is still supposed to be running when we launch the
jobs.
This also introduces a subtle change: now, jobs go through the container
entrypoint prior to running. This is probably a good thing, as it will
avoid forgetting about incorrect environment variables.
In k8s, we find ourselves interacting way too much with the kubectl
utility. Parsing output from the CLI is a pain. So we need to switch to
the native kubernetes client library.
2020-03-25 17:47:36 +00:00
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_is_service_activated(self) -> None:
|
2021-04-06 10:09:00 +00:00
|
|
|
config: Config = {"RUN_SERVICE1": True, "RUN_SERVICE2": False}
|
Improve job running in local and k8s
Running jobs was previously done with "exec". This was because it
allowed us to avoid copying too much container specification information
from the docker-compose/deployments files to the jobs files. However,
this was limiting:
- In order to run a job, the corresponding container had to be running.
This was particularly painful in Kubernetes, where containers are
crashing as long as migrations are not correctly run.
- Containers in which we need to run jobs needed to be present in the
docker-compose/deployments files. This is unnecessary, for example when
mysql is disabled, or in the case of the certbot container.
Now, we create dedicated jobs files, both for local and k8s deployment.
This introduces a little redundancy, but not too much. Note that
dependent containers are not listed in the docker-compose.jobs.yml file,
so an actual platform is still supposed to be running when we launch the
jobs.
This also introduces a subtle change: now, jobs go through the container
entrypoint prior to running. This is probably a good thing, as it will
avoid forgetting about incorrect environment variables.
In k8s, we find ourselves interacting way too much with the kubectl
utility. Parsing output from the CLI is a pain. So we need to switch to
the native kubernetes client library.
2020-03-25 17:47:36 +00:00
|
|
|
self.assertTrue(tutor_config.is_service_activated(config, "service1"))
|
|
|
|
self.assertFalse(tutor_config.is_service_activated(config, "service2"))
|
2021-11-23 08:25:09 +00:00
|
|
|
|
feat: strongly typed hooks
Now that the mypy bugs have been resolved, we are able to define more precisely
and cleanly the types of Actions and Filters.
Moreover, can now strongly type named actions and hooks (in consts.py). With
such a strong typing, we get early alerts of hooks called with incorrect
arguments, which is nothing short of awesome :)
This change breaks the hooks API by removing the `context=...` argument. The
reason for that is that we cannot insert arbitrary arguments between `P.args,
P.kwargs`: https://peps.python.org/pep-0612/#the-components-of-a-paramspec
> A function declared as def inner(a: A, b: B, *args: P.args, **kwargs:
> P.kwargs) -> R has type Callable[Concatenate[A, B, P], R]. Placing
> keyword-only parameters between the *args and **kwargs is forbidden.
Getting the documentation to build in nitpicky mode is quite difficult... We
need to add `nitpick_ignore` to the docs conf.py, otherwise sphinx complains
about many missing class references. This, despite upgrading almost all doc
requirements (except docutils).
2022-10-06 10:05:01 +00:00
|
|
|
@patch.object(fmt, "echo")
|
2021-11-23 08:25:09 +00:00
|
|
|
def test_json_config_is_overwritten_by_yaml(self, _: Mock) -> None:
|
|
|
|
with temporary_root() as root:
|
|
|
|
# Create config from scratch
|
|
|
|
config_yml_path = os.path.join(root, tutor_config.CONFIG_FILENAME)
|
|
|
|
config_json_path = os.path.join(
|
|
|
|
root, tutor_config.CONFIG_FILENAME.replace("yml", "json")
|
|
|
|
)
|
|
|
|
config = tutor_config.load_full(root)
|
|
|
|
|
|
|
|
# Save config to json
|
|
|
|
with open(config_json_path, "w", encoding="utf-8") as f:
|
|
|
|
json.dump(config, f, ensure_ascii=False, indent=4)
|
|
|
|
self.assertFalse(os.path.exists(config_yml_path))
|
|
|
|
self.assertTrue(os.path.exists(config_json_path))
|
|
|
|
|
|
|
|
# Reload and compare
|
|
|
|
current = tutor_config.load_full(root)
|
|
|
|
self.assertTrue(os.path.exists(config_yml_path))
|
|
|
|
self.assertFalse(os.path.exists(config_json_path))
|
|
|
|
self.assertEqual(config, current)
|
2022-02-07 17:11:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConfigPluginTestCase(PluginsTestCase):
|
feat: strongly typed hooks
Now that the mypy bugs have been resolved, we are able to define more precisely
and cleanly the types of Actions and Filters.
Moreover, can now strongly type named actions and hooks (in consts.py). With
such a strong typing, we get early alerts of hooks called with incorrect
arguments, which is nothing short of awesome :)
This change breaks the hooks API by removing the `context=...` argument. The
reason for that is that we cannot insert arbitrary arguments between `P.args,
P.kwargs`: https://peps.python.org/pep-0612/#the-components-of-a-paramspec
> A function declared as def inner(a: A, b: B, *args: P.args, **kwargs:
> P.kwargs) -> R has type Callable[Concatenate[A, B, P], R]. Placing
> keyword-only parameters between the *args and **kwargs is forbidden.
Getting the documentation to build in nitpicky mode is quite difficult... We
need to add `nitpick_ignore` to the docs conf.py, otherwise sphinx complains
about many missing class references. This, despite upgrading almost all doc
requirements (except docutils).
2022-10-06 10:05:01 +00:00
|
|
|
@patch.object(fmt, "echo")
|
2022-02-07 17:11:43 +00:00
|
|
|
def test_removed_entry_is_added_on_save(self, _: Mock) -> None:
|
|
|
|
with temporary_root() as root:
|
|
|
|
mock_random_string = Mock()
|
|
|
|
|
|
|
|
hooks.Filters.ENV_TEMPLATE_FILTERS.add_item(
|
|
|
|
("random_string", mock_random_string),
|
|
|
|
)
|
|
|
|
mock_random_string.return_value = "abcd"
|
|
|
|
config1 = tutor_config.load_full(root)
|
|
|
|
password1 = config1.pop("MYSQL_ROOT_PASSWORD")
|
|
|
|
|
|
|
|
tutor_config.save_config_file(root, config1)
|
|
|
|
|
|
|
|
mock_random_string.return_value = "efgh"
|
|
|
|
config2 = tutor_config.load_full(root)
|
|
|
|
password2 = config2["MYSQL_ROOT_PASSWORD"]
|
|
|
|
|
|
|
|
self.assertEqual("abcd", password1)
|
|
|
|
self.assertEqual("efgh", password2)
|