Skip to content

Commit

Permalink
pkg/hubtest: extract methods + consistent error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Feb 13, 2024
1 parent 4561eb7 commit fb364c0
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 114 deletions.
54 changes: 31 additions & 23 deletions pkg/hubtest/appsecrule.go
Expand Up @@ -50,36 +50,44 @@ func (t *HubTestItem) installAppsecRuleItem(hubAppsecRule *cwhub.Item) error {
return nil
}

func (t *HubTestItem) installAppsecRuleCustomFrom(appsecrule string, customPath string) (bool, error) {
// we check if its a custom appsec-rule
customAppsecRulePath := filepath.Join(customPath, appsecrule)
if _, err := os.Stat(customAppsecRulePath); os.IsNotExist(err) {
return false, nil
}
customAppsecRulePathSplit := strings.Split(customAppsecRulePath, "/")
customAppsecRuleName := customAppsecRulePathSplit[len(customAppsecRulePathSplit)-1]

appsecRuleDirDest := fmt.Sprintf("%s/appsec-rules/", t.RuntimePath)
if err := os.MkdirAll(appsecRuleDirDest, os.ModePerm); err != nil {
return false, fmt.Errorf("unable to create folder '%s': %s", appsecRuleDirDest, err)
}

Check warning on line 65 in pkg/hubtest/appsecrule.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/appsecrule.go#L53-L65

Added lines #L53 - L65 were not covered by tests

// runtime/appsec-rules/
customAppsecRuleDest := fmt.Sprintf("%s/appsec-rules/%s", t.RuntimePath, customAppsecRuleName)
// if path to postoverflow exist, copy it
if err := Copy(customAppsecRulePath, customAppsecRuleDest); err != nil {
return false, fmt.Errorf("unable to copy appsec-rule from '%s' to '%s': %s", customAppsecRulePath, customAppsecRuleDest, err)
}

Check warning on line 72 in pkg/hubtest/appsecrule.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/appsecrule.go#L68-L72

Added lines #L68 - L72 were not covered by tests

return true, nil

Check warning on line 74 in pkg/hubtest/appsecrule.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/appsecrule.go#L74

Added line #L74 was not covered by tests
}


func (t *HubTestItem) installAppsecRuleCustom(appsecrule string) error {
customAppsecRuleExist := false
for _, customPath := range t.CustomItemsLocation {
// we check if its a custom appsec-rule
customAppsecRulePath := filepath.Join(customPath, appsecrule)
if _, err := os.Stat(customAppsecRulePath); os.IsNotExist(err) {
continue
found, err := t.installAppsecRuleCustomFrom(appsecrule, customPath)
if err != nil {
return err

Check warning on line 82 in pkg/hubtest/appsecrule.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/appsecrule.go#L80-L82

Added lines #L80 - L82 were not covered by tests
}
customAppsecRulePathSplit := strings.Split(customAppsecRulePath, "/")
customAppsecRuleName := customAppsecRulePathSplit[len(customAppsecRulePathSplit)-1]

appsecRuleDirDest := fmt.Sprintf("%s/appsec-rules/", t.RuntimePath)
if err := os.MkdirAll(appsecRuleDirDest, os.ModePerm); err != nil {
return fmt.Errorf("unable to create folder '%s': %s", appsecRuleDirDest, err)
if found {
return nil

Check warning on line 86 in pkg/hubtest/appsecrule.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/appsecrule.go#L85-L86

Added lines #L85 - L86 were not covered by tests
}

// runtime/appsec-rules/
customAppsecRuleDest := fmt.Sprintf("%s/appsec-rules/%s", t.RuntimePath, customAppsecRuleName)
// if path to postoverflow exist, copy it
if err := Copy(customAppsecRulePath, customAppsecRuleDest); err != nil {
continue
}
customAppsecRuleExist = true
break
}
if !customAppsecRuleExist {
return fmt.Errorf("couldn't find custom appsec-rule '%s' in the following location: %+v", appsecrule, t.CustomItemsLocation)
}

return nil
return fmt.Errorf("couldn't find custom appsec-rule '%s' in the following location: %+v", appsecrule, t.CustomItemsLocation)

Check warning on line 90 in pkg/hubtest/appsecrule.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/appsecrule.go#L90

Added line #L90 was not covered by tests
}

func (t *HubTestItem) installAppsecRule(name string) error {
Expand Down
72 changes: 37 additions & 35 deletions pkg/hubtest/parser.go
Expand Up @@ -48,50 +48,52 @@ func (t *HubTestItem) installParserItem(hubParser *cwhub.Item) error {
return nil
}

func (t *HubTestItem) installParserCustom(parser string) error {
customParserExist := false
for _, customPath := range t.CustomItemsLocation {
// we check if its a custom parser
customParserPath := filepath.Join(customPath, parser)
if _, err := os.Stat(customParserPath); os.IsNotExist(err) {
continue
//return fmt.Errorf("parser '%s' doesn't exist in the hub and doesn't appear to be a custom one.", parser)
}
func (t *HubTestItem) installParserCustomFrom(parser string, customPath string) (bool, error) {
// we check if its a custom parser
customParserPath := filepath.Join(customPath, parser)
if _, err := os.Stat(customParserPath); os.IsNotExist(err) {
return false, nil
}

Check warning on line 56 in pkg/hubtest/parser.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/parser.go#L51-L56

Added lines #L51 - L56 were not covered by tests

customParserPathSplit, customParserName := filepath.Split(customParserPath)
// because path is parsers/<stage>/<author>/parser.yaml and we wan't the stage
splittedPath := strings.Split(customParserPathSplit, string(os.PathSeparator))
customParserStage := splittedPath[len(splittedPath)-3]
customParserPathSplit, customParserName := filepath.Split(customParserPath)
// because path is parsers/<stage>/<author>/parser.yaml and we wan't the stage
splittedPath := strings.Split(customParserPathSplit, string(os.PathSeparator))
customParserStage := splittedPath[len(splittedPath)-3]

Check warning on line 61 in pkg/hubtest/parser.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/parser.go#L58-L61

Added lines #L58 - L61 were not covered by tests

// check if stage exist
hubStagePath := filepath.Join(t.HubPath, fmt.Sprintf("parsers/%s", customParserStage))
// check if stage exist
hubStagePath := filepath.Join(t.HubPath, fmt.Sprintf("parsers/%s", customParserStage))

Check warning on line 64 in pkg/hubtest/parser.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/parser.go#L63-L64

Added lines #L63 - L64 were not covered by tests

if _, err := os.Stat(hubStagePath); os.IsNotExist(err) {
continue
//return fmt.Errorf("stage '%s' extracted from '%s' doesn't exist in the hub", customParserStage, hubStagePath)
}
if _, err := os.Stat(hubStagePath); os.IsNotExist(err) {
return false, fmt.Errorf("stage '%s' extracted from '%s' doesn't exist in the hub", customParserStage, hubStagePath)
}

Check warning on line 68 in pkg/hubtest/parser.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/parser.go#L66-L68

Added lines #L66 - L68 were not covered by tests

parserDirDest := fmt.Sprintf("%s/parsers/%s/", t.RuntimePath, customParserStage)
if err := os.MkdirAll(parserDirDest, os.ModePerm); err != nil {
continue
//return fmt.Errorf("unable to create folder '%s': %s", parserDirDest, err)
}
parserDirDest := fmt.Sprintf("%s/parsers/%s/", t.RuntimePath, customParserStage)
if err := os.MkdirAll(parserDirDest, os.ModePerm); err != nil {
return false, fmt.Errorf("unable to create folder '%s': %s", parserDirDest, err)
}

Check warning on line 73 in pkg/hubtest/parser.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/parser.go#L70-L73

Added lines #L70 - L73 were not covered by tests

customParserDest := filepath.Join(parserDirDest, customParserName)
// if path to parser exist, copy it
if err := Copy(customParserPath, customParserDest); err != nil {
continue
//return fmt.Errorf("unable to copy custom parser '%s' to '%s': %s", customParserPath, customParserDest, err)
customParserDest := filepath.Join(parserDirDest, customParserName)
// if path to parser exist, copy it
if err := Copy(customParserPath, customParserDest); err != nil {
return false, fmt.Errorf("unable to copy custom parser '%s' to '%s': %s", customParserPath, customParserDest, err)
}

Check warning on line 79 in pkg/hubtest/parser.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/parser.go#L75-L79

Added lines #L75 - L79 were not covered by tests

return true, nil

Check warning on line 81 in pkg/hubtest/parser.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/parser.go#L81

Added line #L81 was not covered by tests
}

func (t *HubTestItem) installParserCustom(parser string) error {
for _, customPath := range t.CustomItemsLocation {
found, err := t.installParserCustomFrom(parser, customPath)
if err != nil {
return err

Check warning on line 88 in pkg/hubtest/parser.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/parser.go#L84-L88

Added lines #L84 - L88 were not covered by tests
}

customParserExist = true
break
}
if !customParserExist {
return fmt.Errorf("couldn't find custom parser '%s' in the following location: %+v", parser, t.CustomItemsLocation)
if found {
return nil
}

Check warning on line 93 in pkg/hubtest/parser.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/parser.go#L91-L93

Added lines #L91 - L93 were not covered by tests
}

return nil
return fmt.Errorf("couldn't find custom parser '%s' in the following locations: %+v", parser, t.CustomItemsLocation)

Check warning on line 96 in pkg/hubtest/parser.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/parser.go#L96

Added line #L96 was not covered by tests
}

func (t *HubTestItem) installParser(name string) error {
Expand Down
72 changes: 38 additions & 34 deletions pkg/hubtest/postoverflow.go
Expand Up @@ -48,49 +48,53 @@ func (t *HubTestItem) installPostoverflowItem(hubPostOverflow *cwhub.Item) error
return nil
}

func (t *HubTestItem) installPostoverflowCustom(postoverflow string) error {
customPostoverflowExist := false
for _, customPath := range t.CustomItemsLocation {
// we check if its a custom postoverflow
customPostOverflowPath := filepath.Join(customPath, postoverflow)
if _, err := os.Stat(customPostOverflowPath); os.IsNotExist(err) {
continue
//return fmt.Errorf("postoverflow '%s' doesn't exist in the hub and doesn't appear to be a custom one.", postoverflow)
}
func (t *HubTestItem) installPostoverflowCustomFrom(postoverflow string, customPath string) (bool, error) {
// we check if its a custom postoverflow
customPostOverflowPath := filepath.Join(customPath, postoverflow)
if _, err := os.Stat(customPostOverflowPath); os.IsNotExist(err) {
return false, nil
}

Check warning on line 56 in pkg/hubtest/postoverflow.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/postoverflow.go#L51-L56

Added lines #L51 - L56 were not covered by tests

customPostOverflowPathSplit := strings.Split(customPostOverflowPath, "/")
customPostoverflowName := customPostOverflowPathSplit[len(customPostOverflowPathSplit)-1]
// because path is postoverflows/<stage>/<author>/parser.yaml and we wan't the stage
customPostoverflowStage := customPostOverflowPathSplit[len(customPostOverflowPathSplit)-3]
customPostOverflowPathSplit := strings.Split(customPostOverflowPath, "/")
customPostoverflowName := customPostOverflowPathSplit[len(customPostOverflowPathSplit)-1]
// because path is postoverflows/<stage>/<author>/parser.yaml and we wan't the stage
customPostoverflowStage := customPostOverflowPathSplit[len(customPostOverflowPathSplit)-3]

Check warning on line 61 in pkg/hubtest/postoverflow.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/postoverflow.go#L58-L61

Added lines #L58 - L61 were not covered by tests

// check if stage exist
hubStagePath := filepath.Join(t.HubPath, fmt.Sprintf("postoverflows/%s", customPostoverflowStage))
// check if stage exist
hubStagePath := filepath.Join(t.HubPath, fmt.Sprintf("postoverflows/%s", customPostoverflowStage))

Check warning on line 64 in pkg/hubtest/postoverflow.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/postoverflow.go#L63-L64

Added lines #L63 - L64 were not covered by tests

if _, err := os.Stat(hubStagePath); os.IsNotExist(err) {
continue
//return fmt.Errorf("stage '%s' from extracted '%s' doesn't exist in the hub", customPostoverflowStage, hubStagePath)
}
if _, err := os.Stat(hubStagePath); os.IsNotExist(err) {
return false, fmt.Errorf("stage '%s' from extracted '%s' doesn't exist in the hub", customPostoverflowStage, hubStagePath)
}

Check warning on line 68 in pkg/hubtest/postoverflow.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/postoverflow.go#L66-L68

Added lines #L66 - L68 were not covered by tests

postoverflowDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, customPostoverflowStage)
if err := os.MkdirAll(postoverflowDirDest, os.ModePerm); err != nil {
return false, fmt.Errorf("unable to create folder '%s': %s", postoverflowDirDest, err)
}

Check warning on line 73 in pkg/hubtest/postoverflow.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/postoverflow.go#L70-L73

Added lines #L70 - L73 were not covered by tests

postoverflowDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, customPostoverflowStage)
if err := os.MkdirAll(postoverflowDirDest, os.ModePerm); err != nil {
continue
//return fmt.Errorf("unable to create folder '%s': %s", postoverflowDirDest, err)
customPostoverflowDest := filepath.Join(postoverflowDirDest, customPostoverflowName)
// if path to postoverflow exist, copy it
if err := Copy(customPostOverflowPath, customPostoverflowDest); err != nil {
return false, fmt.Errorf("unable to copy custom parser '%s' to '%s': %s", customPostOverflowPath, customPostoverflowDest, err)
}

Check warning on line 79 in pkg/hubtest/postoverflow.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/postoverflow.go#L75-L79

Added lines #L75 - L79 were not covered by tests

return true, nil

Check warning on line 81 in pkg/hubtest/postoverflow.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/postoverflow.go#L81

Added line #L81 was not covered by tests
}


func (t *HubTestItem) installPostoverflowCustom(postoverflow string) error {
for _, customPath := range t.CustomItemsLocation {
found, err := t.installPostoverflowCustomFrom(postoverflow, customPath)
if err != nil {
return err

Check warning on line 89 in pkg/hubtest/postoverflow.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/postoverflow.go#L85-L89

Added lines #L85 - L89 were not covered by tests
}

customPostoverflowDest := filepath.Join(postoverflowDirDest, customPostoverflowName)
// if path to postoverflow exist, copy it
if err := Copy(customPostOverflowPath, customPostoverflowDest); err != nil {
continue
//return fmt.Errorf("unable to copy custom parser '%s' to '%s': %s", customPostOverflowPath, customPostoverflowDest, err)
if found {
return nil

Check warning on line 93 in pkg/hubtest/postoverflow.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/postoverflow.go#L92-L93

Added lines #L92 - L93 were not covered by tests
}
customPostoverflowExist = true
break
}
if !customPostoverflowExist {
return fmt.Errorf("couldn't find custom postoverflow '%s' in the following location: %+v", postoverflow, t.CustomItemsLocation)
}

return nil
return fmt.Errorf("couldn't find custom postoverflow '%s' in the following location: %+v", postoverflow, t.CustomItemsLocation)

Check warning on line 97 in pkg/hubtest/postoverflow.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/postoverflow.go#L97

Added line #L97 was not covered by tests
}

func (t *HubTestItem) installPostoverflow(name string) error {
Expand Down
49 changes: 27 additions & 22 deletions pkg/hubtest/scenario.go
Expand Up @@ -47,35 +47,40 @@ func (t *HubTestItem) installScenarioItem(hubScenario *cwhub.Item) error {
return nil
}

func (t *HubTestItem) installScenarioCustomFrom(scenario string, customPath string) (bool, error) {
// we check if its a custom scenario
customScenarioPath := filepath.Join(customPath, scenario)
if _, err := os.Stat(customScenarioPath); os.IsNotExist(err) {
return false, nil
}

Check warning on line 55 in pkg/hubtest/scenario.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/scenario.go#L50-L55

Added lines #L50 - L55 were not covered by tests

scenarioDirDest := fmt.Sprintf("%s/scenarios/", t.RuntimePath)
if err := os.MkdirAll(scenarioDirDest, os.ModePerm); err != nil {
return false, fmt.Errorf("unable to create folder '%s': %s", scenarioDirDest, err)
}

Check warning on line 60 in pkg/hubtest/scenario.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/scenario.go#L57-L60

Added lines #L57 - L60 were not covered by tests

scenarioFileName := filepath.Base(customScenarioPath)
scenarioFileDest := filepath.Join(scenarioDirDest, scenarioFileName)
if err := Copy(customScenarioPath, scenarioFileDest); err != nil {
return false, fmt.Errorf("unable to copy scenario from '%s' to '%s': %s", customScenarioPath, scenarioFileDest, err)
}

Check warning on line 66 in pkg/hubtest/scenario.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/scenario.go#L62-L66

Added lines #L62 - L66 were not covered by tests

return true, nil

Check warning on line 68 in pkg/hubtest/scenario.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/scenario.go#L68

Added line #L68 was not covered by tests
}

func (t *HubTestItem) installScenarioCustom(scenario string) error {
customScenarioExist := false
for _, customPath := range t.CustomItemsLocation {
// we check if its a custom scenario
customScenarioPath := filepath.Join(customPath, scenario)
if _, err := os.Stat(customScenarioPath); os.IsNotExist(err) {
continue
//return fmt.Errorf("scenarios '%s' doesn't exist in the hub and doesn't appear to be a custom one.", scenario)
found, err := t.installScenarioCustomFrom(scenario, customPath)
if err != nil {
return err

Check warning on line 75 in pkg/hubtest/scenario.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/scenario.go#L73-L75

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

scenarioDirDest := fmt.Sprintf("%s/scenarios/", t.RuntimePath)
if err := os.MkdirAll(scenarioDirDest, os.ModePerm); err != nil {
return fmt.Errorf("unable to create folder '%s': %s", scenarioDirDest, err)
if found {
return nil

Check warning on line 79 in pkg/hubtest/scenario.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/scenario.go#L78-L79

Added lines #L78 - L79 were not covered by tests
}

scenarioFileName := filepath.Base(customScenarioPath)
scenarioFileDest := filepath.Join(scenarioDirDest, scenarioFileName)
if err := Copy(customScenarioPath, scenarioFileDest); err != nil {
continue
//return fmt.Errorf("unable to copy scenario from '%s' to '%s': %s", customScenarioPath, scenarioFileDest, err)
}
customScenarioExist = true
break
}
if !customScenarioExist {
return fmt.Errorf("couldn't find custom scenario '%s' in the following location: %+v", scenario, t.CustomItemsLocation)
}

return nil
return fmt.Errorf("couldn't find custom scenario '%s' in the following location: %+v", scenario, t.CustomItemsLocation)

Check warning on line 83 in pkg/hubtest/scenario.go

View check run for this annotation

Codecov / codecov/patch

pkg/hubtest/scenario.go#L83

Added line #L83 was not covered by tests
}

func (t *HubTestItem) installScenario(name string) error {
Expand Down

0 comments on commit fb364c0

Please sign in to comment.