commit | 9afbc829f3d6f884c879d7dbfa3ab7c4088c9a65 | [log] [tgz] |
---|---|---|
author | Petr Hosek <phosek@google.com> | Thu May 18 16:18:54 2017 -0700 |
committer | Petr Hosek <phosek@google.com> | Thu May 18 16:32:01 2017 -0700 |
tree | 49c17cf02cca55662e42f479a76ca8ff11c6259b | |
parent | 0bfd645e8bb5ad4dbe6950221ddbaad0ede7cbf1 [diff] |
Recipe for testing recipes We must go deeper. Change-Id: I57acfe1d178c236b49bf7487b8105846a7980bd5
This repository contains recipes for Fuchsia.
A recipe is a Python script that runs a series of commands, using the recipe engine framework from the LUCI project. We use recipes to automatically check out, build, and test Fuchsia in continuous integration jobs. The commands the recipes use are very similar to the ones you would use as a developer to check out, build, and test Fuchsia in your local environment.
Recipes are parameterized using properties. The values for these properties can be set in the Buildbucket configuration. In the recipe code itself, they are specified in a global dictionary named PROPERTIES
and passed as arguments to a function named RunSteps
. The recipes engine automatically looks for these two objects at the top level of the Python file containing the recipe.
When writing a recipe, you can make your properties whatever you want, but if you plan to run your recipe on the Gerrit commit queue, there will be some standard ones starting with patch_
, which give information about the commit being tested, and which you can see in the existing recipe code.
When a recipe executes, it interacts with the underlying machine by running steps.
A step is basically just a command, represented as a Python list of the arguments. You give the step a name, specify the arguments, and the recipe engine will run it in a subprocess, capture its output, and mark the job as as failed if the command fails.
Here's an example:
api.step('list temporary files', ['ls', '/tmp'])
This will execute the command ls /tmp
on the machine where the recipe is running, and it will cause a failure if, for example, there is no /tmp
directory. When the recipe gets run on Swarming (which is the scheduling system we use to run Fuchsia continuous integration jobs) this step will show up with the label “list temporary files” in a list of all the steps that ran.
Code is reused across recipes in the form of modules, which live either in the recipe_modules
directory of this repo, or in the same directory of the recipe engine repo. The recipe engine's modules provide general functionality, and we have some modules specific to Fuchsia in this repo, such as wrappers for QEMU and Jiri.
The recipe engine looks for a list named DEPS
at the top level of the Python file containing the recipe, where you can specify the modules you want to use. Each item in DEPS
is a string in the form “repo_name/module_name”, where the repo name is “recipe_engine” to get the dependency from the recipe engine repo, or “infra” to get it from this repo.
The reason it's important to only interact with the underlying machine via steps is for testing. The recipes framework provides a way to fake the results of the steps when testing the recipe, instead of actually running the commands. It produces an “expected” JSON file, which shows exactly what commands would have run, along with context such as working directory and environment variables.
You write tests using the GenTests
function. Inside GenTests
, you can use the yield
statement to declare individual test cases. GenTests
takes an API object, which has functions on it allowing you to specify the properties to pass to the recipe, as well as fake results for individual steps.
Here's an example test case from the fuchsia.py
recipe:
yield api.test('failed_tests') + api.properties( manifest='fuchsia', remote='https://fuchsia.googlesource.com/manifest', target='x86-64', tests='tests.json', ) + api.step_data('run tests', retcode=1)
In this example:
api.test
simply gives the test case a name, which will be used to name the generated JSON “expected” file.api.properties
specifies the properties that will be passed to RunSteps
.api.step_data
takes the name of one of the steps in the recipe, in this case “run tests”, and specifies how it should behave. This is where you can make the fake commands produce your choice of fake output. Or, as in this example, you can specify a return code, in order to cover error-handling code branches in the recipe.To run the unit tests and generate the “expected” data, run the following command from the root of this repo:
python recipes.py test train --filter [recipe_name]
The name of the recipe is simply the name of the recipe's Python file minus the .py
extension. So, for example, the recipe in recipes/fuchsia.py
is called “fuchsia”.
After you run the test train
command, the JSON files with expectations will be either generated or updated. Look at diff in Git, and make sure you didn't make any breaking changes.
To just run the tests without updating the expectation files:
python recipes.py test run --filter [recipe_name]
To debug a single test, you can do this, which limits the test run to a single test and runs it in pdb:
python recipes.py test run --filter [recipe_name].[test_name]
Unit tests should be the first thing you try to verify that your code runs. But when writing a new recipe or making major changes, you‘ll also want to make sure the recipe works when you actually run it. There’s a similar command for that:
python recipes.py run --properties-file test.json [recipe_name]
For this command to work, you need to create a temporary file called test.json
specifying what properties you want to run the recipe with. Here's an example of what that file might look like, for the fuchsia.py
recipe:
{ "remote": "https://fuchsia.googlesource.com/manifest", "manifest": "fuchsia", "build_type": "release", "target": "x86-64", "use_goma": false, "tests": "apps/test_runner/config/fuchsia.json" }
Setting use_goma
to false is currently necessary for local testing of the Fuchsia recipes, due to the fact that they try to use a service account with Goma.
Depending on what you're trying to automate, you may not need to write your own recipe. We already have several that you can either use as-is with the appropriate properties, or that may need to be slightly altered to fit a new use case.
This recipe is used for the primary Fuchsia builders, which build and test the entire Fuchsia project, but it can also be adapted for more specialized usage.
The Fuchsia recipe does the following things:
manifest
property.scripts/build-magenta.sh
.packages/gn/gen.py
with modules specified in the modules
property.tests
property is specified, runs the specified tests with Test Runner on QEMU, using the Magenta image and user.bootfs
generated by the previous steps.This is a very flexible recipe, and it should be able to handle use cases such as running a Ninja build with any combination of Fuchsia modules, running a subset of the Fuchsia primary tests on QEMU, or running tests on QEMU that aren't stable or fast enough for the primary builders.
This is similar to the Fuchsia recipe, but it only builds and tests Magenta. It does the following:
runtests
and then shuts down.This recipe uses Go's build tools to build and test Jiri.
This recipe uses Cobalt's cobaltb.py
build tool to build and test Cobalt.
This recipe builds and tests UI modules, by running make presubmit-cq
. Tests are run directly on the host machine using Flutter, without a Fuchsia system or QEMU.