2021-03-29 21:22:32 +00:00
|
|
|
// +build !race
|
|
|
|
|
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2021-04-29 18:32:41 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-03-29 21:22:32 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-04-29 18:32:41 +00:00
|
|
|
"github.com/hashicorp/vault/vault/diagnose"
|
2021-03-29 21:22:32 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2021-04-29 18:32:41 +00:00
|
|
|
func testOperatorDiagnoseCommand(tb testing.TB) *OperatorDiagnoseCommand {
|
2021-03-29 21:22:32 +00:00
|
|
|
tb.Helper()
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
2021-04-29 18:32:41 +00:00
|
|
|
return &OperatorDiagnoseCommand{
|
|
|
|
diagnose: diagnose.New(),
|
2021-03-29 21:22:32 +00:00
|
|
|
BaseCommand: &BaseCommand{
|
|
|
|
UI: ui,
|
|
|
|
},
|
2021-05-02 20:33:13 +00:00
|
|
|
skipEndEnd: true,
|
2021-03-29 21:22:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperatorDiagnoseCommand_Run(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
cases := []struct {
|
2021-04-29 18:32:41 +00:00
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expected []*diagnose.Result
|
2021-03-29 21:22:32 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"diagnose_ok",
|
|
|
|
[]string{
|
2021-04-06 23:40:43 +00:00
|
|
|
"-config", "./server/test-fixtures/config_diagnose_ok.hcl",
|
2021-03-29 21:22:32 +00:00
|
|
|
},
|
2021-04-29 18:32:41 +00:00
|
|
|
[]*diagnose.Result{
|
|
|
|
{
|
|
|
|
Name: "parse-config",
|
|
|
|
Status: diagnose.OkStatus,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "init-listeners",
|
|
|
|
Status: diagnose.WarningStatus,
|
|
|
|
Warnings: []string{
|
|
|
|
"TLS is disabled in a Listener config stanza.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "storage",
|
|
|
|
Status: diagnose.OkStatus,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "service-discovery",
|
|
|
|
Status: diagnose.OkStatus,
|
|
|
|
},
|
|
|
|
},
|
2021-03-29 21:22:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"diagnose_invalid_storage",
|
|
|
|
[]string{
|
|
|
|
"-config", "./server/test-fixtures/nostore_config.hcl",
|
|
|
|
},
|
2021-04-29 18:32:41 +00:00
|
|
|
[]*diagnose.Result{
|
|
|
|
{
|
|
|
|
Name: "parse-config",
|
|
|
|
Status: diagnose.OkStatus,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "init-listeners",
|
|
|
|
Status: diagnose.WarningStatus,
|
|
|
|
Warnings: []string{
|
|
|
|
"TLS is disabled in a Listener config stanza.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "storage",
|
|
|
|
Status: diagnose.ErrorStatus,
|
|
|
|
Message: "A storage backend must be specified",
|
|
|
|
},
|
|
|
|
},
|
2021-03-29 21:22:32 +00:00
|
|
|
},
|
2021-04-06 23:40:43 +00:00
|
|
|
{
|
|
|
|
"diagnose_listener_config_ok",
|
|
|
|
[]string{
|
|
|
|
"-config", "./server/test-fixtures/tls_config_ok.hcl",
|
|
|
|
},
|
2021-04-29 18:32:41 +00:00
|
|
|
[]*diagnose.Result{
|
|
|
|
{
|
|
|
|
Name: "parse-config",
|
|
|
|
Status: diagnose.OkStatus,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "init-listeners",
|
|
|
|
Status: diagnose.OkStatus,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "storage",
|
|
|
|
Status: diagnose.OkStatus,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "service-discovery",
|
|
|
|
Status: diagnose.OkStatus,
|
|
|
|
},
|
|
|
|
},
|
2021-04-06 23:40:43 +00:00
|
|
|
},
|
2021-04-28 15:55:18 +00:00
|
|
|
{
|
|
|
|
"diagnose_invalid_https_storage",
|
|
|
|
[]string{
|
|
|
|
"-config", "./server/test-fixtures/config_bad_https_storage.hcl",
|
|
|
|
},
|
2021-04-29 18:32:41 +00:00
|
|
|
[]*diagnose.Result{
|
|
|
|
{
|
|
|
|
Name: "parse-config",
|
|
|
|
Status: diagnose.OkStatus,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "init-listeners",
|
|
|
|
Status: diagnose.WarningStatus,
|
|
|
|
Warnings: []string{
|
|
|
|
"TLS is disabled in a Listener config stanza.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "storage",
|
|
|
|
Status: diagnose.ErrorStatus,
|
|
|
|
},
|
|
|
|
},
|
2021-04-28 15:55:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"diagnose_invalid_https_hastorage",
|
|
|
|
[]string{
|
|
|
|
"-config", "./server/test-fixtures/config_diagnose_hastorage_bad_https.hcl",
|
|
|
|
},
|
2021-04-29 18:32:41 +00:00
|
|
|
[]*diagnose.Result{
|
|
|
|
{
|
|
|
|
Name: "parse-config",
|
|
|
|
Status: diagnose.OkStatus,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "init-listeners",
|
|
|
|
Status: diagnose.WarningStatus,
|
|
|
|
Warnings: []string{
|
|
|
|
"TLS is disabled in a Listener config stanza.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "storage",
|
|
|
|
Status: diagnose.ErrorStatus,
|
2021-05-02 23:21:06 +00:00
|
|
|
Warnings: []string{
|
|
|
|
diagnose.AddrDNExistErr,
|
|
|
|
},
|
2021-04-29 18:32:41 +00:00
|
|
|
},
|
|
|
|
},
|
2021-04-28 15:55:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"diagnose_invalid_https_sr",
|
|
|
|
[]string{
|
|
|
|
"-config", "./server/test-fixtures/diagnose_bad_https_consul_sr.hcl",
|
|
|
|
},
|
2021-04-29 18:32:41 +00:00
|
|
|
[]*diagnose.Result{
|
|
|
|
{
|
|
|
|
Name: "parse-config",
|
|
|
|
Status: diagnose.OkStatus,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "init-listeners",
|
|
|
|
Status: diagnose.WarningStatus,
|
|
|
|
Warnings: []string{
|
|
|
|
"TLS is disabled in a Listener config stanza.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "storage",
|
|
|
|
Status: diagnose.OkStatus,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "service-discovery",
|
|
|
|
Status: diagnose.ErrorStatus,
|
|
|
|
Message: "failed to verify certificate: x509: certificate has expired or is not yet valid:",
|
2021-05-02 23:21:06 +00:00
|
|
|
Warnings: []string{
|
|
|
|
diagnose.DirAccessErr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"diagnose_direct_storage_access",
|
|
|
|
[]string{
|
|
|
|
"-config", "./server/test-fixtures/diagnose_ok_storage_direct_access.hcl",
|
|
|
|
},
|
|
|
|
[]*diagnose.Result{
|
|
|
|
{
|
|
|
|
Name: "parse-config",
|
|
|
|
Status: diagnose.OkStatus,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "init-listeners",
|
|
|
|
Status: diagnose.WarningStatus,
|
|
|
|
Warnings: []string{
|
|
|
|
"TLS is disabled in a Listener config stanza.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "storage",
|
|
|
|
Status: diagnose.WarningStatus,
|
|
|
|
Warnings: []string{
|
|
|
|
diagnose.DirAccessErr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "service-discovery",
|
|
|
|
Status: diagnose.OkStatus,
|
2021-04-29 18:32:41 +00:00
|
|
|
},
|
|
|
|
},
|
2021-04-28 15:55:18 +00:00
|
|
|
},
|
2021-03-29 21:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("validations", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
client, closer := testVaultServer(t)
|
|
|
|
defer closer()
|
|
|
|
|
2021-04-29 18:32:41 +00:00
|
|
|
cmd := testOperatorDiagnoseCommand(t)
|
2021-03-29 21:22:32 +00:00
|
|
|
cmd.client = client
|
|
|
|
|
2021-04-29 18:32:41 +00:00
|
|
|
cmd.Run(tc.args)
|
|
|
|
result := cmd.diagnose.Finalize(context.Background())
|
2021-03-29 21:22:32 +00:00
|
|
|
|
2021-04-29 18:32:41 +00:00
|
|
|
for i, exp := range tc.expected {
|
|
|
|
act := result.Children[i]
|
|
|
|
if err := compareResult(t, exp, act); err != nil {
|
|
|
|
t.Fatalf("%v", err)
|
2021-03-29 21:22:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-04-29 18:32:41 +00:00
|
|
|
|
|
|
|
func compareResult(t *testing.T, exp *diagnose.Result, act *diagnose.Result) error {
|
|
|
|
if exp.Name != act.Name {
|
|
|
|
return fmt.Errorf("names mismatch: %s vs %s", exp.Name, act.Name)
|
|
|
|
}
|
|
|
|
if exp.Status != act.Status {
|
2021-05-02 20:33:13 +00:00
|
|
|
if act.Status != diagnose.OkStatus {
|
|
|
|
return fmt.Errorf("section %s, status mismatch: %s vs %s, got error %s", exp.Name, exp.Status, act.Status, act.Message)
|
|
|
|
|
|
|
|
}
|
2021-04-29 18:32:41 +00:00
|
|
|
return fmt.Errorf("section %s, status mismatch: %s vs %s", exp.Name, exp.Status, act.Status)
|
|
|
|
}
|
|
|
|
if exp.Message != "" && exp.Message != act.Message && !strings.Contains(act.Message, exp.Message) {
|
|
|
|
return fmt.Errorf("section %s, message not found: %s in %s", exp.Name, exp.Message, act.Message)
|
|
|
|
}
|
|
|
|
if len(exp.Warnings) != len(act.Warnings) {
|
|
|
|
return fmt.Errorf("section %s, warning count mismatch: %d vs %d", exp.Name, len(exp.Warnings), len(act.Warnings))
|
|
|
|
}
|
|
|
|
for j := range exp.Warnings {
|
|
|
|
if !strings.Contains(act.Warnings[j], exp.Warnings[j]) {
|
|
|
|
return fmt.Errorf("section %s, warning message not found: %s in %s", exp.Name, exp.Warnings[j], act.Warnings[j])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(exp.Children) != len(act.Children) {
|
|
|
|
return fmt.Errorf("section %s, child count mismatch: %d vs %d", exp.Name, len(exp.Children), len(act.Children))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|