1#!/usr/bin/python 2# Copyright (c) 2012 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import mox 7import unittest 8 9from config import rpm_config 10import rpm_dispatcher 11 12DUT_SAME_RPM1 = 'chromeos-rack8e-hostbs1' 13DUT_SAME_RPM2 = 'chromeos-rack8e-hostbs2' 14RPM_HOSTNAME = 'chromeos-rack8e-rpm1' 15DUT_DIFFERENT_RPM = 'chromeos-rack1-hostbs1' 16FAKE_DISPATCHER_URI = 'fake-dispatcher' 17FAKE_DISPATCHER_PORT = 9999 18FRONT_END_URI = rpm_config.get('RPM_INFRASTRUCTURE', 'frontend_uri') 19PROPER_URI_FORMAT = 'http://%s:%d' 20 21 22class TestRPMDispatcher(mox.MoxTestBase): 23 """ 24 Simple unit tests to verify that the RPM Dispatcher properly registers with 25 the frontend server, and also initializes and reuses the same RPM Controller 26 for DUT requests on the same RPM. 27 28 queue_request is the only public method of RPM Dispatcher, however its logic 29 is simple and relies mostly on the private methods; therefore, I am testing 30 primarily RPMDispatcher initialization and _get_rpm_controller (which calls 31 _create_rpm_controller) to verify correct implementation. 32 """ 33 34 def setUp(self): 35 super(TestRPMDispatcher, self).setUp() 36 self.frontend_mock = self.mox.CreateMockAnything() 37 expected_uri = PROPER_URI_FORMAT % (FAKE_DISPATCHER_URI, 38 FAKE_DISPATCHER_PORT) 39 self.frontend_mock.register_dispatcher(expected_uri) 40 rpm_dispatcher.xmlrpclib.ServerProxy = self.mox.CreateMockAnything() 41 rpm_dispatcher.xmlrpclib.ServerProxy(FRONT_END_URI).AndReturn( 42 self.frontend_mock) 43 rpm_dispatcher.atexit = self.mox.CreateMockAnything() 44 rpm_dispatcher.atexit.register(mox.IgnoreArg()) 45 self.mox.ReplayAll() 46 self.dispatcher = rpm_dispatcher.RPMDispatcher(FAKE_DISPATCHER_URI, 47 FAKE_DISPATCHER_PORT) 48 49 50 def testRegistration(self): 51 """ 52 Make sure that as a dispatcher is initialized it properly registered 53 with the frontend server. 54 """ 55 self.mox.VerifyAll() 56 57 58 def testGetSameRPMController(self): 59 """ 60 Make sure that calls to _get_rpm_controller with DUT hostnames that 61 belong to the same RPM device create and retrieve the same RPMController 62 instance. 63 """ 64 controller1 = self.dispatcher._get_rpm_controller(RPM_HOSTNAME) 65 controller2 = self.dispatcher._get_rpm_controller(RPM_HOSTNAME) 66 self.assertEquals(controller1, controller2) 67 68 69 def testGetDifferentRPMController(self): 70 """ 71 Make sure that calls to _get_rpm_controller with DUT hostnames that 72 belong to the different RPM device create and retrieve different 73 RPMController instances. 74 """ 75 controller1 = self.dispatcher._get_rpm_controller(DUT_SAME_RPM1) 76 controller2 = self.dispatcher._get_rpm_controller(DUT_DIFFERENT_RPM) 77 self.assertNotEquals(controller1, controller2) 78 79 80if __name__ == '__main__': 81 unittest.main() 82