1"""Tests for rf_switch_utils.""" 2 3import unittest 4 5import mock 6 7from autotest_lib.server.cros.network import rf_switch_utils 8 9 10class RfSwitchUtilsTest(unittest.TestCase): 11 """Tests for RF Switch Utils.""" 12 13 14 def setUp(self): 15 """Initial set up for the tests.""" 16 self.patcher1 = mock.patch('autotest_lib.server.frontend.AFE') 17 self.patcher2 = mock.patch('autotest_lib.server.frontend.Host') 18 self.mock_afe = self.patcher1.start() 19 self.mock_host = self.patcher2.start() 20 21 22 def tearDown(self): 23 """End mock patcher.""" 24 self.patcher1.stop() 25 self.patcher2.stop() 26 27 28 def testAllocateRfSwitch(self): 29 """Test to allocate a RF Switch.""" 30 afe_instance = self.mock_afe() 31 mock_host_instance = self.mock_host() 32 mock_host_instance.hostname = 'chromeos9-rfswitch1' 33 # AFE returns list of RF Switch Hosts. 34 afe_instance.get_hosts.return_value = [mock_host_instance] 35 # Locking the first available RF Switch Host. 36 afe_instance.lock_host.return_value = True 37 rf_switch = rf_switch_utils.allocate_rf_switch() 38 self.assertEquals(rf_switch, mock_host_instance) 39 self.assertEquals(rf_switch.hostname, 'chromeos9-rfswitch1') 40 41 42 def testRfSwitchCannotBeLocked(self): 43 """Test logs when RF Switch cannot be locked.""" 44 afe_instance = self.mock_afe() 45 mock_host_instance = self.mock_host() 46 afe_instance.get_hosts.return_value = [mock_host_instance] 47 mock_host_instance.hostname = 'chromeos9-rfswitch1' 48 afe_instance.lock_host.return_value = False 49 with mock.patch('logging.Logger.error') as mock_logger: 50 rf_switch_utils.allocate_rf_switch() 51 mock_logger.assert_called_with( 52 'RF Switch chromeos9-rfswitch1 could not be locked') 53 54 55 def testRfSwitchesNotAvailable(self): 56 """Test logs when no RF Switches are available to lock.""" 57 afe_instance = self.mock_afe() 58 afe_instance.get_hosts.return_value = [] 59 with mock.patch('logging.Logger.debug') as mock_logger: 60 rf_switch_utils.allocate_rf_switch() 61 mock_logger.assert_called_with( 62 'No RF Switches are available for tests.') 63 64 65 def testDeallocateRfSwitch(self): 66 """Test to deallocate RF Switch.""" 67 afe_instance = self.mock_afe() 68 mock_host_instance = self.mock_host() 69 mock_host_instance.locked = False 70 afe_instance.get_hosts.return_value = mock_host_instance 71 self.assertEquals( 72 rf_switch_utils.deallocate_rf_switch(mock_host_instance), True) 73 74 75 def testRfSwitchCannotBeDeallocated(self): 76 """Test when RF Switch cannot be deallocated.""" 77 afe_instance = self.mock_afe() 78 mock_host_instance = self.mock_host() 79 mock_host_instance.locked = True 80 afe_instance.get_hosts.return_value = mock_host_instance 81 self.assertEquals( 82 rf_switch_utils.deallocate_rf_switch(mock_host_instance), False) 83 84 85if __name__ == '__main__': 86 unittest.main() 87