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 | |
9 import json | 7 import json |
10 import logging | 8 import logging |
11 import os | 9 import os |
12 import re | 10 import re |
13 import sys | 11 import sys |
14 | 12 |
15 from . import env | 13 from . import env |
16 from . import stream | |
17 import expect_tests | 14 import expect_tests |
18 | 15 |
19 # This variable must be set in the dynamic scope of the functions in this file. | 16 # This variable must be set in the dynamic scope of the functions in this file. |
20 # We do this instead of passing because the threading system of expect tests | 17 # We do this instead of passing because the threading system of expect tests |
21 # doesn't know how to serialize it. | 18 # doesn't know how to serialize it. |
22 _UNIVERSE = None | 19 _UNIVERSE = None |
23 | 20 |
24 | 21 |
25 def RenderExpectation(test_data, raw_expectations): | 22 def RenderExpectation(test_data, raw_expectations): |
26 """Applies the step filters (e.g. whitelists, etc.) to the raw_expectations, | 23 """Applies the step filters (e.g. whitelists, etc.) to the raw_expectations, |
(...skipping 24 matching lines...) Expand all Loading... |
51 "The whitelist includes field %r in step %r, but that field" | 48 "The whitelist includes field %r in step %r, but that field" |
52 " doesn't exist." | 49 " doesn't exist." |
53 % (k, step_name)) | 50 % (k, step_name)) |
54 new_step[k] = raw_step[k] | 51 new_step[k] = raw_step[k] |
55 new_result.append(new_step) | 52 new_result.append(new_step) |
56 raw_expectations = new_result | 53 raw_expectations = new_result |
57 | 54 |
58 return expect_tests.Result(raw_expectations) | 55 return expect_tests.Result(raw_expectations) |
59 | 56 |
60 | 57 |
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 | |
79 def RunRecipe(test_data): | 58 def RunRecipe(test_data): |
80 """Actually runs the recipe given the GenTests-supplied test_data.""" | 59 """Actually runs the recipe given the GenTests-supplied test_data.""" |
81 from . import config_types | 60 from . import config_types |
82 from . import loader | 61 from . import loader |
83 from . import run | 62 from . import run |
84 from . import step_runner | 63 from . import step_runner |
85 from . import stream | 64 from . import stream |
86 | 65 |
87 config_types.ResetTostringFns() | 66 config_types.ResetTostringFns() |
88 | 67 with stream.StreamEngineInvariants() as stream_engine: |
89 annotator = SimulationAnnotatorStreamEngine() | 68 step_runner = step_runner.SimulationStepRunner(stream_engine, test_data) |
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) | |
96 | 69 |
97 engine = run.RecipeEngine(step_runner, test_data.properties, _UNIVERSE) | 70 engine = run.RecipeEngine(step_runner, test_data.properties, _UNIVERSE) |
98 recipe_script = _UNIVERSE.load_recipe(test_data.properties['recipe']) | 71 recipe_script = _UNIVERSE.load_recipe(test_data.properties['recipe']) |
99 api = loader.create_recipe_api(recipe_script.LOADED_DEPS, engine, test_data) | 72 api = loader.create_recipe_api(recipe_script.LOADED_DEPS, engine, test_data) |
100 result = engine.run(recipe_script, api) | 73 result = engine.run(recipe_script, api) |
101 | 74 |
102 # Don't include tracebacks in expectations because they are too sensitive to | 75 # Don't include tracebacks in expectations because they are too sensitive to |
103 # change. | 76 # change. |
104 result.result.pop('traceback', None) | 77 result.result.pop('traceback', None) |
105 raw_expectations = step_runner.steps_ran + [result.result] | 78 raw_expectations = step_runner.steps_ran + [result.result] |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 'TESTING_SLAVENAME']: | 160 'TESTING_SLAVENAME']: |
188 if env_var in os.environ: | 161 if env_var in os.environ: |
189 logging.warn("Ignoring %s environment variable." % env_var) | 162 logging.warn("Ignoring %s environment variable." % env_var) |
190 os.environ.pop(env_var) | 163 os.environ.pop(env_var) |
191 | 164 |
192 global _UNIVERSE | 165 global _UNIVERSE |
193 _UNIVERSE = universe | 166 _UNIVERSE = universe |
194 | 167 |
195 expect_tests.main('recipe_simulation_test', GenerateTests, | 168 expect_tests.main('recipe_simulation_test', GenerateTests, |
196 cover_omit=cover_omit(), args=args) | 169 cover_omit=cover_omit(), args=args) |
OLD | NEW |