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