Skip to content

Commit ac5b772

Browse files
authored
[CI] Add missing mage scripts ci (#13823)
Update magefile.go and scripts under dev folder in order to run all the targets required in the CI pipelines.
1 parent 2c53e10 commit ac5b772

19 files changed

+974
-99
lines changed

dev/citools/kibana.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package citools
6+
7+
import (
8+
"fmt"
9+
"strings"
10+
11+
"github.com/Masterminds/semver/v3"
12+
)
13+
14+
func KibanaConstraintPackage(path string) (*semver.Constraints, error) {
15+
manifest, err := readPackageManifest(path)
16+
if err != nil {
17+
return nil, fmt.Errorf("failed to read package manifest: %w", err)
18+
}
19+
20+
kibanaVersion := manifest.Conditions.Kibana.Version
21+
if kibanaVersion == "" {
22+
return nil, nil
23+
}
24+
25+
constraint, err := semver.NewConstraint(kibanaVersion)
26+
if err != nil {
27+
return nil, fmt.Errorf("failed to parse kibana constraint: %w", err)
28+
}
29+
return constraint, nil
30+
}
31+
32+
func IsPackageSupportedInStackVersion(stackVersion string, path string) (bool, error) {
33+
stackVersion = strings.TrimSuffix(stackVersion, "-SNAPSHOT")
34+
35+
stackSemVersion, err := semver.NewVersion(stackVersion)
36+
if err != nil {
37+
return false, fmt.Errorf("failed to parse stack version: %w", err)
38+
}
39+
40+
packageConstraint, err := KibanaConstraintPackage(path)
41+
if err != nil {
42+
return false, err
43+
}
44+
45+
if packageConstraint == nil {
46+
return true, nil
47+
}
48+
49+
return packageConstraint.Check(stackSemVersion), nil
50+
}

dev/citools/kibana_test.go

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package citools
6+
7+
import (
8+
"os"
9+
"path/filepath"
10+
"testing"
11+
12+
"github.com/Masterminds/semver/v3"
13+
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
func TestKibanaConstraintPackage(t *testing.T) {
18+
constraintTest, err := semver.NewConstraint("^8.0.0")
19+
require.NoError(t, err)
20+
21+
cases := []struct {
22+
title string
23+
contents string
24+
expected *semver.Constraints
25+
}{
26+
{
27+
title: "kibana constrasint defined",
28+
contents: `name: "version"
29+
conditions:
30+
kibana:
31+
version: "^8.0.0"
32+
`,
33+
expected: constraintTest,
34+
},
35+
{
36+
title: "kibana constraint defined with dotted field",
37+
contents: `name: "version"
38+
conditions:
39+
kibana.version: "^8.0.0"
40+
`,
41+
expected: constraintTest,
42+
},
43+
{
44+
title: "kibana constraint not defined",
45+
contents: `name: "version"
46+
`,
47+
expected: nil,
48+
},
49+
}
50+
51+
for _, c := range cases {
52+
t.Run(c.title, func(t *testing.T) {
53+
directory := t.TempDir()
54+
pkgManifestPath := filepath.Join(directory, "manifest.yml")
55+
err := os.WriteFile(pkgManifestPath, []byte(c.contents), 0o644)
56+
require.NoError(t, err)
57+
constraint, err := KibanaConstraintPackage(pkgManifestPath)
58+
require.NoError(t, err)
59+
assert.Equal(t, c.expected, constraint)
60+
})
61+
}
62+
}
63+
64+
func TestIsPackageSupportedInStackVersion(t *testing.T) {
65+
cases := []struct {
66+
title string
67+
contents string
68+
stackVersion string
69+
supported bool
70+
}{
71+
{
72+
title: "Test simple kibana constraint",
73+
stackVersion: "8.18.0",
74+
contents: `name: "stack"
75+
conditions:
76+
kibana:
77+
version: "^8.0.0"
78+
`,
79+
supported: true,
80+
},
81+
{
82+
title: "Test or condition",
83+
stackVersion: "8.18.0",
84+
contents: `name: "stack"
85+
conditions:
86+
kibana:
87+
version: "^8.0.0 || ^9.0.0"
88+
`,
89+
supported: true,
90+
},
91+
{
92+
title: "Test snapshot",
93+
stackVersion: "8.18.0-SNAPSHOT",
94+
contents: `name: "stack"
95+
conditions:
96+
kibana:
97+
version: "^8.0.0 || ^9.0.0"
98+
`,
99+
supported: true,
100+
},
101+
{
102+
title: "Test greater or equal",
103+
stackVersion: "8.18.0-SNAPSHOT",
104+
contents: `name: "stack"
105+
conditions:
106+
kibana:
107+
version: ">=8.0.0"
108+
`,
109+
supported: true,
110+
},
111+
{
112+
title: "Test not supported",
113+
stackVersion: "8.18.0-SNAPSHOT",
114+
contents: `name: "stack"
115+
conditions:
116+
kibana:
117+
version: "^9.0.0"
118+
`,
119+
supported: false,
120+
},
121+
{
122+
title: "Test missing kibana version",
123+
stackVersion: "8.18.0-SNAPSHOT",
124+
contents: `name: "stack"
125+
`,
126+
supported: true,
127+
},
128+
}
129+
130+
for _, c := range cases {
131+
t.Run(c.title, func(t *testing.T) {
132+
directory := t.TempDir()
133+
pkgManifestPath := filepath.Join(directory, "manifest.yml")
134+
err := os.WriteFile(pkgManifestPath, []byte(c.contents), 0o644)
135+
require.NoError(t, err)
136+
supported, err := IsPackageSupportedInStackVersion(c.stackVersion, pkgManifestPath)
137+
require.NoError(t, err)
138+
assert.Equal(t, c.supported, supported)
139+
})
140+
}
141+
}

dev/citools/logsdb.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package citools
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/Masterminds/semver/v3"
11+
)
12+
13+
var (
14+
semver8_17_0 = semver.MustParse("8.17.0")
15+
semver8_19_99 = semver.MustParse("8.19.99")
16+
semver9_99_99 = semver.MustParse("9.99.99")
17+
)
18+
19+
func IsVersionLessThanLogsDBGA(version *semver.Version) bool {
20+
return version.LessThan(semver8_17_0)
21+
}
22+
23+
func packageKibanaConstraint(path string) (*semver.Constraints, error) {
24+
manifest, err := readPackageManifest(path)
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
kibanaConstraint := manifest.Conditions.Kibana.Version
30+
if kibanaConstraint == "" {
31+
return nil, nil
32+
}
33+
34+
constraints, err := semver.NewConstraint(kibanaConstraint)
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
return constraints, nil
40+
}
41+
42+
func IsLogsDBSupportedInPackage(path string) (bool, error) {
43+
constraint, err := packageKibanaConstraint(path)
44+
if err != nil {
45+
return false, fmt.Errorf("failed to read kibana.constraint fro mmanifest: %w", err)
46+
}
47+
48+
if constraint == nil {
49+
// Package does not contain any kibana.version
50+
return true, nil
51+
}
52+
53+
// Ensure that the package supports LogsDB mode
54+
// It is not used here "semver8_17_0" since a constraint like "^8.18.0 || ^9.0.0" would return false
55+
if constraint.Check(semver8_19_99) || constraint.Check(semver9_99_99) {
56+
return true, nil
57+
}
58+
return false, nil
59+
}

dev/citools/logsdb_test.go

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package citools
6+
7+
import (
8+
"os"
9+
"path/filepath"
10+
"testing"
11+
12+
"github.com/Masterminds/semver/v3"
13+
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
func TestIsVersionLessThanLogsDBGA(t *testing.T) {
18+
cases := []struct {
19+
title string
20+
version *semver.Version
21+
expected bool
22+
}{
23+
{
24+
title: "less than LogsDB GA",
25+
version: semver.MustParse("8.12.0"),
26+
expected: true,
27+
},
28+
{
29+
title: "greater or equal than LogsSB GA",
30+
version: semver.MustParse("8.17.0"),
31+
expected: false,
32+
},
33+
}
34+
35+
for _, c := range cases {
36+
t.Run(c.title, func(t *testing.T) {
37+
value := IsVersionLessThanLogsDBGA(c.version)
38+
assert.Equal(t, c.expected, value)
39+
})
40+
}
41+
42+
}
43+
44+
func TestIsLogsDBSupportedInPackage(t *testing.T) {
45+
cases := []struct {
46+
title string
47+
contents string
48+
expectedError bool
49+
supported bool
50+
}{
51+
{
52+
title: "Supported LogsDB field",
53+
contents: `name: "logsdb"
54+
conditions:
55+
kibana:
56+
version: "^7.16.0 || ^8.0.0 || ^9.0.0"
57+
`,
58+
expectedError: false,
59+
supported: true,
60+
},
61+
{
62+
title: "Kibana constraint dotted field",
63+
contents: `name: "subscription"
64+
conditions:
65+
kibana.version: "^7.16.0 || ^8.0.0 || ^9.0.0"
66+
`,
67+
expectedError: false,
68+
supported: true,
69+
},
70+
{
71+
title: "LogsDB not supported",
72+
contents: `name: "subscription"
73+
conditions:
74+
kibana.version: "^7.16.0"
75+
`,
76+
expectedError: false,
77+
supported: false,
78+
},
79+
{
80+
title: "No Kibana constraint",
81+
contents: `name: "subscription"
82+
`,
83+
expectedError: false,
84+
supported: true,
85+
},
86+
}
87+
88+
for _, c := range cases {
89+
t.Run(c.title, func(t *testing.T) {
90+
directory := t.TempDir()
91+
pkgManifestPath := filepath.Join(directory, "manifest.yml")
92+
err := os.WriteFile(pkgManifestPath, []byte(c.contents), 0o644)
93+
require.NoError(t, err)
94+
supported, err := IsLogsDBSupportedInPackage(pkgManifestPath)
95+
if c.expectedError {
96+
assert.Error(t, err)
97+
} else {
98+
assert.NoError(t, err)
99+
assert.Equal(t, c.supported, supported)
100+
}
101+
})
102+
}
103+
104+
}

0 commit comments

Comments
 (0)