grafana-cli: Fix file path processing, returning of errors (#26954)
* grafana-cli: Fix file path processing, returning of errors
This commit is contained in:
@@ -4,46 +4,30 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/commands/commandstest"
|
||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFolderNameReplacement(t *testing.T) {
|
||||
Convey("path containing git commit path", t, func() {
|
||||
pluginName := "datasource-plugin-kairosdb"
|
||||
func TestRemoveGitBuildFromName(t *testing.T) {
|
||||
pluginName := "datasource-kairosdb"
|
||||
|
||||
paths := map[string]string{
|
||||
"datasource-plugin-kairosdb-cc4a3965ef5d3eb1ae0ee4f93e9e78ec7db69e64/": "datasource-plugin-kairosdb/",
|
||||
"datasource-plugin-kairosdb-cc4a3965ef5d3eb1ae0ee4f93e9e78ec7db69e64/README.md": "datasource-plugin-kairosdb/README.md",
|
||||
"datasource-plugin-kairosdb-cc4a3965ef5d3eb1ae0ee4f93e9e78ec7db69e64/partials/": "datasource-plugin-kairosdb/partials/",
|
||||
"datasource-plugin-kairosdb-cc4a3965ef5d3eb1ae0ee4f93e9e78ec7db69e64/partials/config.html": "datasource-plugin-kairosdb/partials/config.html",
|
||||
}
|
||||
|
||||
Convey("should be replaced with plugin name", func() {
|
||||
for k, v := range paths {
|
||||
So(RemoveGitBuildFromName(pluginName, k), ShouldEqual, v)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Convey("path containing git commit path", t, func() {
|
||||
pluginName := "app-example"
|
||||
paths := map[string]string{
|
||||
"app-plugin-example-3c28f65ac6fb7f1e234b0364b97081d836495439/": "app-example/",
|
||||
}
|
||||
|
||||
Convey("should be replaced with plugin name", func() {
|
||||
for k, v := range paths {
|
||||
So(RemoveGitBuildFromName(pluginName, k), ShouldEqual, v)
|
||||
}
|
||||
})
|
||||
})
|
||||
// The root directory should get renamed to the plugin name
|
||||
paths := map[string]string{
|
||||
"datasource-plugin-kairosdb-cc4a3965ef5d3eb1ae0ee4f93e9e78ec7db69e64/": "datasource-kairosdb/",
|
||||
"datasource-plugin-kairosdb-cc4a3965ef5d3eb1ae0ee4f93e9e78ec7db69e64/README.md": "datasource-kairosdb/README.md",
|
||||
"datasource-plugin-kairosdb-cc4a3965ef5d3eb1ae0ee4f93e9e78ec7db69e64/partials/": "datasource-kairosdb/partials/",
|
||||
"datasource-plugin-kairosdb-cc4a3965ef5d3eb1ae0ee4f93e9e78ec7db69e64/partials/config.html": "datasource-kairosdb/partials/config.html",
|
||||
}
|
||||
for pth, exp := range paths {
|
||||
name := removeGitBuildFromName(pluginName, pth)
|
||||
assert.Equal(t, exp, name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractFiles(t *testing.T) {
|
||||
@@ -52,28 +36,29 @@ func TestExtractFiles(t *testing.T) {
|
||||
pluginDir, del := setupFakePluginsDir(t)
|
||||
defer del()
|
||||
|
||||
archive := "testdata/grafana-simple-json-datasource-ec18fa4da8096a952608a7e4c7782b4260b41bcf.zip"
|
||||
archive := filepath.Join("testdata", "grafana-simple-json-datasource-ec18fa4da8096a952608a7e4c7782b4260b41bcf.zip")
|
||||
err := extractFiles(archive, "grafana-simple-json-datasource", pluginDir, false)
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
//File in zip has permissions 755
|
||||
fileInfo, err := os.Stat(pluginDir + "/grafana-simple-json-datasource/simple-plugin_darwin_amd64")
|
||||
assert.Nil(t, err)
|
||||
fileInfo, err := os.Stat(filepath.Join(pluginDir, "grafana-simple-json-datasource",
|
||||
"simple-plugin_darwin_amd64"))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "-rwxr-xr-x", fileInfo.Mode().String())
|
||||
|
||||
//File in zip has permission 755
|
||||
fileInfo, err = os.Stat(pluginDir + "/grafana-simple-json-datasource/simple-plugin_linux_amd64")
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "-rwxr-xr-x", fileInfo.Mode().String())
|
||||
|
||||
//File in zip has permission 644
|
||||
fileInfo, err = os.Stat(pluginDir + "/grafana-simple-json-datasource/simple-plugin_windows_amd64.exe")
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "-rw-r--r--", fileInfo.Mode().String())
|
||||
|
||||
//File in zip has permission 755
|
||||
fileInfo, err = os.Stat(pluginDir + "/grafana-simple-json-datasource/non-plugin-binary")
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "-rwxr-xr-x", fileInfo.Mode().String())
|
||||
})
|
||||
|
||||
@@ -82,10 +67,10 @@ func TestExtractFiles(t *testing.T) {
|
||||
defer del()
|
||||
|
||||
err := extractFiles("testdata/plugin-with-symlink.zip", "plugin-with-symlink", pluginDir, false)
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = os.Stat(pluginDir + "/plugin-with-symlink/text.txt")
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
_, err = os.Stat(pluginDir + "/plugin-with-symlink/symlink_to_txt")
|
||||
assert.NotNil(t, err)
|
||||
})
|
||||
@@ -96,10 +81,10 @@ func TestExtractFiles(t *testing.T) {
|
||||
defer del()
|
||||
|
||||
err := extractFiles("testdata/plugin-with-symlink.zip", "plugin-with-symlink", pluginDir, true)
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = os.Stat(pluginDir + "/plugin-with-symlink/symlink_to_txt")
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
fmt.Println(err)
|
||||
})
|
||||
}
|
||||
@@ -199,7 +184,7 @@ func TestSelectVersion(t *testing.T) {
|
||||
),
|
||||
"",
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "1.0.0", ver.Version)
|
||||
})
|
||||
|
||||
@@ -208,7 +193,7 @@ func TestSelectVersion(t *testing.T) {
|
||||
makePluginWithVersions(versionArg{Version: "2.0.0"}, versionArg{Version: "1.0.0"}),
|
||||
"",
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "2.0.0", ver.Version)
|
||||
})
|
||||
|
||||
@@ -220,7 +205,7 @@ func TestSelectVersion(t *testing.T) {
|
||||
),
|
||||
"1.0.0",
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "1.0.0", ver.Version)
|
||||
})
|
||||
}
|
||||
@@ -234,8 +219,8 @@ func setupFakePluginsDir(t *testing.T) (string, func()) {
|
||||
require.Nil(t, err)
|
||||
|
||||
return dirname, func() {
|
||||
err = os.RemoveAll(dirname)
|
||||
assert.Nil(t, err)
|
||||
err := os.RemoveAll(dirname)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user