1#!/usr/bin/python
2#
3# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import logging, mox, subprocess, unittest
8
9import common
10
11from autotest_lib.frontend.health import check_test_health
12
13
14class RunPrepScriptsTests(mox.MoxTestBase):
15    """Test the run_prep_scripts() function."""
16
17    def setUp(self):
18        super(RunPrepScriptsTests, self).setUp()
19        self.mox.StubOutWithMock(subprocess, 'call')
20        self.mox.StubOutWithMock(logging, 'error')
21        self.mox.StubOutWithMock(logging, 'info')
22
23
24    def test_given_scripts_are_called(self):
25        """Test that all the scripts passed in are called when they pass."""
26        scripts = [['script1.sh', 'arg'], ['script2.sh']]
27
28        logging.info(mox.IgnoreArg(), mox.IgnoreArg())
29        subprocess.call(['script1.sh', 'arg']).AndReturn(0)
30        logging.info(mox.IgnoreArg(), mox.IgnoreArg())
31        subprocess.call(['script2.sh']).AndReturn(0)
32
33        self.mox.ReplayAll()
34        check_test_health.run_prep_scripts(scripts)
35
36
37    def test_return_true_if_all_scripts_suceed(self):
38        """Test that True is returned when all the scripts succeed."""
39        scripts = [['script.sh']]
40
41        logging.info(mox.IgnoreArg(), mox.IgnoreArg())
42        subprocess.call(mox.IgnoreArg()).AndReturn(0)
43
44        self.mox.ReplayAll()
45        self.assertTrue(check_test_health.run_prep_scripts(scripts))
46
47
48    def test_script_information_logging(self):
49        """Test that we log prep running and failure."""
50        scripts = [['pass.py'], ['fail.sh', 'arg']]
51
52        logging.info('Running %s', 'pass.py')
53        subprocess.call(['pass.py']).AndReturn(0)
54        logging.info('Running %s', 'fail.sh arg')
55        subprocess.call(['fail.sh', 'arg']).AndReturn(1)
56        logging.error('\'%s\' failed with return code %d',
57                      ('fail.sh arg', 1))
58
59        self.mox.ReplayAll()
60        check_test_health.run_prep_scripts(scripts)
61
62
63    def test_return_false_if_script_fails(self):
64        """Test that False is returned if a preparation step fails."""
65        scripts = [['script.sh']]
66
67        logging.info(mox.IgnoreArg(), mox.IgnoreArg())
68        subprocess.call(mox.IgnoreArg()).AndReturn(1)
69        logging.error(mox.IgnoreArg(), mox.IgnoreArg())
70
71        self.mox.ReplayAll()
72        self.assertFalse(check_test_health.run_prep_scripts(scripts))
73
74
75    def test_do_not_run_other_scripts_after_one_fails(self):
76        """Test that the other prep scripts are not ran if one fails."""
77        scripts = [['script1.sh', 'arg'], ['script2.sh']]
78
79        logging.info(mox.IgnoreArg(), mox.IgnoreArg())
80        subprocess.call(['script1.sh', 'arg']).AndReturn(1)
81        logging.error(mox.IgnoreArg(), mox.IgnoreArg())
82
83        self.mox.ReplayAll()
84        check_test_health.run_prep_scripts(scripts)
85
86
87
88class RunAnalysisScripts(mox.MoxTestBase):
89    """Test the run_analysis_scripts() function."""
90
91    def setUp(self):
92        super(RunAnalysisScripts, self).setUp()
93        self.mox.StubOutWithMock(subprocess, 'call')
94        self.mox.StubOutWithMock(logging, 'error')
95        self.mox.StubOutWithMock(logging, 'info')
96
97
98    def test_given_scripts_are_called(self):
99        """Test that all the scripts passed in are called when they pass."""
100        scripts = [['script1.sh', 'arg'], ['script2.sh']]
101
102        logging.info(mox.IgnoreArg(), mox.IgnoreArg())
103        subprocess.call(['script1.sh', 'arg']).AndReturn(0)
104        logging.info(mox.IgnoreArg(), mox.IgnoreArg())
105        subprocess.call(['script2.sh']).AndReturn(0)
106
107        self.mox.ReplayAll()
108        check_test_health.run_analysis_scripts(scripts)
109
110
111    def test_return_true_if_all_scripts_suceed(self):
112        """Test that True is returned when all the scripts succeed."""
113        scripts = [['script.sh']]
114
115        logging.info(mox.IgnoreArg(), mox.IgnoreArg())
116        subprocess.call(mox.IgnoreArg()).AndReturn(0)
117
118        self.mox.ReplayAll()
119        self.assertTrue(check_test_health.run_analysis_scripts(scripts))
120
121
122    def test_script_information_logging(self):
123        """Test that we log prep running and failure."""
124        scripts = [['pass.py'], ['fail.sh', 'arg']]
125
126        logging.info('Running %s', 'pass.py')
127        subprocess.call(['pass.py']).AndReturn(0)
128        logging.info('Running %s', 'fail.sh arg')
129        subprocess.call(['fail.sh', 'arg']).AndReturn(1)
130        logging.error('\'%s\' failed with return code %d',
131                      ('fail.sh arg', 1))
132
133        self.mox.ReplayAll()
134        check_test_health.run_analysis_scripts(scripts)
135
136
137    def test_return_false_if_script_fails(self):
138        """"Test that False is returned when at least one script fails."""
139        scripts = [['script.sh']]
140
141        logging.info(mox.IgnoreArg(), mox.IgnoreArg())
142        subprocess.call(mox.IgnoreArg()).AndReturn(1)
143        logging.error(mox.IgnoreArg(), mox.IgnoreArg())
144
145        self.mox.ReplayAll()
146        self.assertFalse(check_test_health.run_analysis_scripts(scripts))
147
148
149    def test_run_other_scripts_after_one_fails(self):
150        """Test that the other analysis scripts are ran even if one fails."""
151        scripts = [['script1.sh', 'arg'], ['script2.sh']]
152
153        logging.info(mox.IgnoreArg(), mox.IgnoreArg())
154        subprocess.call(['script1.sh', 'arg']).AndReturn(1)
155        logging.error(mox.IgnoreArg(), mox.IgnoreArg())
156        logging.info(mox.IgnoreArg(), mox.IgnoreArg())
157        subprocess.call(['script2.sh']).AndReturn(0)
158
159        self.mox.ReplayAll()
160        check_test_health.run_analysis_scripts(scripts)
161
162
163class MainTests(mox.MoxTestBase):
164    """Tests the main function."""
165
166    def setUp(self):
167        super(MainTests, self).setUp()
168        self.mox.StubOutWithMock(check_test_health, 'run_prep_scripts')
169        self.mox.StubOutWithMock(check_test_health, 'run_analysis_scripts')
170        self._orig_prep = check_test_health.PREP_SCRIPTS
171        self._orig_analysis = check_test_health.ANALYSIS_SCRIPTS
172
173
174    def tearDown(self):
175        super(MainTests, self).tearDown()
176        check_test_health.PREP_SCRIPTS = self._orig_prep
177        check_test_health.ANALYSIS_SCRIPTS = self._orig_analysis
178
179
180    def test_all_functions_called_if_there_are_no_errors(self):
181        """Test that all the script calling functions are called by default."""
182        check_test_health.PREP_SCRIPTS = [['test_prep']]
183        check_test_health.ANALYSIS_SCRIPTS = [['test_analysis']]
184
185        check_test_health.run_prep_scripts(
186            check_test_health.PREP_SCRIPTS).AndReturn(True)
187        check_test_health.run_analysis_scripts(
188            check_test_health.ANALYSIS_SCRIPTS).AndReturn(True)
189
190        self.mox.ReplayAll()
191        self.assertEqual(check_test_health.main(), 0)
192
193
194    def test_handle_prep_failure(self):
195        """Test that we properly handle a prep script failing."""
196        check_test_health.run_prep_scripts(mox.IgnoreArg()).AndReturn(False)
197
198        self.mox.ReplayAll()
199        self.assertEqual(check_test_health.main(), 1)
200
201
202    def test_handle_analysis_failure(self):
203        """Test that we properly handle an analysis script failing."""
204        check_test_health.run_prep_scripts(mox.IgnoreArg()).AndReturn(True)
205        check_test_health.run_analysis_scripts(mox.IgnoreArg()).AndReturn(False)
206
207        self.mox.ReplayAll()
208        self.assertEqual(check_test_health.main(), 1)
209
210
211if __name__ == '__main__':
212    unittest.main()
213