-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow delete-version-after to be reset to 0 (#8635)
- Loading branch information
1 parent
bbe4a0a
commit 7cb59dc
Showing
2 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package command | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/vault/api" | ||
"github.com/mitchellh/cli" | ||
) | ||
|
||
func testKVMetadataPutCommand(tb testing.TB) (*cli.MockUi, *KVMetadataPutCommand) { | ||
tb.Helper() | ||
|
||
ui := cli.NewMockUi() | ||
return ui, &KVMetadataPutCommand{ | ||
BaseCommand: &BaseCommand{ | ||
UI: ui, | ||
}, | ||
} | ||
} | ||
|
||
func TestKvMetadataPutCommandDeleteVersionAfter(t *testing.T) { | ||
client, closer := testVaultServer(t) | ||
defer closer() | ||
|
||
if err := client.Sys().Mount("kv/", &api.MountInput{ | ||
Type: "kv-v2", | ||
}); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
ui, cmd := testKVMetadataPutCommand(t) | ||
cmd.client = client | ||
|
||
// Set a limit of 1s first. | ||
code := cmd.Run([]string{"-delete-version-after=1s", "kv/secret/my-secret"}) | ||
if code != 0 { | ||
t.Errorf("expected %d but received %d", 0, code) | ||
} | ||
|
||
combined := ui.OutputWriter.String() + ui.ErrorWriter.String() | ||
if !strings.Contains(combined, "Success! Data written to: kv/metadata/secret/my-secret\n") { | ||
t.Errorf("expected %q but received %q", "Success! Data written to: kv/metadata/secret/my-secret\n", combined) | ||
} | ||
|
||
secret, err := client.Logical().Read("kv/metadata/secret/my-secret") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if secret.Data["delete_version_after"] != "1s" { | ||
t.Fatalf("expected 1s but received %q", secret.Data["delete_version_after"]) | ||
} | ||
|
||
// Now verify that we can return it to 0s. | ||
ui, cmd = testKVMetadataPutCommand(t) | ||
cmd.client = client | ||
|
||
// Set a limit of 1s first. | ||
code = cmd.Run([]string{"-delete-version-after=0", "kv/secret/my-secret"}) | ||
if code != 0 { | ||
t.Errorf("expected %d but received %d", 0, code) | ||
} | ||
|
||
combined = ui.OutputWriter.String() + ui.ErrorWriter.String() | ||
if !strings.Contains(combined, "Success! Data written to: kv/metadata/secret/my-secret\n") { | ||
t.Errorf("expected %q but received %q", "Success! Data written to: kv/metadata/secret/my-secret\n", combined) | ||
} | ||
|
||
secret, err = client.Logical().Read("kv/metadata/secret/my-secret") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if secret.Data["delete_version_after"] != "0s" { | ||
t.Fatalf("expected 0s but received %q", secret.Data["delete_version_after"]) | ||
} | ||
} |