Skip to content

Commit

Permalink
feat: Cloud Support (#7)
Browse files Browse the repository at this point in the history
* feat: get jwt from token

* chore: update openapi package

* feat: improvements to allow using app.tracetest.io

* docs: add doc on how to run k6 using https://app.tracetest.io

* feat: Adding Cloud Support

* feat: Adding Cloud Support

* feat: Adding Cloud Support

* feat: Adding Cloud Support

* feat: Adding Cloud Support

* feat: fixing test run link

---------

Co-authored-by: Matheus Nogueira <[email protected]>
  • Loading branch information
xoscar and mathnogueira authored Nov 8, 2023
1 parent 2c72e7e commit 0360da7
Show file tree
Hide file tree
Showing 135 changed files with 19,021 additions and 6,588 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# xk6-tracetest

This extension adds tracetest support to [k6](https://github.com/grafana/k6)!
This extension adds tracetest support to [k6](https://github.com/grafana/k6)!

That means that if you're testing an instrumented system, you can use this extension to trigger test runs.

Expand Down Expand Up @@ -35,6 +35,7 @@ If you want to configure the tracetest k6 binary you can do it by using any of t

- **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)
- **XK6_TRACETEST_SERVER_PATH:** Updates the tracetest server token that will be used to authenticate with the server (can be overwritten by the script config)

You can also set a default tracetest endpoint when running the k6 binary by using the following option:

Expand All @@ -43,3 +44,7 @@ You can also set a default tracetest endpoint when running the k6 binary by usin
## 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)

## Running on https://app.tracetest.io

`./k6 run examples/test-from-id.js --env XK6_TRACETEST_API_TOKEN=<your token> -o xk6-tracetest=https://api.tracetest.io`
41 changes: 41 additions & 0 deletions examples/test-from-id-queue-cloud.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Http, Tracetest } from "k6/x/tracetest";
import { sleep } from "k6";

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

const http = new Http();
const testId = "c80sJ_4SR";
const tracetest = Tracetest();
tracetest.updateFromConfig({
api_token: "your-api-token",
});

export default function () {
const url = "http://localhost:8081/pokemon?take=5";
const response = http.get(url);

tracetest.runTest(
response.trace_id,
{
test_id: testId,
variable_name: "TRACE_ID",
should_wait: true,
},
{
id: "123",
url,
method: "GET",
}
);

sleep(1);
}

export function handleSummary() {
return {
stdout: tracetest.summary(),
};
}
26 changes: 18 additions & 8 deletions examples/test-from-id-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,30 @@ export const options = {
duration: "5s",
};

const http = new Http({ propagator: ["b3"] });
const http = new Http();
const testId = "J0d887oVR";
const tracetest = Tracetest({
serverUrl: "http://localhost:3000",
const tracetest = Tracetest();

tracetest.updateFromConfig({
server_url: "http://localhost:11633",
});

export default function () {
const url = "http://localhost:8081/pokemon?take=5";
const response = http.get(url);
tracetest.runTest(testId, response.trace_id, true, {
id: "123",
url,
method: "GET",
});
tracetest.runTest(
response.trace_id,
{
test_id: testId,
should_wait: true,
variable_name: "TRACE_ID",
},
{
id: "123",
url,
method: "GET",
}
);

sleep(1);
}
Expand Down
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.19

require (
github.com/dop251/goja v0.0.0-20230531210528-d7324b2d74f7
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/sirupsen/logrus v1.9.0
go.k6.io/k6 v0.45.1
go.opentelemetry.io/contrib/propagators/aws v1.12.0
Expand All @@ -12,7 +13,6 @@ require (
go.opentelemetry.io/contrib/propagators/ot v1.12.0
go.opentelemetry.io/otel v1.11.2
go.opentelemetry.io/otel/trace v1.11.2
golang.org/x/oauth2 v0.4.0
)

require (
Expand All @@ -26,7 +26,6 @@ require (
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-sourcemap/sourcemap v2.1.4-0.20211119122758-180fcef48034+incompatible // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.16.5 // indirect
Expand All @@ -50,7 +49,5 @@ require (
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/guregu/null.v3 v3.3.0 // indirect
)
15 changes: 2 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,19 @@ github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5Nq
github.com/go-sourcemap/sourcemap v2.1.4-0.20211119122758-180fcef48034+incompatible h1:bopx7t9jyUNX1ebhr0G4gtQWmUOgwQRI0QsYhdYLgkU=
github.com/go-sourcemap/sourcemap v2.1.4-0.20211119122758-180fcef48034+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 h1:4/hN5RUoecvl+RmJRE2YxKWtnnQls6rQjjW5oV7qg2U=
github.com/google/pprof v0.0.0-20230207041349-798e818bf904/go.mod h1:uglQLonpP8qtYCYyzA+8c/9qtqgA3qsXGYqCPKARAFg=
Expand Down Expand Up @@ -146,7 +144,6 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
Expand All @@ -156,8 +153,6 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/oauth2 v0.4.0 h1:NF0gk8LVPg1Ml7SSbGyySuoxdsXitj7TvgvuRxIMc/M=
golang.org/x/oauth2 v0.4.0/go.mod h1:RznEsdpjGAINPTOF0UH/t+xJ75L18YO3Ho6Pyn+uRec=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -187,7 +182,6 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
Expand All @@ -205,8 +199,6 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 h1:DdoeryqhaXp1LtT/emMP1BRJPHHKFi5akj/nbx/zNTA=
google.golang.org/grpc v1.55.0 h1:3Oj82/tFSCeUrRTg/5E/7d/W5A1tj6Ky1ABAuZuv5ag=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
Expand All @@ -215,10 +207,7 @@ google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQ
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
7 changes: 6 additions & 1 deletion models/apiOptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ import (
type ApiOptions struct {
ServerUrl string
ServerPath string
APIToken string
}

const (
DefaultServerUrl = "http://localhost:3000"
DefaultServerUrl = "https://app.tracetest.io"
ServerURL = "serverUrl"
ServerPath = "serverPath"
APIToken = "apiToken"
)

func NewApiOptions(vu modules.VU, val goja.Value) (ApiOptions, error) {
rawOptions := utils.ParseOptions(vu, val)
options := ApiOptions{
ServerUrl: DefaultServerUrl,
ServerPath: "",
APIToken: "",
}

if len(rawOptions) == 0 {
Expand All @@ -36,6 +39,8 @@ func NewApiOptions(vu modules.VU, val goja.Value) (ApiOptions, error) {
options.ServerUrl = value.ToString().String()
case ServerPath:
options.ServerPath = value.ToString().String()
case APIToken:
options.APIToken = value.ToString().String()
default:
return options, fmt.Errorf("unknown Tracetest option '%s'", key)
}
Expand Down
2 changes: 1 addition & 1 deletion models/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewJob(traceId string, options TracetestOptions, request Request) Job {
}

func (job Job) HandleRunResponse(run *openapi.TestRun, err error) Job {
if run == nil {
if err != nil {
job.JobStatus = Failed
job.Error = err.Error()
} else {
Expand Down
13 changes: 9 additions & 4 deletions models/outputConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,33 @@ import (
var (
ENV_SERVER_URL = "XK6_TRACETEST_SERVER_URL"
ENV_SERVER_PATH = "XK6_TRACETEST_SERVER_PATH"
ENV_API_TOKEN = "XK6_TRACETEST_API_TOKEN"
)

type OutputConfig struct {
ServerUrl string
ServerPath string
APIToken string
}

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

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

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

if val, ok := params.Environment[ENV_API_TOKEN]; ok {
cfg.APIToken = val
}

return cfg, nil
}
4 changes: 3 additions & 1 deletion models/tracetestOptions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package models

import "github.com/dop251/goja"
import (
"github.com/dop251/goja"
)

type TracetestOptions struct {
TestID string
Expand Down
2 changes: 1 addition & 1 deletion models/tracetestRun.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type TracetestRun struct {
}

func (tr *TracetestRun) Summary(baseUrl string) string {
runUrl := fmt.Sprintf("%s/test/%s/run/%s", baseUrl, tr.TestId, *tr.TestRun.Id)
runUrl := fmt.Sprintf("%s/test/%s/run/%d", baseUrl, tr.TestId, *tr.TestRun.Id)

failingSpecs := true
if tr.TestRun != nil && tr.TestRun.Result != nil && tr.TestRun.Result.AllPassed != nil {
Expand Down
4 changes: 4 additions & 0 deletions modules/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type Output struct {
var _ output.Output = new(Output)

func New(params output.Params, tracetest *tracetest.Tracetest) (*Output, error) {
if tracetest == nil {
return nil, fmt.Errorf("tracetest must not be nil")
}

config, err := models.NewConfig(params)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 0360da7

Please sign in to comment.