1#!/usr/bin/python 2# 3# Copyright (c) 2013 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7"""Unit tests for server/cros/ap_configurators/ap_configurator_factory.py. 8""" 9 10import mox 11import unittest 12 13import common 14 15from autotest_lib.server.cros.ap_configurators import \ 16 ap_configurator_config 17from autotest_lib.server.cros.ap_configurators import \ 18 ap_configurator_factory 19from autotest_lib.server.cros.ap_configurators import \ 20 ap_spec 21 22 23class APConfiguratorFactoryTest(mox.MoxTestBase): 24 """Unit tests for ap_configurator_factory.APConfiguratorFactory.""" 25 26 27 class MockAp(object): 28 """Mock object used to test _get_aps_with_bands().""" 29 30 def __init__(self, bands_and_channels=[], bands_and_modes=[], 31 supported_securities=[], visibility_supported=False, 32 host_name='mock_ap', 33 configurator_type=ap_spec.CONFIGURATOR_ANY): 34 """Constructor. 35 36 @param bands_and_channels: a list of dicts of strings, e.g. 37 [{'band': self.ap_config.BAND_2GHZ, 'channels': [5]}, 38 {'band': self.ap_config.BAND_5GHZ, 'channels': [48]}] 39 @param bands_and_modes: a list of dicts of strings, e.g. 40 [{'band': self.ap_config.BAND_2GHZ, 41 'modes': [self.ap_config.MODE_B]}, 42 {'band': self.ap_config.BAND_5GHZ, 43 'modes': [self.ap_config.MODE_G]}] 44 @param supported_securities: a list of integers. 45 @param visibility_supported: a boolean 46 """ 47 self.bands_and_channels = bands_and_channels 48 self.bands_and_modes = bands_and_modes 49 self.supported_securities = supported_securities 50 self.visibility_supported = visibility_supported 51 self.host_name = host_name 52 self.channel = None 53 self.configurator_type = configurator_type 54 55 56 def get_supported_bands(self): 57 """@returns supported bands and channels.""" 58 return self.bands_and_channels 59 60 61 def get_supported_modes(self): 62 """@returns supported bands and modes.""" 63 return self.bands_and_modes 64 65 66 def is_security_mode_supported(self, security): 67 """Checks if security is supported. 68 69 @param security: an integer, security method. 70 @returns a boolean, True iff security is supported. 71 """ 72 return security in self.supported_securities 73 74 75 def is_visibility_supported(self): 76 """Returns if visibility is supported.""" 77 return self.visibility_supported 78 79 80 def host_name(self): 81 """Returns the host name of the AP.""" 82 return self.host_name 83 84 85 def set_using_ap_spec(self, ap_spec): 86 """Sets a limited numberof setting of the AP. 87 88 @param ap_spec: APSpec object 89 """ 90 self.channel = ap_spec.channel 91 92 93 def configurator_type(self): 94 """Returns the configurator type.""" 95 return self.configurator_type 96 97 98 def get_channel(self): 99 """Returns the channel.""" 100 return self.channel 101 102 103 def setUp(self): 104 """Initialize.""" 105 super(APConfiguratorFactoryTest, self).setUp() 106 self.factory = ap_configurator_factory.APConfiguratorFactory() 107 # ap_config is used to fetch constants such as bands, modes, etc. 108 self.ap_config = ap_configurator_config.APConfiguratorConfig() 109 110 111 """New tests that cover the new ap_spec use case.""" 112 def _build_ap_test_inventory(self): 113 # AP1 supports 2.4GHz band, all modes, and all securities. 114 self.mock_ap1 = self.MockAp( 115 bands_and_channels=[{'band': ap_spec.BAND_2GHZ, 116 'channels': ap_spec.VALID_2GHZ_CHANNELS}], 117 bands_and_modes=[{'band': ap_spec.BAND_2GHZ, 118 'modes': ap_spec.VALID_2GHZ_MODES}], 119 supported_securities=ap_spec.VALID_SECURITIES, 120 host_name='chromeos3-row2-rack1-host1', 121 configurator_type=ap_spec.CONFIGURATOR_STATIC 122 ) 123 # AP2 supports 2.4 and 5 GHz, all modes, open system, and visibility. 124 self.mock_ap2 = self.MockAp( 125 bands_and_channels=[{'band': ap_spec.BAND_2GHZ, 126 'channels': ap_spec.VALID_2GHZ_CHANNELS}, 127 {'band': ap_spec.BAND_5GHZ, 128 'channels': ap_spec.VALID_5GHZ_CHANNELS}], 129 bands_and_modes=[{'band': ap_spec.BAND_2GHZ, 130 'modes': ap_spec.VALID_2GHZ_MODES}, 131 {'band': ap_spec.BAND_5GHZ, 132 'modes': ap_spec.VALID_5GHZ_MODES}], 133 supported_securities=[ap_spec.SECURITY_TYPE_DISABLED], 134 visibility_supported=True, 135 configurator_type=ap_spec.CONFIGURATOR_DYNAMIC, 136 host_name='chromeos3-row2-rack1-host2', 137 ) 138 # AP3 supports 2.4GHz band, all modes, all securities, and is not 139 # in the lab. 140 self.mock_ap3 = self.MockAp( 141 bands_and_channels=[{'band': ap_spec.BAND_2GHZ, 142 'channels': ap_spec.VALID_2GHZ_CHANNELS}], 143 bands_and_modes=[{'band': ap_spec.BAND_2GHZ, 144 'modes': ap_spec.VALID_2GHZ_MODES}], 145 supported_securities=ap_spec.VALID_SECURITIES, 146 host_name='chromeos3-row7-rack1-host2', 147 configurator_type=ap_spec.CONFIGURATOR_STATIC 148 ) 149 self.factory.ap_list = [self.mock_ap1, self.mock_ap2, self.mock_ap3] 150 151 152 def testGetApConfigurators_WithBandAPSpec(self): 153 """Test with a band only specified AP Spec""" 154 self._build_ap_test_inventory() 155 156 spec = ap_spec.APSpec(band=ap_spec.BAND_2GHZ) 157 actual = self.factory.get_ap_configurators_by_spec(spec=spec) 158 self.assertEquals([self.mock_ap1, self.mock_ap2].sort(), actual.sort()) 159 160 spec = ap_spec.APSpec(band=ap_spec.BAND_5GHZ) 161 actual = self.factory.get_ap_configurators_by_spec(spec=spec) 162 self.assertEquals([self.mock_ap2], actual) 163 164 165 def testGetAPConfigurators_WithModeAPSpec(self): 166 """Test with a mode only specified AP Spec""" 167 self._build_ap_test_inventory() 168 169 spec = ap_spec.APSpec(mode=ap_spec.DEFAULT_2GHZ_MODE) 170 actual = self.factory.get_ap_configurators_by_spec(spec=spec) 171 self.assertEquals([self.mock_ap1, self.mock_ap2].sort(), actual.sort()) 172 173 spec = ap_spec.APSpec(mode=ap_spec.DEFAULT_5GHZ_MODE) 174 actual = self.factory.get_ap_configurators_by_spec(spec=spec) 175 self.assertEquals([self.mock_ap2], actual) 176 177 178 def testGetAPConfigurators_WithSecurityAPSpec(self): 179 """Test with a security only specified AP Spec""" 180 self._build_ap_test_inventory() 181 spec = ap_spec.APSpec(security=ap_spec.SECURITY_TYPE_WPAPSK) 182 actual = self.factory.get_ap_configurators_by_spec(spec=spec) 183 self.assertEquals([self.mock_ap1], actual) 184 185 186 def testGetAPConfigurators_WithVisibilityAPSpec(self): 187 """Test with a visibility specified AP Spec.""" 188 self._build_ap_test_inventory() 189 190 spec = ap_spec.APSpec(visible=True) 191 actual = self.factory.get_ap_configurators_by_spec(spec=spec) 192 self.assertEquals([self.mock_ap1, self.mock_ap2].sort(), actual.sort()) 193 194 spec = ap_spec.APSpec(band=ap_spec.BAND_5GHZ, visible=False) 195 actual = self.factory.get_ap_configurators_by_spec(spec=spec) 196 self.assertEquals([self.mock_ap2], actual) 197 198 199 def testGetAPConfigurators_ByHostName(self): 200 """Test obtaining a list of APs by hostname.""" 201 self._build_ap_test_inventory() 202 203 spec = ap_spec.APSpec(hostnames=['chromeos3-row2-rack1-host1']) 204 actual = self.factory.get_ap_configurators_by_spec(spec=spec) 205 self.assertEquals([self.mock_ap1], actual) 206 207 spec = ap_spec.APSpec(hostnames=['chromeos3-row2-rack1-host1', 208 'chromeos3-row2-rack1-host2']) 209 actual = self.factory.get_ap_configurators_by_spec(spec=spec) 210 self.assertEquals([self.mock_ap1, self.mock_ap2].sort(), actual.sort()) 211 212 213 def testGetAndPreConfigureAPConfigurators(self): 214 """Test preconfiguring APs.""" 215 self._build_ap_test_inventory() 216 217 # Pick something that is not the default channel. 218 channel = ap_spec.VALID_5GHZ_CHANNELS[-1] 219 spec = ap_spec.APSpec(channel=channel) 220 actual = self.factory.get_ap_configurators_by_spec(spec=spec, 221 pre_configure=True) 222 self.assertEquals([self.mock_ap2], actual) 223 self.assertEquals(actual[0].get_channel(), channel) 224 225 226 def testGetAPConfigurators_ByType(self): 227 """Test obtaining configurators by type.""" 228 self._build_ap_test_inventory() 229 230 spec = ap_spec.APSpec(configurator_type=ap_spec.CONFIGURATOR_STATIC) 231 actual = self.factory.get_ap_configurators_by_spec(spec=spec, 232 pre_configure=True) 233 self.assertEquals([self.mock_ap1], actual) 234 235 236 def testGetAPConfigurators_ByLab(self): 237 """Test obtaining configurators by location relative to the lab.""" 238 self._build_ap_test_inventory() 239 240 spec = ap_spec.APSpec(lab_ap=False) 241 actual = self.factory.get_ap_configurators_by_spec(spec=spec, 242 pre_configure=True) 243 self.assertEquals([self.mock_ap3], actual) 244 245 246if __name__ == '__main__': 247 unittest.main() 248