build-unit-test-docker: Use proxy and apt mirror

When http_proxy environment is set, use the proxy while building the
docker image.
When UBUNTU_MIRROR is set, use apt mirror to speed up the apt downloads.

Signed-off-by: Lei YU <yulei.sh@bytedance.com>
Change-Id: I286e7e26f3639af93caa95195023b14ad296f83e
diff --git a/build-unit-test-docker.sh b/build-unit-test-docker.sh
index b48aaa3..a918a51 100755
--- a/build-unit-test-docker.sh
+++ b/build-unit-test-docker.sh
@@ -11,12 +11,16 @@
 #                     repositories>
 #                     default is master, which will be used if input branch not
 #                     provided or not found
+#   UBUNTU_MIRROR:    <optional, the URL of a mirror of Ubuntu to override the
+#                     default ones in /etc/apt/sources.list>
+#                     default is empty, and no mirror is used.
 
 set -xeuo pipefail
 
 DOCKER_IMG_NAME=${DOCKER_IMG_NAME:-"openbmc/ubuntu-unit-test"}
 DISTRO=${DISTRO:-"ubuntu:focal"}
 BRANCH=${BRANCH:-"master"}
+UBUNTU_MIRROR=${UBUNTU_MIRROR:-""}
 
 # Determine the architecture
 ARCH=$(uname -m)
@@ -161,9 +165,22 @@
 ################################# docker img # #################################
 # Create docker image that can run package unit tests
 if [[ "${DISTRO}" == "ubuntu"* ]]; then
+
+MIRROR=""
+if [[ -n "${UBUNTU_MIRROR}" ]]; then
+    MIRROR="RUN echo \"deb ${UBUNTU_MIRROR} \$(. /etc/os-release && echo \$VERSION_CODENAME) main restricted universe multiverse\" > /etc/apt/sources.list && \
+        echo \"deb ${UBUNTU_MIRROR} \$(. /etc/os-release && echo \$VERSION_CODENAME)-updates main restricted universe multiverse\" >> /etc/apt/sources.list && \
+        echo \"deb ${UBUNTU_MIRROR} \$(. /etc/os-release && echo \$VERSION_CODENAME)-security main restricted universe multiverse\" >> /etc/apt/sources.list && \
+        echo \"deb ${UBUNTU_MIRROR} \$(. /etc/os-release && echo \$VERSION_CODENAME)-proposed main restricted universe multiverse\" >> /etc/apt/sources.list && \
+        echo \"deb ${UBUNTU_MIRROR} \$(. /etc/os-release && echo \$VERSION_CODENAME)-backports main restricted universe multiverse\" >> /etc/apt/sources.list"
+
+fi
+
 Dockerfile=$(cat << EOF
 FROM ${DOCKER_BASE}${DISTRO} as openbmc-base
 
+${MIRROR}
+
 ENV DEBIAN_FRONTEND noninteractive
 
 ENV PYTHONPATH "/usr/local/lib/python3.8/site-packages/"
@@ -482,5 +499,11 @@
 fi
 ################################# docker img # #################################
 
+http_proxy=${http_proxy:-}
+proxy_args=""
+if [[ -n "${http_proxy}" ]]; then
+  proxy_args="--build-arg http_proxy=${http_proxy} --build-arg https_proxy=${http_proxy}"
+fi
+
 # Build above image
-docker build --network=host -t ${DOCKER_IMG_NAME} - <<< "${Dockerfile}"
+docker build ${proxy_args} --network=host -t ${DOCKER_IMG_NAME} - <<< "${Dockerfile}"