CLI: Tune plugin version for auth/secret mounts (#17277)

* Add -plugin-version flag to vault auth/secrets tune
* CLI tests for auth/secrets tune
* CLI test for plugin register
* Plugin catalog listing bug where plugins of different type with the same name could be double counted
* Use constant for -plugin-version flag name
This commit is contained in:
Tom Proctor
2022-09-22 20:55:46 +01:00
committed by GitHub
parent 6fc6bb1bb5
commit 21d13633d7
13 changed files with 207 additions and 54 deletions

View File

@@ -1,6 +1,8 @@
package command
import (
"reflect"
"sort"
"strings"
"testing"
@@ -124,6 +126,75 @@ func TestPluginRegisterCommand_Run(t *testing.T) {
}
})
t.Run("integration with version", func(t *testing.T) {
t.Parallel()
pluginDir, cleanup := vault.MakeTestPluginDir(t)
defer cleanup(t)
client, _, closer := testVaultServerPluginDir(t, pluginDir)
defer closer()
const pluginName = "my-plugin"
versions := []string{"v1.0.0", "v2.0.1"}
_, sha256Sum := testPluginCreate(t, pluginDir, pluginName)
types := []consts.PluginType{consts.PluginTypeCredential, consts.PluginTypeDatabase, consts.PluginTypeSecrets}
for _, typ := range types {
for _, version := range versions {
ui, cmd := testPluginRegisterCommand(t)
cmd.client = client
code := cmd.Run([]string{
"-version=" + version,
"-sha256=" + sha256Sum,
typ.String(),
pluginName,
})
if exp := 0; code != exp {
t.Errorf("expected %d to be %d", code, exp)
}
expected := "Success! Registered plugin: my-plugin"
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
if !strings.Contains(combined, expected) {
t.Errorf("expected %q to contain %q", combined, expected)
}
}
}
resp, err := client.Sys().ListPlugins(&api.ListPluginsInput{
Type: consts.PluginTypeUnknown,
})
if err != nil {
t.Fatal(err)
}
found := make(map[consts.PluginType]int)
versionsFound := make(map[consts.PluginType][]string)
for _, p := range resp.Details {
if p.Name == pluginName {
typ, err := consts.ParsePluginType(p.Type)
if err != nil {
t.Fatal(err)
}
found[typ]++
versionsFound[typ] = append(versionsFound[typ], p.Version)
}
}
for _, typ := range types {
if found[typ] != 2 {
t.Fatalf("expected %q to be found 2 times, but found it %d times for %s type in %#v", pluginName, found[typ], typ.String(), resp.Details)
}
sort.Strings(versions)
sort.Strings(versionsFound[typ])
if !reflect.DeepEqual(versions, versionsFound[typ]) {
t.Fatalf("expected %v versions but got %v", versions, versionsFound[typ])
}
}
})
t.Run("communication_failure", func(t *testing.T) {
t.Parallel()