experiment_file_unittest.py revision 6367e17c2a01a67fe015f4a94a1cbb30d7958dfc
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="""remote: chromeos-alex3 68board: x86-alex 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 85class ExperimentFileTest(unittest.TestCase): 86 def testLoadExperimentFile1(self): 87 input_file = StringIO.StringIO(EXPERIMENT_FILE_1) 88 experiment_file = ExperimentFile(input_file) 89 global_settings = experiment_file.GetGlobalSettings() 90 self.assertEqual(global_settings.GetField("remote"), ["chromeos-alex3"]) 91 self.assertEqual(global_settings.GetField("perf_args"), 92 "record -a -e cycles") 93 benchmark_settings = experiment_file.GetSettings("benchmark") 94 self.assertEqual(len(benchmark_settings), 1) 95 self.assertEqual(benchmark_settings[0].name, "PageCycler") 96 self.assertEqual(benchmark_settings[0].GetField("iterations"), 3) 97 98 label_settings = experiment_file.GetSettings("label") 99 self.assertEqual(len(label_settings), 2) 100 self.assertEqual(label_settings[0].name, "image1") 101 self.assertEqual(label_settings[0].GetField("chromeos_image"), 102 "/usr/local/google/cros_image1.bin") 103 self.assertEqual(label_settings[1].GetField("remote"), ["chromeos-lumpy1"]) 104 self.assertEqual(label_settings[0].GetField("remote"), ["chromeos-alex3"]) 105 106 def testOverrideSetting(self): 107 input_file = StringIO.StringIO(EXPERIMENT_FILE_2) 108 experiment_file = ExperimentFile(input_file) 109 global_settings = experiment_file.GetGlobalSettings() 110 self.assertEqual(global_settings.GetField("remote"), ["chromeos-alex3"]) 111 112 benchmark_settings = experiment_file.GetSettings("benchmark") 113 self.assertEqual(len(benchmark_settings), 2) 114 self.assertEqual(benchmark_settings[0].name, "PageCycler") 115 self.assertEqual(benchmark_settings[0].GetField("iterations"), 3) 116 self.assertEqual(benchmark_settings[1].name, "AndroidBench") 117 self.assertEqual(benchmark_settings[1].GetField("iterations"), 2) 118 119 def testDuplicateLabel(self): 120 input_file = StringIO.StringIO(EXPERIMENT_FILE_3) 121 self.assertRaises(Exception, ExperimentFile, input_file) 122 123 def testCanonicalize(self): 124 input_file = StringIO.StringIO(EXPERIMENT_FILE_1) 125 experiment_file = ExperimentFile(input_file) 126 res = experiment_file.Canonicalize() 127 self.assertEqual(res, OUTPUT_FILE) 128 129if __name__ == "__main__": 130 unittest.main() 131