Skip to content

Commit

Permalink
fix(glob): globbing was not getting files recursively
Browse files Browse the repository at this point in the history
Signed-off-by: Felipe Zipitria <[email protected]>
  • Loading branch information
fzipi committed May 23, 2021
1 parent 19520c7 commit e95fcc3
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 104 deletions.
37 changes: 12 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ My goals are:

## Install

Just get the binary file for your architecture, and run it!

```bash
```
Go to the [releases](https://github.com/fzipi/go-ftw/releases) page and get the one that matches your OS.

## Example Usage

Expand Down Expand Up @@ -73,43 +70,33 @@ docker-compose -f tests/docker-compose.yml up -d modsec2-apache

This is the help for the `run` command:
```bash
./ftw run -h
❯ ftw run -h
Run all tests below a certain subdirectory. The command will search all y[a]ml files recursively and pass it to the test engine.

Usage:
ftw run [flags]

Flags:
-d, --dir string recursively find yaml tests in this directory (default ".")
--exclude string exclude tests matching this Go regexp (e.g. to exclude all tests beginning with "91", use "91.*")
-e, --exclude string exclude tests matching this Go regexp (e.g. to exclude all tests beginning with "91", use "91.*").
If you want more permanent exclusion, check the 'testmodify' option in the config file.
-h, --help help for run
--id string set test id to run
--id string (deprecated). Use --include matching your test only.
-i, --include string include only tests matching this Go regexp (e.g. to include only tests beginning with "91", use "91.*").
-q, --quiet do not show test by test, only results
-t, --time show time spent per test

Global Flags:
--cfg string override config file (default is $PWD/.ftw.yaml)
--debug debug output
--config string override config file (default is $PWD/.ftw.yaml) (default "c")
--debug debug output
--trace trace output: really, really verbose

```

Then run all tests (slightly modified, see [here](https://gist.github.com/fzipi/b9e22b3834a5fa32970878c72775d41e)) in the ModSecurity Core Rule set using:

`ftw check -d tests -t`

If you see errors like these:
```
> ./ftw run -d tests
1:33PM INF Using config file: .ftw
1:33PM INF ftw/config: no duration found
🛠️ Starting tests!
1:33PM ERR yaml: unmarshal errors:
line 44: cannot unmarshal !!int `400` into []int
line 98: cannot unmarshal !!int `400` into []int
line 229: cannot unmarshal !!int `400` into []int
```
After merging [this PR](https://github.com/coreruleset/coreruleset/pull/2080), no changes will be needed.
Until that happens, you can get and apply the [patch](https://patch-diff.githubusercontent.com/raw/coreruleset/coreruleset/pull/2080.patch), using `patch -p1 < 2080.patch`.

You need to apply the patch above. Then you can run your tests using:
Then you can run your tests using:

`ftw run -d tests -t`

Expand Down
7 changes: 6 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
var (
cfgFile string
debug bool
trace bool
)

// rootCmd represents the base command when called without any subcommands
Expand All @@ -34,14 +35,18 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.PersistentFlags().StringVar(&cfgFile, "cfg", "", "override config file (default is $PWD/.ftw.yaml)")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "c", "override config file (default is $PWD/.ftw.yaml)")
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "", false, "debug output")
rootCmd.PersistentFlags().BoolVarP(&trace, "trace", "", false, "trace output: really, really verbose")
}

func initConfig() {
config.Init(cfgFile)
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
if trace {
zerolog.SetGlobalLevel(zerolog.TraceLevel)
}
}
19 changes: 15 additions & 4 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"

"github.com/kyokomi/emoji"
Expand All @@ -18,8 +19,9 @@ var runCmd = &cobra.Command{
Short: "Run Tests",
Long: `Run all tests below a certain subdirectory. The command will search all y[a]ml files recursively and pass it to the test engine.`,
RunE: func(cmd *cobra.Command, args []string) error {
testid, _ := cmd.Flags().GetString("id")
exclude, _ := cmd.Flags().GetString("exclude")
include, _ := cmd.Flags().GetString("include")
id, _ := cmd.Flags().GetString("id")
dir, _ := cmd.Flags().GetString("dir")
showTime, _ := cmd.Flags().GetBool("time")
quiet, _ := cmd.Flags().GetBool("quiet")
Expand All @@ -28,19 +30,28 @@ var runCmd = &cobra.Command{
} else {
zerolog.SetGlobalLevel(zerolog.Disabled)
}
if id != "" {
log.Fatal().Msgf("--id is deprecated in favour of --include|-i")
return errors.New("use --include instead")
}
if exclude != "" && include != "" {
log.Fatal().Msgf("You need to choose one: use --include (%s) or --exclude (%s)", include, exclude)
return errors.New("choose one between --include or --exclude")
}
files := fmt.Sprintf("%s/**/*.yaml", dir)
tests, err := test.GetTestsFromFiles(files)
if err != nil {
log.Error().Msg(err.Error())
}
return runner.Run(testid, exclude, showTime, quiet, tests)
return runner.Run(include, exclude, showTime, quiet, tests)
},
}

func init() {
rootCmd.AddCommand(runCmd)
runCmd.Flags().StringP("id", "", "", "set test id to run")
runCmd.Flags().StringP("exclude", "", "", "exclude tests matching this Go regexp (e.g. to exclude all tests beginning with \"91\", use \"91.*\"). If you want more permanent exclusion, check the 'testmodify' option in the config file.")
runCmd.Flags().StringP("exclude", "e", "", "exclude tests matching this Go regexp (e.g. to exclude all tests beginning with \"91\", use \"91.*\"). \nIf you want more permanent exclusion, check the 'testmodify' option in the config file.")
runCmd.Flags().StringP("include", "i", "", "include only tests matching this Go regexp (e.g. to include only tests beginning with \"91\", use \"91.*\").")
runCmd.Flags().StringP("id", "", "", "(deprecated). Use --include matching your test only.")
runCmd.Flags().StringP("dir", "d", ".", "recursively find yaml tests in this directory")
runCmd.Flags().BoolP("quiet", "q", false, "do not show test by test, only results")
runCmd.Flags().BoolP("time", "t", false, "show time spent per test")
Expand Down
14 changes: 8 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@ require (
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/bykof/gostradamus v1.0.4
github.com/fatih/color v1.11.0 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/goccy/go-yaml v1.8.9
github.com/google/uuid v1.2.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/icza/backscanner v0.0.0-20200205093934-2120fccb01f7
github.com/imdario/mergo v0.3.12 // indirect
github.com/kyokomi/emoji v2.2.4+incompatible
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/copystructure v1.1.2 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/pelletier/go-toml v1.9.0 // indirect
github.com/rs/zerolog v1.21.0
github.com/pelletier/go-toml v1.9.1 // indirect
github.com/rs/zerolog v1.22.0
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v1.1.3
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.7.1
golang.org/x/crypto v0.0.0-20210415154028-4f45737414dc // indirect
golang.org/x/sys v0.0.0-20210415045647-66c3f260301c // indirect
github.com/yargevad/filepathx v1.0.0
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect
golang.org/x/text v0.3.6 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.4.0
)
Loading

0 comments on commit e95fcc3

Please sign in to comment.