vault/command/plugin_reload_test.go
helenfufu abac619e0a
Vault 36295 Improve plugin mgmt ux in api and cli (#30811)
* cli: only set default command parameter to plugin name if sha256 is provided

* api: write warnings to RegisterPluginResponse, propagate up to cli

* api: filter out 'Endpoint replaced the value of these parameters' warning before returning in RegisterPluginWithContext

* docs

* add TODO on filtering that links to api type parameter deprecation ticket

* fix tests

* allocate filteredWarning slice only if there are warnings

* improve deferred resp close and early error return conditionals in RegisterPluginWithContext

* refer to sha256 as cli option -sha256 in command cli usage

* break up ui error lines for sha256 and version flag check

* consolidate if statements for sha256 and command, oci_image check in cli

* consolidate if statements for sha256 and command, oci_image check in api

* new RegisterPluginV2 and RegisterPluginWithContextV2 api client functions for backward compatibility

* add changelog

* more descriptive changelog

* rename RegisterPluginV2 to RegisterPluginDetailed and RegisterPluginWithContextV2 to RegisterPluginWithContextDetailed

* return nil, nil if no warnings to preserve status code

* fix eof from decoding (check if no content before decoding)

* doc for RegisterPluginResponse

* only validate plugin.Command in plugin catalog set for downloaded and binary plugins, which rely on plugin.Command input; extracted artifact plugins don't rely on plugin.Command input

* Update website/content/api-docs/system/plugins-catalog.mdx

Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com>

* Update website/content/api-docs/system/plugins-catalog.mdx

Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com>

* Update website/content/api-docs/system/plugins-catalog.mdx

Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com>

* Update website/content/docs/commands/plugin/register.mdx

Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com>

* Update website/content/docs/commands/plugin/register.mdx

Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com>

* Update website/content/docs/commands/plugin/register.mdx

Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com>

* Update website/content/docs/commands/plugin/register.mdx

Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com>

* move up enterprise note on plugin register command doc

* [DOCS] Editorial suggestions for PR #30811 (#31111)

* suggestions

* move common reqs to a partial

* fix typo

* tweak reqs

* Update website/content/partials/plugins/prepare-plugin.mdx

Co-authored-by: helenfufu <25168806+helenfufu@users.noreply.github.com>

* Update website/content/partials/plugins/prepare-plugin.mdx

Co-authored-by: helenfufu <25168806+helenfufu@users.noreply.github.com>

* Update website/content/partials/plugins/prepare-plugin.mdx

Co-authored-by: helenfufu <25168806+helenfufu@users.noreply.github.com>

* tweak feedback

* remove deprecation

* Update website/content/partials/plugins/common-requirements.mdx

Co-authored-by: helenfufu <25168806+helenfufu@users.noreply.github.com>

* save

* Update website/content/docs/plugins/rollback.mdx

Co-authored-by: helenfufu <25168806+helenfufu@users.noreply.github.com>

* Update website/content/docs/plugins/upgrade.mdx

Co-authored-by: helenfufu <25168806+helenfufu@users.noreply.github.com>

* fix formatting

---------

Co-authored-by: helenfufu <25168806+helenfufu@users.noreply.github.com>

---------

Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com>
2025-06-30 10:00:54 -07:00

181 lines
3.7 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package command
import (
"strings"
"testing"
"github.com/hashicorp/cli"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/helper/testhelpers/corehelpers"
)
func testPluginReloadCommand(tb testing.TB) (*cli.MockUi, *PluginReloadCommand) {
tb.Helper()
ui := cli.NewMockUi()
return ui, &PluginReloadCommand{
BaseCommand: &BaseCommand{
UI: ui,
},
}
}
func testPluginReloadStatusCommand(tb testing.TB) (*cli.MockUi, *PluginReloadStatusCommand) {
tb.Helper()
ui := cli.NewMockUi()
return ui, &PluginReloadStatusCommand{
BaseCommand: &BaseCommand{
UI: ui,
},
}
}
func TestPluginReloadCommand_Run(t *testing.T) {
t.Parallel()
cases := []struct {
name string
args []string
out string
code int
}{
{
"not_enough_args",
nil,
"No plugins specified, must specify exactly one of -plugin or -mounts",
1,
},
{
"too_many_args",
[]string{"-plugin", "foo", "-mounts", "bar"},
"Must specify exactly one of -plugin or -mounts",
1,
},
{
"type_and_mounts_mutually_exclusive",
[]string{"-mounts", "bar", "-type", "secret"},
"Cannot specify -type with -mounts",
1,
},
{
"invalid_type",
[]string{"-plugin", "bar", "-type", "unsupported"},
"Error parsing -type as a plugin type",
1,
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
client, closer := testVaultServer(t)
defer closer()
ui, cmd := testPluginReloadCommand(t)
cmd.client = client
args := append([]string{}, tc.args...)
code := cmd.Run(args)
if code != tc.code {
t.Errorf("expected %d to be %d", code, tc.code)
}
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
if !strings.Contains(combined, tc.out) {
t.Errorf("expected %q to contain %q", combined, tc.out)
}
})
}
t.Run("integration", func(t *testing.T) {
t.Parallel()
pluginDir := corehelpers.MakeTestPluginDir(t)
client, _, closer := testVaultServerPluginDir(t, pluginDir)
defer closer()
pluginName := "my-plugin"
_, sha256Sum := testPluginCreateAndRegister(t, client, pluginDir, pluginName, api.PluginTypeCredential, "")
ui, cmd := testPluginReloadCommand(t)
cmd.client = client
resp, err := client.Sys().RegisterPluginDetailed(&api.RegisterPluginInput{
Name: pluginName,
Type: api.PluginTypeCredential,
Command: pluginName,
SHA256: sha256Sum,
})
if err != nil {
t.Fatal(err)
}
if len(resp.Warnings) > 0 {
t.Errorf("expected no warnings, got: %v", resp.Warnings)
}
code := cmd.Run([]string{
"-plugin", pluginName,
})
if exp := 0; code != exp {
t.Errorf("expected %d to be %d", code, exp)
}
expected := "Success! Reloaded plugin: "
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
if !strings.Contains(combined, expected) {
t.Errorf("expected %q to contain %q", combined, expected)
}
})
}
func TestPluginReloadStatusCommand_Run(t *testing.T) {
t.Parallel()
cases := []struct {
name string
args []string
out string
code int
}{
{
"not_enough_args",
nil,
"Not enough arguments",
1,
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
client, closer := testVaultServer(t)
defer closer()
ui, cmd := testPluginReloadStatusCommand(t)
cmd.client = client
args := append([]string{}, tc.args...)
code := cmd.Run(args)
if code != tc.code {
t.Errorf("expected %d to be %d", code, tc.code)
}
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
if !strings.Contains(combined, tc.out) {
t.Errorf("expected %q to contain %q", combined, tc.out)
}
})
}
}