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