2019-05-11 22:11:44 +00:00
|
|
|
import tempfile
|
2019-05-11 17:31:18 +00:00
|
|
|
import unittest
|
2019-05-11 22:10:14 +00:00
|
|
|
import unittest.mock
|
2019-05-11 17:31:18 +00:00
|
|
|
|
2019-06-03 22:44:12 +00:00
|
|
|
from tutor import config as tutor_config
|
2019-05-11 17:31:18 +00:00
|
|
|
from tutor import env
|
|
|
|
from tutor import exceptions
|
|
|
|
|
|
|
|
|
|
|
|
class EnvTests(unittest.TestCase):
|
|
|
|
def test_walk_templates(self):
|
|
|
|
templates = list(env.walk_templates("local"))
|
|
|
|
self.assertIn("local/docker-compose.yml", templates)
|
|
|
|
|
|
|
|
def test_pathjoin(self):
|
|
|
|
self.assertEqual(
|
|
|
|
"/tmp/env/target/dummy", env.pathjoin("/tmp", "target", "dummy")
|
|
|
|
)
|
|
|
|
self.assertEqual("/tmp/env/dummy", env.pathjoin("/tmp", "dummy"))
|
|
|
|
|
|
|
|
def test_render_str(self):
|
|
|
|
self.assertEqual(
|
|
|
|
"hello world", env.render_str({"name": "world"}, "hello {{ name }}")
|
|
|
|
)
|
|
|
|
|
2019-05-21 10:34:29 +00:00
|
|
|
def test_common_domain(self):
|
|
|
|
self.assertEqual(
|
|
|
|
"mydomain.com",
|
|
|
|
env.render_str(
|
|
|
|
{"d1": "d1.mydomain.com", "d2": "d2.mydomain.com"},
|
|
|
|
"{{ d1|common_domain(d2) }}",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2019-05-11 17:31:18 +00:00
|
|
|
def test_render_str_missing_configuration(self):
|
|
|
|
self.assertRaises(exceptions.TutorError, env.render_str, {}, "hello {{ name }}")
|
2019-05-11 22:11:44 +00:00
|
|
|
|
|
|
|
def test_render_file(self):
|
|
|
|
config = {}
|
|
|
|
tutor_config.merge(config, tutor_config.load_defaults())
|
|
|
|
config["MYSQL_ROOT_PASSWORD"] = "testpassword"
|
2019-06-05 13:43:51 +00:00
|
|
|
rendered = env.render_file(config, "scripts", "mysql-client", "init")
|
2019-05-11 22:11:44 +00:00
|
|
|
self.assertIn("testpassword", rendered)
|
|
|
|
|
2019-05-11 22:10:14 +00:00
|
|
|
@unittest.mock.patch.object(tutor_config.fmt, "echo")
|
|
|
|
def test_render_file_missing_configuration(self, _):
|
2019-05-11 22:11:44 +00:00
|
|
|
self.assertRaises(
|
|
|
|
exceptions.TutorError, env.render_file, {}, "local", "docker-compose.yml"
|
|
|
|
)
|
2019-05-20 17:09:58 +00:00
|
|
|
|
|
|
|
def test_render_full(self):
|
2019-05-21 10:34:29 +00:00
|
|
|
defaults = tutor_config.load_defaults()
|
|
|
|
with tempfile.TemporaryDirectory() as root:
|
|
|
|
env.render_full(root, defaults)
|
|
|
|
|
|
|
|
def test_render_full_with_https(self):
|
|
|
|
defaults = tutor_config.load_defaults()
|
|
|
|
defaults["ACTIVATE_HTTPS"] = True
|
2019-05-20 17:09:58 +00:00
|
|
|
with tempfile.TemporaryDirectory() as root:
|
|
|
|
env.render_full(root, defaults)
|
2019-05-29 09:14:06 +00:00
|
|
|
|
|
|
|
def test_patch(self):
|
|
|
|
patches = {"plugin1": "abcd", "plugin2": "efgh"}
|
|
|
|
with unittest.mock.patch.object(
|
|
|
|
env.plugins, "iter_patches", return_value=patches.items()
|
|
|
|
) as mock_iter_patches:
|
|
|
|
rendered = env.render_str({}, '{{ patch("location") }}')
|
|
|
|
mock_iter_patches.assert_called_once_with({}, "location")
|
|
|
|
self.assertEqual("abcd\nefgh", rendered)
|
|
|
|
|
|
|
|
def test_patch_separator_suffix(self):
|
|
|
|
patches = {"plugin1": "abcd", "plugin2": "efgh"}
|
|
|
|
with unittest.mock.patch.object(
|
|
|
|
env.plugins, "iter_patches", return_value=patches.items()
|
2019-06-03 22:44:12 +00:00
|
|
|
):
|
2019-05-29 09:14:06 +00:00
|
|
|
rendered = env.render_str(
|
|
|
|
{}, '{{ patch("location", separator=",\n", suffix=",") }}'
|
|
|
|
)
|
|
|
|
self.assertEqual("abcd,\nefgh,", rendered)
|