1c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski#!/usr/bin/python 2c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 3d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth Bimport errno 4d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth Bimport logging 5d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth Bimport os 6d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth Bimport select 7d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth Bimport shutil 8d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth Bimport socket 9d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth Bimport StringIO 10d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth Bimport subprocess 11d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth Bimport time 12d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth Bimport unittest 13d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth Bimport urllib2 14c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 15c5205b81292cd323fb9e85354c7ab945d42638a1jadmanskiimport common 16861b2d54aec24228cdb3895dbc40062cb40cb2adEric Lifrom autotest_lib.client.common_lib import base_utils, autotemp 17c5205b81292cd323fb9e85354c7ab945d42638a1jadmanskifrom autotest_lib.client.common_lib.test_utils import mock 18c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 19c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 20c5205b81292cd323fb9e85354c7ab945d42638a1jadmanskiclass test_read_one_line(unittest.TestCase): 21c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski def setUp(self): 22861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.god = mock.mock_god(ut=self) 23861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.god.stub_function(base_utils, "open") 24c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 25c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 26c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski def tearDown(self): 27c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.god.unstub_all() 28c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 29c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 3081edd79142cf9229b59a680d34fa40e23ede3e01mbligh def test_ip_to_long(self): 31861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.ip_to_long('0.0.0.0'), 0) 32861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.ip_to_long('255.255.255.255'), 4294967295) 33861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.ip_to_long('192.168.0.1'), 3232235521) 34861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.ip_to_long('1.2.4.8'), 16909320) 3581edd79142cf9229b59a680d34fa40e23ede3e01mbligh 3681edd79142cf9229b59a680d34fa40e23ede3e01mbligh 3781edd79142cf9229b59a680d34fa40e23ede3e01mbligh def test_long_to_ip(self): 38861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.long_to_ip(0), '0.0.0.0') 39861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.long_to_ip(4294967295), '255.255.255.255') 40861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.long_to_ip(3232235521), '192.168.0.1') 41861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.long_to_ip(16909320), '1.2.4.8') 4281edd79142cf9229b59a680d34fa40e23ede3e01mbligh 4381edd79142cf9229b59a680d34fa40e23ede3e01mbligh 4481edd79142cf9229b59a680d34fa40e23ede3e01mbligh def test_create_subnet_mask(self): 45861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.create_subnet_mask(0), 0) 46861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.create_subnet_mask(32), 4294967295) 47861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.create_subnet_mask(25), 4294967168) 4881edd79142cf9229b59a680d34fa40e23ede3e01mbligh 4981edd79142cf9229b59a680d34fa40e23ede3e01mbligh 5081edd79142cf9229b59a680d34fa40e23ede3e01mbligh def test_format_ip_with_mask(self): 51861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.format_ip_with_mask('192.168.0.1', 0), 5281edd79142cf9229b59a680d34fa40e23ede3e01mbligh '0.0.0.0/0') 53861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.format_ip_with_mask('192.168.0.1', 32), 5481edd79142cf9229b59a680d34fa40e23ede3e01mbligh '192.168.0.1/32') 55861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.format_ip_with_mask('192.168.0.1', 26), 5681edd79142cf9229b59a680d34fa40e23ede3e01mbligh '192.168.0.0/26') 57861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.format_ip_with_mask('192.168.0.255', 26), 5881edd79142cf9229b59a680d34fa40e23ede3e01mbligh '192.168.0.192/26') 5981edd79142cf9229b59a680d34fa40e23ede3e01mbligh 6081edd79142cf9229b59a680d34fa40e23ede3e01mbligh 61c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski def create_test_file(self, contents): 62c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski test_file = StringIO.StringIO(contents) 63861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.open.expect_call("filename", "r").and_return(test_file) 64c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 65c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 66c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski def test_reads_one_line_file(self): 67c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.create_test_file("abc\n") 68861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual("abc", base_utils.read_one_line("filename")) 69c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.god.check_playback() 70c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 71c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 72c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski def test_strips_read_lines(self): 73c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.create_test_file("abc \n") 74861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual("abc ", base_utils.read_one_line("filename")) 75c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.god.check_playback() 76c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 77c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 78c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski def test_drops_extra_lines(self): 79c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.create_test_file("line 1\nline 2\nline 3\n") 80861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual("line 1", base_utils.read_one_line("filename")) 81c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.god.check_playback() 82c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 83c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 84c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski def test_works_on_empty_file(self): 85c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.create_test_file("") 86861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual("", base_utils.read_one_line("filename")) 87c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.god.check_playback() 88c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 89c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 90c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski def test_works_on_file_with_no_newlines(self): 91c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.create_test_file("line but no newline") 92c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.assertEqual("line but no newline", 93861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.read_one_line("filename")) 94c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.god.check_playback() 95c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 96c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 97c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski def test_preserves_leading_whitespace(self): 98c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.create_test_file(" has leading whitespace") 99c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.assertEqual(" has leading whitespace", 100861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.read_one_line("filename")) 101c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 102c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 103c5205b81292cd323fb9e85354c7ab945d42638a1jadmanskiclass test_write_one_line(unittest.TestCase): 104c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski def setUp(self): 105861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.god = mock.mock_god(ut=self) 106861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.god.stub_function(base_utils, "open") 107c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 108c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 109c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski def tearDown(self): 110c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.god.unstub_all() 111c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 112c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 113c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski def get_write_one_line_output(self, content): 114b9d055122081a7a0a159770c5df60bb8f5f187cdmbligh test_file = mock.SaveDataAfterCloseStringIO() 115861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.open.expect_call("filename", "w").and_return(test_file) 116861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.write_one_line("filename", content) 117c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.god.check_playback() 118b9d055122081a7a0a159770c5df60bb8f5f187cdmbligh return test_file.final_data 119c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 120c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 121c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski def test_writes_one_line_file(self): 122c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.assertEqual("abc\n", self.get_write_one_line_output("abc")) 123c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 124c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 125c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski def test_preserves_existing_newline(self): 126c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.assertEqual("abc\n", self.get_write_one_line_output("abc\n")) 127c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 128c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 129c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski def test_preserves_leading_whitespace(self): 130c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.assertEqual(" abc\n", self.get_write_one_line_output(" abc")) 131c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 132c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 133c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski def test_preserves_trailing_whitespace(self): 134c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.assertEqual("abc \n", self.get_write_one_line_output("abc ")) 135c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 136c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 137c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski def test_handles_empty_input(self): 138c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski self.assertEqual("\n", self.get_write_one_line_output("")) 139c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 140c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski 141b9d055122081a7a0a159770c5df60bb8f5f187cdmblighclass test_open_write_close(unittest.TestCase): 142b9d055122081a7a0a159770c5df60bb8f5f187cdmbligh def setUp(self): 143861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.god = mock.mock_god(ut=self) 144861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.god.stub_function(base_utils, "open") 145b9d055122081a7a0a159770c5df60bb8f5f187cdmbligh 146b9d055122081a7a0a159770c5df60bb8f5f187cdmbligh 147b9d055122081a7a0a159770c5df60bb8f5f187cdmbligh def tearDown(self): 148b9d055122081a7a0a159770c5df60bb8f5f187cdmbligh self.god.unstub_all() 149b9d055122081a7a0a159770c5df60bb8f5f187cdmbligh 150b9d055122081a7a0a159770c5df60bb8f5f187cdmbligh 151b9d055122081a7a0a159770c5df60bb8f5f187cdmbligh def test_simple_functionality(self): 152b9d055122081a7a0a159770c5df60bb8f5f187cdmbligh data = "\n\nwhee\n" 153b9d055122081a7a0a159770c5df60bb8f5f187cdmbligh test_file = mock.SaveDataAfterCloseStringIO() 154861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.open.expect_call("filename", "w").and_return(test_file) 155861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.open_write_close("filename", data) 156b9d055122081a7a0a159770c5df60bb8f5f187cdmbligh self.god.check_playback() 157b9d055122081a7a0a159770c5df60bb8f5f187cdmbligh self.assertEqual(data, test_file.final_data) 158b9d055122081a7a0a159770c5df60bb8f5f187cdmbligh 159b9d055122081a7a0a159770c5df60bb8f5f187cdmbligh 160a6014a0cfba588833757033b67ab858e748a4152jadmanskiclass test_read_keyval(unittest.TestCase): 161a6014a0cfba588833757033b67ab858e748a4152jadmanski def setUp(self): 162861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.god = mock.mock_god(ut=self) 163861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.god.stub_function(base_utils, "open") 164a6014a0cfba588833757033b67ab858e748a4152jadmanski self.god.stub_function(os.path, "isdir") 1655896298fb82c48e9cb5d8fbb269cd0629efce225jadmanski self.god.stub_function(os.path, "exists") 166a6014a0cfba588833757033b67ab858e748a4152jadmanski 167a6014a0cfba588833757033b67ab858e748a4152jadmanski 168a6014a0cfba588833757033b67ab858e748a4152jadmanski def tearDown(self): 169a6014a0cfba588833757033b67ab858e748a4152jadmanski self.god.unstub_all() 170a6014a0cfba588833757033b67ab858e748a4152jadmanski 171a6014a0cfba588833757033b67ab858e748a4152jadmanski 172a6014a0cfba588833757033b67ab858e748a4152jadmanski def create_test_file(self, filename, contents): 173a6014a0cfba588833757033b67ab858e748a4152jadmanski test_file = StringIO.StringIO(contents) 1745896298fb82c48e9cb5d8fbb269cd0629efce225jadmanski os.path.exists.expect_call(filename).and_return(True) 175861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.open.expect_call(filename).and_return(test_file) 176a6014a0cfba588833757033b67ab858e748a4152jadmanski 177a6014a0cfba588833757033b67ab858e748a4152jadmanski 178a6014a0cfba588833757033b67ab858e748a4152jadmanski def read_keyval(self, contents): 179a6014a0cfba588833757033b67ab858e748a4152jadmanski os.path.isdir.expect_call("file").and_return(False) 180a6014a0cfba588833757033b67ab858e748a4152jadmanski self.create_test_file("file", contents) 181861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li keyval = base_utils.read_keyval("file") 182a6014a0cfba588833757033b67ab858e748a4152jadmanski self.god.check_playback() 183a6014a0cfba588833757033b67ab858e748a4152jadmanski return keyval 184a6014a0cfba588833757033b67ab858e748a4152jadmanski 185a6014a0cfba588833757033b67ab858e748a4152jadmanski 1865896298fb82c48e9cb5d8fbb269cd0629efce225jadmanski def test_returns_empty_when_file_doesnt_exist(self): 1875896298fb82c48e9cb5d8fbb269cd0629efce225jadmanski os.path.isdir.expect_call("file").and_return(False) 1885896298fb82c48e9cb5d8fbb269cd0629efce225jadmanski os.path.exists.expect_call("file").and_return(False) 189861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual({}, base_utils.read_keyval("file")) 1905896298fb82c48e9cb5d8fbb269cd0629efce225jadmanski self.god.check_playback() 1915896298fb82c48e9cb5d8fbb269cd0629efce225jadmanski 1925896298fb82c48e9cb5d8fbb269cd0629efce225jadmanski 193a6014a0cfba588833757033b67ab858e748a4152jadmanski def test_accesses_files_directly(self): 194a6014a0cfba588833757033b67ab858e748a4152jadmanski os.path.isdir.expect_call("file").and_return(False) 195a6014a0cfba588833757033b67ab858e748a4152jadmanski self.create_test_file("file", "") 196861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.read_keyval("file") 197a6014a0cfba588833757033b67ab858e748a4152jadmanski self.god.check_playback() 198a6014a0cfba588833757033b67ab858e748a4152jadmanski 199a6014a0cfba588833757033b67ab858e748a4152jadmanski 200a6014a0cfba588833757033b67ab858e748a4152jadmanski def test_accesses_directories_through_keyval_file(self): 201a6014a0cfba588833757033b67ab858e748a4152jadmanski os.path.isdir.expect_call("dir").and_return(True) 202a6014a0cfba588833757033b67ab858e748a4152jadmanski self.create_test_file("dir/keyval", "") 203861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.read_keyval("dir") 204a6014a0cfba588833757033b67ab858e748a4152jadmanski self.god.check_playback() 205a6014a0cfba588833757033b67ab858e748a4152jadmanski 206a6014a0cfba588833757033b67ab858e748a4152jadmanski 207a6014a0cfba588833757033b67ab858e748a4152jadmanski def test_values_are_rstripped(self): 208a6014a0cfba588833757033b67ab858e748a4152jadmanski keyval = self.read_keyval("a=b \n") 209a6014a0cfba588833757033b67ab858e748a4152jadmanski self.assertEquals(keyval, {"a": "b"}) 210a6014a0cfba588833757033b67ab858e748a4152jadmanski 211a6014a0cfba588833757033b67ab858e748a4152jadmanski 212a6014a0cfba588833757033b67ab858e748a4152jadmanski def test_comments_are_ignored(self): 213a6014a0cfba588833757033b67ab858e748a4152jadmanski keyval = self.read_keyval("a=b # a comment\n") 214a6014a0cfba588833757033b67ab858e748a4152jadmanski self.assertEquals(keyval, {"a": "b"}) 215a6014a0cfba588833757033b67ab858e748a4152jadmanski 216a6014a0cfba588833757033b67ab858e748a4152jadmanski 217a6014a0cfba588833757033b67ab858e748a4152jadmanski def test_integers_become_ints(self): 218a6014a0cfba588833757033b67ab858e748a4152jadmanski keyval = self.read_keyval("a=1\n") 219a6014a0cfba588833757033b67ab858e748a4152jadmanski self.assertEquals(keyval, {"a": 1}) 220a6014a0cfba588833757033b67ab858e748a4152jadmanski self.assertEquals(int, type(keyval["a"])) 221a6014a0cfba588833757033b67ab858e748a4152jadmanski 222a6014a0cfba588833757033b67ab858e748a4152jadmanski 223a6014a0cfba588833757033b67ab858e748a4152jadmanski def test_float_values_become_floats(self): 224a6014a0cfba588833757033b67ab858e748a4152jadmanski keyval = self.read_keyval("a=1.5\n") 225a6014a0cfba588833757033b67ab858e748a4152jadmanski self.assertEquals(keyval, {"a": 1.5}) 226a6014a0cfba588833757033b67ab858e748a4152jadmanski self.assertEquals(float, type(keyval["a"])) 227a6014a0cfba588833757033b67ab858e748a4152jadmanski 228a6014a0cfba588833757033b67ab858e748a4152jadmanski 229a6014a0cfba588833757033b67ab858e748a4152jadmanski def test_multiple_lines(self): 230a6014a0cfba588833757033b67ab858e748a4152jadmanski keyval = self.read_keyval("a=one\nb=two\n") 231a6014a0cfba588833757033b67ab858e748a4152jadmanski self.assertEquals(keyval, {"a": "one", "b": "two"}) 232a6014a0cfba588833757033b67ab858e748a4152jadmanski 233a6014a0cfba588833757033b67ab858e748a4152jadmanski 234a6014a0cfba588833757033b67ab858e748a4152jadmanski def test_the_last_duplicate_line_is_used(self): 235a6014a0cfba588833757033b67ab858e748a4152jadmanski keyval = self.read_keyval("a=one\nb=two\na=three\n") 236a6014a0cfba588833757033b67ab858e748a4152jadmanski self.assertEquals(keyval, {"a": "three", "b": "two"}) 237a6014a0cfba588833757033b67ab858e748a4152jadmanski 238a6014a0cfba588833757033b67ab858e748a4152jadmanski 239a6014a0cfba588833757033b67ab858e748a4152jadmanski def test_extra_equals_are_included_in_values(self): 240a6014a0cfba588833757033b67ab858e748a4152jadmanski keyval = self.read_keyval("a=b=c\n") 241a6014a0cfba588833757033b67ab858e748a4152jadmanski self.assertEquals(keyval, {"a": "b=c"}) 242a6014a0cfba588833757033b67ab858e748a4152jadmanski 243a6014a0cfba588833757033b67ab858e748a4152jadmanski 244a6014a0cfba588833757033b67ab858e748a4152jadmanski def test_non_alphanumeric_keynames_are_rejected(self): 245a6014a0cfba588833757033b67ab858e748a4152jadmanski self.assertRaises(ValueError, self.read_keyval, "a$=one\n") 246a6014a0cfba588833757033b67ab858e748a4152jadmanski 247a6014a0cfba588833757033b67ab858e748a4152jadmanski 248a6014a0cfba588833757033b67ab858e748a4152jadmanski def test_underscores_are_allowed_in_key_names(self): 249a6014a0cfba588833757033b67ab858e748a4152jadmanski keyval = self.read_keyval("a_b=value\n") 250a6014a0cfba588833757033b67ab858e748a4152jadmanski self.assertEquals(keyval, {"a_b": "value"}) 251a6014a0cfba588833757033b67ab858e748a4152jadmanski 252a6014a0cfba588833757033b67ab858e748a4152jadmanski 253a6014a0cfba588833757033b67ab858e748a4152jadmanski def test_dashes_are_allowed_in_key_names(self): 254a6014a0cfba588833757033b67ab858e748a4152jadmanski keyval = self.read_keyval("a-b=value\n") 255a6014a0cfba588833757033b67ab858e748a4152jadmanski self.assertEquals(keyval, {"a-b": "value"}) 256a6014a0cfba588833757033b67ab858e748a4152jadmanski 257e1e4f2295e949b89961938167ea622dd51053151Fang Deng def test_empty_value_is_allowed(self): 258e1e4f2295e949b89961938167ea622dd51053151Fang Deng keyval = self.read_keyval("a=\n") 259e1e4f2295e949b89961938167ea622dd51053151Fang Deng self.assertEquals(keyval, {"a": ""}) 260e1e4f2295e949b89961938167ea622dd51053151Fang Deng 261a6014a0cfba588833757033b67ab858e748a4152jadmanski 2620b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanskiclass test_write_keyval(unittest.TestCase): 2630b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski def setUp(self): 264861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.god = mock.mock_god(ut=self) 265861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.god.stub_function(base_utils, "open") 2660b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski self.god.stub_function(os.path, "isdir") 2670b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 2680b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 2690b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski def tearDown(self): 2700b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski self.god.unstub_all() 2710b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 2720b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 2730b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski def assertHasLines(self, value, lines): 2740b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski vlines = value.splitlines() 2750b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski vlines.sort() 2760b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski self.assertEquals(vlines, sorted(lines)) 2770b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 2780b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 2790b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski def write_keyval(self, filename, dictionary, expected_filename=None, 2800b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski type_tag=None): 2810b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski if expected_filename is None: 2820b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski expected_filename = filename 2830b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski test_file = StringIO.StringIO() 2840b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski self.god.stub_function(test_file, "close") 285861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.open.expect_call(expected_filename, "a").and_return(test_file) 2860b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski test_file.close.expect_call() 2870b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski if type_tag is None: 288861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.write_keyval(filename, dictionary) 2890b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski else: 290861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.write_keyval(filename, dictionary, type_tag) 2910b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski return test_file.getvalue() 2920b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 2930b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 2940b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski def write_keyval_file(self, dictionary, type_tag=None): 2950b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski os.path.isdir.expect_call("file").and_return(False) 2960b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski return self.write_keyval("file", dictionary, type_tag=type_tag) 2970b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 2980b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 2990b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski def test_accesses_files_directly(self): 3000b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski os.path.isdir.expect_call("file").and_return(False) 3010b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski result = self.write_keyval("file", {"a": "1"}) 3020b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski self.assertEquals(result, "a=1\n") 3030b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 3040b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 3050b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski def test_accesses_directories_through_keyval_file(self): 3060b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski os.path.isdir.expect_call("dir").and_return(True) 3070b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski result = self.write_keyval("dir", {"b": "2"}, "dir/keyval") 3080b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski self.assertEquals(result, "b=2\n") 3090b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 3100b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 3110b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski def test_numbers_are_stringified(self): 3120b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski result = self.write_keyval_file({"c": 3}) 3130b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski self.assertEquals(result, "c=3\n") 3140b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 3150b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 3160b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski def test_type_tags_are_excluded_by_default(self): 3170b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski result = self.write_keyval_file({"d": "a string"}) 3180b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski self.assertEquals(result, "d=a string\n") 3190b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski self.assertRaises(ValueError, self.write_keyval_file, 3200b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski {"d{perf}": "a string"}) 3210b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 3220b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 3230b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski def test_perf_tags_are_allowed(self): 3240b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski result = self.write_keyval_file({"a{perf}": 1, "b{perf}": 2}, 3250b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski type_tag="perf") 3260b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski self.assertHasLines(result, ["a{perf}=1", "b{perf}=2"]) 3270b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski self.assertRaises(ValueError, self.write_keyval_file, 3280b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski {"a": 1, "b": 2}, type_tag="perf") 3290b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 3300b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 3310b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski def test_non_alphanumeric_keynames_are_rejected(self): 3320b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski self.assertRaises(ValueError, self.write_keyval_file, {"x$": 0}) 3330b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 3340b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 3350b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski def test_underscores_are_allowed_in_key_names(self): 3360b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski result = self.write_keyval_file({"a_b": "value"}) 3370b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski self.assertEquals(result, "a_b=value\n") 3380b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 3390b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 3400b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski def test_dashes_are_allowed_in_key_names(self): 3410b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski result = self.write_keyval_file({"a-b": "value"}) 3420b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski self.assertEquals(result, "a-b=value\n") 3430b6eff1b00fc2751f9188fc23a265ba6f4a573e7jadmanski 344a6014a0cfba588833757033b67ab858e748a4152jadmanski 3459d40cb411ac42d2082b4146f75de850b17952492jadmanskiclass test_is_url(unittest.TestCase): 3469d40cb411ac42d2082b4146f75de850b17952492jadmanski def test_accepts_http(self): 347861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertTrue(base_utils.is_url("http://example.com")) 3489d40cb411ac42d2082b4146f75de850b17952492jadmanski 3499d40cb411ac42d2082b4146f75de850b17952492jadmanski 3509d40cb411ac42d2082b4146f75de850b17952492jadmanski def test_accepts_ftp(self): 351861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertTrue(base_utils.is_url("ftp://ftp.example.com")) 3529d40cb411ac42d2082b4146f75de850b17952492jadmanski 3539d40cb411ac42d2082b4146f75de850b17952492jadmanski 3549d40cb411ac42d2082b4146f75de850b17952492jadmanski def test_rejects_local_path(self): 355861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertFalse(base_utils.is_url("/home/username/file")) 3569d40cb411ac42d2082b4146f75de850b17952492jadmanski 3579d40cb411ac42d2082b4146f75de850b17952492jadmanski 3589d40cb411ac42d2082b4146f75de850b17952492jadmanski def test_rejects_local_filename(self): 359861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertFalse(base_utils.is_url("filename")) 3609d40cb411ac42d2082b4146f75de850b17952492jadmanski 3619d40cb411ac42d2082b4146f75de850b17952492jadmanski 3629d40cb411ac42d2082b4146f75de850b17952492jadmanski def test_rejects_relative_local_path(self): 363861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertFalse(base_utils.is_url("somedir/somesubdir/file")) 3649d40cb411ac42d2082b4146f75de850b17952492jadmanski 3659d40cb411ac42d2082b4146f75de850b17952492jadmanski 3669d40cb411ac42d2082b4146f75de850b17952492jadmanski def test_rejects_local_path_containing_url(self): 367861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertFalse(base_utils.is_url("somedir/http://path/file")) 3689d40cb411ac42d2082b4146f75de850b17952492jadmanski 3699d40cb411ac42d2082b4146f75de850b17952492jadmanski 3709d40cb411ac42d2082b4146f75de850b17952492jadmanskiclass test_urlopen(unittest.TestCase): 3719d40cb411ac42d2082b4146f75de850b17952492jadmanski def setUp(self): 372861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.god = mock.mock_god(ut=self) 3739d40cb411ac42d2082b4146f75de850b17952492jadmanski 3749d40cb411ac42d2082b4146f75de850b17952492jadmanski 3759d40cb411ac42d2082b4146f75de850b17952492jadmanski def tearDown(self): 3769d40cb411ac42d2082b4146f75de850b17952492jadmanski self.god.unstub_all() 3779d40cb411ac42d2082b4146f75de850b17952492jadmanski 3789d40cb411ac42d2082b4146f75de850b17952492jadmanski 3799d40cb411ac42d2082b4146f75de850b17952492jadmanski def stub_urlopen_with_timeout_comparison(self, test_func, expected_return, 3809d40cb411ac42d2082b4146f75de850b17952492jadmanski *expected_args): 381b289619149c7c3558152c576cb4debee084ec3c4mbligh expected_args += (None,) * (2 - len(expected_args)) 382b289619149c7c3558152c576cb4debee084ec3c4mbligh def urlopen(url, data=None): 383b289619149c7c3558152c576cb4debee084ec3c4mbligh self.assertEquals(expected_args, (url,data)) 3849d40cb411ac42d2082b4146f75de850b17952492jadmanski test_func(socket.getdefaulttimeout()) 3859d40cb411ac42d2082b4146f75de850b17952492jadmanski return expected_return 386b289619149c7c3558152c576cb4debee084ec3c4mbligh self.god.stub_with(urllib2, "urlopen", urlopen) 3879d40cb411ac42d2082b4146f75de850b17952492jadmanski 3889d40cb411ac42d2082b4146f75de850b17952492jadmanski 3899d40cb411ac42d2082b4146f75de850b17952492jadmanski def stub_urlopen_with_timeout_check(self, expected_timeout, 3909d40cb411ac42d2082b4146f75de850b17952492jadmanski expected_return, *expected_args): 3919d40cb411ac42d2082b4146f75de850b17952492jadmanski def test_func(timeout): 3929d40cb411ac42d2082b4146f75de850b17952492jadmanski self.assertEquals(timeout, expected_timeout) 3939d40cb411ac42d2082b4146f75de850b17952492jadmanski self.stub_urlopen_with_timeout_comparison(test_func, expected_return, 3949d40cb411ac42d2082b4146f75de850b17952492jadmanski *expected_args) 3959d40cb411ac42d2082b4146f75de850b17952492jadmanski 3969d40cb411ac42d2082b4146f75de850b17952492jadmanski 3979d40cb411ac42d2082b4146f75de850b17952492jadmanski def test_timeout_set_during_call(self): 3989d40cb411ac42d2082b4146f75de850b17952492jadmanski self.stub_urlopen_with_timeout_check(30, "retval", "url") 399861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li retval = base_utils.urlopen("url", timeout=30) 4009d40cb411ac42d2082b4146f75de850b17952492jadmanski self.assertEquals(retval, "retval") 4019d40cb411ac42d2082b4146f75de850b17952492jadmanski 4029d40cb411ac42d2082b4146f75de850b17952492jadmanski 4039d40cb411ac42d2082b4146f75de850b17952492jadmanski def test_timeout_reset_after_call(self): 4049d40cb411ac42d2082b4146f75de850b17952492jadmanski old_timeout = socket.getdefaulttimeout() 4059d40cb411ac42d2082b4146f75de850b17952492jadmanski self.stub_urlopen_with_timeout_check(30, None, "url") 4069d40cb411ac42d2082b4146f75de850b17952492jadmanski try: 4079d40cb411ac42d2082b4146f75de850b17952492jadmanski socket.setdefaulttimeout(1234) 408861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.urlopen("url", timeout=30) 4099d40cb411ac42d2082b4146f75de850b17952492jadmanski self.assertEquals(1234, socket.getdefaulttimeout()) 4109d40cb411ac42d2082b4146f75de850b17952492jadmanski finally: 4119d40cb411ac42d2082b4146f75de850b17952492jadmanski socket.setdefaulttimeout(old_timeout) 4129d40cb411ac42d2082b4146f75de850b17952492jadmanski 4139d40cb411ac42d2082b4146f75de850b17952492jadmanski 4149d40cb411ac42d2082b4146f75de850b17952492jadmanski def test_timeout_set_by_default(self): 4159d40cb411ac42d2082b4146f75de850b17952492jadmanski def test_func(timeout): 4169d40cb411ac42d2082b4146f75de850b17952492jadmanski self.assertTrue(timeout is not None) 4179d40cb411ac42d2082b4146f75de850b17952492jadmanski self.stub_urlopen_with_timeout_comparison(test_func, None, "url") 418861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.urlopen("url") 4199d40cb411ac42d2082b4146f75de850b17952492jadmanski 4209d40cb411ac42d2082b4146f75de850b17952492jadmanski 4219d40cb411ac42d2082b4146f75de850b17952492jadmanski def test_args_are_untouched(self): 4229d40cb411ac42d2082b4146f75de850b17952492jadmanski self.stub_urlopen_with_timeout_check(30, None, "http://url", 423b289619149c7c3558152c576cb4debee084ec3c4mbligh "POST data") 424861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.urlopen("http://url", timeout=30, data="POST data") 4259d40cb411ac42d2082b4146f75de850b17952492jadmanski 4269d40cb411ac42d2082b4146f75de850b17952492jadmanski 4279d40cb411ac42d2082b4146f75de850b17952492jadmanskiclass test_urlretrieve(unittest.TestCase): 4289d40cb411ac42d2082b4146f75de850b17952492jadmanski def setUp(self): 429861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.god = mock.mock_god(ut=self) 4309d40cb411ac42d2082b4146f75de850b17952492jadmanski 4319d40cb411ac42d2082b4146f75de850b17952492jadmanski 4329d40cb411ac42d2082b4146f75de850b17952492jadmanski def tearDown(self): 4339d40cb411ac42d2082b4146f75de850b17952492jadmanski self.god.unstub_all() 4349d40cb411ac42d2082b4146f75de850b17952492jadmanski 4359d40cb411ac42d2082b4146f75de850b17952492jadmanski 436b289619149c7c3558152c576cb4debee084ec3c4mbligh def test_urlopen_passed_arguments(self): 437861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.god.stub_function(base_utils, "urlopen") 438861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.god.stub_function(base_utils.shutil, "copyfileobj") 439861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.god.stub_function(base_utils, "open") 4409d40cb411ac42d2082b4146f75de850b17952492jadmanski 441b289619149c7c3558152c576cb4debee084ec3c4mbligh url = "url" 442b289619149c7c3558152c576cb4debee084ec3c4mbligh dest = "somefile" 443b289619149c7c3558152c576cb4debee084ec3c4mbligh data = object() 444b289619149c7c3558152c576cb4debee084ec3c4mbligh timeout = 10 4459d40cb411ac42d2082b4146f75de850b17952492jadmanski 446b289619149c7c3558152c576cb4debee084ec3c4mbligh src_file = self.god.create_mock_class(file, "file") 447b289619149c7c3558152c576cb4debee084ec3c4mbligh dest_file = self.god.create_mock_class(file, "file") 4489d40cb411ac42d2082b4146f75de850b17952492jadmanski 449861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li (base_utils.urlopen.expect_call(url, data=data, timeout=timeout) 450b289619149c7c3558152c576cb4debee084ec3c4mbligh .and_return(src_file)) 451861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.open.expect_call(dest, "wb").and_return(dest_file) 452861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.shutil.copyfileobj.expect_call(src_file, dest_file) 453b289619149c7c3558152c576cb4debee084ec3c4mbligh dest_file.close.expect_call() 454b289619149c7c3558152c576cb4debee084ec3c4mbligh src_file.close.expect_call() 4559d40cb411ac42d2082b4146f75de850b17952492jadmanski 456861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.urlretrieve(url, dest, data=data, timeout=timeout) 457b289619149c7c3558152c576cb4debee084ec3c4mbligh self.god.check_playback() 4589d40cb411ac42d2082b4146f75de850b17952492jadmanski 4599d40cb411ac42d2082b4146f75de850b17952492jadmanski 4604f909251625246dbeb36c1c8b26892cbd4e6c934jadmanskiclass test_merge_trees(unittest.TestCase): 4614f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski # a some path-handling helper functions 4624f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski def src(self, *path_segments): 4635f2bc2822bce3dc615ca1c82ac7b320ed3a6161bjadmanski return os.path.join(self.src_tree.name, *path_segments) 4644f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 4654f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 4664f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski def dest(self, *path_segments): 4675f2bc2822bce3dc615ca1c82ac7b320ed3a6161bjadmanski return os.path.join(self.dest_tree.name, *path_segments) 4684f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 4694f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 4704f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski def paths(self, *path_segments): 4714f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski return self.src(*path_segments), self.dest(*path_segments) 4724f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 4734f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 4744f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski def assertFileEqual(self, *path_segments): 4754f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski src, dest = self.paths(*path_segments) 4764f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski self.assertEqual(True, os.path.isfile(src)) 4774f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski self.assertEqual(True, os.path.isfile(dest)) 4784f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski self.assertEqual(os.path.getsize(src), os.path.getsize(dest)) 4794f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski self.assertEqual(open(src).read(), open(dest).read()) 4804f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 4814f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 4824f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski def assertFileContents(self, contents, *path_segments): 4834f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski dest = self.dest(*path_segments) 4844f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski self.assertEqual(True, os.path.isfile(dest)) 4854f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski self.assertEqual(os.path.getsize(dest), len(contents)) 4864f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski self.assertEqual(contents, open(dest).read()) 4874f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 4884f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 4894f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski def setUp(self): 4905f2bc2822bce3dc615ca1c82ac7b320ed3a6161bjadmanski self.src_tree = autotemp.tempdir(unique_id='utilsrc') 4915f2bc2822bce3dc615ca1c82ac7b320ed3a6161bjadmanski self.dest_tree = autotemp.tempdir(unique_id='utilsdest') 4924f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 4934f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski # empty subdirs 4944f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski os.mkdir(self.src("empty")) 4954f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski os.mkdir(self.dest("empty")) 4964f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 4974f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 4984f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski def tearDown(self): 4995f2bc2822bce3dc615ca1c82ac7b320ed3a6161bjadmanski self.src_tree.clean() 5005f2bc2822bce3dc615ca1c82ac7b320ed3a6161bjadmanski self.dest_tree.clean() 5014f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 5024f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 5034f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski def test_both_dont_exist(self): 504861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.merge_trees(*self.paths("empty")) 5054f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 5064f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 5074f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski def test_file_only_at_src(self): 5084f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski print >> open(self.src("src_only"), "w"), "line 1" 509861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.merge_trees(*self.paths("src_only")) 5104f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski self.assertFileEqual("src_only") 5114f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 5124f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 5134f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski def test_file_only_at_dest(self): 5144f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski print >> open(self.dest("dest_only"), "w"), "line 1" 515861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.merge_trees(*self.paths("dest_only")) 5164f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski self.assertEqual(False, os.path.exists(self.src("dest_only"))) 5174f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski self.assertFileContents("line 1\n", "dest_only") 5184f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 5194f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 5204f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski def test_file_at_both(self): 5214f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski print >> open(self.dest("in_both"), "w"), "line 1" 5224f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski print >> open(self.src("in_both"), "w"), "line 2" 523861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.merge_trees(*self.paths("in_both")) 5244f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski self.assertFileContents("line 1\nline 2\n", "in_both") 5254f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 5264f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 5274f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski def test_directory_with_files_in_both(self): 5284f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski print >> open(self.dest("in_both"), "w"), "line 1" 5294f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski print >> open(self.src("in_both"), "w"), "line 3" 530861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.merge_trees(*self.paths()) 5314f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski self.assertFileContents("line 1\nline 3\n", "in_both") 5324f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 5334f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 5344f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski def test_directory_with_mix_of_files(self): 5354f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski print >> open(self.dest("in_dest"), "w"), "dest line" 5364f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski print >> open(self.src("in_src"), "w"), "src line" 537861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.merge_trees(*self.paths()) 5384f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski self.assertFileContents("dest line\n", "in_dest") 5394f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski self.assertFileContents("src line\n", "in_src") 5404f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 5414f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 5424f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski def test_directory_with_subdirectories(self): 5434f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski os.mkdir(self.src("src_subdir")) 5444f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski print >> open(self.src("src_subdir", "subfile"), "w"), "subdir line" 5454f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski os.mkdir(self.src("both_subdir")) 5464f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski os.mkdir(self.dest("both_subdir")) 5474f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski print >> open(self.src("both_subdir", "subfile"), "w"), "src line" 5484f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski print >> open(self.dest("both_subdir", "subfile"), "w"), "dest line" 549861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.merge_trees(*self.paths()) 5504f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski self.assertFileContents("subdir line\n", "src_subdir", "subfile") 5514f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski self.assertFileContents("dest line\nsrc line\n", "both_subdir", 5524f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski "subfile") 5534f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 5544f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 5554556178ac97dc6160a31ac105f5d987b6c9933d8mblighclass test_get_relative_path(unittest.TestCase): 5564556178ac97dc6160a31ac105f5d987b6c9933d8mbligh def test_not_absolute(self): 557861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertRaises(AssertionError, base_utils.get_relative_path, "a", "b") 5584556178ac97dc6160a31ac105f5d987b6c9933d8mbligh 5594556178ac97dc6160a31ac105f5d987b6c9933d8mbligh def test_same_dir(self): 560861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.get_relative_path("/a/b/c", "/a/b"), "c") 5614556178ac97dc6160a31ac105f5d987b6c9933d8mbligh 5624556178ac97dc6160a31ac105f5d987b6c9933d8mbligh def test_forward_dir(self): 563861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.get_relative_path("/a/b/c/d", "/a/b"), "c/d") 5644556178ac97dc6160a31ac105f5d987b6c9933d8mbligh 5654556178ac97dc6160a31ac105f5d987b6c9933d8mbligh def test_previous_dir(self): 566861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.get_relative_path("/a/b", "/a/b/c/d"), "../..") 5674556178ac97dc6160a31ac105f5d987b6c9933d8mbligh 5684556178ac97dc6160a31ac105f5d987b6c9933d8mbligh def test_parallel_dir(self): 569861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.get_relative_path("/a/c/d", "/a/b/c/d"), 5704556178ac97dc6160a31ac105f5d987b6c9933d8mbligh "../../../c/d") 5714556178ac97dc6160a31ac105f5d987b6c9933d8mbligh 5724f909251625246dbeb36c1c8b26892cbd4e6c934jadmanski 5736fbdb803283146358f65ab81102e0a3537e9b97emblighclass test_sh_escape(unittest.TestCase): 5746fbdb803283146358f65ab81102e0a3537e9b97embligh def _test_in_shell(self, text): 575861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li escaped_text = base_utils.sh_escape(text) 5766fbdb803283146358f65ab81102e0a3537e9b97embligh proc = subprocess.Popen('echo "%s"' % escaped_text, shell=True, 5776fbdb803283146358f65ab81102e0a3537e9b97embligh stdin=open(os.devnull, 'r'), 5786fbdb803283146358f65ab81102e0a3537e9b97embligh stdout=subprocess.PIPE, 5796fbdb803283146358f65ab81102e0a3537e9b97embligh stderr=open(os.devnull, 'w')) 5806fbdb803283146358f65ab81102e0a3537e9b97embligh stdout, _ = proc.communicate() 5816fbdb803283146358f65ab81102e0a3537e9b97embligh self.assertEqual(proc.returncode, 0) 5826fbdb803283146358f65ab81102e0a3537e9b97embligh self.assertEqual(stdout[:-1], text) 5836fbdb803283146358f65ab81102e0a3537e9b97embligh 5846fbdb803283146358f65ab81102e0a3537e9b97embligh 5856fbdb803283146358f65ab81102e0a3537e9b97embligh def test_normal_string(self): 5866fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('abcd') 5876fbdb803283146358f65ab81102e0a3537e9b97embligh 5886fbdb803283146358f65ab81102e0a3537e9b97embligh 5896fbdb803283146358f65ab81102e0a3537e9b97embligh def test_spaced_string(self): 5906fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('abcd efgh') 5916fbdb803283146358f65ab81102e0a3537e9b97embligh 5926fbdb803283146358f65ab81102e0a3537e9b97embligh 5936fbdb803283146358f65ab81102e0a3537e9b97embligh def test_dollar(self): 5946fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('$') 5956fbdb803283146358f65ab81102e0a3537e9b97embligh 5966fbdb803283146358f65ab81102e0a3537e9b97embligh 5976fbdb803283146358f65ab81102e0a3537e9b97embligh def test_single_quote(self): 5986fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('\'') 5996fbdb803283146358f65ab81102e0a3537e9b97embligh 6006fbdb803283146358f65ab81102e0a3537e9b97embligh 6016fbdb803283146358f65ab81102e0a3537e9b97embligh def test_single_quoted_string(self): 6026fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('\'efgh\'') 6036fbdb803283146358f65ab81102e0a3537e9b97embligh 6046fbdb803283146358f65ab81102e0a3537e9b97embligh 605b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger def test_string_with_single_quote(self): 606b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger self._test_in_shell("a'b") 607b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger 608b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger 609b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger def test_string_with_escaped_single_quote(self): 610b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger self._test_in_shell(r"a\'b") 611b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger 612b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger 6136fbdb803283146358f65ab81102e0a3537e9b97embligh def test_double_quote(self): 6146fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('"') 6156fbdb803283146358f65ab81102e0a3537e9b97embligh 6166fbdb803283146358f65ab81102e0a3537e9b97embligh 6176fbdb803283146358f65ab81102e0a3537e9b97embligh def test_double_quoted_string(self): 6186fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('"abcd"') 6196fbdb803283146358f65ab81102e0a3537e9b97embligh 6206fbdb803283146358f65ab81102e0a3537e9b97embligh 6216fbdb803283146358f65ab81102e0a3537e9b97embligh def test_backtick(self): 6226fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('`') 6236fbdb803283146358f65ab81102e0a3537e9b97embligh 6246fbdb803283146358f65ab81102e0a3537e9b97embligh 6256fbdb803283146358f65ab81102e0a3537e9b97embligh def test_backticked_string(self): 6266fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('`jklm`') 6276fbdb803283146358f65ab81102e0a3537e9b97embligh 6286fbdb803283146358f65ab81102e0a3537e9b97embligh 6296fbdb803283146358f65ab81102e0a3537e9b97embligh def test_backslash(self): 6306fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('\\') 6316fbdb803283146358f65ab81102e0a3537e9b97embligh 6326fbdb803283146358f65ab81102e0a3537e9b97embligh 6336fbdb803283146358f65ab81102e0a3537e9b97embligh def test_backslashed_special_characters(self): 6346fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('\\$') 6356fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('\\"') 6366fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('\\\'') 6376fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('\\`') 6386fbdb803283146358f65ab81102e0a3537e9b97embligh 6396fbdb803283146358f65ab81102e0a3537e9b97embligh 6406fbdb803283146358f65ab81102e0a3537e9b97embligh def test_backslash_codes(self): 6416fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('\\n') 6426fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('\\r') 6436fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('\\t') 6446fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('\\v') 6456fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('\\b') 6466fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('\\a') 6476fbdb803283146358f65ab81102e0a3537e9b97embligh self._test_in_shell('\\000') 6486fbdb803283146358f65ab81102e0a3537e9b97embligh 649b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger def test_real_newline(self): 650b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger self._test_in_shell('\n') 651b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger self._test_in_shell('\\\n') 652b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger 653b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger 654b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburgerclass test_sh_quote_word(test_sh_escape): 655b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger """Run tests on sh_quote_word. 656b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger 657b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger Inherit from test_sh_escape to get the same tests to run on both. 658b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger """ 659b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger 660b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger def _test_in_shell(self, text): 661b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger quoted_word = base_utils.sh_quote_word(text) 662b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger echoed_value = subprocess.check_output('echo %s' % quoted_word, 663b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger shell=True) 664b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger self.assertEqual(echoed_value, text + '\n') 665b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger 666b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger 667b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburgerclass test_nested_sh_quote_word(test_sh_quote_word): 668b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger """Run nested tests on sh_quote_word. 669b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger 670b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger Inherit from test_sh_quote_word to get the same tests to run on both. 671b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger """ 672b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger 673b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger def _test_in_shell(self, text): 674b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger command = 'echo ' + base_utils.sh_quote_word(text) 675b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger nested_command = 'echo ' + base_utils.sh_quote_word(command) 676b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger produced_command = subprocess.check_output(nested_command, shell=True) 677b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger echoed_value = subprocess.check_output(produced_command, shell=True) 678b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger self.assertEqual(echoed_value, text + '\n') 679b2d1fc137b403edf24d3ef3f6e6d3e409bb33948Filipe Brandenburger 6806fbdb803283146358f65ab81102e0a3537e9b97embligh 681093a06869b1795ecb11fdff7e7fc400c2be43296jadmanskiclass test_run(unittest.TestCase): 682093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski """ 683861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li Test the base_utils.run() function. 684093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 685861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li Note: This test runs simple external commands to test the base_utils.run() 686093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski API without assuming implementation details. 687093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski """ 688093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski def setUp(self): 689861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.god = mock.mock_god(ut=self) 690df1821be7abe211d5a95a76bbbb9c3f0e757fd24Filipe Brandenburger self.god.stub_function(base_utils.logging, 'warning') 691861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.god.stub_function(base_utils.logging, 'debug') 692093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 693093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 694093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski def tearDown(self): 695093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski self.god.unstub_all() 696093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 697093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 698093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski def __check_result(self, result, command, exit_status=0, stdout='', 699093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski stderr=''): 700093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski self.assertEquals(result.command, command) 701093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski self.assertEquals(result.exit_status, exit_status) 702093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski self.assertEquals(result.stdout, stdout) 703093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski self.assertEquals(result.stderr, stderr) 704093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 705093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 706093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski def test_default_simple(self): 707093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski cmd = 'echo "hello world"' 708093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski # expect some king of logging.debug() call but don't care about args 709861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.logging.debug.expect_any_call() 710861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.__check_result(base_utils.run(cmd), cmd, stdout='hello world\n') 711093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 712093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 713093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski def test_default_failure(self): 714093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski cmd = 'exit 11' 715093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski try: 716861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.run(cmd, verbose=False) 717861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li except base_utils.error.CmdError, err: 718093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski self.__check_result(err.result_obj, cmd, exit_status=11) 719093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 720093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 721093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski def test_ignore_status(self): 722093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski cmd = 'echo error >&2 && exit 11' 723861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.__check_result(base_utils.run(cmd, ignore_status=True, verbose=False), 724093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski cmd, exit_status=11, stderr='error\n') 725093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 726093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 727093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski def test_timeout(self): 72804be2bd5e4666a5c253e9c30ab20555e04286032Ilja H. Friedel # we expect a logging.warning() message, don't care about the contents 729df1821be7abe211d5a95a76bbbb9c3f0e757fd24Filipe Brandenburger base_utils.logging.warning.expect_any_call() 730093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski try: 731861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li base_utils.run('echo -n output && sleep 10', timeout=1, verbose=False) 732861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li except base_utils.error.CmdError, err: 733093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski self.assertEquals(err.result_obj.stdout, 'output') 734093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 735093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 736093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski def test_stdout_stderr_tee(self): 737093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski cmd = 'echo output && echo error >&2' 738093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski stdout_tee = StringIO.StringIO() 739093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski stderr_tee = StringIO.StringIO() 740093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 741861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.__check_result(base_utils.run( 742093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski cmd, stdout_tee=stdout_tee, stderr_tee=stderr_tee, 743093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski verbose=False), cmd, stdout='output\n', stderr='error\n') 744093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski self.assertEqual(stdout_tee.getvalue(), 'output\n') 745093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski self.assertEqual(stderr_tee.getvalue(), 'error\n') 746093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 747093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 748093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski def test_stdin_string(self): 749093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski cmd = 'cat' 750861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.__check_result(base_utils.run(cmd, verbose=False, stdin='hi!\n'), 751093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski cmd, stdout='hi!\n') 752093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 753093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 754093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski def test_safe_args(self): 755e428672caa5230383a6b3d40172d4e6e460d10b9Filipe Brandenburger # NOTE: The string in expected_quoted_cmd depends on the internal 756e428672caa5230383a6b3d40172d4e6e460d10b9Filipe Brandenburger # implementation of shell quoting which is used by base_utils.run(), 757e428672caa5230383a6b3d40172d4e6e460d10b9Filipe Brandenburger # in this case, sh_quote_word(). 758e428672caa5230383a6b3d40172d4e6e460d10b9Filipe Brandenburger expected_quoted_cmd = "echo 'hello \"world' again" 759861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.__check_result(base_utils.run( 760e428672caa5230383a6b3d40172d4e6e460d10b9Filipe Brandenburger 'echo', verbose=False, args=('hello "world', 'again')), 761e428672caa5230383a6b3d40172d4e6e460d10b9Filipe Brandenburger expected_quoted_cmd, stdout='hello "world again\n') 762093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 763093a06869b1795ecb11fdff7e7fc400c2be43296jadmanski 7642c7f7d69ebc62b55444a9a961d31258b49ab4370mbligh def test_safe_args_given_string(self): 765861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertRaises(TypeError, base_utils.run, 'echo', args='hello') 7662c7f7d69ebc62b55444a9a961d31258b49ab4370mbligh 7672c7f7d69ebc62b55444a9a961d31258b49ab4370mbligh 768d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth B def test_wait_interrupt(self): 769d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth B """Test that we actually select twice if the first one returns EINTR.""" 770d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth B base_utils.logging.debug.expect_any_call() 771d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth B 772d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth B bg_job = base_utils.BgJob('echo "hello world"') 773d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth B bg_job.result.exit_status = 0 774d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth B self.god.stub_function(base_utils.select, 'select') 775d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth B 776d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth B base_utils.select.select.expect_any_call().and_raises( 777d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth B select.error(errno.EINTR, 'Select interrupted')) 778df1821be7abe211d5a95a76bbbb9c3f0e757fd24Filipe Brandenburger base_utils.logging.warning.expect_any_call() 779df1821be7abe211d5a95a76bbbb9c3f0e757fd24Filipe Brandenburger 780d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth B base_utils.select.select.expect_any_call().and_return( 781d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth B ([bg_job.sp.stdout, bg_job.sp.stderr], [], None)) 782df1821be7abe211d5a95a76bbbb9c3f0e757fd24Filipe Brandenburger base_utils.logging.warning.expect_any_call() 783df1821be7abe211d5a95a76bbbb9c3f0e757fd24Filipe Brandenburger 784d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth B self.assertFalse( 785d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth B base_utils._wait_for_commands([bg_job], time.time(), None)) 786d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth B 787d84e18ffa85a603e735663cdabca3b5a40c5800cPrashanth B 788777db854ee6e1774dcfef5ca9d6ecaf3ab20ea2fmblighclass test_compare_versions(unittest.TestCase): 7895860932b0d2416245d06222103cb1c2655ef1621mbligh def test_zerofill(self): 790861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.compare_versions('1.7', '1.10'), -1) 791861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.compare_versions('1.222', '1.3'), 1) 792861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.compare_versions('1.03', '1.3'), 0) 793777db854ee6e1774dcfef5ca9d6ecaf3ab20ea2fmbligh 794777db854ee6e1774dcfef5ca9d6ecaf3ab20ea2fmbligh 7955860932b0d2416245d06222103cb1c2655ef1621mbligh def test_unequal_len(self): 796861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.compare_versions('1.3', '1.3.4'), -1) 797861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.compare_versions('1.3.1', '1.3'), 1) 798777db854ee6e1774dcfef5ca9d6ecaf3ab20ea2fmbligh 799777db854ee6e1774dcfef5ca9d6ecaf3ab20ea2fmbligh 8005860932b0d2416245d06222103cb1c2655ef1621mbligh def test_dash_delimited(self): 801861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.compare_versions('1-2-3', '1-5-1'), -1) 802861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.compare_versions('1-2-1', '1-1-1'), 1) 803861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.compare_versions('1-2-4', '1-2-4'), 0) 8045860932b0d2416245d06222103cb1c2655ef1621mbligh 8055860932b0d2416245d06222103cb1c2655ef1621mbligh 8065860932b0d2416245d06222103cb1c2655ef1621mbligh def test_alphabets(self): 807861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.compare_versions('m.l.b', 'n.b.a'), -1) 808861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.compare_versions('n.b.a', 'm.l.b'), 1) 809861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.compare_versions('abc.e', 'abc.e'), 0) 8105860932b0d2416245d06222103cb1c2655ef1621mbligh 8115860932b0d2416245d06222103cb1c2655ef1621mbligh 8125860932b0d2416245d06222103cb1c2655ef1621mbligh def test_mix_symbols(self): 813861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.compare_versions('k-320.1', 'k-320.3'), -1) 814861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.compare_versions('k-231.5', 'k-231.1'), 1) 815861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.compare_versions('k-231.1', 'k-231.1'), 0) 8165860932b0d2416245d06222103cb1c2655ef1621mbligh 817861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.compare_versions('k.320-1', 'k.320-3'), -1) 818861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.compare_versions('k.231-5', 'k.231-1'), 1) 819861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li self.assertEqual(base_utils.compare_versions('k.231-1', 'k.231-1'), 0) 820777db854ee6e1774dcfef5ca9d6ecaf3ab20ea2fmbligh 821777db854ee6e1774dcfef5ca9d6ecaf3ab20ea2fmbligh 8224ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanskiclass test_args_to_dict(unittest.TestCase): 8234ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski def test_no_args(self): 824861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li result = base_utils.args_to_dict([]) 8254ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski self.assertEqual({}, result) 8264ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski 8274ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski 8284ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski def test_matches(self): 829861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li result = base_utils.args_to_dict(['aBc:DeF', 'SyS=DEf', 'XY_Z:', 8304ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski 'F__o0O=', 'B8r:=:=', '_bAZ_=:=:']) 8314ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski self.assertEqual(result, {'abc':'DeF', 'sys':'DEf', 'xy_z':'', 8324ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski 'f__o0o':'', 'b8r':'=:=', '_baz_':':=:'}) 8334ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski 8344ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski 8354ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski def test_unmatches(self): 8364ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski # Temporarily shut warning messages from args_to_dict() when an argument 8374ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski # doesn't match its pattern. 8384ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski logger = logging.getLogger() 8394ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski saved_level = logger.level 8404ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski logger.setLevel(logging.ERROR) 8414ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski 8424ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski try: 843861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li result = base_utils.args_to_dict(['ab-c:DeF', '--SyS=DEf', 'a*=b', 'a*b', 8444ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski ':VAL', '=VVV', 'WORD']) 8454ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski self.assertEqual({}, result) 8464ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski finally: 8474ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski # Restore level. 8484ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski logger.setLevel(saved_level) 8494ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski 8504ceb7c2e594beeb4a7b2758ae2b7c098b18c3c19jadmanski 8516f27d4f22a1ba5063968b8c322fa0845f3279adeEric Liclass test_get_random_port(unittest.TestCase): 8526f27d4f22a1ba5063968b8c322fa0845f3279adeEric Li def do_bind(self, port, socket_type, socket_proto): 8536f27d4f22a1ba5063968b8c322fa0845f3279adeEric Li s = socket.socket(socket.AF_INET, socket_type, socket_proto) 8546f27d4f22a1ba5063968b8c322fa0845f3279adeEric Li s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 8556f27d4f22a1ba5063968b8c322fa0845f3279adeEric Li s.bind(('', port)) 8566f27d4f22a1ba5063968b8c322fa0845f3279adeEric Li return s 8576f27d4f22a1ba5063968b8c322fa0845f3279adeEric Li 8586f27d4f22a1ba5063968b8c322fa0845f3279adeEric Li 8596f27d4f22a1ba5063968b8c322fa0845f3279adeEric Li def test_get_port(self): 8606f27d4f22a1ba5063968b8c322fa0845f3279adeEric Li for _ in xrange(100): 861861b2d54aec24228cdb3895dbc40062cb40cb2adEric Li p = base_utils.get_unused_port() 8626f27d4f22a1ba5063968b8c322fa0845f3279adeEric Li s = self.do_bind(p, socket.SOCK_STREAM, socket.IPPROTO_TCP) 8636f27d4f22a1ba5063968b8c322fa0845f3279adeEric Li self.assert_(s.getsockname()) 8646f27d4f22a1ba5063968b8c322fa0845f3279adeEric Li s = self.do_bind(p, socket.SOCK_DGRAM, socket.IPPROTO_UDP) 8656f27d4f22a1ba5063968b8c322fa0845f3279adeEric Li self.assert_(s.getsockname()) 8666f27d4f22a1ba5063968b8c322fa0845f3279adeEric Li 8676f27d4f22a1ba5063968b8c322fa0845f3279adeEric Li 868c5205b81292cd323fb9e85354c7ab945d42638a1jadmanskiif __name__ == "__main__": 869c5205b81292cd323fb9e85354c7ab945d42638a1jadmanski unittest.main() 870