[9217] | 1 | # Tests for local startup functions. |
---|
[17327] | 2 | import mock |
---|
[9217] | 3 | import os |
---|
| 4 | import shutil |
---|
[17327] | 5 | import sys |
---|
[9217] | 6 | import tempfile |
---|
| 7 | import unittest |
---|
[17327] | 8 | # from zdaemon.zdctl import |
---|
| 9 | import zdaemon.zdctl |
---|
[9217] | 10 | from zope.app.wsgi import WSGIPublisherApplication |
---|
[17327] | 11 | # from zdaemon.tests.testzdctl import run as run_zdctl |
---|
| 12 | from waeup.kofa.startup import ( |
---|
| 13 | env_app_factory, env_debug_app_factory, zdaemon_controller, |
---|
| 14 | ) |
---|
| 15 | try: |
---|
| 16 | from StringIO import StringIO # py2 |
---|
| 17 | except ImportError: |
---|
| 18 | from io import StringIO # py3 |
---|
[9217] | 19 | |
---|
[17327] | 20 | |
---|
| 21 | |
---|
[9217] | 22 | ZOPE_CONF_TEMPL = ''' |
---|
| 23 | site-definition %s |
---|
| 24 | |
---|
| 25 | <zodb> |
---|
| 26 | <mappingstorage /> |
---|
| 27 | </zodb> |
---|
| 28 | |
---|
| 29 | <eventlog> |
---|
| 30 | <logfile> |
---|
| 31 | path STDOUT |
---|
| 32 | </logfile> |
---|
| 33 | </eventlog> |
---|
| 34 | ''' |
---|
| 35 | |
---|
[17327] | 36 | |
---|
| 37 | ZDAEMON_CONF_TEMPL = ''' |
---|
| 38 | <runner> |
---|
| 39 | program echo "zdaemon started" |
---|
| 40 | </runner> |
---|
| 41 | ''' |
---|
| 42 | |
---|
| 43 | |
---|
[9217] | 44 | class StartUpTests(unittest.TestCase): |
---|
| 45 | |
---|
| 46 | def setUp(self): |
---|
| 47 | self.workdir = tempfile.mkdtemp() |
---|
| 48 | self.site_zcml = os.path.join(self.workdir, 'site.zcml') |
---|
| 49 | open(self.site_zcml, 'w').write('<configure />') |
---|
| 50 | self.zope_conf = os.path.join(self.workdir, 'zope.conf') |
---|
| 51 | open(self.zope_conf, 'wb').write(ZOPE_CONF_TEMPL % self.site_zcml) |
---|
[17327] | 52 | self.zdaemon_conf = os.path.join(self.workdir, 'zdaemon.conf') |
---|
| 53 | open(self.zdaemon_conf, 'wb').write(ZDAEMON_CONF_TEMPL) |
---|
[9217] | 54 | return |
---|
| 55 | |
---|
| 56 | def tearDown(self): |
---|
| 57 | shutil.rmtree(self.workdir) |
---|
| 58 | for name in ('TEST_FOO', 'TEST_BAR'): |
---|
| 59 | if name in os.environ: |
---|
| 60 | del os.environ[name] |
---|
| 61 | return |
---|
| 62 | |
---|
| 63 | def test_env_app_factory(self): |
---|
| 64 | # We can create plain WSGI apps |
---|
| 65 | factory = env_app_factory({'zope_conf': self.zope_conf}) |
---|
| 66 | self.assertTrue( |
---|
| 67 | isinstance(factory, WSGIPublisherApplication)) |
---|
| 68 | return |
---|
| 69 | |
---|
| 70 | def test_env_app_factory_single_env_var(self): |
---|
| 71 | # We can create WSGI apps with a single env var set |
---|
| 72 | factory = env_app_factory({'zope_conf': self.zope_conf, |
---|
| 73 | 'env_vars': 'TEST_FOO value1'}) |
---|
| 74 | self.assertEqual(os.environ.get('TEST_FOO', None), 'value1') |
---|
| 75 | return |
---|
| 76 | |
---|
| 77 | def test_env_app_factory_multiple_env_vars(self): |
---|
| 78 | # We can create WSGI apps with multiple env vars set |
---|
| 79 | env_vars = 'TEST_FOO value1 \n TEST_BAR value2' |
---|
| 80 | factory = env_app_factory({'zope_conf': self.zope_conf, |
---|
| 81 | 'env_vars': env_vars}) |
---|
| 82 | self.assertEqual(os.environ.get('TEST_FOO', None), 'value1') |
---|
| 83 | self.assertEqual(os.environ.get('TEST_BAR', None), 'value2') |
---|
| 84 | return |
---|
| 85 | |
---|
| 86 | def test_env_debug_app_factory(self): |
---|
| 87 | # We can create debugger WSGI apps |
---|
| 88 | factory = env_debug_app_factory({'zope_conf': self.zope_conf}) |
---|
| 89 | self.assertTrue( |
---|
| 90 | isinstance(factory, WSGIPublisherApplication)) |
---|
| 91 | return |
---|
| 92 | pass |
---|
| 93 | |
---|
| 94 | def test_env_debug_app_factory_single_env_var(self): |
---|
| 95 | # We can create debugger WSGI apps with a single env var set |
---|
| 96 | factory = env_debug_app_factory({'zope_conf': self.zope_conf, |
---|
| 97 | 'env_vars': 'TEST_FOO value1'}) |
---|
| 98 | self.assertEqual(os.environ.get('TEST_FOO', None), 'value1') |
---|
| 99 | return |
---|
[17327] | 100 | |
---|
| 101 | @mock.patch("sys.stdout", new_callable=StringIO) |
---|
| 102 | def test_zdaemon_has_debug_help(self, mock_out): |
---|
| 103 | # We provide a `debug` help text for `kofactl` |
---|
| 104 | with mock.patch.object(sys, "argv", ["kofactl", "help", "debug"]): |
---|
| 105 | with self.assertRaises(SystemExit): |
---|
| 106 | zdaemon_controller(zdaemon_conf=self.zdaemon_conf) |
---|
| 107 | self.assertTrue("providing a debugger" in mock_out.getvalue()) |
---|
| 108 | |
---|
| 109 | @mock.patch("waeup.kofa.startup.interactive_debug_prompt") |
---|
| 110 | def test_zdaemon_can_start_debug(self, mock_prompt): |
---|
| 111 | # We can actually run the debugger... |
---|
| 112 | with mock.patch.object(sys, "argv", ["kofactl", "debug"]): |
---|
| 113 | with self.assertRaises(SystemExit): |
---|
| 114 | zdaemon_controller(zdaemon_conf=self.zdaemon_conf) |
---|
| 115 | self.assertTrue(mock_prompt.called) |
---|