From 4ad11e371028abc30749f956c74a23099abe2c7e Mon Sep 17 00:00:00 2001
From: Eduardo Olivares <eolivare@redhat.com>
Date: Mon, 23 Jan 2023 17:38:03 +0100
Subject: [PATCH] Download ubuntu images prior to pytest execution

Tobiko downloads images from their URL when their test need to upload
them to glance
In some cases this is problematic because some of those images are big
and there may be timeouts affecting the tests

With this patch, tobiko downloads the ubuntu-minimal image before the
tobiko tests are executed (during the execution of the ansible/infrared
tobiko roles), which should avoid timeouts during the test execution

The intention is to extend this patch to other images such as CentOS,
RHEL and Fedora

NOTE: this patch does not modify the tests, it just adds the role so
that the Ubuntu images are downloaded - a later patch will modify the
tests so that they stop downloading the images and find them in the
configured path instead

Change-Id: Ifff8214b7d4a3c2a78d97b171714090fd64d60dd
---
 .../roles/tobiko-ir-run/tasks/main.yaml           |  3 +++
 playbooks/tripleo/pre.yaml                        |  1 +
 roles/tobiko-common/defaults/main.yaml            |  7 +++++++
 roles/tobiko-configure/tasks/main.yaml            |  5 +++++
 roles/tobiko-download-images/meta/main.yaml       |  4 ++++
 roles/tobiko-download-images/tasks/main.yaml      | 15 +++++++++++++++
 6 files changed, 35 insertions(+)
 create mode 100644 roles/tobiko-download-images/meta/main.yaml
 create mode 100644 roles/tobiko-download-images/tasks/main.yaml

diff --git a/infrared_plugin/roles/tobiko-ir-run/tasks/main.yaml b/infrared_plugin/roles/tobiko-ir-run/tasks/main.yaml
index 8607d6908..9fd36b38b 100644
--- a/infrared_plugin/roles/tobiko-ir-run/tasks/main.yaml
+++ b/infrared_plugin/roles/tobiko-ir-run/tasks/main.yaml
@@ -66,6 +66,9 @@
     - name: apply pre-requisites before tests run
       include_role: name=tobiko-ir-before-run
 
+    - name: "download images"
+      include_role: name=tobiko-download-images
+
     - name: "initialize test execution"
       include_role: name=tobiko-configure
 
diff --git a/playbooks/tripleo/pre.yaml b/playbooks/tripleo/pre.yaml
index bc39a9ffb..2b9db5c0c 100644
--- a/playbooks/tripleo/pre.yaml
+++ b/playbooks/tripleo/pre.yaml
@@ -7,6 +7,7 @@
 - hosts: primary
   roles:
     - tobiko-zuul
+    - tobiko-download-images
     - tobiko-configure
     - ci-common-vars
     - run-test
diff --git a/roles/tobiko-common/defaults/main.yaml b/roles/tobiko-common/defaults/main.yaml
index 8f33223c4..d52961dcb 100644
--- a/roles/tobiko-common/defaults/main.yaml
+++ b/roles/tobiko-common/defaults/main.yaml
@@ -53,3 +53,10 @@ test_log_file: '{{ test_report_dir | realpath }}/tobiko.log'
 
 # Local where test cases results are being collected to
 test_collect_dir: '{{ test_src_dir | realpath }}/{{ test_report_name }}'
+
+# --- download-images options -------------------------------------------------
+download_images_dir: "{{ ansible_user_dir }}/.downloaded-images"
+download_images:
+  ubuntu-minimal:
+    type: ubuntu
+    url: "https://cloud-images.ubuntu.com/minimal/releases/jammy/release/ubuntu-22.04-minimal-cloudimg-amd64.img"
diff --git a/roles/tobiko-configure/tasks/main.yaml b/roles/tobiko-configure/tasks/main.yaml
index e38b3902d..5d19a3d3e 100644
--- a/roles/tobiko-configure/tasks/main.yaml
+++ b/roles/tobiko-configure/tasks/main.yaml
@@ -21,6 +21,11 @@
           value: "{{ value }}"
         {% endfor %}
         {% endfor %}
+        {% for file_name, dict_value in download_images.items() %}
+        - section: "{{ dict_value.type }}"
+          option: image_file
+          value: "{{ download_images_dir }}/{{ file_name }}"
+        {% endfor %}
   vars:
     sections: "{{ test_default_conf | combine(test_conf, recursive=True) }}"
 
diff --git a/roles/tobiko-download-images/meta/main.yaml b/roles/tobiko-download-images/meta/main.yaml
new file mode 100644
index 000000000..0d4361583
--- /dev/null
+++ b/roles/tobiko-download-images/meta/main.yaml
@@ -0,0 +1,4 @@
+---
+
+dependencies:
+  - role: tobiko-common
diff --git a/roles/tobiko-download-images/tasks/main.yaml b/roles/tobiko-download-images/tasks/main.yaml
new file mode 100644
index 000000000..a6a8ea624
--- /dev/null
+++ b/roles/tobiko-download-images/tasks/main.yaml
@@ -0,0 +1,15 @@
+---
+
+- name: create directory to store images
+  file:
+    state: directory
+    dest: "{{ download_images_dir }}"
+
+- name: download images
+  get_url:
+    dest: "{{ download_images_dir }}/{{ item.key }}"
+    url: "{{ item.value.url }}"
+  register: download
+  retries: 5
+  until: download is success
+  with_dict: "{{ download_images }}"