binary_search_tool_tester.py revision 4ddf846fc0f8f3ea261b608e473cbd781aad1109
1#!/usr/bin/python2
2
3# Copyright 2012 Google Inc. All Rights Reserved.
4"""Tests for bisecting tool."""
5
6from __future__ import print_function
7
8__author__ = 'shenhan@google.com (Han Shen)'
9
10import os
11import random
12import sys
13import unittest
14
15from utils import command_executer
16from binary_search_tool import binary_search_state
17
18import common
19import gen_obj
20
21
22class BisectingUtilsTest(unittest.TestCase):
23  """Tests for bisecting tool."""
24
25  def setUp(self):
26    """Generate [100-1000] object files, and 1-5% of which are bad ones."""
27    obj_num = random.randint(100, 1000)
28    bad_obj_num = random.randint(obj_num / 100, obj_num / 20)
29    if bad_obj_num == 0:
30      bad_obj_num = 1
31    gen_obj.Main(['--obj_num', str(obj_num), '--bad_obj_num', str(bad_obj_num)])
32
33    try:
34      os.remove(binary_search_state.STATE_FILE)
35    except OSError:
36      pass
37
38  def tearDown(self):
39    """Cleanup temp files."""
40    os.remove(common.OBJECTS_FILE)
41    os.remove(common.WORKING_SET_FILE)
42    print('Deleted "{0}" and "{1}"'.format(common.OBJECTS_FILE,
43                                           common.WORKING_SET_FILE))
44    try:
45      os.remove(os.readlink(binary_search_state.STATE_FILE))
46      os.remove(binary_search_state.STATE_FILE)
47    except OSError:
48      pass
49
50  def runTest(self):
51    args = ['--get_initial_items', './gen_init_list.py', '--switch_to_good',
52            './switch_to_good.py', '--switch_to_bad', './switch_to_bad.py',
53            '--test_script', './is_good.py', '--prune', '--file_args']
54    binary_search_state.Main(args)
55    self.check_output()
56
57  def test_install_script(self):
58    args = ['--get_initial_items', './gen_init_list.py', '--switch_to_good',
59            './switch_to_good.py', '--switch_to_bad', './switch_to_bad.py',
60            '--test_script', './is_good.py', '--prune', '--file_args',
61            '--install_script', './install.py']
62    common.installed = False
63    binary_search_state.Main(args)
64    self.check_output()
65
66  def test_bad_install_script(self):
67    args = ['--get_initial_items', './gen_init_list.py', '--switch_to_good',
68            './switch_to_good.py', '--switch_to_bad', './switch_to_bad.py',
69            '--test_script', './is_good.py', '--prune', '--file_args',
70            '--install_script', './install_bad.py']
71    with self.assertRaises(AssertionError):
72      binary_search_state.Main(args)
73
74  def test_bad_save_state(self):
75    state_file = binary_search_state.STATE_FILE
76
77    with open(state_file, 'w') as f:
78      f.write('test123')
79
80    bss = binary_search_state.MockBinarySearchState()
81    with self.assertRaises(SystemExit):
82      bss.SaveState()
83
84    with open(state_file, 'r') as f:
85      self.assertEquals(f.read(), 'test123')
86
87    os.remove(state_file)
88
89  def test_save_state(self):
90    state_file = binary_search_state.STATE_FILE
91
92    bss = binary_search_state.MockBinarySearchState()
93    bss.SaveState()
94    self.assertTrue(os.path.exists(state_file))
95    first_state = os.readlink(state_file)
96
97    bss.SaveState()
98    self.assertTrue(os.path.exists(state_file))
99    self.assertTrue(os.readlink(state_file) != first_state)
100    self.assertFalse(os.path.exists(first_state))
101
102  def check_output(self):
103    _, out, _ = command_executer.GetCommandExecuter().RunCommandWOutput(
104        'tail -n 10 logs/binary_search_state.py.out')
105    ls = out.splitlines()
106    for l in ls:
107      t = l.find('Bad items are: ')
108      if t > 0:
109        bad_ones = l[(t + len('Bad items are: ')):].split()
110        objects_file = common.ReadObjectsFile()
111        for b in bad_ones:
112          self.assertEqual(objects_file[int(b)], 1)
113
114
115def Main(argv):
116  num_tests = 2
117  if len(argv) > 1:
118    num_tests = int(argv[1])
119
120  suite = unittest.TestSuite()
121  for _ in range(0, num_tests):
122    suite.addTest(BisectingUtilsTest())
123  suite.addTest(BisectingUtilsTest('test_install_script'))
124  suite.addTest(BisectingUtilsTest('test_bad_install_script'))
125  suite.addTest(BisectingUtilsTest('test_bad_save_state'))
126  suite.addTest(BisectingUtilsTest('test_save_state'))
127  runner = unittest.TextTestRunner()
128  runner.run(suite)
129
130
131if __name__ == '__main__':
132  Main(sys.argv)
133