Create a large container to test apps

Create a large container and pre-install all the apps in it

BUG=chromium:1101583
TEST=None

Change-Id: Ife466f5aa6bd2308272efefec3689eced89ce397
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/containers/cros-container-guest-tools/+/2473973
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Judy Wu <jinrongwu@google.com>
Reviewed-by: David Munro <davidmunro@google.com>
Reviewed-by: Fergus Dall <sidereal@google.com>
Reviewed-by: Matthew Warton <mwarton@google.com>
diff --git a/lxd/build_debian_container.sh b/lxd/build_debian_container.sh
index 2e96fbf..c6d1cc2 100755
--- a/lxd/build_debian_container.sh
+++ b/lxd/build_debian_container.sh
@@ -8,17 +8,13 @@
 LXD="/snap/bin/lxd"
 LXC="/snap/bin/lxc"
 
-build_container() {
+build_containers() {
     local arch=$1
     local src_root=$2
     local results_dir=$3
     local apt_dir=$4
-    local test_image=$5
-    local release=$6
-    local job_name=$7
-
-    local setup_script="${src_root}"/lxd/lxd_setup.sh
-    local setup_test_script="${src_root}"/lxd/lxd_test_setup.sh
+    local release=$5
+    local job_name=$6
 
     local base_image="images:debian/${release}/${arch}"
     local tempdir="$(mktemp -d)"
@@ -28,10 +24,34 @@
     unsquashfs -d "${rootfs}" "${tempdir}/image.root"
     chmod 0755 "${rootfs}"
 
+    for image_type in "prod" "test" "app_test"; do
+        build_and_export "${arch}" \
+                         "${src_root}" \
+                         "${rootfs}" \
+                         "${image_type}" \
+                         "${release}" \
+                         "${job_name}" \
+                         "${results_dir}" \
+                         "${apt_dir}"
+    done
+
+    rm -rf "${tempdir}"
+
+}
+
+build_and_export() {
+    local arch=$1
+    local src_root=$2
+    local rootfs=$3
+    local image_type=$4
+    local release=$5
+    local job_name=$6
+    local results_dir=$7
+    local apt_dir=$8
+
     if [ "${arch}" = "arm64" ]; then
         cp /usr/bin/qemu-aarch64-static "${rootfs}/usr/bin/"
     fi
-
     mkdir -p "${rootfs}/opt/google/cros-containers"
     mount --bind /tmp/cros-containers "${rootfs}/opt/google/cros-containers"
     mount --bind /run/resolvconf/resolv.conf "${rootfs}/etc/resolv.conf"
@@ -42,14 +62,28 @@
     mount -t tmpfs tmpfs "${rootfs}/run"
     mount -t tmpfs tmpfs "${rootfs}/tmp"
 
-    cp "${setup_script}" "${setup_test_script}" "${rootfs}/run/"
     mkdir "${rootfs}/run/apt"
     cp -r "${apt_dir}"/* "${rootfs}/run/apt"
 
-    chroot "${rootfs}" /run/"$(basename ${setup_script})" "${release}" "${job_name}"
+    if [ "${image_type}" = "prod" ]; then
+        local setup_script="${src_root}"/lxd/lxd_setup.sh
+        cp "${setup_script}" "${rootfs}/run/"
+        chroot "${rootfs}" /run/"$(basename ${setup_script})" \
+            "${release}" "${job_name}"
+    fi
 
-    if [ "${test_image}" = true ]; then
-        chroot "${rootfs}" /run/"$(basename ${setup_test_script})" "${release}" "${job_name}"
+    if [ "${image_type}" = "test" ]; then
+        local setup_test_script="${src_root}"/lxd/lxd_test_setup.sh
+        cp "${setup_test_script}" "${rootfs}/run/"
+        chroot "${rootfs}" /run/"$(basename ${setup_test_script})" \
+            "${release}" "${job_name}"
+    fi
+
+    if [ "${image_type}" = "app_test" ]; then
+        local setup_test_app_script="${src_root}"/lxd/lxd_test_app_setup.sh
+        cp "${setup_test_app_script}" "${rootfs}/run/"
+        chroot "${rootfs}" /run/"$(basename ${setup_test_app_script})" \
+            "${arch}"
     fi
 
     umount "${rootfs}/tmp"
@@ -71,8 +105,10 @@
     # rootfs.squashfs is raw rootfs squash'd.
     # lxd.tar.xz is metadata.yaml and templates dir.
     local result_dir="${results_dir}/debian/${release}/${arch}"
-    if [ "${test_image}" = true ]; then
+    if [ "${image_type}" = "test" ]; then
         result_dir="${result_dir}/test"
+    elif [ "${image_type}" = "app_test" ]; then
+        result_dir="${result_dir}/app_test"
     else
         result_dir="${result_dir}/default"
     fi
@@ -84,7 +120,7 @@
     tar -Ipixz --xattrs --acls -cpf "${rootfs_tarball}" -C "${rootfs}" .
     mksquashfs "${rootfs}"/* "${result_dir}/rootfs.squashfs"
 
-    if [ "${arch}" = "amd64" ] && [ "${test_image}" != true ]; then
+    if [ "${arch}" = "amd64" ] && [ "${image_type}" == "prod" ]; then
         # Workaround the "Invalid multipart image" flake by generating a
         # single tarball.
         tar xvf "${tempdir}/image" -C "${tempdir}"
@@ -93,8 +129,6 @@
         "${src_root}"/lxd/test.py "${results_dir}" \
                                   "${tempdir}/unified.tar.xz"
     fi
-
-    rm -rf "${tempdir}"
 }
 
 main() {
@@ -138,15 +172,12 @@
     # Build the normal and test images for each arch.
     for arch in amd64 arm64; do
         for release in stretch buster; do
-            for test_image in false true; do
-                build_container "${arch}" \
-                                "${src_root}" \
-                                "${results_dir}" \
-                                "${apt_dir}" \
-                                "${test_image}" \
-                                "${release}" \
-                                "${job_name}"
-            done
+            build_containers "${arch}" \
+                             "${src_root}" \
+                             "${results_dir}" \
+                             "${apt_dir}" \
+                             "${release}" \
+                             "${job_name}"
         done
     done
 }
diff --git a/lxd/lxd_test_app_setup.sh b/lxd/lxd_test_app_setup.sh
new file mode 100755
index 0000000..00134a7
--- /dev/null
+++ b/lxd/lxd_test_app_setup.sh
@@ -0,0 +1,42 @@
+#!/bin/bash
+# Copyright 2020 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.
+
+# Set up a Debian container for test.
+# This is run from inside the container as root.
+
+set -eux
+
+main() {
+    local arch=$1
+
+    # for testing Gedit.
+    apt-get -q -y install gedit
+
+    # for testing Emacs.
+    apt-get -q -y install emacs
+
+    if [ "${arch}" = "amd64" ]; then
+        # for testing Android Studio.
+        wget https://storage.googleapis.com/chromiumos-test-assets-public/crostini_test_files/android-studio-linux.tar.gz
+        tar -zxvf android-studio-linux.tar.gz
+        rm -f android-studio-linux.tar.gz
+
+        # for testing Eclipse.
+        apt-get -q -y install default-jre
+        wget https://storage.googleapis.com/chromiumos-test-assets-public/crostini_test_files/eclipse.tar.gz
+        tar -zxvf eclipse.tar.gz -C /usr/
+        ln -s /usr/eclipse/eclipse /usr/bin/eclipse
+        rm -f eclipse.tar.gz
+
+        # for testing Visual Studio Code.
+        apt-get -q -y install software-properties-common
+        curl -sSL https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
+        add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"
+        apt-get update
+        apt-get -q -y install code
+    fi
+}
+
+main "$@"