• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Lint as: python2, python3
2# Copyright 2015 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
6from __future__ import absolute_import
7from __future__ import division
8from __future__ import print_function
9
10import mox
11import unittest
12
13import common
14from autotest_lib.frontend import setup_django_lite_environment
15from autotest_lib.server import frontend
16from autotest_lib.server import site_utils
17from autotest_lib.server.cros.dynamic_suite import tools
18from autotest_lib.server.cros.dynamic_suite import suite_common
19import six
20
21
22class SiteUtilsUnittests(mox.MoxTestBase):
23    """Test functions in site_utils.py"""
24
25    def testParseJobName(self):
26        """Test method parse_job_name.
27        """
28        trybot_paladin_build = 'trybot-lumpy-paladin/R27-3837.0.0-b123'
29        trybot_release_build = 'trybot-lumpy-release/R27-3837.0.0-b456'
30        release_build = 'lumpy-release/R27-3837.0.0'
31        paladin_build = 'lumpy-paladin/R27-3878.0.0-rc7'
32        brillo_build = 'git_mnc-brillo-dev/lumpy-eng/1234'
33        chrome_pfq_build = 'lumpy-chrome-pfq/R27-3837.0.0'
34        chromium_pfq_build = 'lumpy-chromium-pfq/R27-3837.0.0'
35
36        builds = [trybot_paladin_build, trybot_release_build, release_build,
37                  paladin_build, brillo_build, chrome_pfq_build,
38                  chromium_pfq_build]
39        test_name = 'login_LoginSuccess'
40        board = 'lumpy'
41        suite = 'bvt'
42        for build in builds:
43            expected_info = {'board': board,
44                             'suite': suite,
45                             'build': build}
46            build_parts = build.split('/')
47            if len(build_parts) == 2:
48                expected_info['build_version'] = build_parts[1]
49            else:
50                expected_info['build_version'] = build_parts[2]
51            suite_job_name = ('%s-%s' %
52                    (build, suite_common.canonicalize_suite_name(suite)))
53            info = site_utils.parse_job_name(suite_job_name)
54            self.assertEqual(info, expected_info, '%s failed to be parsed to '
55                             '%s' % (suite_job_name, expected_info))
56            test_job_name = tools.create_job_name(build, suite, test_name)
57            info = site_utils.parse_job_name(test_job_name)
58            self.assertEqual(info, expected_info, '%s failed to be parsed to '
59                             '%s' % (test_job_name, expected_info))
60
61
62    def testGetViewsFromTko(self):
63        """Test method get_test_views_from_tko
64        """
65        test_results = [
66            ('dummy_Pass', 'GOOD'),
67            ('dummy_Fail.RetrySuccess', 'GOOD'),
68            ('dummy_Fail.RetrySuccess', 'FAIL'),
69            ('dummy_Fail.Fail', 'FAIL'),
70            ('dummy_Fail.Fail', 'FAIL'),
71        ]
72
73        expected_test_views = {
74            'dummy_Pass': ['GOOD'],
75            'dummy_Fail.RetrySuccess': ['FAIL', 'GOOD'],
76            'dummy_Fail.Fail': ['FAIL', 'FAIL'],
77        }
78
79        self.mox.UnsetStubs()
80        tko = self.mox.CreateMock(frontend.TKO)
81        tko.run('get_detailed_test_views', afe_job_id=0).AndReturn(
82            [{'test_name':r[0], 'status':r[1]} for r in test_results])
83
84        self.mox.ReplayAll()
85        test_views = site_utils.get_test_views_from_tko(0, tko)
86        self.mox.VerifyAll()
87
88        self.assertEqual(sorted(test_views.keys()),
89                         sorted(expected_test_views.keys()),
90                         'Test list %s does not match expected test list %s.' %
91                         (sorted(test_views.keys()),
92                          sorted(expected_test_views.keys())))
93
94        for test_name, test_status_list in six.iteritems(test_views):
95            self.assertEqual(sorted(test_status_list),
96                             sorted(expected_test_views[test_name]),
97                             'For test %s the status list %s does not match '
98                             'expected status list %s.' %
99                             (test_name,
100                              sorted(test_status_list),
101                              sorted(expected_test_views[test_name])))
102
103
104if __name__ == '__main__':
105    unittest.main()
106