blob: 9242851e3f5e3dbc0e61d0df1a767a31a74e64be [file] [log] [blame]
Mike Frysingera7f08bc2019-08-27 19:16:331#!/usr/bin/env python2
2# -*- coding: utf-8 -*-
Mike Frysinger8b0fc372022-09-08 07:24:243# Copyright 2014 The ChromiumOS Authors
Gilad Arnold7de05f72014-02-14 21:14:204# 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 Hassanic5af4262019-11-13 21:37:209from __future__ import print_function
10
Gilad Arnold7de05f72014-02-14 21:14:2011import tempfile
12import unittest
13
Amin Hassanic5af4262019-11-13 21:37:2014import cherrypy # pylint: disable=import-error
Gilad Arnold7de05f72014-02-14 21:14:2015import cherrypy_ext
Jack Rosenthal8de609d2023-02-09 20:20:3516import mox # pylint: disable=import-error
Gilad Arnold7de05f72014-02-14 21:14:2017
18
19class CherrypyExtTest(mox.MoxTestBase):
Jack Rosenthal8de609d2023-02-09 20:20:3520 """Tests for the cherrypy_ext module."""
Gilad Arnold7de05f72014-02-14 21:14:2021
Jack Rosenthal8de609d2023-02-09 20:20:3522 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 Arnold7de05f72014-02-14 21:14:2029
Jack Rosenthal8de609d2023-02-09 20:20:3530 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 Arnold7de05f72014-02-14 21:14:2039
Jack Rosenthal8de609d2023-02-09 20:20:3540 self.mox.ReplayAll()
Gilad Arnold7de05f72014-02-14 21:14:2041
Jack Rosenthal8de609d2023-02-09 20:20:3542 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 Arnold7de05f72014-02-14 21:14:2049
Jack Rosenthal8de609d2023-02-09 20:20:3550 self.mox.VerifyAll()
Gilad Arnold7de05f72014-02-14 21:14:2051
Amin Hassanic5af4262019-11-13 21:37:2052
Jack Rosenthal8de609d2023-02-09 20:20:3553if __name__ == "__main__":
54 unittest.main()