2019-05-11 22:11:44 +00:00
|
|
|
import unittest
|
|
|
|
import unittest.mock
|
|
|
|
import tempfile
|
|
|
|
|
2019-06-03 22:44:12 +00:00
|
|
|
from tutor import config as tutor_config
|
2019-05-11 22:11:44 +00:00
|
|
|
from tutor import env
|
2019-06-24 10:36:19 +00:00
|
|
|
from tutor import interactive
|
2019-05-11 22:11:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConfigTests(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
# This is necessary to avoid cached mocks
|
|
|
|
env.Renderer.reset()
|
|
|
|
|
2019-05-20 17:09:58 +00:00
|
|
|
def test_version(self):
|
|
|
|
defaults = tutor_config.load_defaults()
|
|
|
|
self.assertNotIn("TUTOR_VERSION", defaults)
|
|
|
|
|
2019-05-11 22:11:44 +00:00
|
|
|
def test_merge(self):
|
2019-05-29 09:14:06 +00:00
|
|
|
config1 = {"x": "y"}
|
|
|
|
config2 = {"x": "z"}
|
|
|
|
tutor_config.merge(config1, config2)
|
|
|
|
self.assertEqual({"x": "y"}, config1)
|
|
|
|
|
|
|
|
def test_merge_render(self):
|
2019-05-11 22:11:44 +00:00
|
|
|
config = {}
|
|
|
|
defaults = tutor_config.load_defaults()
|
|
|
|
with unittest.mock.patch.object(
|
|
|
|
tutor_config.utils, "random_string", return_value="abcd"
|
|
|
|
):
|
|
|
|
tutor_config.merge(config, defaults)
|
|
|
|
|
|
|
|
self.assertEqual("abcd", config["MYSQL_ROOT_PASSWORD"])
|
2019-05-29 09:20:37 +00:00
|
|
|
|
2019-05-11 22:10:14 +00:00
|
|
|
@unittest.mock.patch.object(tutor_config.fmt, "echo")
|
2019-06-05 13:43:51 +00:00
|
|
|
def test_update_twice(self, _):
|
2019-05-11 22:11:44 +00:00
|
|
|
with tempfile.TemporaryDirectory() as root:
|
2019-06-05 13:43:51 +00:00
|
|
|
tutor_config.update(root)
|
2019-05-11 22:11:44 +00:00
|
|
|
config1 = tutor_config.load_user(root)
|
|
|
|
|
2019-06-05 13:43:51 +00:00
|
|
|
tutor_config.update(root)
|
2019-05-11 22:11:44 +00:00
|
|
|
config2 = tutor_config.load_user(root)
|
|
|
|
|
|
|
|
self.assertEqual(config1, config2)
|
|
|
|
|
2019-05-11 22:10:14 +00:00
|
|
|
@unittest.mock.patch.object(tutor_config.fmt, "echo")
|
2019-05-09 07:51:06 +00:00
|
|
|
def test_removed_entry_is_added_on_save(self, _):
|
2019-05-11 22:11:44 +00:00
|
|
|
with tempfile.TemporaryDirectory() as root:
|
|
|
|
with unittest.mock.patch.object(
|
|
|
|
tutor_config.utils, "random_string"
|
|
|
|
) as mock_random_string:
|
|
|
|
mock_random_string.return_value = "abcd"
|
2019-05-09 07:51:06 +00:00
|
|
|
config1, _ = tutor_config.load_all(root)
|
2019-05-11 22:11:44 +00:00
|
|
|
password1 = config1["MYSQL_ROOT_PASSWORD"]
|
|
|
|
|
|
|
|
config1.pop("MYSQL_ROOT_PASSWORD")
|
2020-02-27 16:14:00 +00:00
|
|
|
tutor_config.save_config_file(root, config1)
|
2019-05-11 22:11:44 +00:00
|
|
|
|
|
|
|
mock_random_string.return_value = "efgh"
|
2019-05-09 07:51:06 +00:00
|
|
|
config2, _ = tutor_config.load_all(root)
|
2019-05-11 22:11:44 +00:00
|
|
|
password2 = config2["MYSQL_ROOT_PASSWORD"]
|
|
|
|
|
|
|
|
self.assertEqual("abcd", password1)
|
|
|
|
self.assertEqual("efgh", password2)
|
2019-06-24 10:36:19 +00:00
|
|
|
|
|
|
|
def test_interactive_load_all(self):
|
|
|
|
with tempfile.TemporaryDirectory() as rootdir:
|
|
|
|
config, defaults = interactive.load_all(rootdir, interactive=False)
|
|
|
|
|
|
|
|
self.assertIn("MYSQL_ROOT_PASSWORD", config)
|
|
|
|
self.assertEqual(8, len(config["MYSQL_ROOT_PASSWORD"]))
|
|
|
|
self.assertNotIn("LMS_HOST", config)
|
|
|
|
self.assertEqual("www.myopenedx.com", defaults["LMS_HOST"])
|
|
|
|
self.assertEqual("studio.{{ LMS_HOST }}", defaults["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
|
|
|
|
|
|
|
def test_is_service_activated(self):
|
|
|
|
config = {"ACTIVATE_SERVICE1": True, "ACTIVATE_SERVICE2": False}
|
|
|
|
|
|
|
|
self.assertTrue(tutor_config.is_service_activated(config, "service1"))
|
|
|
|
self.assertFalse(tutor_config.is_service_activated(config, "service2"))
|