diff options
author | Cassidy Burden <cburden@google.com> | 2016-06-13 12:46:21 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2016-06-14 14:57:57 -0700 |
commit | 4ddf846fc0f8f3ea261b608e473cbd781aad1109 (patch) | |
tree | 917c51dbba8f331044c269aadc7e55776c466107 /binary_search_tool/test | |
parent | 649273e844d7e86a781cb9f7a870b504bc0febbe (diff) | |
download | toolchain-utils-4ddf846fc0f8f3ea261b608e473cbd781aad1109.tar.gz |
Update BinarySearchState's SaveState to be atomic
SaveState will now save the current state of the binary search tool to a
temp file then atomically switch the symlink to the new file.
TEST=Added new unit test for SaveState
Change-Id: Idf97c8ef346700a6e653206083ee7ab75832dbd5
Reviewed-on: https://chrome-internal-review.googlesource.com/264419
Commit-Ready: Cassidy Burden <cburden@google.com>
Tested-by: Cassidy Burden <cburden@google.com>
Reviewed-by: Caroline Tice <cmtice@google.com>
Diffstat (limited to 'binary_search_tool/test')
-rwxr-xr-x | binary_search_tool/test/binary_search_tool_tester.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/binary_search_tool/test/binary_search_tool_tester.py b/binary_search_tool/test/binary_search_tool_tester.py index 475680c6..effa1369 100755 --- a/binary_search_tool/test/binary_search_tool_tester.py +++ b/binary_search_tool/test/binary_search_tool_tester.py @@ -30,12 +30,22 @@ class BisectingUtilsTest(unittest.TestCase): bad_obj_num = 1 gen_obj.Main(['--obj_num', str(obj_num), '--bad_obj_num', str(bad_obj_num)]) + try: + os.remove(binary_search_state.STATE_FILE) + except OSError: + pass + def tearDown(self): """Cleanup temp files.""" os.remove(common.OBJECTS_FILE) os.remove(common.WORKING_SET_FILE) print('Deleted "{0}" and "{1}"'.format(common.OBJECTS_FILE, common.WORKING_SET_FILE)) + try: + os.remove(os.readlink(binary_search_state.STATE_FILE)) + os.remove(binary_search_state.STATE_FILE) + except OSError: + pass def runTest(self): args = ['--get_initial_items', './gen_init_list.py', '--switch_to_good', @@ -61,6 +71,34 @@ class BisectingUtilsTest(unittest.TestCase): with self.assertRaises(AssertionError): binary_search_state.Main(args) + def test_bad_save_state(self): + state_file = binary_search_state.STATE_FILE + + with open(state_file, 'w') as f: + f.write('test123') + + bss = binary_search_state.MockBinarySearchState() + with self.assertRaises(SystemExit): + bss.SaveState() + + with open(state_file, 'r') as f: + self.assertEquals(f.read(), 'test123') + + os.remove(state_file) + + def test_save_state(self): + state_file = binary_search_state.STATE_FILE + + bss = binary_search_state.MockBinarySearchState() + bss.SaveState() + self.assertTrue(os.path.exists(state_file)) + first_state = os.readlink(state_file) + + bss.SaveState() + self.assertTrue(os.path.exists(state_file)) + self.assertTrue(os.readlink(state_file) != first_state) + self.assertFalse(os.path.exists(first_state)) + def check_output(self): _, out, _ = command_executer.GetCommandExecuter().RunCommandWOutput( 'tail -n 10 logs/binary_search_state.py.out') @@ -84,6 +122,8 @@ def Main(argv): suite.addTest(BisectingUtilsTest()) suite.addTest(BisectingUtilsTest('test_install_script')) suite.addTest(BisectingUtilsTest('test_bad_install_script')) + suite.addTest(BisectingUtilsTest('test_bad_save_state')) + suite.addTest(BisectingUtilsTest('test_save_state')) runner = unittest.TextTestRunner() runner.run(suite) |