aboutsummaryrefslogtreecommitdiff
path: root/site_utils/test_push_unittest.py
blob: fbc3d3135adf7d6d0ff8c9e2269e79cd3286fcac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python2
# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import six
import mox
import unittest
from six.moves import urllib

import mock

import common
from autotest_lib.client.common_lib.cros import retry
from autotest_lib.server import site_utils


# Mock retry.retry used in test_push before importing test_push.
retry.retry = mock.create_autospec(retry.retry, return_value=lambda func: func)
from autotest_lib.site_utils import test_push

class TestPushUnittests(mox.MoxTestBase):
    """Unittest for test_push script."""

    _ARGV = [
        'command',
        '--build', 'stumpy-release/R36-5881-0.0',
        '--shard_build', 'quawks-release/R36-5881-0.0'
    ]

    def setUp(self):
        """Initialize the unittest."""
        super(TestPushUnittests, self).setUp()
        # Overwrite expected test results.
        test_push.EXPECTED_TEST_RESULTS = {
            '^SERVER_JOB$':                  ['GOOD'],
            '.*control.dependency$':         ['TEST_NA'],
            '.*dummy_Fail.RetryFail$':       ['FAIL', 'FAIL'],
            }
        test_push.TKO = None


    def stub_out_methods(self, test_views):
        """Stub out methods in test_push module with given test results.

        @param test_views: Desired test result views.

        """
        self.mox.UnsetStubs()
        response = six.StringIO('some_value')
        self.mox.StubOutWithMock(urllib.request, 'urlopen')
        urllib.request.urlopen(mox.IgnoreArg()).AndReturn(response)

        self.mox.StubOutWithMock(test_push, 'check_dut_image')
        test_push.check_dut_image(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(
                None)

        self.mox.StubOutWithMock(test_push, 'do_run_suite')
        test_push.do_run_suite(
                test_push.PUSH_TO_PROD_SUITE, mox.IgnoreArg(), mox.IgnoreArg(),
                mox.IgnoreArg()).AndReturn((1))

        self.mox.StubOutWithMock(site_utils, 'get_test_views_from_tko')
        site_utils.get_test_views_from_tko(1, None).AndReturn(test_views)


    def test_suite_success(self):
        """Test test_suite method with matching results."""
        test_views = {'SERVER_JOB':                        ['GOOD'],
                      'dummy_fail/control.dependency':     ['TEST_NA'],
                      'dummy_Fail.RetryFail':              ['FAIL', 'FAIL'],
                      }

        self.stub_out_methods(test_views)
        self.mox.ReplayAll()
        test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
                             arguments=test_push.parse_arguments(self._ARGV))
        self.mox.VerifyAll()


    def test_suite_fail_with_missing_test(self):
        """Test test_suite method that should fail with missing test."""
        test_views = {'SERVER_JOB':                        ['GOOD'],
                      'dummy_fail/control.dependency':     ['TEST_NA'],
                      }

        self.stub_out_methods(test_views)
        self.mox.ReplayAll()
        test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
                             arguments=test_push.parse_arguments(self._ARGV))
        self.mox.VerifyAll()


    def test_suite_fail_with_unexpected_test_results(self):
        """Test test_suite method that should fail with unexpected test results.
        """
        test_views = {'SERVER_JOB':                        ['FAIL'],
                      'dummy_fail/control.dependency':     ['TEST_NA'],
                      'dummy_Fail.RetryFail':              ['FAIL', 'FAIL'],
                      }

        self.stub_out_methods(test_views)
        self.mox.ReplayAll()
        test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
                             arguments=test_push.parse_arguments(self._ARGV))
        self.mox.VerifyAll()


    def test_suite_fail_with_extra_test(self):
        """Test test_suite method that should fail with extra test."""
        test_views = {'SERVER_JOB':                        ['GOOD'],
                      'dummy_fail/control.dependency':     ['TEST_NA'],
                      'dummy_Fail.RetryFail':              ['FAIL', 'FAIL'],
                      'dummy_Fail.ExtraTest':              ['GOOD'],
                      }

        self.stub_out_methods(test_views)
        self.mox.ReplayAll()
        test_push.test_suite(test_push.PUSH_TO_PROD_SUITE, test_views,
                             arguments=test_push.parse_arguments(self._ARGV))
        self.mox.VerifyAll()


if __name__ == '__main__':
    unittest.main()