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

feat: endpoint to rerun test assertions on result #489

Merged
merged 3 commits into from
May 13, 2022
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
29 changes: 29 additions & 0 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/TestRunResult"

/tests/{testId}/results/{resultId}/select:
get:
tags:
Expand Down Expand Up @@ -245,6 +246,34 @@ paths:
items:
type: string

/tests/{testId}/results/{resultId}/rerun:
post:
tags:
- api
parameters:
- in: path
name: testId
schema:
type: string
format: uuid
required: true
- in: path
name: resultId
schema:
type: string
format: uuid
required: true
summary: "rerun a test result"
description: "rerun a test result"
operationId: rerunTestResult
responses:
200:
description: successful operation
content:
application/json:
schema:
$ref: "#/components/schemas/TestRunResult"

/tests/{testId}/results:
get:
tags:
Expand Down
2 changes: 1 addition & 1 deletion server/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (a *App) Start() error {
runner.Start(5) // worker count. should be configurable
defer runner.Stop()

controller := httpServer.NewController(a.traceDB, a.db, runner)
controller := httpServer.NewController(a.traceDB, a.db, runner, assertionRunner)
apiApiController := openapi.NewApiApiController(controller)

router := openapi.NewRouter(apiApiController)
Expand Down
53 changes: 46 additions & 7 deletions server/http/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@ import (
)

type controller struct {
traceDB tracedb.TraceDB
testDB testdb.Repository
runner executor.Runner
traceDB tracedb.TraceDB
testDB testdb.Repository
runner executor.Runner
assertionRunner executor.AssertionRunner
}

func NewController(traceDB tracedb.TraceDB, testDB testdb.Repository, runner executor.Runner) openapi.ApiApiServicer {
func NewController(
traceDB tracedb.TraceDB,
testDB testdb.Repository,
runner executor.Runner,
assertionRunner executor.AssertionRunner,
) openapi.ApiApiServicer {
return &controller{
traceDB: traceDB,
testDB: testDB,
runner: runner,
traceDB: traceDB,
testDB: testDB,
runner: runner,
assertionRunner: assertionRunner,
}
}

Expand Down Expand Up @@ -292,3 +299,35 @@ func (s *controller) GetTestResultSelectedSpans(ctx context.Context, testID stri

return openapi.Response(http.StatusOK, selectedSpanIds), nil
}

func (s *controller) RerunTestResult(ctx context.Context, testID string, resultID string) (openapi.ImplResponse, error) {
test, err := s.testDB.GetTest(ctx, testID)
if err != nil {
return openapi.Response(http.StatusInternalServerError, err.Error()), err
}

result, err := s.testDB.GetResult(ctx, resultID)
if err != nil {
return openapi.Response(http.StatusInternalServerError, err.Error()), err
}

result.State = executor.TestRunStateAwaitingTestResults
err = s.testDB.UpdateResult(ctx, result)
if err != nil {
return openapi.Response(http.StatusInternalServerError, err.Error()), err
}

testDefinition, err := executor.ConvertAssertionsIntoTestDefinition(test.Assertions)
if err != nil {
return openapi.Response(http.StatusInternalServerError, err.Error()), err
}

assertionRequest := executor.AssertionRequest{
TestDefinition: testDefinition,
Result: *result,
}

s.assertionRunner.RunAssertions(assertionRequest)

return openapi.Response(http.StatusOK, result), nil
}
2 changes: 2 additions & 0 deletions server/openapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ApiApiRouter interface {
GetTestResultSelectedSpans(http.ResponseWriter, *http.Request)
GetTestResults(http.ResponseWriter, *http.Request)
GetTests(http.ResponseWriter, *http.Request)
RerunTestResult(http.ResponseWriter, *http.Request)
RunTest(http.ResponseWriter, *http.Request)
UpdateAssertion(http.ResponseWriter, *http.Request)
UpdateTest(http.ResponseWriter, *http.Request)
Expand All @@ -49,6 +50,7 @@ type ApiApiServicer interface {
GetTestResultSelectedSpans(context.Context, string, string, string) (ImplResponse, error)
GetTestResults(context.Context, string, int32, int32) (ImplResponse, error)
GetTests(context.Context, int32, int32) (ImplResponse, error)
RerunTestResult(context.Context, string, string) (ImplResponse, error)
RunTest(context.Context, string) (ImplResponse, error)
UpdateAssertion(context.Context, string, string, Assertion) (ImplResponse, error)
UpdateTest(context.Context, string, Test) (ImplResponse, error)
Expand Down
24 changes: 24 additions & 0 deletions server/openapi/api_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ func (c *ApiApiController) Routes() Routes {
"/api/tests",
c.GetTests,
},
{
"RerunTestResult",
strings.ToUpper("Post"),
"/api/tests/{testId}/results/{resultId}/rerun",
c.RerunTestResult,
},
{
"RunTest",
strings.ToUpper("Post"),
Expand Down Expand Up @@ -343,6 +349,24 @@ func (c *ApiApiController) GetTests(w http.ResponseWriter, r *http.Request) {

}

// RerunTestResult - rerun a test result
func (c *ApiApiController) RerunTestResult(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
testIdParam := params["testId"]

resultIdParam := params["resultId"]

result, err := c.service.RerunTestResult(r.Context(), testIdParam, resultIdParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)

}

// RunTest - run test
func (c *ApiApiController) RunTest(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
Expand Down