1#!/usr/bin/env python2 2 3# Copyright (c) 2011 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7"""The unittest of experiment_file.""" 8from __future__ import print_function 9import StringIO 10import unittest 11from experiment_file import ExperimentFile 12 13EXPERIMENT_FILE_1 = """ 14 board: x86-alex 15 remote: chromeos-alex3 16 perf_args: record -a -e cycles 17 benchmark: PageCycler { 18 iterations: 3 19 } 20 21 image1 { 22 chromeos_image: /usr/local/google/cros_image1.bin 23 } 24 25 image2 { 26 remote: chromeos-lumpy1 27 chromeos_image: /usr/local/google/cros_image2.bin 28 } 29 """ 30 31EXPERIMENT_FILE_2 = """ 32 board: x86-alex 33 remote: chromeos-alex3 34 iterations: 3 35 36 benchmark: PageCycler { 37 } 38 39 benchmark: AndroidBench { 40 iterations: 2 41 } 42 43 image1 { 44 chromeos_image:/usr/local/google/cros_image1.bin 45 } 46 47 image2 { 48 chromeos_image: /usr/local/google/cros_image2.bin 49 } 50 """ 51 52EXPERIMENT_FILE_3 = """ 53 board: x86-alex 54 remote: chromeos-alex3 55 iterations: 3 56 57 benchmark: PageCycler { 58 } 59 60 image1 { 61 chromeos_image:/usr/local/google/cros_image1.bin 62 } 63 64 image1 { 65 chromeos_image: /usr/local/google/cros_image2.bin 66 } 67 """ 68 69OUTPUT_FILE = """board: x86-alex 70remote: chromeos-alex3 71perf_args: record -a -e cycles 72 73benchmark: PageCycler { 74\titerations: 3 75} 76 77label: image1 { 78\tremote: chromeos-alex3 79\tchromeos_image: /usr/local/google/cros_image1.bin 80} 81 82label: image2 { 83\tremote: chromeos-lumpy1 84\tchromeos_image: /usr/local/google/cros_image2.bin 85}\n\n""" 86 87 88class ExperimentFileTest(unittest.TestCase): 89 """The main class for Experiment File test.""" 90 def testLoadExperimentFile1(self): 91 input_file = StringIO.StringIO(EXPERIMENT_FILE_1) 92 experiment_file = ExperimentFile(input_file) 93 global_settings = experiment_file.GetGlobalSettings() 94 self.assertEqual(global_settings.GetField('remote'), ['chromeos-alex3']) 95 self.assertEqual( 96 global_settings.GetField('perf_args'), 'record -a -e cycles') 97 benchmark_settings = experiment_file.GetSettings('benchmark') 98 self.assertEqual(len(benchmark_settings), 1) 99 self.assertEqual(benchmark_settings[0].name, 'PageCycler') 100 self.assertEqual(benchmark_settings[0].GetField('iterations'), 3) 101 102 label_settings = experiment_file.GetSettings('label') 103 self.assertEqual(len(label_settings), 2) 104 self.assertEqual(label_settings[0].name, 'image1') 105 self.assertEqual(label_settings[0].GetField('chromeos_image'), 106 '/usr/local/google/cros_image1.bin') 107 self.assertEqual(label_settings[1].GetField('remote'), ['chromeos-lumpy1']) 108 self.assertEqual(label_settings[0].GetField('remote'), ['chromeos-alex3']) 109 110 def testOverrideSetting(self): 111 input_file = StringIO.StringIO(EXPERIMENT_FILE_2) 112 experiment_file = ExperimentFile(input_file) 113 global_settings = experiment_file.GetGlobalSettings() 114 self.assertEqual(global_settings.GetField('remote'), ['chromeos-alex3']) 115 116 benchmark_settings = experiment_file.GetSettings('benchmark') 117 self.assertEqual(len(benchmark_settings), 2) 118 self.assertEqual(benchmark_settings[0].name, 'PageCycler') 119 self.assertEqual(benchmark_settings[0].GetField('iterations'), 3) 120 self.assertEqual(benchmark_settings[1].name, 'AndroidBench') 121 self.assertEqual(benchmark_settings[1].GetField('iterations'), 2) 122 123 def testDuplicateLabel(self): 124 input_file = StringIO.StringIO(EXPERIMENT_FILE_3) 125 self.assertRaises(Exception, ExperimentFile, input_file) 126 127 def testCanonicalize(self): 128 input_file = StringIO.StringIO(EXPERIMENT_FILE_1) 129 experiment_file = ExperimentFile(input_file) 130 res = experiment_file.Canonicalize() 131 self.assertEqual(res, OUTPUT_FILE) 132 133 134if __name__ == '__main__': 135 unittest.main() 136