From f3f9b499adde6f4749b9d21cde82f9f8a72671f6 Mon Sep 17 00:00:00 2001
From: Tanguy Leroux <tlrx.dev@gmail.com>
Date: Tue, 13 Sep 2016 17:52:06 +0200
Subject: [PATCH] [Packaging] Run BATS upgrade tests against the current
 version (#20453)

BATS upgrade tests fails on master branch because it tries to install 2.x versions to upgrade from instead of 5.x versions. And since #18554 we should only test upgrades from 5.0.0-alpha4 versions.

This commit changes the vagrant tests so that it tries to list all the previous releases from version N-1. If nothing is found, it will fetch the current version and will run the upgrade tests with it. It works nicely with the current master 6.0.0-alpha1-SNAPSHOT. Once 5.0.0 is released it should run the test with it.
---
 qa/vagrant/build.gradle                       | 11 ++++++++-
 .../packaging/scripts/80_upgrade.bats         | 24 +++++++++----------
 .../packaging/scripts/os_package.bash         |  6 ++++-
 .../scripts/packaging_test_utils.bash         | 16 +++++++++++++
 qa/vagrant/versions                           | 17 +------------
 5 files changed, 44 insertions(+), 30 deletions(-)

diff --git a/qa/vagrant/build.gradle b/qa/vagrant/build.gradle
index 746f3291547..fb7ee1d8d3c 100644
--- a/qa/vagrant/build.gradle
+++ b/qa/vagrant/build.gradle
@@ -126,7 +126,16 @@ Set<String> getVersions() {
   new URL('https://repo1.maven.org/maven2/org/elasticsearch/elasticsearch/maven-metadata.xml').openStream().withStream { s ->
     xml = new XmlParser().parse(s)
   }
-  return new TreeSet<>(xml.versioning.versions.version.collect { it.text() }.findAll { it ==~ /2\.\d\.\d/ })
+
+  // List all N-1 releases from maven central
+  int major = Integer.parseInt(project.version.substring(0, project.version.indexOf('.'))) - 1
+  Set<String> versions = new TreeSet<>(xml.versioning.versions.version.collect { it.text() }.findAll { it ==~ /$major\.\d\.\d/ })
+  if (versions.isEmpty() == false) {
+    return versions;
+  }
+
+  // If no version is found, we run the tests with the current version
+  return Collections.singleton(project.version);
 }
 
 task updatePackagingTestUpgradeFromVersions {
diff --git a/qa/vagrant/src/test/resources/packaging/scripts/80_upgrade.bats b/qa/vagrant/src/test/resources/packaging/scripts/80_upgrade.bats
index 48bf2aca4e3..feca52c7bbc 100644
--- a/qa/vagrant/src/test/resources/packaging/scripts/80_upgrade.bats
+++ b/qa/vagrant/src/test/resources/packaging/scripts/80_upgrade.bats
@@ -37,6 +37,11 @@ load os_package
 # Cleans everything for the 1st execution
 setup() {
     skip_not_dpkg_or_rpm
+
+    sameVersion="false"
+    if [ "$(cat upgrade_from_version)" == "$(cat version)" ]; then
+        sameVersion="true"
+    fi
 }
 
 @test "[UPGRADE] install old version" {
@@ -49,11 +54,7 @@ setup() {
 }
 
 @test "[UPGRADE] check elasticsearch version is old version" {
-    curl -s localhost:9200 | grep \"number\"\ :\ \"$(cat upgrade_from_version)\" || {
-        echo "Installed an unexpected version:"
-        curl -s localhost:9200
-        false
-    }
+    check_elasticsearch_version "$(cat upgrade_from_version)"
 }
 
 @test "[UPGRADE] index some documents into a few indexes" {
@@ -79,7 +80,11 @@ setup() {
 }
 
 @test "[UPGRADE] install version under test" {
-    install_package -u
+    if [ "$sameVersion" == "true" ]; then
+        install_package -f
+    else
+        install_package -u
+    fi
 }
 
 @test "[UPGRADE] start version under test" {
@@ -88,12 +93,7 @@ setup() {
 }
 
 @test "[UPGRADE] check elasticsearch version is version under test" {
-    local versionToCheck=$(cat version | sed -e 's/-SNAPSHOT//')
-    curl -s localhost:9200 | grep \"number\"\ :\ \"$versionToCheck\" || {
-        echo "Installed an unexpected version:"
-        curl -s localhost:9200
-        false
-    }
+    check_elasticsearch_version "$(cat version)"
 }
 
 @test "[UPGRADE] verify that the documents are there after restart" {
diff --git a/qa/vagrant/src/test/resources/packaging/scripts/os_package.bash b/qa/vagrant/src/test/resources/packaging/scripts/os_package.bash
index d46d7fe9a5d..1060aa78849 100644
--- a/qa/vagrant/src/test/resources/packaging/scripts/os_package.bash
+++ b/qa/vagrant/src/test/resources/packaging/scripts/os_package.bash
@@ -44,12 +44,16 @@ export_elasticsearch_paths() {
 install_package() {
     local version=$(cat version)
     local rpmCommand='-i'
-    while getopts ":uv:" opt; do
+    while getopts ":fuv:" opt; do
         case $opt in
             u)
                 rpmCommand='-U'
                 dpkgCommand='--force-confnew'
                 ;;
+            f)
+                rpmCommand='-U --force'
+                dpkgCommand='--force-conflicts'
+                ;;
             v)
                 version=$OPTARG
                 ;;
diff --git a/qa/vagrant/src/test/resources/packaging/scripts/packaging_test_utils.bash b/qa/vagrant/src/test/resources/packaging/scripts/packaging_test_utils.bash
index fbda05d5f30..137f87045de 100644
--- a/qa/vagrant/src/test/resources/packaging/scripts/packaging_test_utils.bash
+++ b/qa/vagrant/src/test/resources/packaging/scripts/packaging_test_utils.bash
@@ -452,6 +452,22 @@ wait_for_elasticsearch_status() {
     }
 }
 
+# Checks the current elasticsearch version using the Info REST endpoint
+# $1 - expected version
+check_elasticsearch_version() {
+    local version=$1
+    local versionToCheck=$(echo $version | sed -e 's/-SNAPSHOT//')
+
+    run curl -s localhost:9200
+    [ "$status" -eq 0 ]
+
+    echo $output | grep \"number\"\ :\ \"$versionToCheck\" || {
+        echo "Installed an unexpected version:"
+        curl -s localhost:9200
+        false
+    }
+}
+
 install_elasticsearch_test_scripts() {
     install_script is_guide.groovy
     install_script is_guide.mustache
diff --git a/qa/vagrant/versions b/qa/vagrant/versions
index c7aef6cb99a..654a95a3a25 100644
--- a/qa/vagrant/versions
+++ b/qa/vagrant/versions
@@ -1,16 +1 @@
-2.0.0
-2.0.1
-2.0.2
-2.1.0
-2.1.1
-2.1.2
-2.2.0
-2.2.1
-2.2.2
-2.3.0
-2.3.1
-2.3.2
-2.3.3
-2.3.4
-2.3.5
-2.4.0
+6.0.0-alpha1-SNAPSHOT