From 8e514a0c5adcd8facc5ff45a1b92a5657bcabf21 Mon Sep 17 00:00:00 2001
From: Ewoud Kohl van Wijngaarden <ewoud@kohlvanwijngaarden.nl>
Date: Mon, 6 Jan 2025 18:20:40 +0100
Subject: [PATCH] Rewrite Foreman's testing to a matrix

This makes it easier to change the Ruby versions and also test on
multiple at the same time.
---
 .../pipelines/release/source/foreman.groovy   | 128 +++++++-----------
 1 file changed, 47 insertions(+), 81 deletions(-)

diff --git a/theforeman.org/pipelines/release/source/foreman.groovy b/theforeman.org/pipelines/release/source/foreman.groovy
index 9943a0bc..7cb7d4b3 100644
--- a/theforeman.org/pipelines/release/source/foreman.groovy
+++ b/theforeman.org/pipelines/release/source/foreman.groovy
@@ -8,101 +8,67 @@ pipeline {
         buildDiscarder(logRotator(daysToKeepStr: '7'))
     }
 
+    environment {
+        BUNDLE_WITHOUT = 'development'
+        RAILS_ENV = TASK == 'assets:precompile' ? 'production' : 'test'
+        // TODO: replace invalid chars
+        // TODO: strip *: from TASK
+        // TODO: limit DB name length to < 63 chars
+        DATABASE_URL = TASK == 'assets:precompile' ? 'nulldb://nohost' : "postgresql://foreman:foreman@localhost/test-${env.JOB_NAME}-${env.BUILD_ID}-${RUBY_VER}-${TASK}"
+        TESTOPTS = '-v'
+    }
+
     stages {
         stage('Test Matrix') {
-            parallel {
-                stage('ruby-2.7-postgres') {
-                    agent { label 'fast' }
-                    environment {
-                        RUBY_VER = '2.7.6'
-                    }
-                    stages {
-                        stage("setup-2.7-postgres") {
-                            steps {
-                                git url: git_url, branch: git_ref
-                                script {
-                                    archive_git_hash()
-                                }
-                                databaseFile("${env.JOB_NAME}-${env.BUILD_ID}")
-                                configureDatabase(env.RUBY_VER)
-                            }
-                        }
-                        stage("unit-tests-2.7-postgres") {
-                            steps {
-                                bundleExec(env.RUBY_VER, 'rake jenkins:unit TESTOPTS="-v" --trace')
-                            }
-                        }
+            matrix {
+                agent { label 'fast' }
+                axes {
+                    axis {
+                        name 'RUBY_VER'
+                        values '2.7.6'
                     }
-                    post {
-                        always {
-                            junit(testResults: 'jenkins/reports/unit/*.xml')
-                        }
-                        cleanup {
-                            cleanup(env.RUBY_VER)
-                            deleteDir()
-                        }
+                    axis {
+                        name 'TASK'
+                        values 'jenkins:unit' 'jenkins:integration' 'assets:precompile'
                     }
                 }
-                stage('ruby-2.7-postgres-integrations') {
-                    agent { label 'fast' }
-                    environment {
-                        RUBY_VER = '2.7.6'
-                    }
-                    stages {
-                        stage("setup-2.7-postgres-ui") {
-                            steps {
-                                git url: git_url, branch: git_ref
-                                databaseFile("${env.JOB_NAME}-${env.BUILD_ID}-ui")
-                                configureDatabase(env.RUBY_VER)
-                                withRuby(env.RUBY_VER, 'npm install --no-audit --legacy-peer-deps')
-                                archiveArtifacts(artifacts: 'package-lock.json')
-                            }
-                        }
-                        stage("integration-tests-2.7-postgres-ui") {
-                            steps {
-                                bundleExec(env.RUBY_VER, 'rake jenkins:integration TESTOPTS="-v" --trace')
-                            }
-                        }
-                    }
-                    post {
-                        always {
-                            junit(testResults: 'jenkins/reports/unit/*.xml')
-                        }
-                        cleanup {
-                            cleanup(env.RUBY_VER)
-                            deleteDir()
+            }
+            stages {
+                stage('setup') {
+                    steps {
+                        git url: git_url, branch: git_ref
+                        script {
+                            archive_git_hash()
                         }
-                    }
-                }
-                stage('ruby-2.7-nulldb-assets') {
-                    agent { label 'fast' }
-                    environment {
-                        RUBY_VER = '2.7.6'
-                    }
-                    stages {
-                        stage("setup-2.7-nulldb") {
-                            steps {
-                                git url: git_url, branch: git_ref
-                                bundleInstall(env.RUBY_VER, '--without=development')
+                        bundleInstall(RUBY_VER)
+                        archiveArtifacts(artifacts: 'Gemfile.lock')
+                        script {
+                            if (TASK == 'assets:precompile') {
                                 sh "cp db/schema.rb.nulldb db/schema.rb"
-                                filter_package_json(env.RUBY_VER)
-                                withRuby(env.RUBY_VER, 'npm install --no-audit --legacy-peer-deps')
+                                filter_package_json(RUBY_VER)
                             }
-                        }
-                        stage("assets-precompile-2.7-nulldb") {
-                            steps {
-                                bundleExec(env.RUBY_VER, 'rake assets:precompile RAILS_ENV=production DATABASE_URL=nulldb://nohost')
+                            if (TASK == 'jenkins:integration' || TASK == 'assets:precompile' ){
+                                withRuby(RUBY_VER, 'npm install --no-audit --legacy-peer-deps')
+                                archiveArtifacts(artifacts: 'package-lock.json')
                             }
                         }
                     }
-                    post {
-                        cleanup {
-                            cleanup(env.RUBY_VER)
-                            deleteDir()
-                        }
+                }
+                stage(TASK) {
+                    steps {
+                        bundleExec(RUBY_VER, "rake ${TASK} --trace")
                     }
                 }
             }
+            post {
+                always {
+                    junit(testResults: 'jenkins/reports/*/*.xml', allowEmptyResults: TASK == 'assets:precompile')
+                }
+                cleanup {
+                    bundleExec(RUBY_VER, 'rake db:drop DISABLE_DATABASE_ENVIRONMENT_CHECK=true')
+                    deleteDir()
+                }
+            }
         }
         stage('Build and Archive Source') {
             steps {