• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#
4# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8"""Unittest for crosperf."""
9
10from __future__ import division
11from __future__ import print_function
12
13import argparse
14import io
15import tempfile
16import unittest
17import unittest.mock as mock
18
19import crosperf
20import settings_factory
21import experiment_file
22
23EXPERIMENT_FILE_1 = """
24  board: x86-alex
25  remote: chromeos-alex3
26  locks_dir: /tmp
27  perf_args: record -a -e cycles
28  benchmark: PageCycler {
29    iterations: 3
30  }
31
32  image1 {
33    chromeos_image: /usr/local/google/cros_image1.bin
34  }
35
36  image2 {
37    remote: chromeos-lumpy1
38    chromeos_image: /usr/local/google/cros_image2.bin
39  }
40  """
41
42
43class CrosperfTest(unittest.TestCase):
44  """Crosperf test class."""
45
46  def setUp(self):
47    input_file = io.StringIO(EXPERIMENT_FILE_1)
48    self.exp_file = experiment_file.ExperimentFile(input_file)
49
50  def testDryRun(self):
51    with tempfile.NamedTemporaryFile('w', encoding='utf-8') as f:
52      f.write(EXPERIMENT_FILE_1)
53      f.flush()
54      crosperf.Main(['', f.name, '--dry_run'])
55
56  def testConvertOptionsToSettings(self):
57    parser = argparse.ArgumentParser()
58    parser.add_argument(
59        '-l',
60        '--log_dir',
61        dest='log_dir',
62        default='',
63        help='The log_dir, default is under '
64        '<crosperf_logs>/logs')
65    crosperf.SetupParserOptions(parser)
66    argv = ['crosperf/crosperf.py', 'temp.exp', '--rerun=True']
67    options, _ = parser.parse_known_args(argv)
68    settings = crosperf.ConvertOptionsToSettings(options)
69    self.assertIsNotNone(settings)
70    self.assertIsInstance(settings, settings_factory.GlobalSettings)
71    self.assertEqual(len(settings.fields), 38)
72    self.assertTrue(settings.GetField('rerun'))
73    argv = ['crosperf/crosperf.py', 'temp.exp']
74    options, _ = parser.parse_known_args(argv)
75    settings = crosperf.ConvertOptionsToSettings(options)
76    self.assertFalse(settings.GetField('rerun'))
77
78  def testExceptionPrintTraceback(self):
79    """Test the main function can print traceback in exception."""
80
81    def mock_RunCrosperf(*_args, **_kwargs):
82      return 10 / 0
83
84    with mock.patch('crosperf.RunCrosperf', new=mock_RunCrosperf):
85      with self.assertRaises(ZeroDivisionError) as context:
86        crosperf.Main([])
87      self.assertEqual('division by zero', str(context.exception))
88
89
90if __name__ == '__main__':
91  unittest.main()
92