Skip to content
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

Add wildcard filters to Include/Exclude tester filters #13

Merged
merged 1 commit into from
Jan 12, 2024
Merged
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
2 changes: 1 addition & 1 deletion example/weather/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module goa.design/clue/example/weather
go 1.21

require (
github.com/gobwas/glob v0.2.3
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.46.1
Expand All @@ -13,7 +14,6 @@ require (
goa.design/goa/v3 v3.14.4
goa.design/model v1.9.1
goa.design/plugins/v3 v3.14.5-0.20240108065040-b05574e08115
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc
google.golang.org/grpc v1.60.1
google.golang.org/protobuf v1.32.0
)
Expand Down
4 changes: 2 additions & 2 deletions example/weather/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
Expand Down Expand Up @@ -101,8 +103,6 @@ goa.design/model v1.9.1 h1:vhY89pjUWW1firAHaccW9MZTiMEEVvtcwN9g8odyywQ=
goa.design/model v1.9.1/go.mod h1:PlrrmVbrBm7gxqoWqburVUudaMd/d3Sdul7/Kr4Ap54=
goa.design/plugins/v3 v3.14.5-0.20240108065040-b05574e08115 h1:b1eDZk0rqa4DhWVUi/uZ8j3teScG/jwqgT8SOuZtWyE=
goa.design/plugins/v3 v3.14.5-0.20240108065040-b05574e08115/go.mod h1:S8Gz3HR6r4aMK9MTuv1U2RSX9IdugLX/tb5KktTsoN8=
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc h1:ao2WRsKSzW6KuUY9IWPwWahcHCgR0s52IfwutMfEbdM=
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
Expand Down
4 changes: 2 additions & 2 deletions example/weather/services/tester/design/design.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ var TestResults = Type("TestResults", func() {

var SystemTestPayload = Type("TesterPayload", func() {
Description("Payload for the tester service")
Field(1, "Include", ArrayOf(String), "Tests to run", func() {
Field(1, "Include", ArrayOf(String), "Tests to run. Allows wildcards.", func() {
Example([]string{"TestNameToInclude"})
})
Field(2, "Exclude", ArrayOf(String), "Tests to exclude", func() {
Field(2, "Exclude", ArrayOf(String), "Tests to exclude. Allows wildcards.", func() {
Example([]string{"TestNameToExclude"})
})
})

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ service Tester {
}

message TestAllRequest {
// Tests to run
// Tests to run. Allows wildcards.
repeated string include = 1;
// Tests to exclude
// Tests to exclude. Allows wildcards.
repeated string exclude = 2;
}

Expand Down
4 changes: 2 additions & 2 deletions example/weather/services/tester/gen/tester/service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 36 additions & 7 deletions example/weather/services/tester/run_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"sync"
"time"

"github.com/gobwas/glob"
"goa.design/clue/log"
"golang.org/x/exp/slices"

gentester "goa.design/clue/example/weather/services/tester/gen/tester"
)
Expand Down Expand Up @@ -77,6 +77,24 @@ func recoverFromTestPanic(ctx context.Context, testName string, testCollection *
}
}

// Filters a testMap based on a test name that is a glob string
// using standard wildcards https://tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm
func matchTestFilter(ctx context.Context, test string, testMap map[string]func(context.Context, *TestCollection)) (bool, []func(context.Context, *TestCollection)) {
match := false
testMatches := []func(context.Context, *TestCollection){}
var g glob.Glob
g = glob.MustCompile(test)
i := 0
for testName, _ := range testMap {
match = g.Match(testName)
if match {
testMatches = append(testMatches, testMap[testName])
}
i++
}
return match, testMatches
}

// Runs the tests from the testmap and handles filtering/exclusion of tests
// Pass in `true` for runSynchronously to run the tests synchronously instead
// of in parallel.
Expand All @@ -97,16 +115,27 @@ func (svc *Service) runTests(ctx context.Context, p *gentester.TesterPayload, te
for _, test := range p.Include {
if testFunc, ok := testMap[test]; ok {
testsToRun[test] = testFunc
} else {
err := fmt.Errorf("test [%v] not found in test map", test)
_ = logError(ctx, err)
} else { // Test didn't match exactly, so we're gonna try for a wildcard match
findByWildcard, testFuncs := matchTestFilter(ctx, test, testMap)
if findByWildcard {
for i, testFunc := range testFuncs {
testsToRun[fmt.Sprintf("%s_%d", test, i)] = testFunc
}
} else { // No wildcard match either
err := fmt.Errorf("test [%v] not found in test map", test)
_ = logError(ctx, err)
}
}
}
} else if len(p.Exclude) > 0 { // If there is only an exclude list, we add tests not found in that exclude list to the tests to run
for testName, test := range testMap {
// This is from golang's experimental slices package
// (https://godoc.org/golang.org/x/exp/slices)
if !slices.Contains(p.Exclude, testName) {
wildcardMatch := false
for _, excludeTest := range p.Exclude {
var g glob.Glob
g = glob.MustCompile(excludeTest)
wildcardMatch = wildcardMatch || g.Match(testName)
}
if !wildcardMatch {
testsToRun[testName] = test
} else {
log.Debugf(ctx, "Test [%v] excluded", testName)
Expand Down