1#!/usr/bin/python 2#pylint: disable=C0111 3 4import unittest 5 6import common 7from autotest_lib.client.common_lib import utils 8from autotest_lib.client.common_lib.test_utils import mock 9 10 11def test_function(arg1, arg2, arg3, arg4=4, arg5=5, arg6=6): 12 """Test global function. 13 """ 14 15 16class TestClass(object): 17 """Test class. 18 """ 19 20 def test_instance_function(self, arg1, arg2, arg3, arg4=4, arg5=5, arg6=6): 21 """Test instance function. 22 """ 23 24 25 @classmethod 26 def test_class_function(cls, arg1, arg2, arg3, arg4=4, arg5=5, arg6=6): 27 """Test class function. 28 """ 29 30 31 @staticmethod 32 def test_static_function(arg1, arg2, arg3, arg4=4, arg5=5, arg6=6): 33 """Test static function. 34 """ 35 36 37class GetFunctionArgUnittest(unittest.TestCase): 38 """Tests for method get_function_arg_value.""" 39 40 def run_test(self, func, insert_arg): 41 """Run test. 42 43 @param func: Function being called with given arguments. 44 @param insert_arg: Set to True to insert an object in the argument list. 45 This is to mock instance/class object. 46 """ 47 if insert_arg: 48 args = (None, 1, 2, 3) 49 else: 50 args = (1, 2, 3) 51 for i in range(1, 7): 52 self.assertEquals(utils.get_function_arg_value( 53 func, 'arg%d'%i, args, {}), i) 54 55 self.assertEquals(utils.get_function_arg_value( 56 func, 'arg7', args, {'arg7': 7}), 7) 57 self.assertRaises( 58 KeyError, utils.get_function_arg_value, 59 func, 'arg3', args[:-1], {}) 60 61 62 def test_global_function(self): 63 """Test global function. 64 """ 65 self.run_test(test_function, False) 66 67 68 def test_instance_function(self): 69 """Test instance function. 70 """ 71 self.run_test(TestClass().test_instance_function, True) 72 73 74 def test_class_function(self): 75 """Test class function. 76 """ 77 self.run_test(TestClass.test_class_function, True) 78 79 80 def test_static_function(self): 81 """Test static function. 82 """ 83 self.run_test(TestClass.test_static_function, False) 84 85 86class VersionMatchUnittest(unittest.TestCase): 87 """Test version_match function.""" 88 89 def test_version_match(self): 90 """Test version_match function.""" 91 canary_build = 'lumpy-release/R43-6803.0.0' 92 canary_release = '6803.0.0' 93 cq_build = 'lumpy-release/R43-6803.0.0-rc1' 94 cq_release = '6803.0.0-rc1' 95 trybot_paladin_build = 'trybot-lumpy-paladin/R43-6803.0.0-b123' 96 trybot_paladin_release = '6803.0.2015_03_12_2103' 97 trybot_pre_cq_build = 'trybot-wifi-pre-cq/R43-7000.0.0-b36' 98 trybot_pre_cq_release = '7000.0.2016_03_12_2103' 99 100 101 builds = [canary_build, cq_build, trybot_paladin_build, 102 trybot_pre_cq_build] 103 releases = [canary_release, cq_release, trybot_paladin_release, 104 trybot_pre_cq_release] 105 for i in range(len(builds)): 106 for j in range(len(releases)): 107 self.assertEqual( 108 utils.version_match(builds[i], releases[j]), i==j, 109 'Build version %s should%s match release version %s.' % 110 (builds[i], '' if i==j else ' not', releases[j])) 111 112 113class IsPuppylabVmUnittest(unittest.TestCase): 114 """Test is_puppylab_vm function.""" 115 116 def test_is_puppylab_vm(self): 117 """Test is_puppylab_vm function.""" 118 self.assertTrue(utils.is_puppylab_vm('localhost:8001')) 119 self.assertTrue(utils.is_puppylab_vm('127.0.0.1:8002')) 120 self.assertFalse(utils.is_puppylab_vm('localhost')) 121 self.assertFalse(utils.is_puppylab_vm('localhost:')) 122 self.assertFalse(utils.is_puppylab_vm('127.0.0.1')) 123 self.assertFalse(utils.is_puppylab_vm('127.0.0.1:')) 124 self.assertFalse(utils.is_puppylab_vm('chromeos-server.mtv')) 125 self.assertFalse(utils.is_puppylab_vm('chromeos-server.mtv:8001')) 126 127 128class IsInSameSubnetUnittest(unittest.TestCase): 129 """Test is_in_same_subnet function.""" 130 131 def test_is_in_same_subnet(self): 132 """Test is_in_same_subnet function.""" 133 self.assertTrue(utils.is_in_same_subnet('192.168.0.0', '192.168.1.2', 134 23)) 135 self.assertFalse(utils.is_in_same_subnet('192.168.0.0', '192.168.1.2', 136 24)) 137 self.assertTrue(utils.is_in_same_subnet('192.168.0.0', '192.168.0.255', 138 24)) 139 self.assertFalse(utils.is_in_same_subnet('191.168.0.0', '192.168.0.0', 140 24)) 141 142class GetWirelessSsidUnittest(unittest.TestCase): 143 """Test get_wireless_ssid function.""" 144 145 DEFAULT_SSID = 'default' 146 SSID_1 = 'ssid_1' 147 SSID_2 = 'ssid_2' 148 149 def test_get_wireless_ssid(self): 150 """Test is_in_same_subnet function.""" 151 god = mock.mock_god() 152 god.stub_function_to_return(utils.CONFIG, 'get_config_value', 153 self.DEFAULT_SSID) 154 god.stub_function_to_return(utils.CONFIG, 'get_config_value_regex', 155 {'wireless_ssid_1.2.3.4/24': self.SSID_1, 156 'wireless_ssid_4.3.2.1/16': self.SSID_2}) 157 self.assertEqual(self.SSID_1, utils.get_wireless_ssid('1.2.3.100')) 158 self.assertEqual(self.SSID_2, utils.get_wireless_ssid('4.3.2.100')) 159 self.assertEqual(self.DEFAULT_SSID, 160 utils.get_wireless_ssid('100.0.0.100')) 161 162 163if __name__ == "__main__": 164 unittest.main() 165