Normalize format output for vault status [VAULT-508] (#9976)

* normalize format output for vault status

* interim commit

* interim commit

* make formatting idiomatic

* clean up comments

* added formatting test

* updated comments in format test to match godocs

Co-authored-by: HridoyRoy <hridoyroy@Hridoys-MBP.hitronhub.home>
Co-authored-by: HridoyRoy <hridoyroy@Hridoys-MacBook-Pro.local>
This commit is contained in:
Hridoy Roy
2020-09-23 10:30:01 -07:00
committed by GitHub
parent d23bd7ae9b
commit d5c20be2dc
3 changed files with 255 additions and 78 deletions

View File

@@ -2,6 +2,7 @@ package command
import (
"bytes"
"fmt"
"os"
"strings"
"testing"
@@ -69,6 +70,8 @@ func TestYamlFormatter(t *testing.T) {
func TestTableFormatter(t *testing.T) {
os.Setenv(EnvVaultFormat, "table")
ui := mockUi{t: t}
// Testing secret formatting
s := api.Secret{Data: map[string]interface{}{"k": "something"}}
if err := outputWithFormat(ui, &s, &s); err != 0 {
t.Fatal(err)
@@ -78,6 +81,145 @@ func TestTableFormatter(t *testing.T) {
}
}
// TestStatusFormat tests to verify that the embedded struct
// SealStatusOutput ignores omitEmpty fields and prints out
// fields in the embedded struct explicitly. It also checks the spacing,
// indentation, and delimiters of table formatting explicitly.
func TestStatusFormat(t *testing.T) {
ui := mockUi{t: t}
os.Setenv(EnvVaultFormat, "table")
statusHA := getMockStatusData(false)
statusOmitEmpty := getMockStatusData(true)
// Testing that HA fields are formatted properly for table.
// All fields (including new HA fields) are expected
if err := outputWithFormat(ui, nil, statusHA); err != 0 {
t.Fatal(err)
}
expectedOutputString :=
`Key Value
--- -----
Recovery Seal Type type
Initialized true
Sealed true
Total Recovery Shares 2
Threshold 1
Unseal Progress 3/1
Unseal Nonce nonce
Seal Migration in Progress true
Version version
Storage Type storage type
Cluster Name cluster name
Cluster ID cluster id
HA Enabled true
Raft Committed Index 3
Raft Applied Index 4
Last WAL 2`
if expectedOutputString != output {
fmt.Printf("%s\n%+v\n %s\n%+v\n", "output found was: ", output, "versus", expectedOutputString)
t.Fatal("format output for status does not match expected format. Check print statements above.")
}
// Testing that omitEmpty fields are omitted from status
// no HA fields are expected, except HA Enabled
if err := outputWithFormat(ui, nil, statusOmitEmpty); err != 0 {
t.Fatal(err)
}
expectedOutputString =
`Key Value
--- -----
Recovery Seal Type type
Initialized true
Sealed true
Total Recovery Shares 2
Threshold 1
Unseal Progress 3/1
Unseal Nonce nonce
Seal Migration in Progress true
Version version
Storage Type n/a
HA Enabled false`
if expectedOutputString != output {
fmt.Printf("%s\n%+v\n %s\n%+v\n", "output found was: ", output, "versus", expectedOutputString)
t.Fatal("format output for status does not match expected format. Check print statements above.")
}
}
// getMockStatusData outputs a SealStatusOutput struct from format.go to be used
// for testing. The emptyfields parameter specifies whether the struct will be
// initialized with all the omitempty fields as empty or not.
func getMockStatusData(emptyFields bool) SealStatusOutput {
var status SealStatusOutput
var sealStatusResponseMock api.SealStatusResponse
if !emptyFields {
sealStatusResponseMock = api.SealStatusResponse{
Type: "type",
Initialized: true,
Sealed: true,
T: 1,
N: 2,
Progress: 3,
Nonce: "nonce",
Version: "version",
Migration: true,
ClusterName: "cluster name",
ClusterID: "cluster id",
RecoverySeal: true,
StorageType: "storage type",
}
// must initialize this struct without explicit field names due to embedding
status = SealStatusOutput{
sealStatusResponseMock,
true, // HAEnabled
true, // IsSelf
"leader address", // LeaderAddress
"leader cluster address", // LeaderClusterAddress
true, // PerfStandby
1, // PerfStandbyLastRemoteWAL
2, // LastWAL
3, // RaftCommittedIndex
4, // RaftAppliedIndex
}
} else {
sealStatusResponseMock = api.SealStatusResponse{
Type: "type",
Initialized: true,
Sealed: true,
T: 1,
N: 2,
Progress: 3,
Nonce: "nonce",
Version: "version",
Migration: true,
ClusterName: "",
ClusterID: "",
RecoverySeal: true,
StorageType: "",
}
// must initialize this struct without explicit field names due to embedding
status = SealStatusOutput{
sealStatusResponseMock,
false, // HAEnabled
false, // IsSelf
"", // LeaderAddress
"", // LeaderClusterAddress
false, // PerfStandby
0, // PerfStandbyLastRemoteWAL
0, // LastWAL
0, // RaftCommittedIndex
0, // RaftAppliedIndex
}
}
return status
}
func Test_Format_Parsing(t *testing.T) {
defer func() {
os.Setenv(EnvVaultCLINoColor, "")