1#!/usr/bin/env python 2"""End to end tests for the utility programs. 3 4This test assumes the utilities inside src directory have already been 5built. 6 7At the moment top_buiddir is not in the environment, but top_builddir would be 8more reliable than '..', so it's worth trying to pull it from the environment. 9""" 10 11__author__ = 'Jim Morrison <jim@twist.com>' 12 13 14import os 15import subprocess 16import time 17import unittest 18 19 20_PORT = 9893 21 22 23def _run_server(port, args): 24 srcdir = os.environ.get('srcdir', '.') 25 testdata = '%s/testdata' % srcdir 26 top_builddir = os.environ.get('top_builddir', '..') 27 base_args = ['%s/src/spdyd' % top_builddir, '-d', testdata] 28 if args: 29 base_args.extend(args) 30 base_args.extend([str(port), '%s/privkey.pem' % testdata, 31 '%s/cacert.pem' % testdata]) 32 return subprocess.Popen(base_args) 33 34def _check_server_up(port): 35 # Check this check for now. 36 time.sleep(1) 37 38def _kill_server(server): 39 while server.returncode is None: 40 server.terminate() 41 time.sleep(1) 42 server.poll() 43 44 45class EndToEndSpdyTests(unittest.TestCase): 46 @classmethod 47 def setUpClass(cls): 48 cls.setUpServer([]) 49 50 @classmethod 51 def setUpServer(cls, args): 52 cls.server = _run_server(_PORT, args) 53 _check_server_up(_PORT) 54 55 @classmethod 56 def tearDownClass(cls): 57 _kill_server(cls.server) 58 59 def setUp(self): 60 build_dir = os.environ.get('top_builddir', '..') 61 self.client = '%s/src/spdycat' % build_dir 62 self.stdout = 'No output' 63 64 def call(self, path, args): 65 full_args = [self.client,'http://localhost:%d%s' % (_PORT, path)] + args 66 p = subprocess.Popen(full_args, stdout=subprocess.PIPE, 67 stdin=subprocess.PIPE) 68 self.stdout, self.stderr = p.communicate() 69 return p.returncode 70 71 72class EndToEndSpdy2Tests(EndToEndSpdyTests): 73 def testSimpleRequest(self): 74 self.assertEquals(0, self.call('/', [])) 75 76 def testSimpleRequestSpdy3(self): 77 self.assertEquals(0, self.call('/', ['-v', '-3'])) 78 self.assertIn('NPN selected the protocol: spdy/3', self.stdout) 79 80 def testFailedRequests(self): 81 self.assertEquals( 82 2, self.call('/', ['https://localhost:25/', 'http://localhost:79'])) 83 84 def testOneFailedRequest(self): 85 self.assertEquals(1, subprocess.call([self.client, 'http://localhost:2/'])) 86 87 def testOneTimedOutRequest(self): 88 self.assertEquals(1, self.call('/?spdyd_do_not_respond_to_req=yes', 89 ['--timeout=2'])) 90 self.assertEquals(0, self.call('/', ['--timeout=20'])) 91 92 93class EndToEndSpdy3Tests(EndToEndSpdyTests): 94 @classmethod 95 def setUpClass(cls): 96 cls.setUpServer(['-3']) 97 98 def testSimpleRequest(self): 99 self.assertEquals(0, self.call('/', ['-v'])) 100 self.assertIn('NPN selected the protocol: spdy/3', self.stdout) 101 102 103if __name__ == '__main__': 104 unittest.main() 105