Mike Frysinger | a7f08bc | 2019-08-27 19:16:33 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # -*- coding: utf-8 -*- |
Mike Frysinger | 8b0fc37 | 2022-09-08 07:24:24 | [diff] [blame] | 3 | # Copyright 2014 The ChromiumOS Authors |
Gilad Arnold | 7de05f7 | 2014-02-14 21:14:20 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Unit tests for cherrypy_ext.""" |
| 8 | |
Amin Hassani | c5af426 | 2019-11-13 21:37:20 | [diff] [blame] | 9 | from __future__ import print_function |
| 10 | |
Gilad Arnold | 7de05f7 | 2014-02-14 21:14:20 | [diff] [blame] | 11 | import tempfile |
| 12 | import unittest |
| 13 | |
Amin Hassani | c5af426 | 2019-11-13 21:37:20 | [diff] [blame] | 14 | import cherrypy # pylint: disable=import-error |
Gilad Arnold | 7de05f7 | 2014-02-14 21:14:20 | [diff] [blame] | 15 | import cherrypy_ext |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 16 | import mox # pylint: disable=import-error |
Gilad Arnold | 7de05f7 | 2014-02-14 21:14:20 | [diff] [blame] | 17 | |
| 18 | |
| 19 | class CherrypyExtTest(mox.MoxTestBase): |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 20 | """Tests for the cherrypy_ext module.""" |
Gilad Arnold | 7de05f7 | 2014-02-14 21:14:20 | [diff] [blame] | 21 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 22 | def testPortFile(self): |
| 23 | """Check that PortFile correctly reports a bound port.""" |
| 24 | with tempfile.NamedTemporaryFile(delete=False) as f: |
| 25 | portfile = f.name |
| 26 | bus = self.mox.CreateMock(cherrypy.engine) |
| 27 | self.mox.StubOutWithMock(bus, "log") |
| 28 | bus.log(mox.IsA(str)).MultipleTimes() |
Gilad Arnold | 7de05f7 | 2014-02-14 21:14:20 | [diff] [blame] | 29 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 30 | cherrypy.server = self.mox.CreateMock(object) |
| 31 | cherrypy.server.httpserver = self.mox.CreateMock(object) |
| 32 | cherrypy.server.httpserver.socket = self.mox.CreateMock(object) |
| 33 | cherrypy.server.httpserver.socket.getsockname = None |
| 34 | self.mox.StubOutWithMock( |
| 35 | cherrypy.server.httpserver.socket, "getsockname" |
| 36 | ) |
| 37 | cherrypy.server.httpserver.socket.getsockname().AndReturn(None) |
| 38 | cherrypy.server.httpserver.socket.getsockname().AndReturn(("", 55555)) |
Gilad Arnold | 7de05f7 | 2014-02-14 21:14:20 | [diff] [blame] | 39 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 40 | self.mox.ReplayAll() |
Gilad Arnold | 7de05f7 | 2014-02-14 21:14:20 | [diff] [blame] | 41 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 42 | plugin = cherrypy_ext.PortFile(bus, portfile) |
| 43 | plugin.start() # Signal server start; no socket binding yet. |
| 44 | with open(portfile) as f: |
| 45 | self.assertEqual("", f.read()) |
| 46 | plugin.log("foo", 1) # Emit a log signal; socket "bound" at this point. |
| 47 | with open(portfile) as f: |
| 48 | self.assertEqual("55555", f.read()) |
Gilad Arnold | 7de05f7 | 2014-02-14 21:14:20 | [diff] [blame] | 49 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 50 | self.mox.VerifyAll() |
Gilad Arnold | 7de05f7 | 2014-02-14 21:14:20 | [diff] [blame] | 51 | |
Amin Hassani | c5af426 | 2019-11-13 21:37:20 | [diff] [blame] | 52 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 53 | if __name__ == "__main__": |
| 54 | unittest.main() |