From 3e6f4804b553050b63a4f334a23a64a496f8e9eb Mon Sep 17 00:00:00 2001 From: Kaloyan Tanev Date: Thu, 16 May 2024 13:09:34 +0300 Subject: [PATCH] Add available tests per test category in help --- cmd/test.go | 38 ++++++++++++++++++++++++++---- cmd/testbeacon.go | 2 +- cmd/testbeacon_internal_test.go | 2 +- cmd/testpeers.go | 2 +- cmd/testpeers_internal_test.go | 8 +++---- cmd/testvalidator.go | 2 +- cmd/testvalidator_internal_test.go | 12 +++++----- 7 files changed, 48 insertions(+), 18 deletions(-) diff --git a/cmd/test.go b/cmd/test.go index 695d8c0eb..c4167100a 100644 --- a/cmd/test.go +++ b/cmd/test.go @@ -16,9 +16,11 @@ import ( "github.com/pelletier/go-toml/v2" "github.com/spf13/cobra" + "golang.org/x/exp/maps" "github.com/obolnetwork/charon/app/errors" "github.com/obolnetwork/charon/app/log" + "github.com/obolnetwork/charon/app/z" ) var ( @@ -26,6 +28,12 @@ var ( errNoTicker = testResultError{errors.New("no ticker")} ) +const ( + peersTestCategory = "peers" + beaconTestCategory = "beacon" + validatorTestCategory = "validator" +) + type testConfig struct { OutputToml string Quiet bool @@ -47,11 +55,33 @@ func newTestCmd(cmds ...*cobra.Command) *cobra.Command { func bindTestFlags(cmd *cobra.Command, config *testConfig) { cmd.Flags().StringVar(&config.OutputToml, "output-toml", "", "File path to which output can be written in TOML format.") - cmd.Flags().StringSliceVar(&config.TestCases, "test-cases", nil, "List of comma separated names of tests to be exeucted.") + cmd.Flags().StringSliceVar(&config.TestCases, "test-cases", nil, fmt.Sprintf("List of comma separated names of tests to be exeucted. Available tests are: %v", listTestCases(cmd))) cmd.Flags().DurationVar(&config.Timeout, "timeout", 5*time.Minute, "Execution timeout for all tests.") cmd.Flags().BoolVar(&config.Quiet, "quiet", false, "Do not print test results to stdout.") } +func listTestCases(cmd *cobra.Command) []string { + var testCaseNames []testCaseName + switch cmd.Name() { + case peersTestCategory: + testCaseNames = maps.Keys(supportedPeerTestCases()) + testCaseNames = append(testCaseNames, maps.Keys(supportedSelfTestCases())...) + case beaconTestCategory: + testCaseNames = maps.Keys(supportedBeaconTestCases()) + case validatorTestCategory: + testCaseNames = maps.Keys(supportedValidatorTestCases()) + default: + log.Warn(cmd.Context(), "Unknown command for listing test cases", nil, z.Str("name", cmd.Name())) + } + + var stringNames []string + for _, tcn := range testCaseNames { + stringNames = append(stringNames, tcn.name) + } + + return stringNames +} + func mustOutputToFileOnQuiet(cmd *cobra.Command) error { if cmd.Flag("quiet").Changed && !cmd.Flag("output-toml").Changed { return errors.New("on --quiet, an --output-toml is required") @@ -160,11 +190,11 @@ func writeResultToWriter(res testCategoryResult, w io.Writer) error { var lines []string switch res.CategoryName { - case "peers": + case peersTestCategory: lines = append(lines, peersASCII()...) - case "beacon": + case beaconTestCategory: lines = append(lines, beaconASCII()...) - case "validator": + case validatorTestCategory: lines = append(lines, validatorASCII()...) default: lines = append(lines, categoryDefaultASCII()...) diff --git a/cmd/testbeacon.go b/cmd/testbeacon.go index 1139cd7a5..39b3e1434 100644 --- a/cmd/testbeacon.go +++ b/cmd/testbeacon.go @@ -107,7 +107,7 @@ func runTestBeacon(ctx context.Context, w io.Writer, cfg testBeaconConfig) (err } res := testCategoryResult{ - CategoryName: "beacon", + CategoryName: beaconTestCategory, Targets: testResults, ExecutionTime: execTime, Score: score, diff --git a/cmd/testbeacon_internal_test.go b/cmd/testbeacon_internal_test.go index 2b5a962b1..56dcc82b9 100644 --- a/cmd/testbeacon_internal_test.go +++ b/cmd/testbeacon_internal_test.go @@ -205,7 +205,7 @@ func TestBeaconTest(t *testing.T) { }, }, Score: categoryScoreC, - CategoryName: "beacon", + CategoryName: beaconTestCategory, }, expectedErr: "", cleanup: func(t *testing.T, p string) { diff --git a/cmd/testpeers.go b/cmd/testpeers.go index 42dee3d34..305278eef 100644 --- a/cmd/testpeers.go +++ b/cmd/testpeers.go @@ -298,7 +298,7 @@ func runTestPeers(ctx context.Context, w io.Writer, conf testPeersConfig) error } res := testCategoryResult{ - CategoryName: "peers", + CategoryName: peersTestCategory, Targets: testResults, ExecutionTime: execTime, Score: score, diff --git a/cmd/testpeers_internal_test.go b/cmd/testpeers_internal_test.go index 04b7787a5..9ce10f8c8 100644 --- a/cmd/testpeers_internal_test.go +++ b/cmd/testpeers_internal_test.go @@ -67,7 +67,7 @@ func TestPeersTest(t *testing.T) { }, }, expected: testCategoryResult{ - CategoryName: "peers", + CategoryName: peersTestCategory, Targets: map[string][]testResult{ "self": { {Name: "libp2pTCPPortOpenTest", Verdict: testVerdictOk, Measurement: "", Suggestion: "", Error: testResultError{}}, @@ -131,7 +131,7 @@ func TestPeersTest(t *testing.T) { Log: log.DefaultConfig(), }, expected: testCategoryResult{ - CategoryName: "peers", + CategoryName: peersTestCategory, Targets: map[string][]testResult{ "self": { {Name: "libp2pTCPPortOpenTest", Verdict: testVerdictOk, Measurement: "", Suggestion: "", Error: testResultError{}}, @@ -186,7 +186,7 @@ func TestPeersTest(t *testing.T) { Log: log.DefaultConfig(), }, expected: testCategoryResult{ - CategoryName: "peers", + CategoryName: peersTestCategory, Targets: map[string][]testResult{ "inexpensive-farm - enr:-HW4QBHlcyD3fYWUMADiOv4OxODaL5wJG0a7P7d_ltu4VZe1MibZ1N-twFaoaq0BoCtXcY71etxLJGeEZT5p3XCO6GOAgmlkgnY0iXNlY3AyNTZrMaEDI2HRUlVBag__njkOWEEQRLlC9ylIVCrIXOuNBSlrx6o": { {Name: "ping", Verdict: testVerdictFail, Measurement: "", Suggestion: "", Error: errTimeoutInterrupted}, @@ -219,7 +219,7 @@ func TestPeersTest(t *testing.T) { Log: log.DefaultConfig(), }, expected: testCategoryResult{ - CategoryName: "peers", + CategoryName: peersTestCategory, Targets: map[string][]testResult{ "self": { {Name: "libp2pTCPPortOpenTest", Verdict: testVerdictOk, Measurement: "", Suggestion: "", Error: testResultError{}}, diff --git a/cmd/testvalidator.go b/cmd/testvalidator.go index 364d0b88d..46bf547ed 100644 --- a/cmd/testvalidator.go +++ b/cmd/testvalidator.go @@ -97,7 +97,7 @@ func runTestValidator(ctx context.Context, w io.Writer, cfg testValidatorConfig) } res := testCategoryResult{ - CategoryName: "validator", + CategoryName: validatorTestCategory, Targets: testResults, ExecutionTime: execTime, Score: score, diff --git a/cmd/testvalidator_internal_test.go b/cmd/testvalidator_internal_test.go index 4476ed8ea..88ff1a8fb 100644 --- a/cmd/testvalidator_internal_test.go +++ b/cmd/testvalidator_internal_test.go @@ -58,7 +58,7 @@ func TestValidatorTest(t *testing.T) { }, }, Score: categoryScoreA, - CategoryName: "validator", + CategoryName: validatorTestCategory, }, expectedErr: "", }, @@ -80,7 +80,7 @@ func TestValidatorTest(t *testing.T) { }, }, Score: categoryScoreC, - CategoryName: "validator", + CategoryName: validatorTestCategory, }, expectedErr: "", }, @@ -104,7 +104,7 @@ func TestValidatorTest(t *testing.T) { }, }, Score: categoryScoreA, - CategoryName: "validator", + CategoryName: validatorTestCategory, }, expectedErr: "", }, @@ -121,7 +121,7 @@ func TestValidatorTest(t *testing.T) { }, expected: testCategoryResult{ Score: categoryScoreC, - CategoryName: "validator", + CategoryName: validatorTestCategory, }, expectedErr: "test case not supported", }, @@ -143,7 +143,7 @@ func TestValidatorTest(t *testing.T) { }, }, Score: categoryScoreA, - CategoryName: "validator", + CategoryName: validatorTestCategory, }, expectedErr: "", }, @@ -167,7 +167,7 @@ func TestValidatorTest(t *testing.T) { }, }, Score: categoryScoreA, - CategoryName: "validator", + CategoryName: validatorTestCategory, }, expectedErr: "", cleanup: func(t *testing.T, p string) {