2018-07-13 17:35:08 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-03-23 21:47:43 +00:00
|
|
|
"context"
|
2018-07-13 17:35:08 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
2018-07-13 17:35:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// testPluginDir creates a temporary directory suitable for holding plugins.
|
|
|
|
// This helper also resolves symlinks to make tests happy on OS X.
|
|
|
|
func testPluginDir(tb testing.TB) (string, func(tb testing.TB)) {
|
|
|
|
tb.Helper()
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "")
|
|
|
|
if err != nil {
|
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OSX tempdir are /var, but actually symlinked to /private/var
|
|
|
|
dir, err = filepath.EvalSymlinks(dir)
|
|
|
|
if err != nil {
|
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir, func(tb testing.TB) {
|
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// testPluginCreate creates a sample plugin in a tempdir and returns the shasum
|
|
|
|
// and filepath to the plugin.
|
|
|
|
func testPluginCreate(tb testing.TB, dir, name string) (string, string) {
|
|
|
|
tb.Helper()
|
|
|
|
|
|
|
|
pth := dir + "/" + name
|
2021-04-08 16:43:39 +00:00
|
|
|
if err := ioutil.WriteFile(pth, nil, 0o755); err != nil {
|
2018-07-13 17:35:08 +00:00
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(pth)
|
|
|
|
if err != nil {
|
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
h := sha256.New()
|
|
|
|
if _, err := io.Copy(h, f); err != nil {
|
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
|
|
|
sha256Sum := fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
|
|
|
|
return pth, sha256Sum
|
|
|
|
}
|
|
|
|
|
|
|
|
// testPluginCreateAndRegister creates a plugin and registers it in the catalog.
|
2018-11-07 01:21:24 +00:00
|
|
|
func testPluginCreateAndRegister(tb testing.TB, client *api.Client, dir, name string, pluginType consts.PluginType) (string, string) {
|
2018-07-13 17:35:08 +00:00
|
|
|
tb.Helper()
|
|
|
|
|
|
|
|
pth, sha256Sum := testPluginCreate(tb, dir, name)
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
if err := client.Sys().RegisterPluginWithContext(context.Background(), &api.RegisterPluginInput{
|
2018-07-13 17:35:08 +00:00
|
|
|
Name: name,
|
2018-11-07 01:21:24 +00:00
|
|
|
Type: pluginType,
|
2018-07-13 17:35:08 +00:00
|
|
|
Command: name,
|
|
|
|
SHA256: sha256Sum,
|
|
|
|
}); err != nil {
|
|
|
|
tb.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pth, sha256Sum
|
|
|
|
}
|