Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions dev/citools/kibana.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package citools

import (
"fmt"
"strings"

"github.com/Masterminds/semver/v3"
)

func KibanaConstraintPackage(path string) (*semver.Constraints, error) {
manifest, err := readPackageManifest(path)
if err != nil {
return nil, fmt.Errorf("failed to read package manifest: %w", err)
}

kibanaVersion := manifest.Conditions.Kibana.Version
if kibanaVersion == "" {
return nil, nil
}

constraint, err := semver.NewConstraint(kibanaVersion)
if err != nil {
return nil, fmt.Errorf("failed to parse kibana constraint: %w", err)
}
return constraint, nil
}

func IsPackageSupportedInStackVersion(stackVersion string, path string) (bool, error) {
stackVersion = strings.TrimSuffix(stackVersion, "-SNAPSHOT")

stackSemVersion, err := semver.NewVersion(stackVersion)
if err != nil {
return false, fmt.Errorf("failed to parse stack version: %w", err)
}

packageConstraint, err := KibanaConstraintPackage(path)
if err != nil {
return false, err
}

if packageConstraint == nil {
return true, nil
}

return packageConstraint.Check(stackSemVersion), nil
}
141 changes: 141 additions & 0 deletions dev/citools/kibana_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package citools

import (
"os"
"path/filepath"
"testing"

"github.com/Masterminds/semver/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestKibanaConstraintPackage(t *testing.T) {
constraintTest, err := semver.NewConstraint("^8.0.0")
require.NoError(t, err)

cases := []struct {
title string
contents string
expected *semver.Constraints
}{
{
title: "kibana constrasint defined",
contents: `name: "version"
conditions:
kibana:
version: "^8.0.0"
`,
expected: constraintTest,
},
{
title: "kibana constraint defined with dotted field",
contents: `name: "version"
conditions:
kibana.version: "^8.0.0"
`,
expected: constraintTest,
},
{
title: "kibana constraint not defined",
contents: `name: "version"
`,
expected: nil,
},
}

for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
directory := t.TempDir()
pkgManifestPath := filepath.Join(directory, "manifest.yml")
err := os.WriteFile(pkgManifestPath, []byte(c.contents), 0o644)
require.NoError(t, err)
constraint, err := KibanaConstraintPackage(pkgManifestPath)
require.NoError(t, err)
assert.Equal(t, c.expected, constraint)
})
}
}

func TestIsPackageSupportedInStackVersion(t *testing.T) {
cases := []struct {
title string
contents string
stackVersion string
supported bool
}{
{
title: "Test simple kibana constraint",
stackVersion: "8.18.0",
contents: `name: "stack"
conditions:
kibana:
version: "^8.0.0"
`,
supported: true,
},
{
title: "Test or condition",
stackVersion: "8.18.0",
contents: `name: "stack"
conditions:
kibana:
version: "^8.0.0 || ^9.0.0"
`,
supported: true,
},
{
title: "Test snapshot",
stackVersion: "8.18.0-SNAPSHOT",
contents: `name: "stack"
conditions:
kibana:
version: "^8.0.0 || ^9.0.0"
`,
supported: true,
},
{
title: "Test greater or equal",
stackVersion: "8.18.0-SNAPSHOT",
contents: `name: "stack"
conditions:
kibana:
version: ">=8.0.0"
`,
supported: true,
},
{
title: "Test not supported",
stackVersion: "8.18.0-SNAPSHOT",
contents: `name: "stack"
conditions:
kibana:
version: "^9.0.0"
`,
supported: false,
},
{
title: "Test missing kibana version",
stackVersion: "8.18.0-SNAPSHOT",
contents: `name: "stack"
`,
supported: true,
},
}

for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
directory := t.TempDir()
pkgManifestPath := filepath.Join(directory, "manifest.yml")
err := os.WriteFile(pkgManifestPath, []byte(c.contents), 0o644)
require.NoError(t, err)
supported, err := IsPackageSupportedInStackVersion(c.stackVersion, pkgManifestPath)
require.NoError(t, err)
assert.Equal(t, c.supported, supported)
})
}
}
59 changes: 59 additions & 0 deletions dev/citools/logsdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package citools

import (
"fmt"

"github.com/Masterminds/semver/v3"
)

var (
semver8_17_0 = semver.MustParse("8.17.0")
semver8_19_99 = semver.MustParse("8.19.99")
semver9_99_99 = semver.MustParse("9.99.99")
)

func IsVersionLessThanLogsDBGA(version *semver.Version) bool {
return version.LessThan(semver8_17_0)
}

func packageKibanaConstraint(path string) (*semver.Constraints, error) {
manifest, err := readPackageManifest(path)
if err != nil {
return nil, err
}

kibanaConstraint := manifest.Conditions.Kibana.Version
if kibanaConstraint == "" {
return nil, nil
}

constraints, err := semver.NewConstraint(kibanaConstraint)
if err != nil {
return nil, err
}

return constraints, nil
}

func IsLogsDBSupportedInPackage(path string) (bool, error) {
constraint, err := packageKibanaConstraint(path)
if err != nil {
return false, fmt.Errorf("failed to read kibana.constraint fro mmanifest: %w", err)
}

if constraint == nil {
// Package does not contain any kibana.version
return true, nil
}

// Ensure that the package supports LogsDB mode
// It is not used here "semver8_17_0" since a constraint like "^8.18.0 || ^9.0.0" would return false
if constraint.Check(semver8_19_99) || constraint.Check(semver9_99_99) {
return true, nil
}
return false, nil
}
104 changes: 104 additions & 0 deletions dev/citools/logsdb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package citools

import (
"os"
"path/filepath"
"testing"

"github.com/Masterminds/semver/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestIsVersionLessThanLogsDBGA(t *testing.T) {
cases := []struct {
title string
version *semver.Version
expected bool
}{
{
title: "less than LogsDB GA",
version: semver.MustParse("8.12.0"),
expected: true,
},
{
title: "greater or equal than LogsSB GA",
version: semver.MustParse("8.17.0"),
expected: false,
},
}

for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
value := IsVersionLessThanLogsDBGA(c.version)
assert.Equal(t, c.expected, value)
})
}

}

func TestIsLogsDBSupportedInPackage(t *testing.T) {
cases := []struct {
title string
contents string
expectedError bool
supported bool
}{
{
title: "Supported LogsDB field",
contents: `name: "logsdb"
conditions:
kibana:
version: "^7.16.0 || ^8.0.0 || ^9.0.0"
`,
expectedError: false,
supported: true,
},
{
title: "Kibana constraint dotted field",
contents: `name: "subscription"
conditions:
kibana.version: "^7.16.0 || ^8.0.0 || ^9.0.0"
`,
expectedError: false,
supported: true,
},
{
title: "LogsDB not supported",
contents: `name: "subscription"
conditions:
kibana.version: "^7.16.0"
`,
expectedError: false,
supported: false,
},
{
title: "No Kibana constraint",
contents: `name: "subscription"
`,
expectedError: false,
supported: true,
},
}

for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
directory := t.TempDir()
pkgManifestPath := filepath.Join(directory, "manifest.yml")
err := os.WriteFile(pkgManifestPath, []byte(c.contents), 0o644)
require.NoError(t, err)
supported, err := IsLogsDBSupportedInPackage(pkgManifestPath)
if c.expectedError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, c.supported, supported)
}
})
}

}
Loading