source: main/waeup.kofa/trunk/src/waeup/kofa/tests/test_startup.py @ 17341

Last change on this file since 17341 was 17327, checked in by uli, 20 months ago

Upgrade grokcore.startup

while still keeping the possibility to use zdaemon. This change requires
rerunning buildout.

  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1# Tests for local startup functions.
2import mock
3import os
4import shutil
5import sys
6import tempfile
7import unittest
8# from zdaemon.zdctl import
9import zdaemon.zdctl
10from zope.app.wsgi import WSGIPublisherApplication
11# from zdaemon.tests.testzdctl import run as run_zdctl
12from waeup.kofa.startup import (
13        env_app_factory, env_debug_app_factory, zdaemon_controller,
14        )
15try:
16    from StringIO import StringIO  # py2
17except ImportError:
18    from io import StringIO        # py3
19
20
21
22ZOPE_CONF_TEMPL = '''
23site-definition %s
24
25<zodb>
26  <mappingstorage />
27</zodb>
28
29<eventlog>
30  <logfile>
31    path STDOUT
32  </logfile>
33</eventlog>
34'''
35
36
37ZDAEMON_CONF_TEMPL = '''
38<runner>
39  program echo "zdaemon started"
40</runner>
41'''
42
43
44class 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)
52        self.zdaemon_conf = os.path.join(self.workdir, 'zdaemon.conf')
53        open(self.zdaemon_conf, 'wb').write(ZDAEMON_CONF_TEMPL)
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
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)
Note: See TracBrowser for help on using the repository browser.