From 69d979c048d5ec92084414d698e3571d7a4b4920 Mon Sep 17 00:00:00 2001
From: Boxiang Zhu <zhu.boxiang@99cloud.net>
Date: Tue, 18 Oct 2022 10:41:30 +0800
Subject: [PATCH] [CI] Add skyline scenario

Skyline is a new service for dashboard.
This patch adds a CI scenario which tests Skyline deployment.

Depends-On: https://review.opendev.org/c/openstack/kolla/+/826948
Depends-On: https://review.opendev.org/c/openstack/kolla-ansible/+/828464

Implements: blueprint skyline
Change-Id: I48488a24d6c8a03cd129929347b1bdac25f198b0
---
 tests/run.yml                      |  7 ++++
 tests/setup_gate.sh                |  4 ++
 tests/templates/globals-default.j2 |  4 ++
 tests/templates/inventory.j2       | 10 +++++
 tests/test-skyline.sh              | 60 ++++++++++++++++++++++++++++++
 zuul.d/base.yaml                   | 10 +++++
 zuul.d/jobs.yaml                   | 14 +++++++
 zuul.d/project.yaml                |  2 +
 8 files changed, 111 insertions(+)
 create mode 100644 tests/test-skyline.sh

diff --git a/tests/run.yml b/tests/run.yml
index 843f59cf18..5503902a58 100644
--- a/tests/run.yml
+++ b/tests/run.yml
@@ -555,6 +555,13 @@
             chdir: "{{ kolla_ansible_src_dir }}"
           when: scenario == "venus"
 
+        - name: Run test-skyline.sh script
+          script:
+            cmd: test-skyline.sh
+            executable: /bin/bash
+            chdir: "{{ kolla_ansible_src_dir }}"
+          when: scenario == "skyline"
+
       when: scenario != "bifrost"
 
 # NOTE(yoctozepto): each host checks itself
diff --git a/tests/setup_gate.sh b/tests/setup_gate.sh
index 7dc9c92a96..33098ac006 100755
--- a/tests/setup_gate.sh
+++ b/tests/setup_gate.sh
@@ -106,6 +106,10 @@ function prepare_images {
         GATE_IMAGES="^cron,^opensearch,^fluentd,^haproxy,^keepalived,^keystone,^kolla-toolbox,^mariadb,^memcached,^rabbitmq,^venus"
     fi
 
+    if [[ $SCENARIO == "skyline" ]]; then
+        GATE_IMAGES+=",^skyline"
+    fi
+
     sudo tee -a /etc/kolla/kolla-build.conf <<EOF
 [DEFAULT]
 engine = ${CONTAINER_ENGINE}
diff --git a/tests/templates/globals-default.j2 b/tests/templates/globals-default.j2
index 86cdb67f42..73029ab62b 100644
--- a/tests/templates/globals-default.j2
+++ b/tests/templates/globals-default.j2
@@ -218,3 +218,7 @@ kolla_internal_fqdn: "{{ kolla_internal_fqdn }}"
 kolla_enable_tls_backend: "no"
 kolla_admin_openrc_cacert: "{% raw %}{{ kolla_certificates_dir }}{% endraw %}/ca/pebble.crt"
 {% endif %}
+
+{% if scenario == "skyline" %}
+enable_skyline: "yes"
+{% endif %}
diff --git a/tests/templates/inventory.j2 b/tests/templates/inventory.j2
index e4c33c08cf..e1b1222884 100644
--- a/tests/templates/inventory.j2
+++ b/tests/templates/inventory.j2
@@ -261,6 +261,9 @@ monitoring
 [letsencrypt:children]
 loadbalancer
 
+[skyline:children]
+control
+
 # Additional control implemented here. These groups allow you to control which
 # services run on which hosts at a per-service level.
 #
@@ -758,3 +761,10 @@ letsencrypt
 
 [letsencrypt-lego:children]
 letsencrypt
+
+# Skyline
+[skyline-apiserver:children]
+skyline
+
+[skyline-console:children]
+skyline
diff --git a/tests/test-skyline.sh b/tests/test-skyline.sh
new file mode 100644
index 0000000000..5b276392e2
--- /dev/null
+++ b/tests/test-skyline.sh
@@ -0,0 +1,60 @@
+#!/bin/bash
+
+set -o xtrace
+set -o pipefail
+
+# Enable unbuffered output
+export PYTHONUNBUFFERED=1
+
+function check_skyline {
+    skyline_endpoint=$(openstack endpoint list --interface public --service skyline -f value -c URL)
+    # 9998 is the default port for skyline apiserver.
+    # 9999 is the default port for skyline console.
+    skyline_login_url="${skyline_endpoint//9998/9999}/api/openstack/skyline/api/v1/login"
+    skyline_body="{\"region\": \"${OS_REGION_NAME}\", \"domain\": \"${OS_USER_DOMAIN_NAME}\", \"username\": \"${OS_USERNAME}\", \"password\": \"${OS_PASSWORD}\"}"
+
+    output_path=$1
+    if ! curl -k --include --fail -X POST $skyline_login_url -H "Accept: application/json" -H "Content-Type: application/json" -d "${skyline_body}"  > $output_path; then
+        return 1
+    fi
+    if ! grep -E '"keystone_token":' $output_path >/dev/null; then
+        return 1
+    fi
+}
+
+function test_skyline {
+    echo "TESTING: Skyline"
+    output_path=$(mktemp)
+    attempt=1
+    while ! check_skyline $output_path; do
+        echo "Skyline not accessible yet"
+        attempt=$((attempt+1))
+        if [[ $attempt -eq 12 ]]; then
+            echo "FAILED: Skyline did not become accessible. Response:"
+            cat $output_path
+            return 1
+        fi
+        sleep 10
+    done
+    echo "SUCCESS: Skyline"
+}
+
+function test_skyline_logged {
+    . /etc/kolla/admin-openrc.sh
+    . ~/openstackclient-venv/bin/activate
+    test_skyline
+}
+
+function test_skyline_scenario {
+    echo "Testing Skyline"
+    test_skyline_logged > /tmp/logs/ansible/test-skyline 2>&1
+    result=$?
+    if [[ $result != 0 ]]; then
+        echo "Testing Skyline failed. See ansible/test-skyline for details"
+    else
+        echo "Successfully tested Skyline. See ansible/test-skyline for details"
+    fi
+    return $result
+}
+
+test_skyline_scenario
diff --git a/zuul.d/base.yaml b/zuul.d/base.yaml
index 04e84ba122..bccb904884 100644
--- a/zuul.d/base.yaml
+++ b/zuul.d/base.yaml
@@ -297,3 +297,13 @@
       scenario: lets-encrypt
       tls_enabled: true
       le_enabled: true
+
+- job:
+    name: kolla-ansible-skyline-base
+    parent: kolla-ansible-base
+    voting: false
+    files:
+      - ^ansible/roles/skyline/
+      - ^tests/test-skyline.sh
+    vars:
+      scenario: skyline
diff --git a/zuul.d/jobs.yaml b/zuul.d/jobs.yaml
index 6b62a95533..3528142887 100644
--- a/zuul.d/jobs.yaml
+++ b/zuul.d/jobs.yaml
@@ -471,3 +471,17 @@
     nodeset: kolla-ansible-rocky9
     vars:
       base_distro: rocky
+
+- job:
+    name: kolla-ansible-ubuntu-skyline
+    parent: kolla-ansible-skyline-base
+    nodeset: kolla-ansible-jammy
+    vars:
+      base_distro: ubuntu
+
+- job:
+    name: kolla-ansible-rocky9-skyline
+    parent: kolla-ansible-skyline-base
+    nodeset: kolla-ansible-rocky9
+    vars:
+      base_distro: rocky
diff --git a/zuul.d/project.yaml b/zuul.d/project.yaml
index 83fd33ceaf..6ff073b416 100644
--- a/zuul.d/project.yaml
+++ b/zuul.d/project.yaml
@@ -64,6 +64,8 @@
         - kolla-ansible-rocky9-hashi-vault
         - kolla-ansible-ubuntu-lets-encrypt
         - kolla-ansible-rocky9-lets-encrypt
+        - kolla-ansible-ubuntu-skyline
+        - kolla-ansible-rocky9-skyline
     check-arm64:
       jobs:
         - kolla-ansible-debian-aarch64