Skip to content

Commit

Permalink
feature: updating binary to support non-zero status codes and environ…
Browse files Browse the repository at this point in the history
…ment variables
  • Loading branch information
xoscar committed Jan 24, 2023
1 parent 0fe5e80 commit 096c929
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 155 deletions.
77 changes: 11 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,72 +29,17 @@ $ go install go.k6.io/xk6/cmd/xk6@latest
$ xk6 build --with github.com/kubeshop/xk6-tracetest@latest
```

## Example
## Available Variables

```javascript
import { Http, Tracetest } from "k6/x/tracetest";
import { sleep } from "k6";

export const options = {
vus: 1,
duration: "5s",
};

const http = new Http();
const tracetest = Tracetest({
serverUrl: "<your-tracetest-server-url>",
});
const testId = "<your-test-id>";

export default function () {
/// successful test run
http.get("http://localhost:8081/pokemon?take=5", {
tracetest: {
testId,
},
});

sleep(1);
}

export function handleSummary() {
return {
stdout: tracetest.summary(),
'tracetest.json': tracetest.json(),
};
}
```
If you want to configure the tracetest k6 binary you can do it by using any of the following environment variables

Result output:
- **XK6_TRACETEST_SERVER_URL:** Updates the tracetest server url for API interactions (can be overwritten by the script config)
- **XK6_TRACETEST_SERVER_PATH:** Updates the tracetest server path for API interactions (can be overwritten by the script config)

```bash
$ ./k6 run examples/test-from-id.js -o xk6-tracetest

/\ |‾‾| /‾‾/ /‾‾/
/\ / \ | |/ / / /
/ \/ \ | ( / ‾‾\
/ \ | |\ \ | (‾) |
/ __________ \ |__| \__\ \_____/ .io

execution: local
script: examples/test-from-id.js
output: xk6-tracetest-output (TestRunID: 79680)

scenarios: (100.00%) 1 scenario, 1 max VUs, 35s max duration (incl. graceful stop):
* default: 1 looping VUs for 5s (gracefulStop: 30s)


running (05.1s), 0/1 VUs, 5 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs 5s
[TotalRuns=8, SuccessfulRus=4, FailedRuns=4]
[FAILED]
[Request=GET - http://localhost:8081/pokemon?take=10, TraceID=dc0718f6bd99b3dc30cc624b154beb23, RunState=FINISHED FailingSpecs=false, TracetestURL= http://localhost:11633/test/nDdBCnoVg/run/28]
[Request=GET - http://localhost:8081/pokemon?take=10, TraceID=dc0718dacd99b3dc30dc0ed028659513, RunState=FINISHED FailingSpecs=false, TracetestURL= http://localhost:11633/test/nDdBCnoVg/run/25]
[Request=GET - http://localhost:8081/pokemon?take=10, TraceID=dc071882b699b3dc30f993c5e0a8b330, RunState=FINISHED FailingSpecs=false, TracetestURL= http://localhost:11633/test/nDdBCnoVg/run/26]
[Request=GET - http://localhost:8081/pokemon?take=10, TraceID=dc0718e3c599b3dc30c09b0fff1e83d7, RunState=FINISHED FailingSpecs=false, TracetestURL= http://localhost:11633/test/nDdBCnoVg/run/27]
[SUCCESSFUL]
[Request=GET - http://localhost:8081/pokemon?take=5, TraceID=dc0718cfcd99b3dc301f7fc40fa024a8, RunState=FINISHED FailingSpecs=false, TracetestURL= http://localhost:11633/test/J0d887oVR/run/145]
[Request=GET - http://localhost:8081/pokemon?take=5, TraceID=dc0718f2bd99b3dc300da669a9c1d4b5, RunState=FINISHED FailingSpecs=false, TracetestURL= http://localhost:11633/test/J0d887oVR/run/142]
[Request=GET - http://localhost:8081/pokemon?take=5, TraceID=dc0718e0c599b3dc30e774886605729d, RunState=FINISHED FailingSpecs=false, TracetestURL= http://localhost:11633/test/J0d887oVR/run/143]
[Request=GET - http://localhost:8081/pokemon?take=5, TraceID=dc0718f7b599b3dc30f5fab29c1b01f4, RunState=FINISHED FailingSpecs=false, TracetestURL= http://localhost:11633/test/J0d887oVR/run/144]
```
You can also set a default tracetest endpoint when running the k6 binary by using the following option:

`./k6 run examples/test-from-id.js -o xk6-tracetest=<server-url>`

## Example

To run a full example take a look at the fully flesh demo we have for you in the Tracetest main mono repo: [examples/tracetest-k6](https://github.com/kubeshop/tracetest/tree/main/examples/tracetest-k6)
45 changes: 0 additions & 45 deletions examples/test-from-id.js

This file was deleted.

34 changes: 34 additions & 0 deletions models/outputConfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package models

import (
"go.k6.io/k6/output"
)

var (
ENV_SERVER_URL = "XK6_TRACETEST_SERVER_URL"
ENV_SERVER_PATH = "XK6_TRACETEST_SERVER_PATH"
)

type OutputConfig struct {
ServerUrl string
ServerPath string
}

func NewConfig(params output.Params) (OutputConfig, error) {
cfg := OutputConfig{
ServerUrl: ServerURL,
ServerPath: ServerPath,
}

if params.ConfigArgument != "" {
cfg.ServerUrl = params.ConfigArgument
} else if val, ok := params.Environment[ENV_SERVER_URL]; ok {
cfg.ServerUrl = val
}

if val, ok := params.Environment[ENV_SERVER_PATH]; ok {
cfg.ServerPath = val
}

return cfg, nil
}
15 changes: 13 additions & 2 deletions models/tracetestRun.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,23 @@ type TracetestRun struct {
func (tr *TracetestRun) Summary(baseUrl string) string {
runUrl := fmt.Sprintf("%s/test/%s/run/%s", baseUrl, tr.TestId, *tr.TestRun.Id)

failingSpecs := false
failingSpecs := true
if tr.TestRun != nil && tr.TestRun.Result != nil && tr.TestRun.Result.AllPassed != nil {
failingSpecs = !*tr.TestRun.Result.AllPassed
}

return fmt.Sprintf("RunState=%s FailingSpecs=%t, TracetestURL= %s", *tr.TestRun.State, failingSpecs, runUrl)
lastError := ""
if tr.TestRun != nil && tr.TestRun.LastErrorState != nil {
lastError = *tr.TestRun.LastErrorState
}

summary := fmt.Sprintf("RunState=%s FailingSpecs=%t, TracetestURL= %s", *tr.TestRun.State, failingSpecs, runUrl)

if lastError != "" {
summary += fmt.Sprintf(", LastError=%s", lastError)
}

return summary
}

func (tr *TracetestRun) IsSuccessful() bool {
Expand Down
28 changes: 0 additions & 28 deletions modules/output/config.go

This file was deleted.

7 changes: 5 additions & 2 deletions modules/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math/rand"

"github.com/kubeshop/xk6-tracetest/models"
"github.com/kubeshop/xk6-tracetest/modules/tracetest"
"github.com/sirupsen/logrus"

Expand All @@ -12,7 +13,7 @@ import (
)

type Output struct {
config Config
config models.OutputConfig
testRunID int64
logger logrus.FieldLogger
tracetest *tracetest.Tracetest
Expand All @@ -21,11 +22,13 @@ type Output struct {
var _ output.Output = new(Output)

func New(params output.Params, tracetest *tracetest.Tracetest) (*Output, error) {
config, err := NewConfig(params)
config, err := models.NewConfig(params)
if err != nil {
return nil, err
}

tracetest.UpdateFromConfig(config)

return &Output{
config: config,
tracetest: tracetest,
Expand Down
34 changes: 22 additions & 12 deletions modules/tracetest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,38 @@ func (t *Tracetest) getIsTestReady(ctx context.Context, testID, testRunId string
return nil, nil
}

func (t *Tracetest) stringSummary() string {
failedSummary := "[FAILED] \n"
successfulSummary := "[SUCCESSFUL] \n"
totalRuns := 0
failedRuns := 0
successfulRuns := 0

func (t *Tracetest) jobSummary() (successfulJobs, failedJobs []models.Job) {
t.processedBuffer.Range(func(_, value interface{}) bool {
if job, ok := value.(models.Job); ok {
totalRuns += 1
if job.IsSuccessful() {
successfulSummary += fmt.Sprintf("[%s] \n", job.Summary(t.apiOptions.ServerUrl))
successfulRuns += 1
successfulJobs = append(successfulJobs, job)
} else {
failedSummary += fmt.Sprintf("[%s] \n", job.Summary(t.apiOptions.ServerUrl))
failedRuns += 1
failedJobs = append(failedJobs, job)
}
}

return true
})

return
}

func (t *Tracetest) stringSummary() string {
successfulJobs, failedJobs := t.jobSummary()
failedSummary := "[FAILED] \n"
successfulSummary := "[SUCCESSFUL] \n"
totalRuns := len(successfulJobs) + len(failedJobs)
failedRuns := len(failedJobs)
successfulRuns := len(successfulJobs)

for _, job := range failedJobs {
failedSummary += fmt.Sprintf("[%s] \n", job.Summary(t.apiOptions.ServerUrl))
}

for _, job := range successfulJobs {
successfulSummary += fmt.Sprintf("[%s] \n", job.Summary(t.apiOptions.ServerUrl))
}

totalResults := fmt.Sprintf("[TotalRuns=%d, SuccessfulRus=%d, FailedRuns=%d] \n", totalRuns, successfulRuns, failedRuns)

if failedRuns == 0 {
Expand Down
22 changes: 22 additions & 0 deletions modules/tracetest/tracetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tracetest

import (
"encoding/json"
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -41,6 +42,15 @@ func New() *Tracetest {
return tracetest
}

func (t *Tracetest) UpdateFromConfig(config models.OutputConfig) {
apiOptions := models.ApiOptions{
ServerUrl: config.ServerUrl,
ServerPath: config.ServerPath,
}

t.client = NewAPIClient(apiOptions)
}

func (t *Tracetest) Constructor(call goja.ConstructorCall) *goja.Object {
rt := t.Vu.Runtime()
apiOptions, err := models.NewApiOptions(t.Vu, call.Argument(0))
Expand All @@ -66,6 +76,18 @@ func (t *Tracetest) Summary() string {
return t.stringSummary()
}

func (t *Tracetest) ValidateResult() {
if len(t.buffer) != 0 {
t.processQueue()
}

_, failedJobs := t.jobSummary()

if len(failedJobs) > 0 {
panic(fmt.Sprintf("Tracetest: %d jobs failed", len(failedJobs)))
}
}

func (t *Tracetest) Json() string {
rt := t.Vu.Runtime()
jsonString, err := json.Marshal(t.jsonSummary())
Expand Down
File renamed without changes.
Loading

0 comments on commit 096c929

Please sign in to comment.