Skip to content

Commit

Permalink
cscli: refactor hubtests / extract methods run(), coverage()
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Jun 13, 2024
1 parent 4e09ae2 commit dce0528
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 162 deletions.
14 changes: 7 additions & 7 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ linters-settings:

gocognit:
# lower this after refactoring
min-complexity: 128
min-complexity: 118

govet:
enable-all: true
Expand All @@ -24,7 +24,7 @@ linters-settings:

maintidx:
# raise this after refactoring
under: 11
under: 16

misspell:
locale: US
Expand Down Expand Up @@ -473,6 +473,11 @@ issues:
- canonicalheader
path: pkg/apiserver/middlewares/v1/tls_auth.go

# tolerate complex functions in tests for now
- linters:
- maintidx
path: "(.+)_test.go"

# tolerate long functions in tests
- linters:
- revive
Expand Down Expand Up @@ -512,11 +517,6 @@ issues:
path: "cmd/crowdsec-cli/utils.go"
text: "deep-exit: .*"

- linters:
- revive
path: "cmd/crowdsec-cli/hubtest.go"
text: "deep-exit: .*"

- linters:
- revive
path: "pkg/leakybucket/overflows.go"
Expand Down
319 changes: 164 additions & 155 deletions cmd/crowdsec-cli/hubtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,44 @@ cscli hubtest create my-scenario-test --parsers crowdsecurity/nginx --scenarios
return cmd
}


func (cli *cliHubTest) run(runAll bool, NucleiTargetHost string, AppSecHost string, args []string) error {
cfg := cli.cfg()

if !runAll && len(args) == 0 {
return errors.New("please provide test to run or --all flag")
}
hubPtr.NucleiTargetHost = NucleiTargetHost
hubPtr.AppSecHost = AppSecHost
if runAll {
if err := hubPtr.LoadAllTests(); err != nil {
return fmt.Errorf("unable to load all tests: %+v", err)
}
} else {
for _, testName := range args {
_, err := hubPtr.LoadTestItem(testName)
if err != nil {
return fmt.Errorf("unable to load test '%s': %w", testName, err)
}

Check warning on line 255 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L238-L255

Added lines #L238 - L255 were not covered by tests
}
}

// set timezone to avoid DST issues
os.Setenv("TZ", "UTC")
for _, test := range hubPtr.Tests {
if cfg.Cscli.Output == "human" {
log.Infof("Running test '%s'", test.Name)
}
err := test.Run()
if err != nil {
log.Errorf("running test '%s' failed: %+v", test.Name, err)
}

Check warning on line 268 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L260-L268

Added lines #L260 - L268 were not covered by tests
}

return nil

Check warning on line 271 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L271

Added line #L271 was not covered by tests
}


func (cli *cliHubTest) NewRunCmd() *cobra.Command {
var (
noClean bool
Expand All @@ -247,41 +285,8 @@ func (cli *cliHubTest) NewRunCmd() *cobra.Command {
Use: "run",
Short: "run [test_name]",
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
cfg := cli.cfg()

if !runAll && len(args) == 0 {
_ = cmd.Help()
return errors.New("please provide test to run or --all flag")
}
hubPtr.NucleiTargetHost = NucleiTargetHost
hubPtr.AppSecHost = AppSecHost
if runAll {
if err := hubPtr.LoadAllTests(); err != nil {
return fmt.Errorf("unable to load all tests: %+v", err)
}
} else {
for _, testName := range args {
_, err := hubPtr.LoadTestItem(testName)
if err != nil {
return fmt.Errorf("unable to load test '%s': %w", testName, err)
}
}
}

// set timezone to avoid DST issues
os.Setenv("TZ", "UTC")
for _, test := range hubPtr.Tests {
if cfg.Cscli.Output == "human" {
log.Infof("Running test '%s'", test.Name)
}
err := test.Run()
if err != nil {
log.Errorf("running test '%s' failed: %+v", test.Name, err)
}
}

return nil
RunE: func(_ *cobra.Command, args []string) error {
return cli.run(runAll, NucleiTargetHost, AppSecHost, args)

Check warning on line 289 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L289

Added line #L289 was not covered by tests
},
PersistentPostRunE: func(_ *cobra.Command, _ []string) error {
cfg := cli.cfg()
Expand Down Expand Up @@ -493,140 +498,144 @@ func (cli *cliHubTest) NewListCmd() *cobra.Command {
return cmd
}

func (cli *cliHubTest) NewCoverageCmd() *cobra.Command {
var (
showParserCov bool
showScenarioCov bool
showOnlyPercent bool
showAppsecCov bool
)
func (cli *cliHubTest) coverage(showScenarioCov bool, showParserCov bool, showAppsecCov bool, showOnlyPercent bool) error {
cfg := cli.cfg()

Check warning on line 502 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L501-L502

Added lines #L501 - L502 were not covered by tests

cmd := &cobra.Command{
Use: "coverage",
Short: "coverage",
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, _ []string) error {
cfg := cli.cfg()

// for this one we explicitly don't do for appsec
if err := HubTest.LoadAllTests(); err != nil {
return fmt.Errorf("unable to load all tests: %+v", err)
}
var err error
scenarioCoverage := []hubtest.Coverage{}
parserCoverage := []hubtest.Coverage{}
appsecRuleCoverage := []hubtest.Coverage{}
scenarioCoveragePercent := 0
parserCoveragePercent := 0
appsecRuleCoveragePercent := 0

// if both are false (flag by default), show both
showAll := !showScenarioCov && !showParserCov && !showAppsecCov

if showParserCov || showAll {
parserCoverage, err = HubTest.GetParsersCoverage()
if err != nil {
return fmt.Errorf("while getting parser coverage: %w", err)
}
parserTested := 0
for _, test := range parserCoverage {
if test.TestsCount > 0 {
parserTested++
}
}
parserCoveragePercent = int(math.Round((float64(parserTested) / float64(len(parserCoverage)) * 100)))
// for this one we explicitly don't do for appsec
if err := HubTest.LoadAllTests(); err != nil {
return fmt.Errorf("unable to load all tests: %+v", err)
}
var err error
scenarioCoverage := []hubtest.Coverage{}
parserCoverage := []hubtest.Coverage{}
appsecRuleCoverage := []hubtest.Coverage{}
scenarioCoveragePercent := 0
parserCoveragePercent := 0
appsecRuleCoveragePercent := 0

// if both are false (flag by default), show both
showAll := !showScenarioCov && !showParserCov && !showAppsecCov

if showParserCov || showAll {
parserCoverage, err = HubTest.GetParsersCoverage()
if err != nil {
return fmt.Errorf("while getting parser coverage: %w", err)
}
parserTested := 0
for _, test := range parserCoverage {
if test.TestsCount > 0 {
parserTested++

Check warning on line 527 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L504-L527

Added lines #L504 - L527 were not covered by tests
}
}
parserCoveragePercent = int(math.Round((float64(parserTested) / float64(len(parserCoverage)) * 100)))

Check warning on line 530 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L530

Added line #L530 was not covered by tests
}

if showScenarioCov || showAll {
scenarioCoverage, err = HubTest.GetScenariosCoverage()
if err != nil {
return fmt.Errorf("while getting scenario coverage: %w", err)
}

scenarioTested := 0
for _, test := range scenarioCoverage {
if test.TestsCount > 0 {
scenarioTested++
}
}
if showScenarioCov || showAll {
scenarioCoverage, err = HubTest.GetScenariosCoverage()
if err != nil {
return fmt.Errorf("while getting scenario coverage: %w", err)
}

Check warning on line 537 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L533-L537

Added lines #L533 - L537 were not covered by tests

scenarioCoveragePercent = int(math.Round((float64(scenarioTested) / float64(len(scenarioCoverage)) * 100)))
scenarioTested := 0
for _, test := range scenarioCoverage {
if test.TestsCount > 0 {
scenarioTested++

Check warning on line 542 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L539-L542

Added lines #L539 - L542 were not covered by tests
}
}

if showAppsecCov || showAll {
appsecRuleCoverage, err = HubTest.GetAppsecCoverage()
if err != nil {
return fmt.Errorf("while getting scenario coverage: %w", err)
}
scenarioCoveragePercent = int(math.Round((float64(scenarioTested) / float64(len(scenarioCoverage)) * 100)))

Check warning on line 546 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L546

Added line #L546 was not covered by tests
}

appsecRuleTested := 0
for _, test := range appsecRuleCoverage {
if test.TestsCount > 0 {
appsecRuleTested++
}
}
appsecRuleCoveragePercent = int(math.Round((float64(appsecRuleTested) / float64(len(appsecRuleCoverage)) * 100)))
}
if showAppsecCov || showAll {
appsecRuleCoverage, err = HubTest.GetAppsecCoverage()
if err != nil {
return fmt.Errorf("while getting scenario coverage: %w", err)
}

Check warning on line 553 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L549-L553

Added lines #L549 - L553 were not covered by tests

if showOnlyPercent {
switch {
case showAll:
fmt.Printf("parsers=%d%%\nscenarios=%d%%\nappsec_rules=%d%%", parserCoveragePercent, scenarioCoveragePercent, appsecRuleCoveragePercent)
case showParserCov:
fmt.Printf("parsers=%d%%", parserCoveragePercent)
case showScenarioCov:
fmt.Printf("scenarios=%d%%", scenarioCoveragePercent)
case showAppsecCov:
fmt.Printf("appsec_rules=%d%%", appsecRuleCoveragePercent)
}
return nil
appsecRuleTested := 0
for _, test := range appsecRuleCoverage {
if test.TestsCount > 0 {
appsecRuleTested++

Check warning on line 558 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L555-L558

Added lines #L555 - L558 were not covered by tests
}
}
appsecRuleCoveragePercent = int(math.Round((float64(appsecRuleTested) / float64(len(appsecRuleCoverage)) * 100)))

Check warning on line 561 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L561

Added line #L561 was not covered by tests
}

switch cfg.Cscli.Output {
case "human":
if showParserCov || showAll {
hubTestParserCoverageTable(color.Output, parserCoverage)
}
if showOnlyPercent {
switch {
case showAll:
fmt.Printf("parsers=%d%%\nscenarios=%d%%\nappsec_rules=%d%%", parserCoveragePercent, scenarioCoveragePercent, appsecRuleCoveragePercent)
case showParserCov:
fmt.Printf("parsers=%d%%", parserCoveragePercent)
case showScenarioCov:
fmt.Printf("scenarios=%d%%", scenarioCoveragePercent)
case showAppsecCov:
fmt.Printf("appsec_rules=%d%%", appsecRuleCoveragePercent)

Check warning on line 573 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L564-L573

Added lines #L564 - L573 were not covered by tests
}
return nil

Check warning on line 575 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L575

Added line #L575 was not covered by tests
}

if showScenarioCov || showAll {
hubTestScenarioCoverageTable(color.Output, scenarioCoverage)
}
switch cfg.Cscli.Output {
case "human":
if showParserCov || showAll {
hubTestParserCoverageTable(color.Output, parserCoverage)
}

Check warning on line 582 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L578-L582

Added lines #L578 - L582 were not covered by tests

if showScenarioCov || showAll {
hubTestScenarioCoverageTable(color.Output, scenarioCoverage)
}

Check warning on line 586 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L584-L586

Added lines #L584 - L586 were not covered by tests

if showAppsecCov || showAll {
hubTestAppsecRuleCoverageTable(color.Output, appsecRuleCoverage)
}

Check warning on line 590 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L588-L590

Added lines #L588 - L590 were not covered by tests

fmt.Println()
if showParserCov || showAll {
fmt.Printf("PARSERS : %d%% of coverage\n", parserCoveragePercent)
}
if showScenarioCov || showAll {
fmt.Printf("SCENARIOS : %d%% of coverage\n", scenarioCoveragePercent)
}
if showAppsecCov || showAll {
fmt.Printf("APPSEC RULES : %d%% of coverage\n", appsecRuleCoveragePercent)
}
case "json":
dump, err := json.MarshalIndent(parserCoverage, "", " ")
if err != nil {
return err
}
fmt.Printf("%s", dump)
dump, err = json.MarshalIndent(scenarioCoverage, "", " ")
if err != nil {
return err
}
fmt.Printf("%s", dump)
dump, err = json.MarshalIndent(appsecRuleCoverage, "", " ")
if err != nil {
return err
}
fmt.Printf("%s", dump)
default:
return errors.New("only human/json output modes are supported")

Check warning on line 619 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L592-L619

Added lines #L592 - L619 were not covered by tests
}

if showAppsecCov || showAll {
hubTestAppsecRuleCoverageTable(color.Output, appsecRuleCoverage)
}
return nil

Check warning on line 622 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L622

Added line #L622 was not covered by tests
}

fmt.Println()
if showParserCov || showAll {
fmt.Printf("PARSERS : %d%% of coverage\n", parserCoveragePercent)
}
if showScenarioCov || showAll {
fmt.Printf("SCENARIOS : %d%% of coverage\n", scenarioCoveragePercent)
}
if showAppsecCov || showAll {
fmt.Printf("APPSEC RULES : %d%% of coverage\n", appsecRuleCoveragePercent)
}
case "json":
dump, err := json.MarshalIndent(parserCoverage, "", " ")
if err != nil {
return err
}
fmt.Printf("%s", dump)
dump, err = json.MarshalIndent(scenarioCoverage, "", " ")
if err != nil {
return err
}
fmt.Printf("%s", dump)
dump, err = json.MarshalIndent(appsecRuleCoverage, "", " ")
if err != nil {
return err
}
fmt.Printf("%s", dump)
default:
return errors.New("only human/json output modes are supported")
}
func (cli *cliHubTest) NewCoverageCmd() *cobra.Command {
var (
showParserCov bool
showScenarioCov bool
showOnlyPercent bool
showAppsecCov bool
)

return nil
cmd := &cobra.Command{
Use: "coverage",
Short: "coverage",
DisableAutoGenTag: true,
RunE: func(_ *cobra.Command, _ []string) error {
return cli.coverage(showScenarioCov, showParserCov, showAppsecCov, showOnlyPercent)

Check warning on line 638 in cmd/crowdsec-cli/hubtest.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hubtest.go#L638

Added line #L638 was not covered by tests
},
}

Expand Down

0 comments on commit dce0528

Please sign in to comment.