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