• 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 unittest
11from unittest.mock import patch
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(unittest.TestCase):
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    def testGetViewsFromTko(self):
62        """Test method get_test_views_from_tko
63        """
64        test_results = [
65                ('stub_Pass', 'GOOD'),
66                ('stub_Fail.RetrySuccess', 'GOOD'),
67                ('stub_Fail.RetrySuccess', 'FAIL'),
68                ('stub_Fail.Fail', 'FAIL'),
69                ('stub_Fail.Fail', 'FAIL'),
70        ]
71
72        expected_test_views = {
73                'stub_Pass': ['GOOD'],
74                'stub_Fail.RetrySuccess': ['FAIL', 'GOOD'],
75                'stub_Fail.Fail': ['FAIL', 'FAIL'],
76        }
77
78        patcher = patch.object(frontend, 'TKO')
79        tko = patcher.start()
80        self.addCleanup(patcher.stop)
81
82        tko.run.return_value = ([{
83                'test_name': r[0],
84                'status': r[1]
85        } for r in test_results])
86        test_views = site_utils.get_test_views_from_tko(0, tko)
87        tko.run.assert_called_with('get_detailed_test_views', afe_job_id=0)
88
89        self.assertEqual(sorted(test_views.keys()),
90                         sorted(expected_test_views.keys()),
91                         'Test list %s does not match expected test list %s.' %
92                         (sorted(test_views.keys()),
93                          sorted(expected_test_views.keys())))
94
95        for test_name, test_status_list in six.iteritems(test_views):
96            self.assertEqual(sorted(test_status_list),
97                             sorted(expected_test_views[test_name]),
98                             'For test %s the status list %s does not match '
99                             'expected status list %s.' %
100                             (test_name,
101                              sorted(test_status_list),
102                              sorted(expected_test_views[test_name])))
103
104
105if __name__ == '__main__':
106    unittest.main()
107