Skip to content

Commit

Permalink
chore: reuse parameters to make the openapi file smaller (#2382)
Browse files Browse the repository at this point in the history
* reuse parameters to make the openapi file smaller

* remove files

* fix tests

* use runnableResourceSortBy in getResources
  • Loading branch information
mathnogueira committed Apr 14, 2023
1 parent fc49f09 commit 861a204
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 534 deletions.
550 changes: 86 additions & 464 deletions api/openapi.yaml

Large diffs are not rendered by default.

127 changes: 127 additions & 0 deletions api/parameters.yaml
@@ -0,0 +1,127 @@
version: 3.0.0
components:
parameters:

# Test parameters
testId:
in: path
name: testId
required: true
description: id of the test
schema:
type: string

# versioning and runs
runId:
in: path
name: runId
required: true
description: id of the run
schema:
type: integer

version:
in: path
name: version
required: true
description: version of the test
schema:
type: integer

# transaction
transactionId:
in: path
name: transactionId
required: true
description: id of the transaction
schema:
type: string

# Query parameters
take:
in: query
name: take
description: "indicates how many resources can be returned by each page"
schema:
type: integer
default: 20

skip:
in: query
name: skip
description: "indicates how many resources will be skipped when paginating"
schema:
type: integer
default: 0

query:
in: query
name: query
description: "query to search resources"
schema:
type: string

runnableResourceSortBy:
in: query
name: sortBy
description: "indicates the sort field for the resources"
schema:
type: string
enum: [created, name, last_run]

sortBy:
in: query
name: sortBy
description: "indicates the sort field for the resources"
schema:
type: string
enum: [created, name]

switchableResourceSortBy:
in: query
name: sortBy
description: "indicates the sort field for the resources"
schema:
type: string
enum: [type, enabled]

sortDirection:
in: query
name: sortDirection
description: "indicates the sort direction for the resources"
schema:
type: string
enum: [asc, desc]

# resource ids
configId:
in: path
name: configId
schema:
type: string
required: true
description: "ID of the configuration resource used on Tracetest. It should be set as 'current'"

demoId:
in: path
name: demoId
required: true
description: "ID of a demonstration used on Tracetest as quick start examples"
schema:
type: string

pollingProfileId:
in: path
name: pollingProfileId
required: true
description: "ID of a polling profile used on Tracetest to configure how to fetch traces in a test. It should be set as 'current'"
schema:
type: string

dataStoreId:
in: path
name: dataStoreId
required: true
description: "ID of a datastore used on Tracetest to configure how to fetch traces in a test"
schema:
type: string
67 changes: 17 additions & 50 deletions server/http/controller.go
Expand Up @@ -139,18 +139,14 @@ func (c *controller) GetTestSpecs(ctx context.Context, testID string) (openapi.I
return openapi.Response(200, c.mappers.Out.Specs(test.Specs)), nil
}

func (c *controller) GetTestResultSelectedSpans(ctx context.Context, testID, runID, selectorQuery string) (openapi.ImplResponse, error) {
func (c *controller) GetTestResultSelectedSpans(ctx context.Context, testID string, runID int32, selectorQuery string) (openapi.ImplResponse, error) {
selector, err := selectors.New(selectorQuery)

if err != nil {
return handleDBError(err), err
}

rid, err := strconv.Atoi(runID)
if err != nil {
return openapi.Response(http.StatusBadRequest, fmt.Errorf("%s is not a number", runID)), err
}
run, err := c.testDB.GetRun(ctx, id.ID(testID), rid)
run, err := c.testDB.GetRun(ctx, id.ID(testID), int(runID))
if err != nil {
return openapi.Response(http.StatusInternalServerError, ""), nil
}
Expand All @@ -174,12 +170,8 @@ func (c *controller) GetTestResultSelectedSpans(ctx context.Context, testID, run
return openapi.Response(http.StatusOK, res), nil
}

func (c *controller) GetTestRun(ctx context.Context, testID, runID string) (openapi.ImplResponse, error) {
rid, err := strconv.Atoi(runID)
if err != nil {
return openapi.Response(http.StatusBadRequest, fmt.Errorf("%s is not a number", runID)), err
}
run, err := c.testDB.GetRun(ctx, id.ID(testID), rid)
func (c *controller) GetTestRun(ctx context.Context, testID string, runID int32) (openapi.ImplResponse, error) {
run, err := c.testDB.GetRun(ctx, id.ID(testID), int(runID))
if err != nil {
return handleDBError(err), err
}
Expand All @@ -196,12 +188,8 @@ func (c *controller) GetTestRunEvents(ctx context.Context, testID string, runID
return openapi.Response(http.StatusOK, c.mappers.Out.TestRunEvents(events)), nil
}

func (c *controller) DeleteTestRun(ctx context.Context, testID, runID string) (openapi.ImplResponse, error) {
rid, err := strconv.Atoi(runID)
if err != nil {
return openapi.Response(http.StatusBadRequest, fmt.Errorf("%s is not a number", runID)), err
}
run, err := c.testDB.GetRun(ctx, id.ID(testID), rid)
func (c *controller) DeleteTestRun(ctx context.Context, testID string, runID int32) (openapi.ImplResponse, error) {
run, err := c.testDB.GetRun(ctx, id.ID(testID), int(runID))
if err != nil {
return handleDBError(err), err
}
Expand Down Expand Up @@ -256,17 +244,13 @@ func (c *controller) GetTests(ctx context.Context, take, skip int32, query strin
}), nil
}

func (c *controller) RerunTestRun(ctx context.Context, testID, runID string) (openapi.ImplResponse, error) {
func (c *controller) RerunTestRun(ctx context.Context, testID string, runID int32) (openapi.ImplResponse, error) {
test, err := c.testDB.GetLatestTestVersion(ctx, id.ID(testID))
if err != nil {
return handleDBError(err), err
}

rid, err := strconv.Atoi(runID)
if err != nil {
return openapi.Response(http.StatusBadRequest, fmt.Errorf("%s is not a number", runID)), err
}
run, err := c.testDB.GetRun(ctx, id.ID(testID), rid)
run, err := c.testDB.GetRun(ctx, id.ID(testID), int(runID))
if err != nil {
return handleDBError(err), err
}
Expand Down Expand Up @@ -322,13 +306,8 @@ func (c *controller) RunTest(ctx context.Context, testID string, runInformation
return openapi.Response(200, c.mappers.Out.Run(&run)), nil
}

func (c *controller) StopTestRun(_ context.Context, testID, runID string) (openapi.ImplResponse, error) {
parsedRunID, err := strconv.Atoi(runID)
if err != nil {
return openapi.Response(http.StatusBadRequest, err.Error()), nil
}

c.runner.StopTest(id.ID(testID), parsedRunID)
func (c *controller) StopTestRun(_ context.Context, testID string, runID int32) (openapi.ImplResponse, error) {
c.runner.StopTest(id.ID(testID), int(runID))

return openapi.Response(http.StatusOK, map[string]string{"result": "success"}), nil
}
Expand Down Expand Up @@ -359,18 +338,14 @@ func (c *controller) doUpdateTest(ctx context.Context, testID id.ID, updated mod
return openapi.Response(204, nil), nil
}

func (c *controller) DryRunAssertion(ctx context.Context, testID, runID string, def openapi.TestSpecs) (openapi.ImplResponse, error) {
rid, err := strconv.Atoi(runID)
if err != nil {
return openapi.Response(http.StatusBadRequest, fmt.Errorf("%s is not a number", runID)), err
}
run, err := c.testDB.GetRun(ctx, id.ID(testID), rid)
func (c *controller) DryRunAssertion(ctx context.Context, testID string, runID int32, def openapi.TestSpecs) (openapi.ImplResponse, error) {
run, err := c.testDB.GetRun(ctx, id.ID(testID), int(runID))
if err != nil {
return openapi.Response(http.StatusInternalServerError, ""), nil
}

if run.Trace == nil {
return openapi.Response(http.StatusUnprocessableEntity, fmt.Sprintf(`run "%s" has no trace associated`, runID)), nil
return openapi.Response(http.StatusUnprocessableEntity, fmt.Sprintf(`run "%d" has no trace associated`, runID)), nil
}

definition, err := c.mappers.In.Definition(def)
Expand All @@ -393,12 +368,8 @@ func (c *controller) DryRunAssertion(ctx context.Context, testID, runID string,
return openapi.Response(200, res), nil
}

func (c *controller) GetRunResultJUnit(ctx context.Context, testID, runID string) (openapi.ImplResponse, error) {
rid, err := strconv.Atoi(runID)
if err != nil {
return openapi.Response(http.StatusBadRequest, fmt.Errorf("%s is not a number", runID)), err
}
run, err := c.testDB.GetRun(ctx, id.ID(testID), rid)
func (c *controller) GetRunResultJUnit(ctx context.Context, testID string, runID int32) (openapi.ImplResponse, error) {
run, err := c.testDB.GetRun(ctx, id.ID(testID), int(runID))
if err != nil {
return handleDBError(err), err
}
Expand Down Expand Up @@ -439,12 +410,8 @@ func (c controller) GetTestVersionDefinitionFile(ctx context.Context, testID str
return openapi.Response(200, enc), nil
}

func (c controller) ExportTestRun(ctx context.Context, testID, runID string) (openapi.ImplResponse, error) {
rid, err := strconv.Atoi(runID)
if err != nil {
return openapi.Response(http.StatusBadRequest, fmt.Errorf("%s is not a number", runID)), err
}
run, err := c.testDB.GetRun(ctx, id.ID(testID), rid)
func (c controller) ExportTestRun(ctx context.Context, testID string, runID int32) (openapi.ImplResponse, error) {
run, err := c.testDB.GetRun(ctx, id.ID(testID), int(runID))
if err != nil {
return handleDBError(err), err
}
Expand Down
3 changes: 1 addition & 2 deletions server/http/controller_test.go
Expand Up @@ -2,7 +2,6 @@ package http_test

import (
"context"
"strconv"
"testing"

"github.com/kubeshop/tracetest/server/assertions/comparator"
Expand Down Expand Up @@ -104,7 +103,7 @@ func TestContains_Issue617(t *testing.T) {
f := setupController(t)
f.expectGetRun(exampleRun)

actual, err := f.c.DryRunAssertion(context.TODO(), exampleRun.TestID.String(), strconv.Itoa(exampleRun.ID), spec)
actual, err := f.c.DryRunAssertion(context.TODO(), exampleRun.TestID.String(), int32(exampleRun.ID), spec)
require.NoError(t, err)

assert.Equal(t, 200, actual.Code)
Expand Down
7 changes: 5 additions & 2 deletions server/http/custom_routes.go
Expand Up @@ -95,9 +95,12 @@ func (c *customController) GetTestRuns(w http.ResponseWriter, r *http.Request) {
func (c *customController) GetRunResultJUnit(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
testIdParam := params["testId"]
runIdParam := params["runId"]
runIdParam, err := strconv.Atoi(params["runId"])
if err != nil {
c.errorHandler(w, r, fmt.Errorf("could not convert runId to integer: %w", err), nil)
}

result, err := c.service.GetRunResultJUnit(r.Context(), testIdParam, runIdParam)
result, err := c.service.GetRunResultJUnit(r.Context(), testIdParam, int32(runIdParam))
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
Expand Down
Binary file added server/main
Binary file not shown.
16 changes: 8 additions & 8 deletions server/openapi/api.go
Expand Up @@ -95,12 +95,12 @@ type ApiApiServicer interface {
DeleteDataStore(context.Context, string) (ImplResponse, error)
DeleteEnvironment(context.Context, string) (ImplResponse, error)
DeleteTest(context.Context, string) (ImplResponse, error)
DeleteTestRun(context.Context, string, string) (ImplResponse, error)
DeleteTestRun(context.Context, string, int32) (ImplResponse, error)
DeleteTransaction(context.Context, string) (ImplResponse, error)
DeleteTransactionRun(context.Context, string, int32) (ImplResponse, error)
DryRunAssertion(context.Context, string, string, TestSpecs) (ImplResponse, error)
DryRunAssertion(context.Context, string, int32, TestSpecs) (ImplResponse, error)
ExecuteDefinition(context.Context, TextDefinition) (ImplResponse, error)
ExportTestRun(context.Context, string, string) (ImplResponse, error)
ExportTestRun(context.Context, string, int32) (ImplResponse, error)
ExpressionResolve(context.Context, ResolveRequestInfo) (ImplResponse, error)
GetDataStore(context.Context, string) (ImplResponse, error)
GetDataStoreDefinitionFile(context.Context, string) (ImplResponse, error)
Expand All @@ -109,10 +109,10 @@ type ApiApiServicer interface {
GetEnvironmentDefinitionFile(context.Context, string) (ImplResponse, error)
GetEnvironments(context.Context, int32, int32, string, string, string) (ImplResponse, error)
GetResources(context.Context, int32, int32, string, string, string) (ImplResponse, error)
GetRunResultJUnit(context.Context, string, string) (ImplResponse, error)
GetRunResultJUnit(context.Context, string, int32) (ImplResponse, error)
GetTest(context.Context, string) (ImplResponse, error)
GetTestResultSelectedSpans(context.Context, string, string, string) (ImplResponse, error)
GetTestRun(context.Context, string, string) (ImplResponse, error)
GetTestResultSelectedSpans(context.Context, string, int32, string) (ImplResponse, error)
GetTestRun(context.Context, string, int32) (ImplResponse, error)
GetTestRunEvents(context.Context, string, int32) (ImplResponse, error)
GetTestRuns(context.Context, string, int32, int32) (ImplResponse, error)
GetTestSpecs(context.Context, string) (ImplResponse, error)
Expand All @@ -126,10 +126,10 @@ type ApiApiServicer interface {
GetTransactionVersionDefinitionFile(context.Context, string, int32) (ImplResponse, error)
GetTransactions(context.Context, int32, int32, string, string, string) (ImplResponse, error)
ImportTestRun(context.Context, ExportedTestInformation) (ImplResponse, error)
RerunTestRun(context.Context, string, string) (ImplResponse, error)
RerunTestRun(context.Context, string, int32) (ImplResponse, error)
RunTest(context.Context, string, RunInformation) (ImplResponse, error)
RunTransaction(context.Context, string, RunInformation) (ImplResponse, error)
StopTestRun(context.Context, string, string) (ImplResponse, error)
StopTestRun(context.Context, string, int32) (ImplResponse, error)
TestConnection(context.Context, DataStore) (ImplResponse, error)
UpdateDataStore(context.Context, string, DataStore) (ImplResponse, error)
UpdateEnvironment(context.Context, string, Environment) (ImplResponse, error)
Expand Down

0 comments on commit 861a204

Please sign in to comment.