Skip to content

Commit

Permalink
Add available tests per test category in help
Browse files Browse the repository at this point in the history
  • Loading branch information
KaloyanTanev committed May 16, 2024
1 parent ddb95fd commit 3e6f480
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 18 deletions.
38 changes: 34 additions & 4 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@ 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 (
errTimeoutInterrupted = testResultError{errors.New("timeout/interrupted")}
errNoTicker = testResultError{errors.New("no ticker")}
)

const (
peersTestCategory = "peers"
beaconTestCategory = "beacon"
validatorTestCategory = "validator"
)

type testConfig struct {
OutputToml string
Quiet bool
Expand All @@ -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()))

Check warning on line 74 in cmd/test.go

View check run for this annotation

Codecov / codecov/patch

cmd/test.go#L73-L74

Added lines #L73 - L74 were not covered by tests
}

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")
Expand Down Expand Up @@ -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()...)
Expand Down
2 changes: 1 addition & 1 deletion cmd/testbeacon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cmd/testbeacon_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func TestBeaconTest(t *testing.T) {
},
},
Score: categoryScoreC,
CategoryName: "beacon",
CategoryName: beaconTestCategory,
},
expectedErr: "",
cleanup: func(t *testing.T, p string) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/testpeers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions cmd/testpeers_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}},
Expand Down Expand Up @@ -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{}},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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{}},
Expand Down
2 changes: 1 addition & 1 deletion cmd/testvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions cmd/testvalidator_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestValidatorTest(t *testing.T) {
},
},
Score: categoryScoreA,
CategoryName: "validator",
CategoryName: validatorTestCategory,
},
expectedErr: "",
},
Expand All @@ -80,7 +80,7 @@ func TestValidatorTest(t *testing.T) {
},
},
Score: categoryScoreC,
CategoryName: "validator",
CategoryName: validatorTestCategory,
},
expectedErr: "",
},
Expand All @@ -104,7 +104,7 @@ func TestValidatorTest(t *testing.T) {
},
},
Score: categoryScoreA,
CategoryName: "validator",
CategoryName: validatorTestCategory,
},
expectedErr: "",
},
Expand All @@ -121,7 +121,7 @@ func TestValidatorTest(t *testing.T) {
},
expected: testCategoryResult{
Score: categoryScoreC,
CategoryName: "validator",
CategoryName: validatorTestCategory,
},
expectedErr: "test case not supported",
},
Expand All @@ -143,7 +143,7 @@ func TestValidatorTest(t *testing.T) {
},
},
Score: categoryScoreA,
CategoryName: "validator",
CategoryName: validatorTestCategory,
},
expectedErr: "",
},
Expand All @@ -167,7 +167,7 @@ func TestValidatorTest(t *testing.T) {
},
},
Score: categoryScoreA,
CategoryName: "validator",
CategoryName: validatorTestCategory,
},
expectedErr: "",
cleanup: func(t *testing.T, p string) {
Expand Down

0 comments on commit 3e6f480

Please sign in to comment.