Support branch option in unit-test script

Building on the support of the branch option within the
docker container, add it to unit-test for any dependent
repositories that do not get built into the docker
container

Testing:
- Verified with no branch input, worked as expected
- Verified with invalid branch, master was still used

Change-Id: I4f57d6165287ffe3b4e2f0377292128474199ca3
Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
diff --git a/run-unit-test-docker.sh b/run-unit-test-docker.sh
index d2a86f1..2ebc224 100755
--- a/run-unit-test-docker.sh
+++ b/run-unit-test-docker.sh
@@ -29,7 +29,8 @@
 set -uo pipefail
 
 # Default variables
-DOCKER_IMG_NAME=${DOCKER_IMG_NAME:-"openbmc/ubuntu-unit-test-${BRANCH:-master}"}
+BRANCH=${BRANCH:-"master"}
+DOCKER_IMG_NAME=${DOCKER_IMG_NAME:-"openbmc/ubuntu-unit-test-${BRANCH}"}
 DISTRO=${DISTRO:-ubuntu:bionic}
 OBMC_BUILD_SCRIPTS="openbmc-build-scripts"
 UNIT_TEST_PY_DIR="scripts"
@@ -87,7 +88,8 @@
 ./build-unit-test-docker.sh
 
 # Unit test and parameters
-UNIT_TEST="${DOCKER_WORKDIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},-p,${UNIT_TEST_PKG},-v"
+UNIT_TEST="${DOCKER_WORKDIR}/${UNIT_TEST_PY},-w,${DOCKER_WORKDIR},\
+-p,${UNIT_TEST_PKG},-b,$BRANCH,-v"
 
 # Run the docker unit test container with the unit test execution script
 echo "Executing docker image"
diff --git a/scripts/unit-test.py b/scripts/unit-test.py
index 587488d..1a330ca 100755
--- a/scripts/unit-test.py
+++ b/scripts/unit-test.py
@@ -229,21 +229,30 @@
     check_call(cmd)
 
 
-def clone_pkg(pkg):
+def clone_pkg(pkg, branch):
     """
     Clone the given openbmc package's git repository from gerrit into
     the WORKSPACE location
 
     Parameter descriptions:
     pkg                 Name of the package to clone
+    branch              Branch to clone from pkg
     """
     pkg_dir = os.path.join(WORKSPACE, pkg)
     if os.path.exists(os.path.join(pkg_dir, '.git')):
         return pkg_dir
     pkg_repo = urljoin('https://gerrit.openbmc-project.xyz/openbmc/', pkg)
     os.mkdir(pkg_dir)
-    printline(pkg_dir, "> git clone", pkg_repo, "./")
-    return Repo.clone_from(pkg_repo, pkg_dir).working_dir
+    printline(pkg_dir, "> git clone", pkg_repo, branch, "./")
+    try:
+        # first try the branch
+        repo_inst = Repo.clone_from(pkg_repo, pkg_dir,
+                branch=branch).working_dir
+    except:
+        printline("Input branch not found, default to master")
+        repo_inst = Repo.clone_from(pkg_repo, pkg_dir,
+                branch="master").working_dir
+    return repo_inst
 
 
 def get_autoconf_deps(pkgdir):
@@ -413,7 +422,7 @@
         check_call_cmd(pkgdir, *make_parallel)
         check_call_cmd(pkgdir, 'sudo', '-n', '--', *(make_parallel + [ 'install' ]))
 
-def build_dep_tree(pkg, pkgdir, dep_added, head, dep_tree=None):
+def build_dep_tree(pkg, pkgdir, dep_added, head, branch, dep_tree=None):
     """
     For each package(pkg), starting with the package to be unit tested,
     parse its 'configure.ac' file from within the package's directory(pkgdir)
@@ -425,6 +434,7 @@
     pkgdir              Directory where package source is located
     dep_added           Current dict of dependencies and added status
     head                Head node of the dependency tree
+    branch              Branch to clone from pkg
     dep_tree            Current dependency tree node
     """
     if not dep_tree:
@@ -446,7 +456,7 @@
             # Dependency package not added
             new_child = dep_tree.AddChild(dep)
             dep_added[dep] = False
-            dep_pkgdir = clone_pkg(dep)
+            dep_pkgdir = clone_pkg(dep,branch)
             # Determine this dependency package's
             # dependencies and add them before
             # returning to add this package
@@ -454,6 +464,7 @@
                                        dep_pkgdir,
                                        dep_added,
                                        head,
+                                       branch,
                                        new_child)
         else:
             # Dependency package known and added
@@ -626,9 +637,13 @@
                         help="Print additional package status messages")
     parser.add_argument("-r", "--repeat", help="Repeat tests N times",
                         type=int, default=1)
+    parser.add_argument("-b", "--branch", dest="BRANCH", required=False,
+                        help="Branch to target for dependent repositories",
+                        default="master")
     args = parser.parse_args(sys.argv[1:])
     WORKSPACE = args.WORKSPACE
     UNIT_TEST_PKG = args.PACKAGE
+    BRANCH = args.BRANCH
     if args.verbose:
         def printline(*line):
             for arg in line:
@@ -654,7 +669,8 @@
         build_dep_tree(UNIT_TEST_PKG,
                        os.path.join(WORKSPACE, UNIT_TEST_PKG),
                        dep_added,
-                       dep_tree)
+                       dep_tree,
+                       BRANCH)
 
         # Reorder Dependency Tree
         for pkg_name, regex_str in DEPENDENCIES_REGEX.iteritems():