Home | History | Annotate | Download | only in site_utils
      1 #!/usr/bin/python
      2 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
      3 # Use of this source code is governed by a BSD-style license that can be
      4 # found in the LICENSE file.
      5 
      6 import StringIO
      7 import mox
      8 import unittest
      9 import urllib2
     10 
     11 import mock
     12 
     13 import common
     14 from autotest_lib.client.common_lib.cros import retry
     15 from autotest_lib.server import site_utils
     16 
     17 
     18 # Mock retry.retry used in test_push before importing test_push.
     19 retry.retry = mock.create_autospec(retry.retry, return_value=lambda func: func)
     20 from autotest_lib.site_utils import test_push
     21 
     22 class TestPushUnittests(mox.MoxTestBase):
     23     """Unittest for test_push script."""
     24 
     25     _ARGV = [
     26         'command',
     27         '--build', 'stumpy-release/R36-5881-0.0',
     28         '--shard_build', 'quawks-release/R36-5881-0.0'
     29     ]
     30 
     31     def setUp(self):
     32         """Initialize the unittest."""
     33         super(TestPushUnittests, self).setUp()
     34         # Overwrite expected test results.
     35         test_push.EXPECTED_TEST_RESULTS = {
     36             '^SERVER_JOB$':                  'GOOD',
     37             '.*control.dependency$':         'TEST_NA',
     38             '.*dummy_Fail.RetryFail$':       'FAIL',
     39             }
     40         test_push.TKO = None
     41 
     42 
     43     def stub_out_methods(self, test_views):
     44         """Stub out methods in test_push module with given test results.
     45 
     46         @param test_views: Desired test result views.
     47 
     48         """
     49         self.mox.UnsetStubs()
     50         response = StringIO.StringIO('some_value')
     51         self.mox.StubOutWithMock(urllib2, 'urlopen')
     52         urllib2.urlopen(mox.IgnoreArg()).AndReturn(response)
     53 
     54         self.mox.StubOutWithMock(test_push, 'check_dut_image')
     55         test_push.check_dut_image(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(
     56                 None)
     57 
     58         self.mox.StubOutWithMock(test_push, 'do_run_suite')
     59         test_push.do_run_suite(
     60                 test_push.PUSH_TO_PROD_SUITE, mox.IgnoreArg(), mox.IgnoreArg(),
     61                 mox.IgnoreArg()).AndReturn((1))
     62 
     63         self.mox.StubOutWithMock(site_utils, 'get_test_views_from_tko')
     64         site_utils.get_test_views_from_tko(1, None).AndReturn(test_views)
     65 
     66 
     67     def test_suite_success(self):
     68         """Test test_suite method with matching results."""
     69         test_views = {'SERVER_JOB':                        'GOOD',
     70                       'dummy_fail/control.dependency':     'TEST_NA',
     71                       'dummy_Fail.RetryFail':              'FAIL'
     72                       }
     73 
     74         self.stub_out_methods(test_views)
     75         self.mox.ReplayAll()
     76         test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
     77                              arguments=test_push.parse_arguments(self._ARGV))
     78         self.mox.VerifyAll()
     79 
     80 
     81     def test_suite_fail_with_missing_test(self):
     82         """Test test_suite method that should fail with missing test."""
     83         test_views = {'SERVER_JOB':                        'GOOD',
     84                       'dummy_fail/control.dependency':     'TEST_NA',
     85                       }
     86 
     87         self.stub_out_methods(test_views)
     88         self.mox.ReplayAll()
     89         test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
     90                              arguments=test_push.parse_arguments(self._ARGV))
     91         self.mox.VerifyAll()
     92 
     93 
     94     def test_suite_fail_with_unexpected_test_results(self):
     95         """Test test_suite method that should fail with unexpected test results.
     96         """
     97         test_views = {'SERVER_JOB':                        'FAIL',
     98                       'dummy_fail/control.dependency':     'TEST_NA',
     99                       'dummy_Fail.RetryFail':              'FAIL',
    100                       }
    101 
    102         self.stub_out_methods(test_views)
    103         self.mox.ReplayAll()
    104         test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
    105                              arguments=test_push.parse_arguments(self._ARGV))
    106         self.mox.VerifyAll()
    107 
    108 
    109     def test_suite_fail_with_extra_test(self):
    110         """Test test_suite method that should fail with extra test."""
    111         test_views = {'SERVER_JOB':                        'GOOD',
    112                       'dummy_fail/control.dependency':     'TEST_NA',
    113                       'dummy_Fail.RetryFail':              'FAIL',
    114                       'dummy_Fail.ExtraTest':              'GOOD',
    115                       }
    116 
    117         self.stub_out_methods(test_views)
    118         self.mox.ReplayAll()
    119         test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
    120                              arguments=test_push.parse_arguments(self._ARGV))
    121         self.mox.VerifyAll()
    122 
    123 
    124 if __name__ == '__main__':
    125     unittest.main()
    126