Skip to content

Allow filtering of sweepers using regular expressions #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,24 @@ func runSweepers(regions []string, sweepers map[string]*Sweeper, allowFailures b
// to be ran, and returns a filtered set from the list of all of sweepers to
// run based on the names given.
func filterSweepers(f string, source map[string]*Sweeper) map[string]*Sweeper {
filterSlice := strings.Split(strings.ToLower(f), ",")
filterSlice := strings.Split(f, ",")
if len(filterSlice) == 1 && filterSlice[0] == "" {
// if the filter slice is a single element of "" then no sweeper list was
// given, so just return the full list
return source
}

// Downcase filter elements and create regular expressions
filterRegexes := make([]regexp.Regexp, len(filterSlice))
for i, filter := range filterSlice {
filter = strings.ToLower(filter)
filterRegexes[i] = *regexp.MustCompile(filter)
}

sweepers := make(map[string]*Sweeper)
for name := range source {
for _, s := range filterSlice {
if strings.Contains(strings.ToLower(name), s) {
for _, r := range filterRegexes {
if r.MatchString(strings.ToLower(name)) {
for foundName, foundSweeper := range filterSweeperWithDependencies(name, source) {
sweepers[foundName] = foundSweeper
}
Expand Down
68 changes: 63 additions & 5 deletions helper/resource/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func TestFilterSweepers(t *testing.T) {
ExpectedSweepers: []string{"aws_dummy"},
},
{
Name: "with dep",
Name: "with dependencies",
Sweepers: map[string]*Sweeper{
"aws_dummy": {
Name: "aws_dummy",
Expand Down Expand Up @@ -250,6 +250,25 @@ func TestFilterSweepers(t *testing.T) {
ExpectedSweepers: []string{"aws_dummy"},
Filter: "aws_dummy",
},
{
Name: "with regex filter",
Sweepers: map[string]*Sweeper{
"aws_dummy": {
Name: "aws_dummy",
F: mockSweeperFunc,
},
"aws_top": {
Name: "aws_top",
F: mockSweeperFunc,
},
"aws_sub": {
Name: "aws_sub",
F: mockSweeperFunc,
},
},
ExpectedSweepers: []string{"aws_dummy", "aws_sub", "aws_top"},
Filter: "^aws_",
},
{
Name: "with two filters",
Sweepers: map[string]*Sweeper{
Expand All @@ -271,7 +290,7 @@ func TestFilterSweepers(t *testing.T) {
Filter: "aws_dummy,aws_sub",
},
{
Name: "with dep and filter",
Name: "with dependency and filter",
Sweepers: map[string]*Sweeper{
"aws_dummy": {
Name: "aws_dummy",
Expand Down Expand Up @@ -310,7 +329,46 @@ func TestFilterSweepers(t *testing.T) {
Filter: "none",
},
{
Name: "with nested depenencies and top level filter",
Name: "with non-matching regex filter",
Sweepers: map[string]*Sweeper{
"aws_dummy": {
Name: "aws_dummy",
F: mockSweeperFunc,
},
"aws_top": {
Name: "aws_top",
Dependencies: []string{"aws_sub"},
F: mockSweeperFunc,
},
"aws_sub": {
Name: "aws_sub",
F: mockSweeperFunc,
},
},
Filter: "^aws_$",
},
{
Name: "with redundant filter",
Sweepers: map[string]*Sweeper{
"aws_dummy": {
Name: "aws_dummy",
F: mockSweeperFunc,
},
"aws_top": {
Name: "aws_top",
Dependencies: []string{"aws_sub"},
F: mockSweeperFunc,
},
"aws_sub": {
Name: "aws_sub",
F: mockSweeperFunc,
},
},
ExpectedSweepers: []string{"aws_sub", "aws_top", "aws_dummy"},
Filter: "aws_,aws_dummy",
},
{
Name: "with nested dependencies and top level filter",
Sweepers: map[string]*Sweeper{
"not_matching": {
Name: "not_matching",
Expand All @@ -335,7 +393,7 @@ func TestFilterSweepers(t *testing.T) {
Filter: "matching_level1",
},
{
Name: "with nested depenencies and middle level filter",
Name: "with nested dependencies and middle level filter",
Sweepers: map[string]*Sweeper{
"not_matching": {
Name: "not_matching",
Expand All @@ -360,7 +418,7 @@ func TestFilterSweepers(t *testing.T) {
Filter: "matching_level2",
},
{
Name: "with nested depenencies and bottom level filter",
Name: "with nested dependencies and bottom level filter",
Sweepers: map[string]*Sweeper{
"not_matching": {
Name: "not_matching",
Expand Down
Loading