build-unit-test-docker: create hashed tags for final image

Create a tag for the final image in the same format as the stage
images.  Display this tag to stdout at the end of the docker build
so that other scripts can consume it via calls like:
    $(scripts/build-unit-test-docker)

Modify `run-unit-test-docker.sh` to get the docker image from
`build-unit-test-docker` rather than a static image name.  This
ensures that the docker image used in unit testing is the exact
image requested to be built.  (Soon this will allow us to incorporate
Gerrit changes into the docker image itself.)

Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Idd7a9a7b0157f1c520b33b3ec7ac0d5b52018d9e
diff --git a/run-unit-test-docker.sh b/run-unit-test-docker.sh
index 696b6b8..ef4d897 100755
--- a/run-unit-test-docker.sh
+++ b/run-unit-test-docker.sh
@@ -20,8 +20,6 @@
 #   BRANCH:          Optional, branch to build from each of the
 #                    openbmc repositories. default is master, which will be
 #                    used if input branch not provided or not found
-#   DOCKER_IMG_NAME: Optional, default is openbmc/ubuntu-unit-test-master with a
-#                    -$BRANCH replacing -master if $BRANCH provided
 #   dbus_sys_config_file: Optional, with the default being
 #                         `/usr/share/dbus-1/system.conf`
 #   NO_FORMAT_CODE:  Optional, do not run format-code.sh
@@ -32,7 +30,6 @@
 
 # Default variables
 BRANCH=${BRANCH:-"master"}
-DOCKER_IMG_NAME=${DOCKER_IMG_NAME:-"openbmc/ubuntu-unit-test-${BRANCH}"}
 DISTRO=${DISTRO:-ubuntu:focal}
 OBMC_BUILD_SCRIPTS="openbmc-build-scripts"
 UNIT_TEST_PY_DIR="scripts"
@@ -87,10 +84,9 @@
 cd "${WORKSPACE}"/${OBMC_BUILD_SCRIPTS}
 echo "Building docker image with build-unit-test-docker"
 # Export input env variables
-export DOCKER_IMG_NAME
 export DISTRO
 export BRANCH
-./scripts/build-unit-test-docker
+export DOCKER_IMG_NAME=$(./scripts/build-unit-test-docker)
 
 # Allow the user to pass options through to unit-test.py:
 #   EXTRA_UNIT_TEST_ARGS="-r 100" ...
diff --git a/scripts/build-unit-test-docker b/scripts/build-unit-test-docker
index f6ee0ef..4dd4c44 100755
--- a/scripts/build-unit-test-docker
+++ b/scripts/build-unit-test-docker
@@ -448,10 +448,7 @@
 
 
 def docker_img_build(pkg, tag, dockerfile):
-    if not force_build and pkg != "final":
-        # TODO: the 'final' is here because we do not tag the final image yet
-        # so we always need to rebuild it.  This will be changed in a future
-        # commit so that we tag even the final image.
+    if not force_build:
         if docker.image.ls(tag, "--format", '"{{.Repository}}:{{.Tag}}"'):
             print(f"Image {tag} already exists.  Skipping.", file=sys.stderr)
             return
@@ -692,4 +689,7 @@
 """
 
 # Do the final docker build
-docker_img_build("final", docker_image_name, dockerfile)
+docker_final_img_name = docker_img_tagname(docker_image_name, dockerfile)
+docker_img_build("final", docker_final_img_name, dockerfile)
+# Print the tag of the final image.
+print(docker_final_img_name)