| OLD | NEW |
| 1 # Copyright 2013 The LUCI Authors. All rights reserved. | 1 # Copyright 2013 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 from recipe_engine import recipe_api, config | 5 from recipe_engine import recipe_api, config |
| 6 | 6 |
| 7 DEPS = [ | 7 DEPS = [ |
| 8 'path', | 8 'path', |
| 9 'properties', | 9 'properties', |
| 10 'step', | 10 'step', |
| 11 ] | 11 ] |
| 12 | 12 |
| 13 | 13 |
| 14 RETURN_SCHEMA = config.ReturnSchema( | 14 RETURN_SCHEMA = config.ReturnSchema( |
| 15 test_me=config.Single(int) | 15 test_me=config.Single(int) |
| 16 ) | 16 ) |
| 17 | 17 |
| 18 | 18 |
| 19 PROPERTIES = { | 19 PROPERTIES = { |
| 20 'bad_return': recipe_api.Property(default=False), | 20 'bad_return': recipe_api.Property(default=False), |
| 21 'raise_infra_failure': recipe_api.Property(default=False), |
| 21 'access_invalid_data': recipe_api.Property(default=False), | 22 'access_invalid_data': recipe_api.Property(default=False), |
| 22 'timeout': recipe_api.Property(default=0, kind=int), | 23 'timeout': recipe_api.Property(default=0, kind=int), |
| 23 } | 24 } |
| 24 | 25 |
| 25 | 26 |
| 26 def RunSteps(api, bad_return, access_invalid_data, timeout): | 27 def RunSteps( |
| 28 api, bad_return, raise_infra_failure, access_invalid_data, timeout): |
| 27 if bad_return: | 29 if bad_return: |
| 28 return RETURN_SCHEMA.new(test_me='this should fail') | 30 return RETURN_SCHEMA.new(test_me='this should fail') |
| 29 elif timeout: | 31 elif timeout: |
| 30 # Timeout causes the recipe engine to raise an exception if your step takes | 32 # Timeout causes the recipe engine to raise an exception if your step takes |
| 31 # longer to run than you allow. Units are seconds. | 33 # longer to run than you allow. Units are seconds. |
| 32 if timeout == 1: | 34 if timeout == 1: |
| 33 api.step('timeout', ['sleep', '20'], timeout=1) | 35 api.step('timeout', ['sleep', '20'], timeout=1) |
| 34 elif timeout == 2: | 36 elif timeout == 2: |
| 35 try: | 37 try: |
| 36 api.step('caught timeout', ['sleep', '20'], timeout=1) | 38 api.step('caught timeout', ['sleep', '20'], timeout=1) |
| 37 except api.step.StepTimeout: | 39 except api.step.StepTimeout: |
| 38 return RETURN_SCHEMA(test_me=4) | 40 return RETURN_SCHEMA(test_me=4) |
| 39 | 41 |
| 40 | 42 |
| 41 # TODO(martinis) change this | 43 # TODO(martinis) change this |
| 42 # The api.step object is directly callable. | 44 # The api.step object is directly callable. |
| 43 api.step('hello', ['echo', 'Hello World']) | 45 api.step('hello', ['echo', 'Hello World']) |
| 44 api.step('hello', ['echo', 'Why hello, there.']) | 46 api.step('hello', ['echo', 'Why hello, there.']) |
| 45 | 47 |
| 46 # You can change the current working directory as well | |
| 47 api.step('mk subdir', ['mkdir', 'something']) | |
| 48 with api.step.context({'cwd': api.path['start_dir'].join('something')}): | |
| 49 api.step('something', ['bash', '-c', 'echo Why hello, there, in a subdir.']) | |
| 50 | |
| 51 # By default, all steps run in 'start_dir', or the cwd of the recipe engine | |
| 52 # when the recipe begins. Because of this, setting cwd to start_dir doesn't | |
| 53 # show anything in particular in the expectations. | |
| 54 with api.step.context({'cwd': api.path['start_dir']}): | |
| 55 api.step('start_dir ignored', ['bash', '-c', 'echo what happen']) | |
| 56 | |
| 57 # You can also manipulate various aspects of the step, such as env. | 48 # You can also manipulate various aspects of the step, such as env. |
| 58 # These are passed straight through to subprocess.Popen. | 49 # These are passed straight through to subprocess.Popen. |
| 59 # Also, abusing bash -c in this way is a TERRIBLE IDEA DON'T DO IT. | 50 # Also, abusing bash -c in this way is a TERRIBLE IDEA DON'T DO IT. |
| 60 with api.step.context({'env': {'friend': 'Darth Vader'}}): | 51 with api.step.context({'env': {'friend': 'Darth Vader'}}): |
| 61 api.step('goodbye', ['bash', '-c', 'echo Good bye, $friend.']) | 52 api.step('goodbye', ['bash', '-c', 'echo Good bye, $friend.']) |
| 62 | 53 |
| 63 # You can modify environment in terms of old environment. Environment | 54 # You can modify environment in terms of old environment. Environment |
| 64 # variables are substituted in for expressions of the form %(VARNAME)s. | 55 # variables are substituted in for expressions of the form %(VARNAME)s. |
| 65 with api.step.context({'env': {'PATH': api.path.pathsep.join( | 56 with api.step.context({'env': {'PATH': api.path.pathsep.join( |
| 66 [str(api.step.package_repo_resource()), '%(PATH)s'])}}): | 57 [str(api.step.package_repo_resource()), '%(PATH)s'])}}): |
| (...skipping 10 matching lines...) Expand all Loading... |
| 77 step_result.presentation.logs['the reason'] = ['The reason\nit failed'] | 68 step_result.presentation.logs['the reason'] = ['The reason\nit failed'] |
| 78 | 69 |
| 79 # Without a command, a step can be used to present some data from the recipe. | 70 # Without a command, a step can be used to present some data from the recipe. |
| 80 step_result = api.step('Just print stuff', cmd=None) | 71 step_result = api.step('Just print stuff', cmd=None) |
| 81 step_result.presentation.logs['more'] = ['More stuff'] | 72 step_result.presentation.logs['more'] = ['More stuff'] |
| 82 | 73 |
| 83 try: | 74 try: |
| 84 api.step('goodbye', ['echo', 'goodbye']) | 75 api.step('goodbye', ['echo', 'goodbye']) |
| 85 # Modifying step_result now would raise an AssertionError. | 76 # Modifying step_result now would raise an AssertionError. |
| 86 except api.step.StepFailure: | 77 except api.step.StepFailure: |
| 87 # Raising anything besides StepFailure or StepWarning causes the build to go | 78 # Raising anything besides StepFailure or StepWarning causes the build to go
|
| 88 # purple. | 79 # purple. |
| 89 raise ValueError('goodbye must exit 0!') | 80 raise ValueError('goodbye must exit 0!') |
| 90 | 81 |
| 91 try: | 82 try: |
| 92 api.step('warning', ['echo', 'warning']) | 83 api.step('warning', ['echo', 'warning']) |
| 93 except api.step.StepFailure as e: | 84 except api.step.StepFailure as e: |
| 94 e.result.presentation.status = api.step.WARNING | 85 e.result.presentation.status = api.step.WARNING |
| 95 raise api.step.StepWarning(e.message) | 86 raise api.step.StepWarning(e.message) |
| 96 | 87 |
| 97 | 88 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 ) | 150 ) |
| 160 | 151 |
| 161 yield ( | 152 yield ( |
| 162 api.test('invalid_access') + | 153 api.test('invalid_access') + |
| 163 api.properties(access_invalid_data=True) + | 154 api.properties(access_invalid_data=True) + |
| 164 api.expect_exception('StepDataAttributeError') | 155 api.expect_exception('StepDataAttributeError') |
| 165 ) | 156 ) |
| 166 | 157 |
| 167 yield ( | 158 yield ( |
| 168 api.test('infra_failure') + | 159 api.test('infra_failure') + |
| 160 api.properties(raise_infra_failure=True) + |
| 169 api.step_data('cleanup', retcode=1) | 161 api.step_data('cleanup', retcode=1) |
| 170 ) | 162 ) |
| 171 | 163 |
| 172 yield ( | 164 yield ( |
| 173 api.test('bad_return') + | 165 api.test('bad_return') + |
| 174 api.properties(bad_return=True) + | 166 api.properties(bad_return=True) + |
| 175 api.expect_exception('TypeError') | 167 api.expect_exception('TypeError') |
| 176 ) | 168 ) |
| 177 | 169 |
| 178 yield ( | 170 yield ( |
| 179 api.test('timeout') + | 171 api.test('timeout') + |
| 180 api.properties(timeout=1) + | 172 api.properties(timeout=1) + |
| 181 api.step_data('timeout', times_out_after=20) | 173 api.step_data('timeout', times_out_after=20) |
| 182 ) | 174 ) |
| 183 | 175 |
| 184 yield ( | 176 yield ( |
| 185 api.test('catch_timeout') + | 177 api.test('catch_timeout') + |
| 186 api.properties(timeout=2) + | 178 api.properties(timeout=2) + |
| 187 api.step_data('caught timeout', times_out_after=20) | 179 api.step_data('caught timeout', times_out_after=20) |
| 188 ) | 180 ) |
| OLD | NEW |