1"""Tests for rf_switch_ap_box.""" 2 3import mock 4import os 5import tempfile 6import unittest 7 8from autotest_lib.server import frontend 9from autotest_lib.server.cros.network import rf_switch_ap_box 10 11 12AP_CONF = '\n'.join([ 13 '[1a:2b:3c:4d:5e:6f]', 14 'brand = Google', 15 'wan_hostname = chromeos9-ap1', 16 'ssid = rf_switch_router', 17 'frequency = 2432', 18 'bss = 1a:2b:3c:4d:5e:6f', 19 'wan mac = 1a:2b:3c:4d:5e:6f', 20 'model = dummy', 21 'security = wpa2', 22 'psk = chromeos', 23 'class_name = StaticAPConfigurator']) 24 25 26class RfSwitchApBoxTest(unittest.TestCase): 27 """Tests for RFSwitchAPBox.""" 28 29 30 def setUp(self): 31 """Initial set up for the tests.""" 32 self.ap_config_file = tempfile.NamedTemporaryFile() 33 self.patcher1 = mock.patch('autotest_lib.server.frontend.Host') 34 self.patcher2 = mock.patch('os.path.join') 35 self.mock_host = self.patcher1.start() 36 self.mock_host.hostname = 'chromeos9-apbox1' 37 self.mock_os_path_join = self.patcher2.start() 38 self.mock_os_path_join.return_value = self.ap_config_file.name 39 40 41 def tearDown(self): 42 """End the patchers and Close the file.""" 43 self.patcher1.stop() 44 self.patcher2.stop() 45 self.ap_config_file.close() 46 47 48 def testGetApsList(self): 49 """Test to get all APs from an AP Box.""" 50 self.mock_host.labels = ['rf_switch_1', 'ap_box_1', 'rf_switch_aps'] 51 self.ap_config_file.write(AP_CONF) 52 self.ap_config_file.seek(0) 53 ap_box = rf_switch_ap_box.APBox(self.mock_host) 54 self.assertEquals(ap_box.ap_box_label, 'ap_box_1') 55 self.assertEquals(ap_box.rf_switch_label, 'rf_switch_1') 56 aps = ap_box.get_ap_list() 57 self.assertEquals(len(aps), 1) 58 ap = aps[0] 59 self.assertEquals(ap.get_wan_host(), 'chromeos9-ap1') 60 self.assertEquals(ap.get_bss(), '1a:2b:3c:4d:5e:6f') 61 62 63 def testMissingApboxLabel(self): 64 """Test when ap_box_label is missing """ 65 self.mock_host.labels = ['rf_switch_1', 'rf_switch_aps'] 66 with self.assertRaises(Exception) as context: 67 rf_switch_ap_box.APBox(self.mock_host) 68 self.assertTrue( 69 'AP Box chromeos9-apbox1 does not have ap_box and/or ' 70 'rf_switch labels' in context.exception) 71 72 73 def testMissingRfSwitchLabel(self): 74 """Test when rf_switch_lable is missing.""" 75 self.mock_host.labels = ['ap_box_1', 'rf_switch_aps'] 76 with self.assertRaises(Exception) as context: 77 rf_switch_ap_box.APBox(self.mock_host) 78 self.assertTrue( 79 'AP Box chromeos9-apbox1 does not have ap_box and/or ' 80 'rf_switch labels' in context.exception) 81 82 83 def testForEmptyApbox(self): 84 """Test when no APs are in the APBox.""" 85 self.mock_host.labels = ['rf_switch_1', 'ap_box_1', 'rf_switch_aps'] 86 self.ap_config_file.write('') 87 self.ap_config_file.seek(0) 88 ap_box = rf_switch_ap_box.APBox(self.mock_host) 89 aps = ap_box.get_ap_list() 90 self.assertEquals(len(aps), 0) 91 92 93if __name__ == '__main__': 94 unittest.main() 95