Skip to content

Commit

Permalink
Adding parallel_run_idx template function
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Hinderberger committed Jun 7, 2024
1 parent 40bba7a commit 259dd17
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 42 deletions.
42 changes: 25 additions & 17 deletions api_testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Suite struct {
manifestPath string
reporterRoot *report.ReportElement
index int
serverURL string
serverURL *url.URL
httpServer http.Server
httpServerProxy *httpproxy.Proxy
httpServerDir string
Expand Down Expand Up @@ -151,6 +151,12 @@ func NewTestSuite(config TestToolConfig, manifestPath string, manifestDir string
//Append suite manifest path to name, so we know in an automatic setup where the test is loaded from
suite.Name = fmt.Sprintf("%s (%s)", suite.Name, manifestPath)

// Parse serverURL
suite.serverURL, err = url.Parse(suite.Config.ServerURL)
if err != nil {
return nil, fmt.Errorf("can not load server url : %s", err)
}

// init store
err = suite.datastore.SetMap(suite.Store)
if err != nil {
Expand Down Expand Up @@ -187,7 +193,7 @@ func (ats *Suite) Run() bool {
ats.manifestDir,
ats.manifestPath,
child,
ats.loader,
ats.buildLoader(ats.loader, -1),
true, // parallel exec allowed for top-level tests
)

Expand Down Expand Up @@ -225,23 +231,21 @@ type TestContainer struct {
Path string
}

func (ats *Suite) parseAndRunTest(
v any, manifestDir, testFilePath string, r *report.ReportElement,
rootLoader template.Loader, allowParallelExec bool,
) bool {
//Init variables
// logrus.Warnf("Test %s, Prev delimiters: %#v", testFilePath, rootLoader.Delimiters)
func (ats *Suite) buildLoader(rootLoader template.Loader, parallelRunIdx int) template.Loader {
loader := template.NewLoader(ats.datastore)
loader.Delimiters = rootLoader.Delimiters
loader.HTTPServerHost = ats.HTTPServerHost
serverURL, err := url.Parse(ats.Config.ServerURL)
if err != nil {
logrus.Error(fmt.Errorf("can not load server url into test (%s): %s", testFilePath, err))
return false
}
loader.ServerURL = serverURL
loader.ServerURL = ats.serverURL
loader.OAuthClient = ats.Config.OAuthClient
loader.ParallelRunIdx = parallelRunIdx

return loader
}

func (ats *Suite) parseAndRunTest(
v any, manifestDir, testFilePath string, r *report.ReportElement,
loader template.Loader, allowParallelExec bool,
) bool {
// Parse PathSpec (if any) and determine number of parallel runs
parallelRuns := 1
if vStr, ok := v.(string); ok {
Expand Down Expand Up @@ -307,12 +311,16 @@ func (ats *Suite) parseAndRunTest(
waitGroup.Add(parallelRuns)

for runIdx := range parallelRuns {
runIdxCapture := runIdx

go func() {
defer waitGroup.Done()

for testIdx, testCase := range testCases {
var success bool

caseLoader := ats.buildLoader(loader, runIdxCapture)

// If testCase can be unmarshalled as string, we may have a
// reference to another test using @ notation at hand
var testCaseStr string
Expand All @@ -324,7 +332,7 @@ func (ats *Suite) parseAndRunTest(
filepath.Join(manifestDir, dir),
testFilePath,
r,
loader,
caseLoader,
false, // no parallel exec allowed in nested tests
)
} else {
Expand All @@ -336,8 +344,8 @@ func (ats *Suite) parseAndRunTest(
},
r,
testFilePath,
loader,
runIdx*len(testCases)+testIdx,
caseLoader,
runIdxCapture*len(testCases)+testIdx,
)
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/lib/template/template_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type Loader struct {
ServerURL *url.URL
OAuthClient util.OAuthClientsConfig
Delimiters delimiters

// ParallelRunIdx is the index of the Parallel Run that this Loader is used in
ParallelRunIdx int
}

func NewLoader(datastore *datastore.Datastore) Loader {
Expand Down Expand Up @@ -489,6 +492,11 @@ func (loader *Loader) Render(
q := u.Query()
return q.Get(qKey)
},
// parallel_run_idx returns the index of the Parallel Run that the current template
// is rendered in.
"parallel_run_idx": func() int {
return loader.ParallelRunIdx
},
}
tmpl, err := template.
New("tmpl").
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/check_collected_responses.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"responses": [
{{ range $idx, $n := N (datastore "n_parallel") }}
{{ if gt $idx 0 }}, {{ end }}
1
{{ $idx }}
{{ end }}
]
}
}
}
}
]
]
25 changes: 2 additions & 23 deletions test/parallel/parallel.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
[
{
"name": "bounce-json: bounce n=1",
"request": {
"server_url": "http://localhost{{ datastore `local_port` }}",
"endpoint": "bounce-json",
"method": "POST",
"body": {
"n": 1
}
},
"response": {
"statuscode": 200,
"body": {
"body": {
"n": 1
}
}
},
"store_response_qjson": {
"responses[]": "body.body.n"
}
}
]
"@parallel_case.json"
]
24 changes: 24 additions & 0 deletions test/parallel/parallel_case.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"name": "bounce-json: bounce n=1",
"request": {
"server_url": "http://localhost{{ datastore `local_port` }}",
"endpoint": "bounce-json",
"method": "POST",
"body": {
"n": {{ parallel_run_idx }}
}
},
"response": {
"statuscode": 200,
"body": {
"body": {
"n": {{ parallel_run_idx }}
}
}
},
"store_response_qjson": {
"responses[]": "body.body.n"
}
}
]

0 comments on commit 259dd17

Please sign in to comment.