blob: 44211acd36fcbe8bc42b1ed707892253665fca20 [file] [log] [blame]
Mike Frysingera7f08bc2019-08-27 15:16:33 -04001#!/usr/bin/env python2
2# -*- coding: utf-8 -*-
Gilad Arnold7de05f72014-02-14 13:14:20 -08003# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
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 Hassanic5af4262019-11-13 13:37:20 -08009from __future__ import print_function
10
Gilad Arnold7de05f72014-02-14 13:14:20 -080011import tempfile
12import unittest
13
Amin Hassanic5af4262019-11-13 13:37:20 -080014import mox # pylint: disable=import-error
15import cherrypy # pylint: disable=import-error
Gilad Arnold7de05f72014-02-14 13:14:20 -080016
17import cherrypy_ext
18
19
20class CherrypyExtTest(mox.MoxTestBase):
21 """Tests for the cherrypy_ext module."""
22
23 def testPortFile(self):
24 """Check that PortFile correctly reports a bound port."""
25 with tempfile.NamedTemporaryFile(delete=False) as f:
26 portfile = f.name
27 bus = self.mox.CreateMock(cherrypy.engine)
28 self.mox.StubOutWithMock(bus, 'log')
29 bus.log(mox.IsA(str)).MultipleTimes()
30
31 cherrypy.server = self.mox.CreateMock(object)
32 cherrypy.server.httpserver = self.mox.CreateMock(object)
33 cherrypy.server.httpserver.socket = self.mox.CreateMock(object)
34 cherrypy.server.httpserver.socket.getsockname = None
35 self.mox.StubOutWithMock(cherrypy.server.httpserver.socket, 'getsockname')
36 cherrypy.server.httpserver.socket.getsockname().AndReturn(None)
37 cherrypy.server.httpserver.socket.getsockname().AndReturn(('', 55555))
38
39 self.mox.ReplayAll()
40
41 plugin = cherrypy_ext.PortFile(bus, portfile)
42 plugin.start() # Signal server start; no socket binding yet.
43 with open(portfile) as f:
44 self.assertEqual('', f.read())
45 plugin.log('foo', 1) # Emit a log signal; socket "bound" at this point.
46 with open(portfile) as f:
47 self.assertEqual('55555', f.read())
48
49 self.mox.VerifyAll()
50
Amin Hassanic5af4262019-11-13 13:37:20 -080051
Gilad Arnold7de05f72014-02-14 13:14:20 -080052if __name__ == '__main__':
53 unittest.main()