Skip to content

Commit

Permalink
feat: Enabling Test Resource definition string
Browse files Browse the repository at this point in the history
  • Loading branch information
xoscar committed Apr 3, 2024
1 parent ecf11ad commit a43be62
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
13 changes: 13 additions & 0 deletions examples/list-pokemons.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ export const options = {

const http = new Http();
const tracetest = Tracetest();
const definition = `type: Test
spec:
id: k6-test
name: K6 Test
trigger:
type: k6
specs:
- selector: span[tracetest.span.type="database" name="findMany pokeshop.pokemon" db.system="postgres" db.name="pokeshop" db.user="ashketchum" db.operation="findMany" db.sql.table="pokemon"]
name: Correct db name
assertions:
- attr:db.name = "pokeshop"
`;

export default function () {
const url = "http://localhost:8081/pokemon?take=5";
Expand All @@ -17,6 +29,7 @@ export default function () {
response.trace_id,
{
should_wait: true,
definition,
},
{
url,
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
go.opentelemetry.io/contrib/propagators/ot v1.12.0
go.opentelemetry.io/otel v1.19.0
go.opentelemetry.io/otel/trace v1.19.0
sigs.k8s.io/yaml v1.2.0
)

require (
Expand Down Expand Up @@ -56,4 +57,5 @@ require (
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
gopkg.in/guregu/null.v3 v3.3.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,11 @@ github.com/klauspost/compress v1.17.3/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6K
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubeshop/tracetest v0.16.2-rc.1 h1:zUT5UiynqX259Y8nPMrx+uDYaCTUiJgJ8762GzlLoBQ=
github.com/kubeshop/tracetest v0.16.2-rc.1/go.mod h1:WQ4vRh+wWvcPf39e5vRClyiZXdCe5e0YU5D9zsSJBmk=
Expand Down Expand Up @@ -216,6 +218,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e h1:zWKUYT07mGmVBH+9UgnHXd/ekCK99C8EbDSAt5qsjXE=
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e/go.mod h1:Yow6lPLSAXx2ifx470yD/nUe22Dv5vBvxK/UK9UUTVs=
Expand Down Expand Up @@ -590,6 +593,7 @@ google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs
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 h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
Expand All @@ -599,6 +603,7 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkep
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Expand All @@ -615,3 +620,5 @@ honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
2 changes: 2 additions & 0 deletions models/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Job struct {
TracetestOptions TracetestOptions
Error string
Metadata metadata.Metadata
Definition string
}

func NewJob(traceId string, options TracetestOptions, request Request) Job {
Expand All @@ -49,6 +50,7 @@ func NewJob(traceId string, options TracetestOptions, request Request) Job {
TracetestOptions: options,
RunGroupId: options.RunGroupId,
Metadata: metadata.GetMetadata(),
Definition: options.Definition,
}
}

Expand Down
3 changes: 3 additions & 0 deletions models/tracetestOptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type TracetestOptions struct {
ShouldWait bool
VariableName string
RunGroupId string
Definition string
}

func NewTracetestOptions(runTime *goja.Runtime, params *goja.Object) TracetestOptions {
Expand All @@ -27,6 +28,8 @@ func NewTracetestOptions(runTime *goja.Runtime, params *goja.Object) TracetestOp
switch key {
case "testId":
options.TestID = optionsObject.Get(key).String()
case "definition":
options.Definition = optionsObject.Get(key).String()
case "shouldWait":
options.ShouldWait = optionsObject.Get(key).ToBoolean()
case "variableName":
Expand Down
20 changes: 17 additions & 3 deletions modules/tracetest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/golang-jwt/jwt"
"github.com/kubeshop/tracetest/cli/openapi"
"github.com/kubeshop/xk6-tracetest/models"
"sigs.k8s.io/yaml"
)

var (
Expand Down Expand Up @@ -153,17 +154,30 @@ func getTokenClaims(tokenString string) (jwt.MapClaims, error) {
return claims, nil
}

func (t *Tracetest) upsertTest(ctx context.Context) (*openapi.TestResource, error) {
func (t *Tracetest) upsertTest(ctx context.Context, definition string) (*openapi.TestResource, error) {
req := t.client.ResourceApiApi.UpsertTest(ctx)
req = req.TestResource(defaultTest)
raw := defaultTest

if definition != "" {
rawJson, err := yaml.YAMLToJSON([]byte(definition))
if err != nil {
return nil, fmt.Errorf("could not parse YAML to JSON: %w", err)
}

err = json.Unmarshal(rawJson, &raw)
if err != nil {
return nil, fmt.Errorf("could not unmarshal definition: %w", err)
}
}

req = req.TestResource(raw)
test, _, err := t.client.ResourceApiApi.UpsertTestExecute(req)
return test, err
}

func (t *Tracetest) runTest(job *models.Job) (*openapi.TestRun, error) {
if job.TestID == "" {
_, err := t.upsertTest(context.Background())
_, err := t.upsertTest(context.Background(), job.Definition)
if err != nil {
return nil, fmt.Errorf("could not create test: %w", err)
}
Expand Down

0 comments on commit a43be62

Please sign in to comment.