bootstrap: Create script to launch cbuildbot on branch.

This script is invoked with the normal cbuildbot command line options,
but if cbuildbot is passed a branch to use, it will checkout chromite
on the branch before launching cbuildbot, otherwise, it runs cbuildbot
on a clean master branch.

This allows us to move from TOT to any branch invisibly.

One caveat, when building with a pinned manifest, it's still important
to specify the chromite branch on the cbuildbot command line.

BUG=chromium:657224
TEST=bootstrap --buildbot --debug --buildroot <root> master-paladin
     bootstrap --buildbot --debug --buildroot <root> --branch release-R56-9000.B master-paladin
     bootstrap foo; echo $? (to verify non-zero exit code returned)

Change-Id: Idf8c6e4267c3ff9f4db425705a9c98676bf1d758
Reviewed-on: https://chromium-review.googlesource.com/406568
Commit-Ready: Don Garrett <dgarrett@chromium.org>
Tested-by: Don Garrett <dgarrett@chromium.org>
Reviewed-by: Don Garrett <dgarrett@chromium.org>
diff --git a/scripts/bootstrap_unittest.py b/scripts/bootstrap_unittest.py
new file mode 100644
index 0000000..21bc7a9
--- /dev/null
+++ b/scripts/bootstrap_unittest.py
@@ -0,0 +1,56 @@
+# Copyright 2016 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Unit tests for chromite.lib.git and helpers for testing that module."""
+
+from __future__ import print_function
+
+from chromite.scripts import bootstrap
+from chromite.lib import cros_build_lib_unittest
+from chromite.lib import cros_test_lib
+
+
+class BootstrapTest(cros_build_lib_unittest.RunCommandTestCase):
+  """Tests for bootstrap script."""
+
+  def testExtractBranchName(self):
+    """Test that we can correctly extract branch values from cbuildbot args."""
+    cases = (
+        ([], 'master'),
+        (['--arg1', 'value', '-a', '--arg2'], 'master'),
+        (['--branch', 'branch'], 'branch'),
+        (['-b', 'branch'], 'branch'),
+        (['--arg1', 'value', '-a', '--branch', 'branch', '--arg2'], 'branch'),
+        (['--arg1', '-a', '--branch', 'branch', 'config'], 'branch'),
+    )
+
+    for args, expected in cases:
+      result = bootstrap.ExtractBranchName(args)
+      self.assertEqual(result, expected)
+
+  def testRunCbuildbot(self):
+    bootstrap.RunCbuildbot('/mock/chromite', ['foo', 'bar', 'arg'])
+    self.assertCommandContains(
+        ['/mock/chromite/bin/cbuildbot', 'foo', 'bar', 'arg'])
+
+  def testMainMaster(self):
+    bootstrap.main(['foo'])
+
+    # cbuildbot script is in tempdir, so can't be listed here.
+    self.assertCommandContains(['foo'])
+
+  def testMainBranch(self):
+    bootstrap.main(['--branch', 'branch', 'foo'])
+
+    # cbuildbot script is in tempdir, so can't be listed here.
+    self.assertCommandContains(['--branch', 'branch', 'foo'])
+
+
+class LiveWorktreeTest(cros_test_lib.TempDirTestCase):
+  """Integration tests for git worktree usage."""
+
+  @cros_test_lib.NetworkTest()
+  def testCreateChromiteBranch(self):
+    """Test that we can a git worktree, from the current chromite, branched."""
+    bootstrap.CloneChromiteOnBranch('release-R56-9000.B', self.tempdir)