From 1fd40a48d976ace061b5636094d3d71a43da8bc7 Mon Sep 17 00:00:00 2001 From: bergquist Date: Wed, 10 Jan 2018 15:07:04 +0100 Subject: [PATCH] supports windows compatible plugin binaries --- pkg/plugins/datasource_plugin.go | 15 +++++++++--- pkg/plugins/datasource_plugin_test.go | 35 ++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/pkg/plugins/datasource_plugin.go b/pkg/plugins/datasource_plugin.go index 2c1faf2581a..a28111bdcff 100644 --- a/pkg/plugins/datasource_plugin.go +++ b/pkg/plugins/datasource_plugin.go @@ -65,8 +65,14 @@ var handshakeConfig = plugin.HandshakeConfig{ MagicCookieValue: "datasource", } -func buildExecutablePath(pluginDir, executable, os, arch string) string { - return path.Join(pluginDir, fmt.Sprintf("%s_%s_%s", executable, strings.ToLower(os), strings.ToLower(arch))) +func composeBinaryName(executable, os, arch string) string { + var extension string + os = strings.ToLower(os) + if os == "windows" { + extension = ".exe" + } + + return fmt.Sprintf("%s_%s_%s%s", executable, os, strings.ToLower(arch), extension) } func (p *DataSourcePlugin) initBackendPlugin(ctx context.Context, log log.Logger) error { @@ -81,12 +87,13 @@ func (p *DataSourcePlugin) initBackendPlugin(ctx context.Context, log log.Logger } func (p *DataSourcePlugin) spawnSubProcess() error { - cmd := buildExecutablePath(p.PluginDir, p.Executable, runtime.GOOS, runtime.GOARCH) + cmd := composeBinaryName(p.Executable, runtime.GOOS, runtime.GOARCH) + fullpath := path.Join(p.PluginDir, cmd) p.client = plugin.NewClient(&plugin.ClientConfig{ HandshakeConfig: handshakeConfig, Plugins: map[string]plugin.Plugin{p.Id: &shared.TsdbPluginImpl{}}, - Cmd: exec.Command(cmd), + Cmd: exec.Command(fullpath), AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC}, Logger: LogWrapper{Logger: p.log}, }) diff --git a/pkg/plugins/datasource_plugin_test.go b/pkg/plugins/datasource_plugin_test.go index a9a86e73d88..147f0310f5c 100644 --- a/pkg/plugins/datasource_plugin_test.go +++ b/pkg/plugins/datasource_plugin_test.go @@ -1,12 +1,35 @@ package plugins -import "testing" +import ( + "testing" +) -func TestExecutablePathBuilder(t *testing.T) { +func TestComposeBinaryName(t *testing.T) { + tests := []struct { + name string + os string + arch string - have := buildExecutablePath("/var/grafana/plugins/grafana-simple-json-datasource", "simple-json", "linux", "amd64") - want := `/var/grafana/plugins/grafana-simple-json-datasource/simple-json_linux_amd64` - if have != want { - t.Errorf("expected %s got %s", want, have) + expectedPath string + }{ + { + name: "simple-json", + os: "linux", + arch: "amd64", + expectedPath: `simple-json_linux_amd64`, + }, + { + name: "simple-json", + os: "windows", + arch: "amd64", + expectedPath: `simple-json_windows_amd64.exe`, + }, + } + + for _, v := range tests { + have := composeBinaryName(v.name, v.os, v.arch) + if have != v.expectedPath { + t.Errorf("expected %s got %s", v.expectedPath, have) + } } }