From 76ae374413c67eb67ff7590745eaf957a0446ed3 Mon Sep 17 00:00:00 2001
From: Ian Wienand <iwienand@redhat.com>
Date: Thu, 23 Mar 2017 11:48:06 +1100
Subject: [PATCH] functests: skip qcow2 generically but add specific test

We somewhat discussed skipping qcow2 generation previously in
I9372e195913798a851c96e62eee89029e067baa1.  As recent issues with PPC
testing have shown, we are not actually testing the "vm" element and
hence the bootloader path in the functional tests.

I don't think we need to test this on every element; it overlaps
somewhat with the testing done by the nodepool jobs which build full
images and boot them.  I also didn't want to introduce a separate run
for this.  Thus it seems valuable to at least have one element
enhanced to do this installation and conversion in our default tests
for basic sanity.

This disables qcow generation by default, as per the other change, but
allows an element to drop a file that will override the output
formats.  The Xenial element is modified to produce a qcow2 using
this, and also introduces a dependency on the "vm" element so it tries
to install the bootloader.

We now exit if the .qcow2 fails to build as well.

Change-Id: I1a6acefe52f8c696c39b2d592fdc7ae32a87e6fe
---
 .../xenial-build-succeeds/README.rst          |  3 ++
 .../xenial-build-succeeds/element-deps        |  1 +
 .../xenial-build-succeeds/test-output-formats |  1 +
 tests/run_functests.sh                        | 38 ++++++++++++++-----
 4 files changed, 34 insertions(+), 9 deletions(-)
 create mode 100644 diskimage_builder/elements/ubuntu-minimal/test-elements/xenial-build-succeeds/element-deps
 create mode 100644 diskimage_builder/elements/ubuntu-minimal/test-elements/xenial-build-succeeds/test-output-formats

diff --git a/diskimage_builder/elements/ubuntu-minimal/test-elements/xenial-build-succeeds/README.rst b/diskimage_builder/elements/ubuntu-minimal/test-elements/xenial-build-succeeds/README.rst
index b7190827e..624e34e46 100644
--- a/diskimage_builder/elements/ubuntu-minimal/test-elements/xenial-build-succeeds/README.rst
+++ b/diskimage_builder/elements/ubuntu-minimal/test-elements/xenial-build-succeeds/README.rst
@@ -1 +1,4 @@
 Verify we can build a ubuntu-minimal image.
+
+Note this test includes the vm element to test the bootloader install,
+and specifies to output a .qcow2
diff --git a/diskimage_builder/elements/ubuntu-minimal/test-elements/xenial-build-succeeds/element-deps b/diskimage_builder/elements/ubuntu-minimal/test-elements/xenial-build-succeeds/element-deps
new file mode 100644
index 000000000..d40003916
--- /dev/null
+++ b/diskimage_builder/elements/ubuntu-minimal/test-elements/xenial-build-succeeds/element-deps
@@ -0,0 +1 @@
+vm
\ No newline at end of file
diff --git a/diskimage_builder/elements/ubuntu-minimal/test-elements/xenial-build-succeeds/test-output-formats b/diskimage_builder/elements/ubuntu-minimal/test-elements/xenial-build-succeeds/test-output-formats
new file mode 100644
index 000000000..c780936b8
--- /dev/null
+++ b/diskimage_builder/elements/ubuntu-minimal/test-elements/xenial-build-succeeds/test-output-formats
@@ -0,0 +1 @@
+tar,qcow2
\ No newline at end of file
diff --git a/tests/run_functests.sh b/tests/run_functests.sh
index 66e83a1d6..a93a406f8 100755
--- a/tests/run_functests.sh
+++ b/tests/run_functests.sh
@@ -35,6 +35,10 @@ DEFAULT_SKIP_TESTS=(
     centos/build-succeeds
 )
 
+# The default output formats (specified to disk-image-create's "-t"
+# command.  Elements can override with a test-output-formats file
+DEFAULT_OUTPUT_FORMATS="tar"
+
 function log_with_prefix {
     local pr=$1
     local log
@@ -82,13 +86,15 @@ function wait_minus_n {
     fi
 }
 
-# run_disk_element_test <test_element> <element>
-#  Run a disk-image-build .tar build of ELEMENT including any elements
-#  specified by TEST_ELEMENT
+# run_disk_element_test <test_element> <element> <use_tmp> <output_formats>
+#  Run a disk-image-build build of ELEMENT including any elements
+#  specified by TEST_ELEMENT.  Pass OUTPUT_FORMAT to "-t"
 function run_disk_element_test() {
     local test_element=$1
     local element=$2
     local dont_use_tmp=$3
+    local output_format="$4"
+
     local use_tmp_flag=""
     local dest_dir=$(mktemp -d)
 
@@ -102,12 +108,17 @@ function run_disk_element_test() {
         break_cmd="cp -v \$TMP_MOUNT_PATH/tmp/dib-test-should-fail ${dest_dir} || true" \
         DIB_SHOW_IMAGE_USAGE=1 \
         ELEMENTS_PATH=$DIB_ELEMENTS/$element/test-elements \
-        $DIB_CMD -x -t tar,qcow2 ${use_tmp_flag} -o $dest_dir/image -n $element $test_element 2>&1 \
+        $DIB_CMD -x -t ${output_format} \
+                       ${use_tmp_flag} \
+                       -o $dest_dir/image -n $element $test_element 2>&1 \
            | log_with_prefix "${element}/${test_element}"; then
 
-        if ! [ -f "$dest_dir/image.qcow2" ]; then
-            echo "Error: qcow2 build failed for element: $element, test-element: $test_element."
-            echo "No image $dest_dir/image.qcow2 found!"
+        if [[ "qcow2" =~ "$output_format" ]]; then
+            if ! [ -f "$dest_dir/image.qcow2" ]; then
+                echo "Error: qcow2 build failed for element: $element, test-element: $test_element."
+                echo "No image $dest_dir/image.qcow2 found!"
+                exit 1
+            fi
         fi
 
         # check inside the tar for sentinel files
@@ -305,16 +316,25 @@ for test in "${TESTS_TO_RUN[@]}"; do
     element=${test%/*}
     test_element=${test#*/}
 
+    element_dir=$DIB_ELEMENTS/${element}/test-elements/${test_element}/
+
     # tests default to disk-based, but "element-type" can optionally
     # override that
     element_type=disk
-    element_type_override=$DIB_ELEMENTS/${element}/test-elements/${test_element}/element-type
+    element_type_override=${element_dir}/element-type
     if [ -f ${element_type_override} ]; then
         element_type=$(cat ${element_type_override})
     fi
 
+    # override the output format if specified
+    element_output=${DEFAULT_OUTPUT_FORMATS}
+    element_output_override=${element_dir}/test-output-formats
+    if [ -f $element_output_override ]; then
+        element_output=$(cat ${element_output_override})
+    fi
+
     echo "Running $test ($element_type)"
-    run_${element_type}_element_test $test_element $element ${DONT_USE_TMP} &
+    run_${element_type}_element_test $test_element $element ${DONT_USE_TMP} "${element_output}" &
 done
 
 # Wait for the rest of the jobs