annotate test/test_demo.py @ 6324:3e33b22a3158

BAsic test of demo and server intialization. This needs more work. I exit the server by hacking serve_forever to raise a KeyboardInterrupt. Otherwise the test just hangs. I need a replacement that exits after one (or more configurable) connections and some way to background the server so the test can make the connections to the running server instance.
author John Rouillard <rouilj@ieee.org>
date Fri, 05 Feb 2021 00:09:08 -0500
parents
children 5a3a386aa8e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6324
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
1 import unittest
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
2 import os, sys, shutil
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
3
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
4 from roundup.demo import install_demo, run_demo
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
5
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
6 import roundup.scripts.roundup_server
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
7
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
8 # https://stackoverflow.com/questions/4219717/how-to-assert-output-with-nosetest-unittest-in-python
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
9 # lightly modified
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
10 from contextlib import contextmanager
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
11 _py3 = sys.version_info[0] > 2
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
12 if _py3:
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
13 from io import StringIO # py3
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
14 else:
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
15 from StringIO import StringIO # py2
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
16 @contextmanager
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
17 def captured_output():
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
18 new_out, new_err = StringIO(), StringIO()
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
19 old_out, old_err = sys.stdout, sys.stderr
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
20 try:
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
21 sys.stdout, sys.stderr = new_out, new_err
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
22 yield sys.stdout, sys.stderr
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
23 finally:
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
24 sys.stdout, sys.stderr = old_out, old_err
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
25
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
26 class TestDemo(unittest.TestCase):
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
27 def setUp(self):
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
28 self.home = os.path.abspath('_test_demo')
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
29
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
30 def tearDown(self):
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
31 try:
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
32 shutil.rmtree(self.home)
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
33 except FileNotFoundError:
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
34 pass
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
35
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
36 def testDemo(self):
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
37 with captured_output() as (out, err):
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
38 install_demo(self.home, 'anydbm', 'classic')
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
39 output = out.getvalue().strip()
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
40 print(output)
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
41
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
42 # dummy up the return of get_server so the serve_forever method
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
43 # raises keyboard interrupt exiting the server so the test exits.
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
44 gs = roundup.scripts.roundup_server.ServerConfig.get_server
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
45 def raise_KeyboardInterrupt():
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
46 raise KeyboardInterrupt
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
47
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
48 def test_get_server(self):
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
49 httpd = gs(self)
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
50 httpd.serve_forever = raise_KeyboardInterrupt
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
51 return httpd
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
52
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
53 roundup.scripts.roundup_server.ServerConfig.get_server = test_get_server
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
54
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
55 # Run under context manager to capture output of startup text.
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
56 with captured_output() as (out, err):
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
57 run_demo(self.home)
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
58 output = out.getvalue().strip()
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
59 print(output)
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
60 # if the server installed and started this will be the
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
61 # last line in the output.
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
62 self.assertIn("Keyboard Interrupt: exiting", output.split('\n'))
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
63
3e33b22a3158 BAsic test of demo and server intialization.
John Rouillard <rouilj@ieee.org>
parents:
diff changeset
64

Roundup Issue Tracker: http://roundup-tracker.org/