Skip to content

Commit

Permalink
Allow Wildcards for Include/Exclude Tester filtering (goadesign#355)
Browse files Browse the repository at this point in the history
* Add wildcard filters to Include/Exclude tester filters

* Review Feedback 1

* Linting Fixes 1
  • Loading branch information
jasondborneman authored Jan 12, 2024
1 parent 605ad53 commit cc13afd
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 18 deletions.
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
6 changes: 4 additions & 2 deletions example/weather/services/tester/design/design.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var _ = Service("tester", func() {
})
Description("The Weather System Tester Service is used to manage the integration testing of the weater system")
Error("include_exclude_both", ErrorResult, "Cannot specify both include and exclude")
Error("wildcard_compile_error", ErrorResult, "Wildcard glob did not compile")

Method("test_all", func() {
Description("Runs all tests in the iam system")
Expand All @@ -26,6 +27,7 @@ var _ = Service("tester", func() {
GRPC(func() {
Response(CodeOK)
Response("include_exclude_both", CodeInvalidArgument)
Response("wildcard_compile_error", CodeInvalidArgument)
})
})

Expand Down Expand Up @@ -108,10 +110,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

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

4 changes: 4 additions & 0 deletions example/weather/services/tester/gen/tester/client.go

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

9 changes: 7 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.

56 changes: 49 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,29 @@ 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)) ([]func(context.Context, *TestCollection), error) {
match := false
var testMatches []func(context.Context, *TestCollection)
var g glob.Glob
g, err := glob.Compile(test)
if err != nil {
_ = logError(ctx, err)
err = fmt.Errorf("wildcard glob [%s] did not compile: %v", test, err)
return testMatches, err
}
i := 0
for testName := range testMap {
match = g.Match(testName)
if match {
testMatches = append(testMatches, testMap[testName])
}
i++
}
return testMatches, nil
}

// 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 +120,35 @@ 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
testFuncs, err := matchTestFilter(ctx, test, testMap)
if err != nil {
return nil, gentester.MakeWildcardCompileError(err)
}
if len(testFuncs) > 0 {
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, err := glob.Compile(excludeTest)
if err != nil {
_ = logError(ctx, err)
err = fmt.Errorf("wildcard glob [%s] did not compile: %v", excludeTest, err)
return nil, gentester.MakeWildcardCompileError(err)
}
wildcardMatch = wildcardMatch || g.Match(testName)
}
if !wildcardMatch {
testsToRun[testName] = test
} else {
log.Debugf(ctx, "Test [%v] excluded", testName)
Expand Down

0 comments on commit cc13afd

Please sign in to comment.