1#!/usr/bin/env python 2# 3# Copyright (C) 2017 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18import threading 19import unittest 20import time 21 22try: 23 from unittest import mock 24except ImportError: 25 import mock 26 27from host_controller import tfc_host_controller 28from host_controller.tfc import command_task 29from host_controller.tfc import device_info 30 31 32class HostControllerTest(unittest.TestCase): 33 """A test for tfc_host_controller.HostController. 34 35 Args: 36 _remote_client: A mock remote_client.RemoteClient. 37 _tfc_client: A mock tfc_client.TfcClient. 38 _host_controller: The HostController being tested. 39 """ 40 _AVAILABLE_DEVICE = device_info.DeviceInfo( 41 device_serial="ABC001", 42 run_target="sailfish", 43 state="Available") 44 _ALLOCATED_DEVICE = device_info.DeviceInfo( 45 device_serial="ABC002", 46 run_target="sailfish", 47 state="Allocated") 48 _STUB_DEVICE = device_info.DeviceInfo( 49 device_serial="emulator-5554", 50 run_target="unknown", 51 state="Available", 52 stub=True) 53 _DEVICES = [_AVAILABLE_DEVICE, _ALLOCATED_DEVICE, _STUB_DEVICE] 54 _TASKS = [command_task.CommandTask(task_id="1-0", 55 command_line="vts -m SampleShellTest", 56 device_serials=["ABC001"])] 57 58 def setUp(self): 59 """Creates the HostController.""" 60 self._remote_client = mock.Mock() 61 self._tfc_client = mock.Mock() 62 self._host_controller = tfc_host_controller.HostController( 63 self._remote_client, self._tfc_client, "host1", ["cluster1"]) 64 65 @mock.patch("host_controller.invocation_thread." 66 "InvocationThread.run") 67 def testDeviceStateDuringInvocation(self, mock_run): 68 """Tests LeaseHostTasks and ListAvailableDevices.""" 69 self._remote_client.ListDevices.return_value = self._DEVICES 70 self._tfc_client.LeaseHostTasks.return_value = self._TASKS 71 run_event = threading.Event() 72 mock_run.side_effect = lambda: run_event.wait() 73 74 self._host_controller.LeaseCommandTasks() 75 devices = self._host_controller.ListAvailableDevices() 76 self.assertEqual([], devices) 77 run_event.set() 78 # Wait for thread termination 79 time.sleep(0.2) 80 devices = self._host_controller.ListAvailableDevices() 81 self.assertEqual([self._AVAILABLE_DEVICE], devices) 82 83 def testListDevices(self): 84 """Tests ListDevices.""" 85 self._remote_client.ListDevices.return_value = self._DEVICES 86 devices = self._host_controller.ListDevices() 87 self.assertEqual([self._AVAILABLE_DEVICE, self._ALLOCATED_DEVICE], 88 devices) 89 90 91if __name__ == "__main__": 92 unittest.main() 93