CI: Call for Grafana version on demand - remove need for version.json (#54638)
* Remove need for version.json * Fix lint * log.Info -> fmt.Print * Add back tests * Remove non-used file * Fix lint * Update grabpl version to v3.0.6
This commit is contained in:
@@ -2,10 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
@@ -57,40 +54,6 @@ func (md *Metadata) GetReleaseMode() (ReleaseMode, error) {
|
||||
// than the way it will be built in 'main'
|
||||
type VersionMap map[VersionMode]Version
|
||||
|
||||
// GetMetadata attempts to read the JSON file located at 'path' and decode it as a Metadata{} type.
|
||||
// If the provided path does not exist, then an error is not returned. Instead, an empty metadata is returned with no error.
|
||||
func GetMetadata(path string) (*Metadata, error) {
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return &Metadata{}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
// Ignore gosec G304 as this function is only used in the build process.
|
||||
//nolint:gosec
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
if err := file.Close(); err != nil {
|
||||
log.Printf("error closing file at '%s': %s", path, err.Error())
|
||||
}
|
||||
}()
|
||||
|
||||
return DecodeMetadata(file)
|
||||
}
|
||||
|
||||
// DecodeMetadata decodes the data in the io.Reader 'r' as Metadata.
|
||||
func DecodeMetadata(r io.Reader) (*Metadata, error) {
|
||||
m := &Metadata{}
|
||||
if err := json.NewDecoder(r).Decode(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetVersions reads the embedded config.json and decodes it.
|
||||
func GetVersion(mode VersionMode) (*Version, error) {
|
||||
if v, ok := Versions[mode]; ok {
|
||||
|
||||
@@ -1,167 +0,0 @@
|
||||
package config_test
|
||||
|
||||
var configJSON = []byte(`{
|
||||
"pull_request": {
|
||||
"variants": [
|
||||
"linux-amd64",
|
||||
"linux-amd64-musl",
|
||||
"darwin-amd64",
|
||||
"windows-amd64"
|
||||
],
|
||||
"pluginSignature": {
|
||||
"sign": false,
|
||||
"adminSign": false
|
||||
},
|
||||
"docker": {
|
||||
"shouldSave": false,
|
||||
"archs": [
|
||||
"amd64"
|
||||
]
|
||||
}
|
||||
},
|
||||
"main": {
|
||||
"variants": [
|
||||
"linux-armv6",
|
||||
"linux-armv7",
|
||||
"linux-armv7-musl",
|
||||
"linux-arm64",
|
||||
"linux-arm64-musl",
|
||||
"darwin-amd64",
|
||||
"windows-amd64",
|
||||
"linux-amd64",
|
||||
"linux-amd64-musl"
|
||||
],
|
||||
"pluginSignature": {
|
||||
"sign": true,
|
||||
"adminSign": true
|
||||
},
|
||||
"docker": {
|
||||
"shouldSave": false,
|
||||
"archs": [
|
||||
"amd64",
|
||||
"arm64",
|
||||
"armv7"
|
||||
]
|
||||
},
|
||||
"packagesBucket": "grafana-downloads",
|
||||
"CDNAssetsBucket": "grafana-static-assets"
|
||||
},
|
||||
"branch": {
|
||||
"variants": [
|
||||
"linux-armv6",
|
||||
"linux-armv7",
|
||||
"linux-armv7-musl",
|
||||
"linux-arm64",
|
||||
"linux-arm64-musl",
|
||||
"darwin-amd64",
|
||||
"windows-amd64",
|
||||
"linux-amd64",
|
||||
"linux-amd64-musl"
|
||||
],
|
||||
"pluginSignature": {
|
||||
"sign": true,
|
||||
"adminSign": true
|
||||
},
|
||||
"docker": {
|
||||
"shouldSave": true,
|
||||
"archs": [
|
||||
"amd64",
|
||||
"arm64",
|
||||
"armv7"
|
||||
]
|
||||
},
|
||||
"packagesBucket": "grafana-downloads",
|
||||
"packagesBucketEnterprise2": "grafana-downloads-enterprise2",
|
||||
"CDNAssetsBucket": "grafana-static-assets"
|
||||
},
|
||||
"release": {
|
||||
"variants": [
|
||||
"linux-armv6",
|
||||
"linux-armv7",
|
||||
"linux-armv7-musl",
|
||||
"linux-arm64",
|
||||
"linux-arm64-musl",
|
||||
"darwin-amd64",
|
||||
"windows-amd64",
|
||||
"linux-amd64",
|
||||
"linux-amd64-musl"
|
||||
],
|
||||
"pluginSignature": {
|
||||
"sign": true,
|
||||
"adminSign": true
|
||||
},
|
||||
"docker": {
|
||||
"shouldSave": true,
|
||||
"archs": [
|
||||
"amd64",
|
||||
"arm64",
|
||||
"armv7"
|
||||
]
|
||||
},
|
||||
"packagesBucket": "grafana-prerelease/artifacts/downloads",
|
||||
"CDNAssetsBucket": "grafana-prerelease",
|
||||
"CDNAssetsDir": "artifacts/static-assets",
|
||||
"storybookBucket": "grafana-prerelease",
|
||||
"storybookSrcDir": "artifacts/storybook"
|
||||
},
|
||||
"beta": {
|
||||
"variants": [
|
||||
"linux-armv6",
|
||||
"linux-armv7",
|
||||
"linux-armv7-musl",
|
||||
"linux-arm64",
|
||||
"linux-arm64-musl",
|
||||
"darwin-amd64",
|
||||
"windows-amd64",
|
||||
"linux-amd64",
|
||||
"linux-amd64-musl"
|
||||
],
|
||||
"pluginSignature": {
|
||||
"sign": true,
|
||||
"adminSign": true
|
||||
},
|
||||
"docker": {
|
||||
"shouldSave": true,
|
||||
"archs": [
|
||||
"amd64",
|
||||
"arm64",
|
||||
"armv7"
|
||||
]
|
||||
},
|
||||
"packagesBucket": "grafana-prerelease/artifacts/downloads",
|
||||
"CDNAssetsBucket": "grafana-prerelease",
|
||||
"CDNAssetsDir": "artifacts/static-assets",
|
||||
"storybookBucket": "grafana-prerelease",
|
||||
"storybookSrcDir": "artifacts/storybook"
|
||||
},
|
||||
"test": {
|
||||
"variants": [
|
||||
"linux-armv6",
|
||||
"linux-armv7",
|
||||
"linux-armv7-musl",
|
||||
"linux-arm64",
|
||||
"linux-arm64-musl",
|
||||
"darwin-amd64",
|
||||
"windows-amd64",
|
||||
"linux-amd64",
|
||||
"linux-amd64-musl"
|
||||
],
|
||||
"pluginSignature": {
|
||||
"sign": true,
|
||||
"adminSign": true
|
||||
},
|
||||
"docker": {
|
||||
"shouldSave": true,
|
||||
"archs": [
|
||||
"amd64",
|
||||
"arm64",
|
||||
"armv7"
|
||||
]
|
||||
},
|
||||
"packagesBucket": "grafana-prerelease/artifacts/downloads",
|
||||
"CDNAssetsBucket": "grafana-prerelease",
|
||||
"CDNAssetsDir": "artifacts/static-assets",
|
||||
"storybookBucket": "grafana-prerelease",
|
||||
"storybookSrcDir": "artifacts/storybook"
|
||||
}
|
||||
}`)
|
||||
@@ -1,96 +0,0 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/build/config"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGetMetadata(t *testing.T) {
|
||||
tcs := []struct {
|
||||
version string
|
||||
mode config.ReleaseMode
|
||||
}{
|
||||
{"v1.2.3", config.ReleaseMode{Mode: config.TagMode}},
|
||||
{"v1.2.3-12345pre", config.ReleaseMode{Mode: config.PullRequestMode}},
|
||||
{"v1.2.3-beta1", config.ReleaseMode{Mode: config.TagMode, IsBeta: true}},
|
||||
{"v1.2.3-test1", config.ReleaseMode{Mode: config.TagMode, IsTest: true}},
|
||||
{"v1.2.3-foobar", config.ReleaseMode{Mode: config.ReleaseBranchMode}},
|
||||
{"v1.2.3-foobar", config.ReleaseMode{Mode: config.PullRequestMode}},
|
||||
}
|
||||
|
||||
t.Run("Should return empty metadata, dist/ is not present", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
metadata, err := config.GetMetadata(filepath.Join(dir, "dist"))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, metadata, &config.Metadata{})
|
||||
if err := os.RemoveAll(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
|
||||
for _, tc := range tcs {
|
||||
dir := t.TempDir()
|
||||
t.Run("Should return valid metadata, tag mode, ", func(t *testing.T) {
|
||||
testMetadata(t, dir, tc.version, tc.mode)
|
||||
})
|
||||
if err := os.RemoveAll(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testMetadata(t *testing.T, dir string, version string, releaseMode config.ReleaseMode) {
|
||||
t.Helper()
|
||||
file := filepath.Join(dir, "version.json")
|
||||
createVersionJSON(t, version, file, releaseMode)
|
||||
|
||||
metadata, err := config.GetMetadata(file)
|
||||
require.NoError(t, err)
|
||||
t.Run("with a valid version", func(t *testing.T) {
|
||||
expVersion := metadata.GrafanaVersion
|
||||
require.Equal(t, expVersion, version)
|
||||
})
|
||||
|
||||
t.Run("with a valid release mode from the built-in list", func(t *testing.T) {
|
||||
expMode := metadata.ReleaseMode
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expMode, releaseMode)
|
||||
})
|
||||
|
||||
t.Run("with a valid configuration from a JSON file", func(t *testing.T) {
|
||||
version, err := config.GetVersion(metadata.ReleaseMode.Mode)
|
||||
require.NoError(t, err)
|
||||
parsed := verModeFromConfig(t, metadata)
|
||||
require.EqualValues(t, parsed, *version)
|
||||
})
|
||||
}
|
||||
|
||||
func verModeFromConfig(t *testing.T, metadata *config.Metadata) config.Version {
|
||||
t.Helper()
|
||||
|
||||
metadataComp := config.VersionMap{}
|
||||
|
||||
require.NoError(t, json.Unmarshal(configJSON, &metadataComp))
|
||||
|
||||
return metadataComp[metadata.ReleaseMode.Mode]
|
||||
}
|
||||
|
||||
func createVersionJSON(t *testing.T, version string, file string, releaseMode config.ReleaseMode) {
|
||||
t.Helper()
|
||||
|
||||
metadata := &config.Metadata{
|
||||
GrafanaVersion: version,
|
||||
ReleaseMode: releaseMode,
|
||||
}
|
||||
|
||||
//nolint:gosec
|
||||
f, err := os.Create(file)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, json.NewEncoder(f).Encode(metadata))
|
||||
}
|
||||
Reference in New Issue
Block a user