Skip to content

Commit f19d3ca

Browse files
authored
Implement isolated (#300)
* feat: add support for 'isolated' * fix: typo
1 parent e59892f commit f19d3ca

File tree

6 files changed

+62
-13
lines changed

6 files changed

+62
-13
lines changed

check/logs.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,13 @@ func (c *FTWCheck) assertLogContains() bool {
5454
}
5555
}
5656

57+
if c.expected.Isolated {
58+
ruleIds := c.log.TriggeredRules()
59+
result = len(ruleIds) == 1
60+
if !result {
61+
log.Debug().Msgf("Found more than one triggered rule for isolated test: %v", ruleIds)
62+
}
63+
}
64+
5765
return result
5866
}

check/logs_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,12 @@ func (s *checkLogsTestSuite) TestAssertLogNoExpectIds_Subset() {
151151
s.check.expected.Log.NoExpectIds = []uint{123, 920300}
152152
s.False(s.check.AssertLogs(), "Expected to find '920300'")
153153
}
154+
155+
func (s *checkLogsTestSuite) TestAssertLogIsolated() {
156+
s.check.expected.Log.ExpectIds = []uint{920300}
157+
s.False(s.check.expected.Isolated)
158+
s.True(s.check.AssertLogs(), "Expected to find 920300")
159+
160+
s.check.expected.Isolated = true
161+
s.False(s.check.AssertLogs(), "Expected to find multiple IDs")
162+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.21
44

55
require (
66
github.com/Masterminds/sprig v2.22.0+incompatible
7-
github.com/coreruleset/ftw-tests-schema v1.1.1-0.20240509130026-48cda85f1d5f
7+
github.com/coreruleset/ftw-tests-schema v1.1.1-0.20240513043400-8a1a71caae7a
88
github.com/go-logr/zerologr v1.2.3
99
github.com/goccy/go-yaml v1.9.2
1010
github.com/google/uuid v1.6.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ github.com/antchfx/htmlquery v1.3.0/go.mod h1:zKPDVTMhfOmcwxheXUsx4rKJy8KEY/PU6e
99
github.com/antchfx/xpath v1.2.3 h1:CCZWOzv5bAqjVv0offZ2LVgVYFbeldKQVuLNbViZdes=
1010
github.com/antchfx/xpath v1.2.3/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
1111
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
12-
github.com/coreruleset/ftw-tests-schema v1.1.1-0.20240509130026-48cda85f1d5f h1:lncLdzrrw40U6m5C26lQ7T/EYzzWIlQonnb3JYSaKPE=
13-
github.com/coreruleset/ftw-tests-schema v1.1.1-0.20240509130026-48cda85f1d5f/go.mod h1:stf2FA6YhkPEMq30FzKvs97Qn5P9F7LHqWQbNGvGhNk=
12+
github.com/coreruleset/ftw-tests-schema v1.1.1-0.20240513043400-8a1a71caae7a h1:YbO+KcDY2UN1mQ0pZpGWpjlfAnXBu6PvrA+MangtxTE=
13+
github.com/coreruleset/ftw-tests-schema v1.1.1-0.20240513043400-8a1a71caae7a/go.mod h1:stf2FA6YhkPEMq30FzKvs97Qn5P9F7LHqWQbNGvGhNk=
1414
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
1515
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1616
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

runner/run.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import (
2323
"github.com/coreruleset/go-ftw/waflog"
2424
)
2525

26-
var errBadTestInput = errors.New("ftw/run: bad test input: choose between data, encoded_request, or raw_request")
27-
2826
// Run runs your tests with the specified Config.
2927
func Run(cfg *config.FTWConfiguration, tests []*test.FTWTest, c RunnerConfig, out *output.Output) (*TestRunContext, error) {
3028
out.Println("%s", out.Message("** Running go-ftw!"))
@@ -147,8 +145,8 @@ func RunStage(runContext *TestRunContext, ftwCheck *check.FTWCheck, testCase sch
147145
}
148146

149147
// Check sanity first
150-
if checkTestSanity(testInput) {
151-
return errBadTestInput
148+
if err := checkTestSanity(&stage); err != nil {
149+
return err
152150
}
153151

154152
// Do not even run test if result is overridden. Just use the override and display the overridden result.
@@ -289,12 +287,23 @@ func needToSkipTest(include *regexp.Regexp, exclude *regexp.Regexp, testCase *sc
289287
return result
290288
}
291289

292-
func checkTestSanity(testInput test.Input) bool {
293-
return (utils.IsNotEmpty(testInput.Data) && testInput.EncodedRequest != "") ||
294-
//nolint:staticcheck
295-
(utils.IsNotEmpty(testInput.Data) && testInput.RAWRequest != "") ||
296-
//nolint:staticcheck
297-
(testInput.EncodedRequest != "" && testInput.RAWRequest != "")
290+
func checkTestSanity(stage *schema.Stage) error {
291+
if utils.IsNotEmpty(stage.Input.Data) && stage.Input.EncodedRequest != "" {
292+
return errors.New("'data' and 'encoded_request' must not be set simultaneously")
293+
}
294+
//nolint:staticcheck
295+
if utils.IsNotEmpty(stage.Input.Data) && stage.Input.RAWRequest != "" {
296+
return errors.New("'data' and 'raw_request' must not be set simultaneously")
297+
}
298+
//nolint:staticcheck
299+
if stage.Input.EncodedRequest != "" && stage.Input.RAWRequest != "" {
300+
return errors.New("'encoded_request' and 'raw_request' must not be set simultaneously")
301+
}
302+
if len(stage.Output.Log.ExpectIds) != 1 && stage.Output.Isolated {
303+
return errors.New("'isolated' is only valid if 'expected_ids' has exactly one entry")
304+
}
305+
306+
return nil
298307
}
299308

300309
func displayResult(testCase *schema.Test, rc *TestRunContext, result TestResult, roundTripTime time.Duration, stageTime time.Duration) {

runner/run_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/rs/zerolog/log"
1717
"github.com/stretchr/testify/suite"
1818

19+
"github.com/coreruleset/ftw-tests-schema/types"
20+
"github.com/coreruleset/go-ftw/check"
1921
"github.com/coreruleset/go-ftw/config"
2022
"github.com/coreruleset/go-ftw/ftwhttp"
2123
"github.com/coreruleset/go-ftw/output"
@@ -530,3 +532,24 @@ func (s *runTestSuite) TestFailFast() {
530532
s.Equal(1, res.Stats.TotalFailed(), "Oops, test run failed!")
531533
s.Equal(2, res.Stats.Run)
532534
}
535+
536+
func (s *runTestSuite) TestIsolatedSanity() {
537+
rc := &TestRunContext{
538+
Config: s.cfg,
539+
}
540+
stage := types.Stage{
541+
Input: types.Input{},
542+
Output: types.Output{
543+
Isolated: true,
544+
Log: types.Log{
545+
ExpectIds: []uint{},
546+
},
547+
},
548+
}
549+
err := RunStage(rc, &check.FTWCheck{}, types.Test{}, stage)
550+
s.ErrorContains(err, "'isolated' is only valid if 'expected_ids' has exactly one entry")
551+
552+
stage.Output.Log.ExpectIds = []uint{1, 2}
553+
err = RunStage(rc, &check.FTWCheck{}, types.Test{}, stage)
554+
s.ErrorContains(err, "'isolated' is only valid if 'expected_ids' has exactly one entry")
555+
}

0 commit comments

Comments
 (0)