Recipe for testing recipes

We must go deeper.

Change-Id: I57acfe1d178c236b49bf7487b8105846a7980bd5
diff --git a/recipes/recipes.expected/cq_try.json b/recipes/recipes.expected/cq_try.json
new file mode 100644
index 0000000..47f2b4e
--- /dev/null
+++ b/recipes/recipes.expected/cq_try.json
@@ -0,0 +1,130 @@
+[
+  {
+    "cmd": [],
+    "name": "ensure_jiri"
+  },
+  {
+    "cmd": [
+      "cipd",
+      "ensure",
+      "--root",
+      "[START_DIR]/cipd/jiri",
+      "--list",
+      "fuchsia/tools/jiri/linux-amd64 latest",
+      "--json-output",
+      "/path/to/tmp/json"
+    ],
+    "infra_step": true,
+    "name": "ensure_jiri.ensure_installed",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@",
+      "@@@STEP_LOG_LINE@json.output@{@@@",
+      "@@@STEP_LOG_LINE@json.output@  \"result\": [@@@",
+      "@@@STEP_LOG_LINE@json.output@    {@@@",
+      "@@@STEP_LOG_LINE@json.output@      \"instance_id\": \"resolved-instance_id-of-latest----------\", @@@",
+      "@@@STEP_LOG_LINE@json.output@      \"package\": \"fuchsia/tools/jiri/linux-amd64\"@@@",
+      "@@@STEP_LOG_LINE@json.output@    }@@@",
+      "@@@STEP_LOG_LINE@json.output@  ]@@@",
+      "@@@STEP_LOG_LINE@json.output@}@@@",
+      "@@@STEP_LOG_END@json.output@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "[START_DIR]/cipd/jiri/jiri",
+      "init",
+      "-cache",
+      "[CACHE]/git",
+      "-shared"
+    ],
+    "infra_step": true,
+    "name": "jiri init"
+  },
+  {
+    "cmd": [
+      "[START_DIR]/cipd/jiri/jiri",
+      "import",
+      "-overwrite=true",
+      "infra",
+      "https://fuchsia.googlesource.com/manifest"
+    ],
+    "infra_step": true,
+    "name": "jiri import"
+  },
+  {
+    "cmd": [
+      "[START_DIR]/cipd/jiri/jiri",
+      "project",
+      "-clean-all"
+    ],
+    "infra_step": true,
+    "name": "jiri project clean"
+  },
+  {
+    "cmd": [
+      "[START_DIR]/cipd/jiri/jiri",
+      "update",
+      "-autoupdate=false",
+      "-gc=true"
+    ],
+    "infra_step": true,
+    "name": "jiri update"
+  },
+  {
+    "cmd": [
+      "[START_DIR]/cipd/jiri/jiri",
+      "project",
+      "-json-output",
+      "/path/to/tmp/json",
+      "infra/recipes"
+    ],
+    "infra_step": true,
+    "name": "jiri project",
+    "~followup_annotations": [
+      "@@@STEP_LOG_LINE@json.output@[@@@",
+      "@@@STEP_LOG_LINE@json.output@  {@@@",
+      "@@@STEP_LOG_LINE@json.output@    \"branches\": [@@@",
+      "@@@STEP_LOG_LINE@json.output@      \"(HEAD detached at c22471f)\", @@@",
+      "@@@STEP_LOG_LINE@json.output@      \"master\"@@@",
+      "@@@STEP_LOG_LINE@json.output@    ], @@@",
+      "@@@STEP_LOG_LINE@json.output@    \"current_branch\": \"\", @@@",
+      "@@@STEP_LOG_LINE@json.output@    \"name\": \"infra/recipes\", @@@",
+      "@@@STEP_LOG_LINE@json.output@    \"path\": \"/path/to/repo\", @@@",
+      "@@@STEP_LOG_LINE@json.output@    \"remote\": \"https://fuchsia.googlesource.com/repo\", @@@",
+      "@@@STEP_LOG_LINE@json.output@    \"revision\": \"c22471f4e3f842ae18dd9adec82ed9eb78ed1127\"@@@",
+      "@@@STEP_LOG_LINE@json.output@  }@@@",
+      "@@@STEP_LOG_LINE@json.output@]@@@",
+      "@@@STEP_LOG_END@json.output@@@",
+      "@@@SET_BUILD_PROPERTY@got_revision@\"c22471f4e3f842ae18dd9adec82ed9eb78ed1127\"@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "[START_DIR]/cipd/jiri/jiri",
+      "patch",
+      "-host",
+      "fuchsia-review.googlesource.com",
+      "-rebase=true",
+      "refs/changes/89/456789/12"
+    ],
+    "name": "jiri patch"
+  },
+  {
+    "cmd": [
+      "python",
+      "-u",
+      "[START_DIR]/infra/recipes/recipes.py",
+      "test",
+      "run"
+    ],
+    "cwd": "[START_DIR]/infra/recipes",
+    "name": "test"
+  },
+  {
+    "name": "$result",
+    "recipe_result": {
+      "got_revision": "c22471f4e3f842ae18dd9adec82ed9eb78ed1127"
+    },
+    "status_code": 0
+  }
+]
\ No newline at end of file
diff --git a/recipes/recipes.py b/recipes/recipes.py
new file mode 100644
index 0000000..feeef45
--- /dev/null
+++ b/recipes/recipes.py
@@ -0,0 +1,59 @@
+# Copyright 2017 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Recipe for testing Recipes."""
+
+from recipe_engine.config import ReturnSchema, Single
+from recipe_engine.recipe_api import Property
+
+
+DEPS = [
+  'infra/jiri',
+  'recipe_engine/context',
+  'recipe_engine/path',
+  'recipe_engine/properties',
+  'recipe_engine/python',
+  'recipe_engine/step',
+]
+
+PROPERTIES = {
+  'patch_gerrit_url': Property(kind=str, help='Gerrit host', default=None),
+  'patch_ref': Property(kind=str, help='Gerrit patch ref', default=None),
+  'manifest': Property(kind=str, help='Jiri manifest to use'),
+  'remote': Property(kind=str, help='Remote manifest repository'),
+}
+
+RETURN_SCHEMA = ReturnSchema(
+  got_revision=Single(str)
+)
+
+
+def RunSteps(api, patch_gerrit_url, patch_ref, manifest, remote):
+  api.jiri.ensure_jiri()
+
+  with api.context(infra_steps=True):
+    api.jiri.init()
+    api.jiri.import_manifest(manifest, remote, overwrite=True)
+    api.jiri.clean(all=True)
+    api.jiri.update(gc=True)
+    revision = api.jiri.project('infra/recipes').json.output[0]['revision']
+    api.step.active_result.presentation.properties['got_revision'] = revision
+
+  if patch_ref is not None:
+    api.jiri.patch(patch_ref, host=patch_gerrit_url, rebase=True)
+
+  with api.context(cwd=api.path['start_dir'].join('infra', 'recipes')):
+    api.python('test', api.context.cwd.join('recipes.py'),
+               args=['test', 'run'])
+
+  return RETURN_SCHEMA.new(got_revision=revision)
+
+
+def GenTests(api):
+  yield api.test('cq_try') + api.properties.tryserver(
+      gerrit_project='infra/recipes',
+      patch_gerrit_url='fuchsia-review.googlesource.com',
+      manifest='infra',
+      remote='https://fuchsia.googlesource.com/manifest',
+  )