| OLD | NEW |
| 1 # Copyright 2014 The LUCI Authors. All rights reserved. | 1 # Copyright 2014 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
| 4 | 4 |
| 5 """Provides simulator test coverage for individual recipes.""" | 5 """Provides simulator test coverage for individual recipes.""" |
| 6 | 6 |
| 7 import StringIO |
| 8 import contextlib |
| 7 import json | 9 import json |
| 8 import logging | 10 import logging |
| 9 import os | 11 import os |
| 10 import re | 12 import re |
| 11 import sys | 13 import sys |
| 12 | 14 |
| 13 from . import env | 15 from . import env |
| 16 from . import stream |
| 14 import expect_tests | 17 import expect_tests |
| 15 | 18 |
| 16 # This variable must be set in the dynamic scope of the functions in this file. | 19 # This variable must be set in the dynamic scope of the functions in this file. |
| 17 # We do this instead of passing because the threading system of expect tests | 20 # We do this instead of passing because the threading system of expect tests |
| 18 # doesn't know how to serialize it. | 21 # doesn't know how to serialize it. |
| 19 _UNIVERSE = None | 22 _UNIVERSE = None |
| 20 | 23 |
| 21 | 24 |
| 22 def RenderExpectation(test_data, raw_expectations): | 25 def RenderExpectation(test_data, raw_expectations): |
| 23 """Applies the step filters (e.g. whitelists, etc.) to the raw_expectations, | 26 """Applies the step filters (e.g. whitelists, etc.) to the raw_expectations, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 48 "The whitelist includes field %r in step %r, but that field" | 51 "The whitelist includes field %r in step %r, but that field" |
| 49 " doesn't exist." | 52 " doesn't exist." |
| 50 % (k, step_name)) | 53 % (k, step_name)) |
| 51 new_step[k] = raw_step[k] | 54 new_step[k] = raw_step[k] |
| 52 new_result.append(new_step) | 55 new_result.append(new_step) |
| 53 raw_expectations = new_result | 56 raw_expectations = new_result |
| 54 | 57 |
| 55 return expect_tests.Result(raw_expectations) | 58 return expect_tests.Result(raw_expectations) |
| 56 | 59 |
| 57 | 60 |
| 61 class SimulationAnnotatorStreamEngine(stream.AnnotatorStreamEngine): |
| 62 |
| 63 def __init__(self): |
| 64 self._step_buffer_map = {} |
| 65 super(SimulationAnnotatorStreamEngine, self).__init__( |
| 66 self.step_buffer(None)) |
| 67 |
| 68 def step_buffer(self, step_name): |
| 69 return self._step_buffer_map.setdefault(step_name, StringIO.StringIO()) |
| 70 |
| 71 def _new_step_stream(self, step_name, allow_subannotations, nest_level): |
| 72 return self._create_step_stream( |
| 73 step_name, |
| 74 self.step_buffer(step_name), |
| 75 allow_subannotations, |
| 76 nest_level) |
| 77 |
| 78 |
| 58 def RunRecipe(test_data): | 79 def RunRecipe(test_data): |
| 59 """Actually runs the recipe given the GenTests-supplied test_data.""" | 80 """Actually runs the recipe given the GenTests-supplied test_data.""" |
| 60 from . import config_types | 81 from . import config_types |
| 61 from . import loader | 82 from . import loader |
| 62 from . import run | 83 from . import run |
| 63 from . import step_runner | 84 from . import step_runner |
| 64 from . import stream | 85 from . import stream |
| 65 | 86 |
| 66 config_types.ResetTostringFns() | 87 config_types.ResetTostringFns() |
| 67 with stream.StreamEngineInvariants() as stream_engine: | 88 |
| 68 step_runner = step_runner.SimulationStepRunner(stream_engine, test_data) | 89 annotator = SimulationAnnotatorStreamEngine() |
| 90 stream_engine = stream.ProductStreamEngine( |
| 91 stream.StreamEngineInvariants(), |
| 92 annotator) |
| 93 with stream_engine: |
| 94 step_runner = step_runner.SimulationStepRunner(stream_engine, test_data, |
| 95 annotator) |
| 69 | 96 |
| 70 engine = run.RecipeEngine(step_runner, test_data.properties, _UNIVERSE) | 97 engine = run.RecipeEngine(step_runner, test_data.properties, _UNIVERSE) |
| 71 recipe_script = _UNIVERSE.load_recipe(test_data.properties['recipe']) | 98 recipe_script = _UNIVERSE.load_recipe(test_data.properties['recipe']) |
| 72 api = loader.create_recipe_api(recipe_script.LOADED_DEPS, engine, test_data) | 99 api = loader.create_recipe_api(recipe_script.LOADED_DEPS, engine, test_data) |
| 73 result = engine.run(recipe_script, api) | 100 result = engine.run(recipe_script, api) |
| 74 | 101 |
| 75 # Don't include tracebacks in expectations because they are too sensitive to | 102 # Don't include tracebacks in expectations because they are too sensitive to |
| 76 # change. | 103 # change. |
| 77 result.result.pop('traceback', None) | 104 result.result.pop('traceback', None) |
| 78 raw_expectations = step_runner.steps_ran + [result.result] | 105 raw_expectations = step_runner.steps_ran + [result.result] |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 'TESTING_SLAVENAME']: | 187 'TESTING_SLAVENAME']: |
| 161 if env_var in os.environ: | 188 if env_var in os.environ: |
| 162 logging.warn("Ignoring %s environment variable." % env_var) | 189 logging.warn("Ignoring %s environment variable." % env_var) |
| 163 os.environ.pop(env_var) | 190 os.environ.pop(env_var) |
| 164 | 191 |
| 165 global _UNIVERSE | 192 global _UNIVERSE |
| 166 _UNIVERSE = universe | 193 _UNIVERSE = universe |
| 167 | 194 |
| 168 expect_tests.main('recipe_simulation_test', GenerateTests, | 195 expect_tests.main('recipe_simulation_test', GenerateTests, |
| 169 cover_omit=cover_omit(), args=args) | 196 cover_omit=cover_omit(), args=args) |
| OLD | NEW |