Cleanup containers and images at the end of tests. (#17)

Cleanup containers and images at the end of tests.
diff --git a/rules/docker_config.sh.tpl b/rules/docker_config.sh.tpl
index 4734447..e786bd9 100644
--- a/rules/docker_config.sh.tpl
+++ b/rules/docker_config.sh.tpl
@@ -25,7 +25,7 @@
   # Call the script produced by the docker_build rule to load the image
   %{LOAD_IMAGE_SH}
   # Run the container image to build the config repos
-  docker run -e USER_ID="$(id -u)" -v $(pwd):/bazel-config -i %{IMAGE_NAME}
+  docker run --rm -e USER_ID="$(id -u)" -v $(pwd):/bazel-config -i %{IMAGE_NAME}
   # Create a tar file with all the config repos that were built
   tar -cf outputs.tar %{CONFIG_REPOS}
   # Copy the tar file to its desired output location
diff --git a/test/configs/BUILD b/test/configs/BUILD
index 3a3f129..ddf9471 100644
--- a/test/configs/BUILD
+++ b/test/configs/BUILD
@@ -19,8 +19,9 @@
 load("@bazel_toolchains//rules:docker_config.bzl", "container_install_pkgs", "docker_toolchain_autoconfig")
 load("@bazel_toolchains//rules:environments.bzl", "clang_env", "gcc_env")
 
+# Use "rbe-test-" prefix for easy identification of images to remove after the tests.
 container_install_pkgs(
-    name = "jessie-with-pkgs",
+    name = "rbe-test-jessie-with-pkgs",
     base = "@official_jessie//image",
     packages = [
         "bazel",
@@ -48,7 +49,7 @@
 # maintain these configs anywhere
 docker_toolchain_autoconfig(
     name = "debian-jessie-autoconfig",
-    base = ":jessie-with-pkgs.tar",
+    base = ":rbe-test-jessie-with-pkgs.tar",
     env = gcc_env(),
     tags = ["manual"],
     test = True,
@@ -56,7 +57,7 @@
 
 docker_toolchain_autoconfig(
     name = "debian-jessie-bazel-head-autoconfig",
-    base = ":jessie-with-pkgs.tar",
+    base = ":rbe-test-jessie-with-pkgs.tar",
     env = gcc_env(),
     tags = ["manual"],
     use_bazel_head = True,
@@ -65,7 +66,7 @@
 
 docker_toolchain_autoconfig(
     name = "debian-jessie-custom-bazel-version-autoconfig",
-    base = ":jessie-with-pkgs.tar",
+    base = ":rbe-test-jessie-with-pkgs.tar",
     bazel_version = "0.11.0",
     env = gcc_env(),
     tags = ["manual"],
@@ -75,7 +76,7 @@
 # Note that we only support Bazel rc versions at 0.10.0 or above.
 docker_toolchain_autoconfig(
     name = "debian-jessie-custom-bazel-rc-version-autoconfig",
-    base = ":jessie-with-pkgs.tar",
+    base = ":rbe-test-jessie-with-pkgs.tar",
     bazel_version = "0.11.0",
     bazel_rc_version = "1",
     env = gcc_env(),
@@ -83,8 +84,9 @@
     test = True,
 )
 
+# Use "rbe-test-" prefix for easy identification of images to remove after the tests.
 container_install_pkgs(
-    name = "xenial-with-pkgs",
+    name = "rbe-test-xenial-with-pkgs",
     base = "@official_xenial//image",
     packages = [
         "bazel",
@@ -107,7 +109,7 @@
 
 docker_toolchain_autoconfig(
     name = "ubuntu-xenial-autoconfig",
-    base = ":xenial-with-pkgs.tar",
+    base = ":rbe-test-xenial-with-pkgs.tar",
     env = gcc_env(),
     tags = ["manual"],
     test = True,
@@ -115,7 +117,7 @@
 
 docker_toolchain_autoconfig(
     name = "ubuntu-xenial-bazel-head-autoconfig",
-    base = ":xenial-with-pkgs.tar",
+    base = ":rbe-test-xenial-with-pkgs.tar",
     env = gcc_env(),
     tags = ["manual"],
     use_bazel_head = True,
@@ -124,7 +126,7 @@
 
 docker_toolchain_autoconfig(
     name = "ubuntu-xenial-custom-bazel-version-autoconfig",
-    base = ":xenial-with-pkgs.tar",
+    base = ":rbe-test-xenial-with-pkgs.tar",
     bazel_version = "0.11.0",
     env = gcc_env(),
     tags = ["manual"],
@@ -134,7 +136,7 @@
 # Note that we only support Bazel rc versions at 0.10.0 or above.
 docker_toolchain_autoconfig(
     name = "ubuntu-xenial-custom-bazel-rc-version-autoconfig",
-    base = ":xenial-with-pkgs.tar",
+    base = ":rbe-test-xenial-with-pkgs.tar",
     bazel_version = "0.11.0",
     bazel_rc_version = "1",
     env = gcc_env(),
diff --git a/test/configs/autoconfig_test.sh b/test/configs/autoconfig_test.sh
index c959320..8364898 100755
--- a/test/configs/autoconfig_test.sh
+++ b/test/configs/autoconfig_test.sh
@@ -30,9 +30,25 @@
 TARGET=${TEST_BINARY%_test}
 NAME=${TARGET##*/}
 DIR=${TARGET%${NAME}}
-
 autoconfig_script=${WORKSPACE_ROOT}/${DIR}${NAME}
 
+# Helper function for always delete the containers / temporary files on exit
+function cleanup_on_finish {
+  echo "=== Deleting images  ==="
+  # Images to be removed are expected to have "rbe-test-" as name prefix.
+  images=($(docker images -a | grep "rbe-test-" | awk '{print $3}'))
+  for image in "${images[@]}"
+  do
+    # Only delete the image if it is not used by any running container.
+    if [[ -z $(docker ps -q -f ancestor=${image}) ]]; then
+       docker rmi -f ${image}
+    fi
+  done
+  docker images -f "dangling=true" -q | xargs -r docker rmi -f
+}
+
+trap cleanup_on_finish EXIT # always delete the containers
+
 # Change the output location to a tmp location inside the current Bazel workspace.
 sed -i "s|/tmp|${TEST_TMPDIR}|g" ${autoconfig_script}
 
diff --git a/test/configs/debian8_clang_autoconfig_test.sh b/test/configs/debian8_clang_autoconfig_test.sh
index a2188d3..df2be79 100755
--- a/test/configs/debian8_clang_autoconfig_test.sh
+++ b/test/configs/debian8_clang_autoconfig_test.sh
@@ -30,6 +30,22 @@
 TEST_CONFIGS_DIR=${TEST_TMPDIR}/bazel-toolchains-${COMMIT}/configs/debian8_clang/${CONFIG_VERSION}/bazel_${BAZEL_VERSION}/
 AUTOCONFIG_SCRIPT=${WORKSPACE_ROOT}/rules/debian8-clang-${CONFIG_VERSION}-bazel_${BAZEL_VERSION}-autoconfig
 
+# Helper function for always delete the containers / temporary files on exit
+function cleanup_on_finish {
+  echo "=== Deleting images  ==="
+  images=($(docker images -a | grep "debian8-clang-${CONFIG_VERSION}-bazel_${BAZEL_VERSION}-autoconfig" | awk '{print $3}'))
+  for image in "${images[@]}"
+  do
+    # Only delete the image if it is not used by any running container.
+    if [[ -z $(docker ps -q -f ancestor=${image}) ]]; then
+       docker rmi -f ${image}
+    fi
+  done
+  docker images -f "dangling=true" -q | xargs -r docker rmi -f
+}
+
+trap cleanup_on_finish EXIT # always delete the containers
+
 # Change the output location to a tmp location inside the current Bazel workspace.
 sed -i "s|/tmp|${TEST_TMPDIR}|g" ${AUTOCONFIG_SCRIPT}