| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2016 The Chromium 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 | |
| 6 import copy | |
| 7 import os | |
| 8 import sys | |
| 9 import unittest | |
| 10 | |
| 11 root_dir = os.path.abspath(os.path.join( | |
| 12 os.path.dirname(__file__), os.path.pardir, | |
| 13 os.path.pardir, os.path.pardir, os.path.pardir)) | |
| 14 sys.path.insert(0, os.path.join(root_dir, 'third_party', 'mock-1.0.1')) | |
| 15 sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.path.pardir)) | |
| 16 | |
| 17 import mock | |
| 18 | |
| 19 from auto_bisect import config_validation | |
| 20 | |
| 21 | |
| 22 class ConfigValidationTest(unittest.TestCase): # pragma: no cover | |
| 23 | |
| 24 def test_validate_bisect_config_empty_config(self): | |
| 25 config_validation.validate_bisect_config(config={}, schema={}) | |
| 26 | |
| 27 def test_validate_bisect_config_with_missing_required_fails(self): | |
| 28 with self.assertRaises(config_validation.ValidationFail): | |
| 29 config_validation.validate_bisect_config( | |
| 30 config={}, | |
| 31 schema={'foo': {'type': 'integer', 'required': True}}) | |
| 32 | |
| 33 def test_validate_bisect_config_with_one_field_passes(self): | |
| 34 config_validation.validate_bisect_config( | |
| 35 config={'foo': 123}, | |
| 36 schema={'foo': {'type': 'integer'}}) | |
| 37 | |
| 38 def test_validate_optional_field_passes(self): | |
| 39 config_validation.validate_bisect_config( | |
| 40 config={}, | |
| 41 schema={'foo': {'type': 'integer'}}) | |
| 42 | |
| 43 def test_validate_not_in_schema_passes(self): | |
| 44 config_validation.validate_bisect_config( | |
| 45 config={'foo': 'asdf'}, | |
| 46 schema={}) | |
| 47 | |
| 48 def test_validate_bisect_config_larger_passing_example(self): | |
| 49 schema = { | |
| 50 'good_revision': {'type': 'revision'}, | |
| 51 'bad_revision': {'type': 'revision'}, | |
| 52 'str1': {'type': 'string'}, | |
| 53 'str2': {'type': 'string'}, | |
| 54 'int1': {'type': 'integer'}, | |
| 55 'int2': {'type': 'integer'}, | |
| 56 'bool1': {'type': 'boolean'}, | |
| 57 'bool2': {'type': 'boolean'}, | |
| 58 } | |
| 59 config = { | |
| 60 'good_revision': '0123456789abcdeabcde0123456789abcdeabcde', | |
| 61 'bad_revision': 'bbbbbaaaaa0000011111bbbbbaaaaa0000011111', | |
| 62 'str1': u'unicode-string', | |
| 63 'str2': '', | |
| 64 'int1': '12345', | |
| 65 'int2': 12345, | |
| 66 'bool1': True, | |
| 67 'bool2': False, | |
| 68 } | |
| 69 config_validation.validate_bisect_config(config, schema) | |
| 70 | |
| 71 def test_validate_revisions_out_of_order_failure(self): | |
| 72 schema = { | |
| 73 'good_revision': {'type': 'revision'}, | |
| 74 'bad_revision': {'type': 'revision'}, | |
| 75 } | |
| 76 config = { | |
| 77 'good_revision': 200, | |
| 78 'bad_revision': 100, | |
| 79 } | |
| 80 with self.assertRaises(config_validation.ValidationFail): | |
| 81 config_validation.validate_bisect_config(config, schema) | |
| 82 | |
| 83 def test_validate_metric_with_return_code_not_required(self): | |
| 84 schema = { | |
| 85 'metric': {'type': 'string'}, | |
| 86 'bisect_mode': {'type': 'string'}, | |
| 87 } | |
| 88 config = { | |
| 89 'bisect_mode': 'return_code', | |
| 90 } | |
| 91 config_validation.validate_bisect_config(config, schema) | |
| 92 | |
| 93 def test_validate_metric_missing_failure(self): | |
| 94 schema = { | |
| 95 'metric': {'type': 'string'}, | |
| 96 'bisect_mode': {'type': 'string'}, | |
| 97 } | |
| 98 config = { | |
| 99 'bisect_mode': 'mean', | |
| 100 } | |
| 101 with self.assertRaises(config_validation.ValidationFail): | |
| 102 config_validation.validate_bisect_config(config, schema) | |
| 103 | |
| 104 def test_validate_metric_format_failure(self): | |
| 105 schema = { | |
| 106 'metric': {'type': 'string'}, | |
| 107 'bisect_mode': {'type': 'string'}, | |
| 108 } | |
| 109 config = { | |
| 110 'bisect_mode': 'mean', | |
| 111 'metric': 'a/b/c', | |
| 112 } | |
| 113 with self.assertRaises(config_validation.ValidationFail): | |
| 114 config_validation.validate_bisect_config(config, schema) | |
| 115 | |
| 116 def test_validate_metric_format_pass(self): | |
| 117 schema = { | |
| 118 'metric': {'type': 'string'}, | |
| 119 'bisect_mode': {'type': 'string'}, | |
| 120 } | |
| 121 config = { | |
| 122 'bisect_mode': 'mean', | |
| 123 'metric': 'a/b', | |
| 124 } | |
| 125 config_validation.validate_bisect_config(config, schema) | |
| 126 | |
| 127 def test_validate_string_failure(self): | |
| 128 with self.assertRaises(config_validation.ValidationFail): | |
| 129 config_validation.validate_key( | |
| 130 config={'x': 12345}, | |
| 131 schema={'x': {'type': 'string'}}, | |
| 132 key='x') | |
| 133 | |
| 134 def test_validate_integer_failure(self): | |
| 135 with self.assertRaises(config_validation.ValidationFail): | |
| 136 config_validation.validate_key( | |
| 137 config={'x': '123a'}, | |
| 138 schema={'x': {'type': 'integer'}}, | |
| 139 key='x') | |
| 140 | |
| 141 def test_validate_revision_failure(self): | |
| 142 with self.assertRaises(config_validation.ValidationFail): | |
| 143 config_validation.validate_key( | |
| 144 config={'x': 'abcdef'}, | |
| 145 schema={'x': {'type': 'revision'}}, | |
| 146 key='x') | |
| 147 | |
| 148 def test_validate_boolean_failure(self): | |
| 149 with self.assertRaises(config_validation.ValidationFail): | |
| 150 config_validation.validate_key( | |
| 151 config={'x': 'true'}, | |
| 152 schema={'x': {'type': 'boolean'}}, | |
| 153 key='x') | |
| 154 | |
| 155 def test_validate_choice_failure(self): | |
| 156 with self.assertRaises(config_validation.ValidationFail): | |
| 157 config_validation.validate_key( | |
| 158 config={'x': 3}, | |
| 159 schema={'x': {'type': 'int', 'choices': [1, 2]}}, | |
| 160 key='x') | |
| 161 | |
| 162 | |
| 163 if __name__ == '__main__': | |
| 164 unittest.main() # pragma: no cover | |
| OLD | NEW |