blob: 12d0fce95b7f37fdf5ccabc4cfad96a23024a082 [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
9import cherrypy
10import tempfile
11import unittest
12
13import mox
14
15import cherrypy_ext
16
17
18class CherrypyExtTest(mox.MoxTestBase):
19 """Tests for the cherrypy_ext module."""
20
21 def testPortFile(self):
22 """Check that PortFile correctly reports a bound port."""
23 with tempfile.NamedTemporaryFile(delete=False) as f:
24 portfile = f.name
25 bus = self.mox.CreateMock(cherrypy.engine)
26 self.mox.StubOutWithMock(bus, 'log')
27 bus.log(mox.IsA(str)).MultipleTimes()
28
29 cherrypy.server = self.mox.CreateMock(object)
30 cherrypy.server.httpserver = self.mox.CreateMock(object)
31 cherrypy.server.httpserver.socket = self.mox.CreateMock(object)
32 cherrypy.server.httpserver.socket.getsockname = None
33 self.mox.StubOutWithMock(cherrypy.server.httpserver.socket, 'getsockname')
34 cherrypy.server.httpserver.socket.getsockname().AndReturn(None)
35 cherrypy.server.httpserver.socket.getsockname().AndReturn(('', 55555))
36
37 self.mox.ReplayAll()
38
39 plugin = cherrypy_ext.PortFile(bus, portfile)
40 plugin.start() # Signal server start; no socket binding yet.
41 with open(portfile) as f:
42 self.assertEqual('', f.read())
43 plugin.log('foo', 1) # Emit a log signal; socket "bound" at this point.
44 with open(portfile) as f:
45 self.assertEqual('55555', f.read())
46
47 self.mox.VerifyAll()
48
Gilad Arnold7de05f72014-02-14 13:14:20 -080049if __name__ == '__main__':
50 unittest.main()