From 41f2a7732effb6e4ce66d4febb716c0935827d2d Mon Sep 17 00:00:00 2001 From: Hridoy Roy Date: Mon, 29 Mar 2021 14:22:32 -0700 Subject: [PATCH] Diagnose Stub Command Tests (#11180) * a few tests to the operator diagnose stub command * a few tests to the operator diagnose stub command * a few tests to the operator diagnose stub command * empty commit to fix circle ci permissions issue * empty commit to fix circle ci permissions issue --- command/operator_diagnose_test.go | 80 +++++++++++++++++++ .../server/test-fixtures/nostore_config.hcl | 17 ++++ 2 files changed, 97 insertions(+) create mode 100644 command/operator_diagnose_test.go create mode 100644 command/server/test-fixtures/nostore_config.hcl diff --git a/command/operator_diagnose_test.go b/command/operator_diagnose_test.go new file mode 100644 index 0000000000..d153d40701 --- /dev/null +++ b/command/operator_diagnose_test.go @@ -0,0 +1,80 @@ +// +build !race + +package command + +import ( + "strings" + "testing" + + "github.com/mitchellh/cli" +) + +func testOperatorDiagnoseCommand(tb testing.TB) (*cli.MockUi, *OperatorDiagnoseCommand) { + tb.Helper() + + ui := cli.NewMockUi() + return ui, &OperatorDiagnoseCommand{ + BaseCommand: &BaseCommand{ + UI: ui, + }, + } +} + +func TestOperatorDiagnoseCommand_Run(t *testing.T) { + t.Parallel() + + cases := []struct { + name string + args []string + outFragments []string + code int + }{ + { + "diagnose_ok", + []string{ + "-config", "./server/test-fixtures/config.hcl", + }, + []string{"Parse configuration\n\x1b[F\x1b[32m[ ok ]\x1b[0m Parse configuration\n[ ] Access storage\n\x1b[F\x1b[32m[ ok ]\x1b[0m Access storage\n"}, + 0, + }, + { + "diagnose_invalid_storage", + []string{ + "-config", "./server/test-fixtures/nostore_config.hcl", + }, + []string{"Parse configuration\n\x1b[F\x1b[32m[ ok ]\x1b[0m Parse configuration\n[ ] Access storage\n\x1b[F\x1b[31m[failed]\x1b[0m Access storage\nA storage backend must be specified\n"}, + 1, + }, + } + + 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() + + ui, cmd := testOperatorDiagnoseCommand(t) + cmd.client = client + + code := cmd.Run(tc.args) + if code != tc.code { + t.Errorf("%s: expected %d to be %d", tc.name, code, tc.code) + } + + combined := ui.OutputWriter.String() + ui.ErrorWriter.String() + + for _, outputFragment := range tc.outFragments { + if !strings.Contains(combined, outputFragment) { + t.Errorf("%s: expected %q to contain %q", tc.name, combined, outputFragment) + } + } + }) + } + }) +} diff --git a/command/server/test-fixtures/nostore_config.hcl b/command/server/test-fixtures/nostore_config.hcl new file mode 100644 index 0000000000..ad86e3dcee --- /dev/null +++ b/command/server/test-fixtures/nostore_config.hcl @@ -0,0 +1,17 @@ +disable_cache = true +disable_mlock = true + +ui = true + +listener "tcp" { + address = "127.0.0.1:443" + allow_stuff = true +} + +ha_backend "consul" { + bar = "baz" + advertise_addr = "snafu" + disable_clustering = "true" +} + +// No backend stanza in config!