1 #!/usr/bin/python 2 #pylint: disable=C0111 3 4 import unittest 5 6 import common 7 from autotest_lib.client.common_lib import base_utils 8 from autotest_lib.client.common_lib import error 9 from autotest_lib.client.common_lib import lsbrelease_utils 10 from autotest_lib.client.common_lib import site_utils 11 from autotest_lib.client.common_lib import utils 12 from autotest_lib.client.common_lib.test_utils import mock 13 14 15 metrics = utils.metrics_mock 16 17 def test_function(arg1, arg2, arg3, arg4=4, arg5=5, arg6=6): 18 """Test global function. 19 """ 20 21 22 class TestClass(object): 23 """Test class. 24 """ 25 26 def test_instance_function(self, arg1, arg2, arg3, arg4=4, arg5=5, arg6=6): 27 """Test instance function. 28 """ 29 30 31 @classmethod 32 def test_class_function(cls, arg1, arg2, arg3, arg4=4, arg5=5, arg6=6): 33 """Test class function. 34 """ 35 36 37 @staticmethod 38 def test_static_function(arg1, arg2, arg3, arg4=4, arg5=5, arg6=6): 39 """Test static function. 40 """ 41 42 43 class GetFunctionArgUnittest(unittest.TestCase): 44 """Tests for method get_function_arg_value.""" 45 46 def run_test(self, func, insert_arg): 47 """Run test. 48 49 @param func: Function being called with given arguments. 50 @param insert_arg: Set to True to insert an object in the argument list. 51 This is to mock instance/class object. 52 """ 53 if insert_arg: 54 args = (None, 1, 2, 3) 55 else: 56 args = (1, 2, 3) 57 for i in range(1, 7): 58 self.assertEquals(utils.get_function_arg_value( 59 func, 'arg%d'%i, args, {}), i) 60 61 self.assertEquals(utils.get_function_arg_value( 62 func, 'arg7', args, {'arg7': 7}), 7) 63 self.assertRaises( 64 KeyError, utils.get_function_arg_value, 65 func, 'arg3', args[:-1], {}) 66 67 68 def test_global_function(self): 69 """Test global function. 70 """ 71 self.run_test(test_function, False) 72 73 74 def test_instance_function(self): 75 """Test instance function. 76 """ 77 self.run_test(TestClass().test_instance_function, True) 78 79 80 def test_class_function(self): 81 """Test class function. 82 """ 83 self.run_test(TestClass.test_class_function, True) 84 85 86 def test_static_function(self): 87 """Test static function. 88 """ 89 self.run_test(TestClass.test_static_function, False) 90 91 92 class VersionMatchUnittest(unittest.TestCase): 93 """Test version_match function.""" 94 95 def test_version_match(self): 96 """Test version_match function.""" 97 canary_build = 'lumpy-release/R43-6803.0.0' 98 canary_release = '6803.0.0' 99 cq_build = 'lumpy-release/R43-6803.0.0-rc1' 100 cq_release = '6803.0.0-rc1' 101 trybot_paladin_build = 'trybot-lumpy-paladin/R43-6803.0.0-b123' 102 trybot_paladin_release = '6803.0.2015_03_12_2103' 103 trybot_pre_cq_build = 'trybot-wifi-pre-cq/R43-7000.0.0-b36' 104 trybot_pre_cq_release = '7000.0.2016_03_12_2103' 105 trybot_toolchain_build = 'trybot-nyan_big-gcc-toolchain/R56-8936.0.0-b14' 106 trybot_toolchain_release = '8936.0.2016_10_26_1403' 107 108 109 builds = [canary_build, cq_build, trybot_paladin_build, 110 trybot_pre_cq_build, trybot_toolchain_build] 111 releases = [canary_release, cq_release, trybot_paladin_release, 112 trybot_pre_cq_release, trybot_toolchain_release] 113 for i in range(len(builds)): 114 for j in range(len(releases)): 115 self.assertEqual( 116 utils.version_match(builds[i], releases[j]), i==j, 117 'Build version %s should%s match release version %s.' % 118 (builds[i], '' if i==j else ' not', releases[j])) 119 120 121 class IsPuppylabVmUnittest(unittest.TestCase): 122 """Test is_puppylab_vm function.""" 123 124 def test_is_puppylab_vm(self): 125 """Test is_puppylab_vm function.""" 126 self.assertTrue(utils.is_puppylab_vm('localhost:8001')) 127 self.assertTrue(utils.is_puppylab_vm('127.0.0.1:8002')) 128 self.assertFalse(utils.is_puppylab_vm('localhost')) 129 self.assertFalse(utils.is_puppylab_vm('localhost:')) 130 self.assertFalse(utils.is_puppylab_vm('127.0.0.1')) 131 self.assertFalse(utils.is_puppylab_vm('127.0.0.1:')) 132 self.assertFalse(utils.is_puppylab_vm('chromeos-server.mtv')) 133 self.assertFalse(utils.is_puppylab_vm('chromeos-server.mtv:8001')) 134 135 136 class IsInSameSubnetUnittest(unittest.TestCase): 137 """Test is_in_same_subnet function.""" 138 139 def test_is_in_same_subnet(self): 140 """Test is_in_same_subnet function.""" 141 self.assertTrue(utils.is_in_same_subnet('192.168.0.0', '192.168.1.2', 142 23)) 143 self.assertFalse(utils.is_in_same_subnet('192.168.0.0', '192.168.1.2', 144 24)) 145 self.assertTrue(utils.is_in_same_subnet('192.168.0.0', '192.168.0.255', 146 24)) 147 self.assertFalse(utils.is_in_same_subnet('191.168.0.0', '192.168.0.0', 148 24)) 149 150 class GetWirelessSsidUnittest(unittest.TestCase): 151 """Test get_wireless_ssid function.""" 152 153 DEFAULT_SSID = 'default' 154 SSID_1 = 'ssid_1' 155 SSID_2 = 'ssid_2' 156 SSID_3 = 'ssid_3' 157 158 def test_get_wireless_ssid(self): 159 """Test is_in_same_subnet function.""" 160 god = mock.mock_god() 161 god.stub_function_to_return(utils.CONFIG, 'get_config_value', 162 self.DEFAULT_SSID) 163 god.stub_function_to_return(utils.CONFIG, 'get_config_value_regex', 164 {'wireless_ssid_1.2.3.4/24': self.SSID_1, 165 'wireless_ssid_4.3.2.1/16': self.SSID_2, 166 'wireless_ssid_4.3.2.111/32': self.SSID_3}) 167 self.assertEqual(self.SSID_1, utils.get_wireless_ssid('1.2.3.100')) 168 self.assertEqual(self.SSID_2, utils.get_wireless_ssid('4.3.2.100')) 169 self.assertEqual(self.SSID_3, utils.get_wireless_ssid('4.3.2.111')) 170 self.assertEqual(self.DEFAULT_SSID, 171 utils.get_wireless_ssid('100.0.0.100')) 172 173 174 class LaunchControlBuildParseUnittest(unittest.TestCase): 175 """Test various parsing functions related to Launch Control builds and 176 devices. 177 """ 178 179 def test_parse_launch_control_target(self): 180 """Test parse_launch_control_target function.""" 181 target_tests = { 182 ('shamu', 'userdebug'): 'shamu-userdebug', 183 ('shamu', 'eng'): 'shamu-eng', 184 ('shamu-board', 'eng'): 'shamu-board-eng', 185 (None, None): 'bad_target', 186 (None, None): 'target'} 187 for result, target in target_tests.items(): 188 self.assertEqual(result, utils.parse_launch_control_target(target)) 189 190 191 class GetOffloaderUriTest(unittest.TestCase): 192 """Test get_offload_gsuri function.""" 193 _IMAGE_STORAGE_SERVER = 'gs://test_image_bucket' 194 195 def setUp(self): 196 self.god = mock.mock_god() 197 198 def tearDown(self): 199 self.god.unstub_all() 200 201 def test_get_default_lab_offload_gsuri(self): 202 """Test default lab offload gsuri .""" 203 self.god.mock_up(utils.CONFIG, 'CONFIG') 204 self.god.stub_function_to_return(lsbrelease_utils, 'is_moblab', False) 205 self.assertEqual(utils.DEFAULT_OFFLOAD_GSURI, 206 utils.get_offload_gsuri()) 207 208 self.god.check_playback() 209 210 def test_get_default_moblab_offload_gsuri(self): 211 self.god.mock_up(utils.CONFIG, 'CONFIG') 212 self.god.stub_function_to_return(lsbrelease_utils, 'is_moblab', True) 213 utils.CONFIG.get_config_value.expect_call( 214 'CROS', 'image_storage_server').and_return( 215 self._IMAGE_STORAGE_SERVER) 216 self.god.stub_function_to_return(site_utils, 217 'get_interface_mac_address', 'test_mac') 218 self.god.stub_function_to_return(site_utils, 'get_moblab_id', 'test_id') 219 expected_gsuri = '%sresults/%s/%s/' % ( 220 self._IMAGE_STORAGE_SERVER, 'test_mac', 'test_id') 221 cached_gsuri = site_utils.DEFAULT_OFFLOAD_GSURI 222 site_utils.DEFAULT_OFFLOAD_GSURI = None 223 gsuri = utils.get_offload_gsuri() 224 site_utils.DEFAULT_OFFLOAD_GSURI = cached_gsuri 225 self.assertEqual(expected_gsuri, gsuri) 226 227 self.god.check_playback() 228 229 def test_get_moblab_offload_gsuri(self): 230 """Test default lab offload gsuri .""" 231 self.god.mock_up(utils.CONFIG, 'CONFIG') 232 self.god.stub_function_to_return(lsbrelease_utils, 'is_moblab', True) 233 self.god.stub_function_to_return(site_utils, 234 'get_interface_mac_address', 'test_mac') 235 self.god.stub_function_to_return(site_utils, 'get_moblab_id', 'test_id') 236 gsuri = '%s%s/%s/' % ( 237 utils.DEFAULT_OFFLOAD_GSURI, 'test_mac', 'test_id') 238 self.assertEqual(gsuri, utils.get_offload_gsuri()) 239 240 self.god.check_playback() 241 242 243 class GetBuiltinEthernetNicNameTest(unittest.TestCase): 244 """Test get moblab public network interface name.""" 245 246 def setUp(self): 247 self.god = mock.mock_god() 248 249 def tearDown(self): 250 self.god.unstub_all() 251 252 def test_is_eth0(self): 253 """Test when public network interface is eth0.""" 254 run_func = self.god.create_mock_function('run_func') 255 self.god.stub_with(base_utils, 'run', run_func) 256 run_func.expect_call('readlink -f /sys/class/net/eth0').and_return( 257 base_utils.CmdResult(exit_status=0, stdout='not_u_s_b')) 258 eth = utils.get_built_in_ethernet_nic_name() 259 self.assertEqual('eth0', eth) 260 self.god.check_playback() 261 262 def test_readlin_fails(self): 263 """Test when readlink does not work.""" 264 run_func = self.god.create_mock_function('run_func') 265 self.god.stub_with(base_utils, 'run', run_func) 266 run_func.expect_call('readlink -f /sys/class/net/eth0').and_return( 267 base_utils.CmdResult(exit_status=-1, stdout='blah')) 268 eth = utils.get_built_in_ethernet_nic_name() 269 self.assertEqual('eth0', eth) 270 self.god.check_playback() 271 272 def test_no_readlink(self): 273 """Test when readlink does not exist.""" 274 run_func = self.god.create_mock_function('run_func') 275 self.god.stub_with(base_utils, 'run', run_func) 276 run_func.expect_call('readlink -f /sys/class/net/eth0').and_raises( 277 error.CmdError('readlink', 'no such command')) 278 eth = utils.get_built_in_ethernet_nic_name() 279 self.assertEqual('eth0', eth) 280 self.god.check_playback() 281 282 def test_is_eth1(self): 283 """Test when public network interface is eth1.""" 284 run_func = self.god.create_mock_function('run_func') 285 self.god.stub_with(base_utils, 'run', run_func) 286 run_func.expect_call('readlink -f /sys/class/net/eth0').and_return( 287 base_utils.CmdResult(exit_status=0, stdout='is usb')) 288 run_func.expect_call('readlink -f /sys/class/net/eth1').and_return( 289 base_utils.CmdResult(exit_status=0, stdout='not_u_s_b')) 290 eth = utils.get_built_in_ethernet_nic_name() 291 self.assertEqual('eth1', eth) 292 self.god.check_playback() 293 294 295 class MockMetricsTest(unittest.TestCase): 296 """Test metrics mock class can handle various metrics calls.""" 297 298 def test_Counter(self): 299 """Test the mock class can create an instance and call any method. 300 """ 301 c = metrics.Counter('counter') 302 c.increment(fields={'key': 1}) 303 304 305 def test_Context(self): 306 """Test the mock class can handle context class. 307 """ 308 test_value = None 309 with metrics.SecondsTimer('context') as t: 310 test_value = 'called_in_context' 311 t['random_key'] = 'pass' 312 self.assertEqual('called_in_context', test_value) 313 314 315 def test_decorator(self): 316 """Test the mock class can handle decorator. 317 """ 318 class TestClass(object): 319 320 def __init__(self): 321 self.value = None 322 323 test_value = TestClass() 324 test_value.value = None 325 @metrics.SecondsTimerDecorator('decorator') 326 def test(arg): 327 arg.value = 'called_in_decorator' 328 329 test(test_value) 330 self.assertEqual('called_in_decorator', test_value.value) 331 332 333 def test_setitem(self): 334 """Test the mock class can handle set item call. 335 """ 336 timer = metrics.SecondsTimer('name') 337 timer['random_key'] = 'pass' 338 339 340 if __name__ == "__main__": 341 unittest.main() 342