From 38bb73f2114e9dac25f3b3c6f4e907701e02bdbb Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 10 Mar 2022 12:08:21 +0100 Subject: [PATCH] Ignore bugfix version when running version compatibility check against Elasticsearch (#30746) (#30750) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 46be42e7d5a7b87a7511b715f8ce9e1a969f2926) Co-authored-by: Noémi Ványi --- CHANGELOG.next.asciidoc | 1 + libbeat/cmd/instance/beat.go | 2 +- libbeat/common/version.go | 21 ++++++++++++--- libbeat/common/version_test.go | 49 ++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 78922cb58e0a..855d8186710e 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -39,6 +39,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...main[Check the HEAD dif - Fixes Beats crashing when glibc >= 2.35 is used {issue}30576[30576] - Log errors when parsing and applying config blocks and if the input is disabled. {pull}30534[30534] - Wildcard fields no longer have a default ignore_above setting of 1024. {issue}30096[30096] {pull}30668[30668] +- Ignore bugfix version when running version compatibility check against Elasticsearch. {pull}30746[30746] *Auditbeat* diff --git a/libbeat/cmd/instance/beat.go b/libbeat/cmd/instance/beat.go index 7fbb6a0beff8..b18192d0fd4f 100644 --- a/libbeat/cmd/instance/beat.go +++ b/libbeat/cmd/instance/beat.go @@ -867,7 +867,7 @@ func (b *Beat) checkElasticsearchVersion() { if err != nil { return err } - if esVersion.LessThan(beatVersion) { + if esVersion.LessThanMajorMinor(beatVersion) { return fmt.Errorf("%v ES=%s, Beat=%s.", elasticsearch.ErrTooOld, esVersion.String(), b.Info.Version) } return nil diff --git a/libbeat/common/version.go b/libbeat/common/version.go index 366611531aec..9f1e5caec1ee 100644 --- a/libbeat/common/version.go +++ b/libbeat/common/version.go @@ -121,15 +121,28 @@ func (v *Version) LessThanOrEqual(withMeta bool, v1 *Version) bool { // LessThan returns true if v is strictly smaller than v1. When comparing, the major, // minor and bugfix numbers are compared in order. The meta part is not taken into account. func (v *Version) LessThan(v1 *Version) bool { + lessThan := v.LessThanMajorMinor(v1) + if lessThan { + return true + } + + if v.Minor == v1.Minor { + if v.Bugfix < v1.Bugfix { + return true + } + } + + return false +} + +// LessThanMajorMinor returns true if v is smaller or equal to v1 based on the major and minor version. The bugfix version and meta part are not taken into account. +// minor numbers are compared in order. The and bugfix version and meta part is not taken into account. +func (v *Version) LessThanMajorMinor(v1 *Version) bool { if v.Major < v1.Major { return true } else if v.Major == v1.Major { if v.Minor < v1.Minor { return true - } else if v.Minor == v1.Minor { - if v.Bugfix < v1.Bugfix { - return true - } } } return false diff --git a/libbeat/common/version_test.go b/libbeat/common/version_test.go index 1a94569a596b..c7a6c60a8047 100644 --- a/libbeat/common/version_test.go +++ b/libbeat/common/version_test.go @@ -207,3 +207,52 @@ func TestVersionLessThan(t *testing.T) { assert.Equal(t, v.LessThan(v1), test.result, test.name) } } + +func TestVersionLessThanMajorMinor(t *testing.T) { + tests := []struct { + name string + version string + version1 string + result bool + }{ + { + name: "1.2.x < 2.0.x", + version: "1.2.3", + version1: "2.0.0", + result: true, + }, + { + name: "1.2.3 = 1.2.3-beta1", + version: "1.2.3", + version1: "1.2.3-beta1", + result: false, + }, + { + name: "5.4.x == 5.4.x", + version: "5.4.1", + version1: "5.4.2", + result: false, + }, + { + name: "5.5.x > 5.4.x", + version: "5.5.1", + version1: "5.4.2", + result: false, + }, + { + name: "6.1.x < 6.2.x", + version: "6.1.1-alpha3", + version1: "6.2.0", + result: true, + }, + } + + for _, test := range tests { + v, err := NewVersion(test.version) + assert.NoError(t, err) + v1, err := NewVersion(test.version1) + assert.NoError(t, err) + + assert.Equal(t, v.LessThanMajorMinor(v1), test.result, test.name) + } +}