Skip to content

Commit

Permalink
refactor: prepare for external addons
Browse files Browse the repository at this point in the history
  • Loading branch information
szkiba committed Jun 12, 2023
1 parent 10fa731 commit b788376
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 174 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ linters:
- gochecknoglobals
- lll
- wrapcheck
- gochecknoinits
# deprecated
- deadcode
- nosnakecase
Expand Down
16 changes: 13 additions & 3 deletions g0/assert.go → g0/addon/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MIT

package g0
package addon

import (
"errors"
Expand All @@ -18,11 +18,22 @@ import (
"go.k6.io/k6/metrics"
)

type TestingT interface {
assert.TestingT
require.TestingT

Check(string, bool)
}

type checker struct {
vu modules.VU
fail bool
}

func NewTestingT(vu modules.VU, fail bool) TestingT {
return &checker{vu: vu, fail: fail}
}

func (c *checker) Errorf(format string, args ...interface{}) {
state := c.vu.State()
if state == nil {
Expand Down Expand Up @@ -97,8 +108,7 @@ func (c *checker) FailNow() {
}

var (
_ assert.TestingT = (*checker)(nil)
_ require.TestingT = (*checker)(nil)
_ TestingT = (*checker)(nil)

errAssertionFailed = errors.New("assertion failed")
)
128 changes: 128 additions & 0 deletions g0/addon/transport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// SPDX-FileCopyrightText: 2023 Iván Szkiba
//
// SPDX-License-Identifier: MIT

package addon

import (
"bytes"
"io"
"net/http"
"strconv"
"strings"
"time"

"go.k6.io/k6/js/modules"
khttp "go.k6.io/k6/js/modules/k6/http"
"go.k6.io/k6/lib/netext/httpext"
)

type tripware struct {
vu modules.VU
}

var _ http.RoundTripper = (*tripware)(nil)

func NewTransport(vu modules.VU) http.RoundTripper {
return &tripware{vu: vu}
}

const (
httpTimeout = 60 * time.Second
protoFields = 2
)

func (t *tripware) toParsedRequest(req *http.Request) (*httpext.ParsedHTTPRequest, error) {
state := t.vu.State()
if state == nil {
return nil, khttp.ErrHTTPForbiddenInInitContext
}

u := req.URL.String()

url, err := httpext.NewURL(u, u)
if err != nil {
return nil, err
}

preq := &httpext.ParsedHTTPRequest{ // nolint:exhaustruct
URL: &url,
Req: req,
Timeout: httpTimeout,
Throw: state.Options.Throw.Bool,
Redirects: state.Options.MaxRedirects,
Cookies: make(map[string]*httpext.HTTPRequestCookie),
TagsAndMeta: t.vu.State().Tags.GetCurrentValues(),
}

if state.Options.DiscardResponseBodies.Bool {
preq.ResponseType = httpext.ResponseTypeNone
} else {
preq.ResponseType = httpext.ResponseTypeBinary
}

if req.Body != nil {
data, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}

preq.Body = bytes.NewBuffer(data)

req.Body.Close()
}

preq.Req.Header.Set("User-Agent", state.Options.UserAgent.String)

return preq, nil
}

func (t *tripware) toResponse(req *http.Request, eresp *httpext.Response) (*http.Response, error) {
resp := new(http.Response)

resp.Request = req

resp.Header = http.Header{}

for k, v := range eresp.Headers {
resp.Header.Set(k, v)
}

resp.Proto = eresp.Proto
fields := strings.SplitN(resp.Proto, "/", protoFields)

if major, err := strconv.Atoi(fields[0]); err == nil {
resp.ProtoMajor = major
}

if len(fields) > 0 {
if minor, err := strconv.Atoi(fields[1]); err == nil {
resp.ProtoMinor = minor
}
}

if eresp.Body != nil {
if data, ok := eresp.Body.([]byte); ok {
resp.Body = io.NopCloser(bytes.NewBuffer(data))
}
}

resp.Status = eresp.StatusText
resp.StatusCode = eresp.Status

return resp, nil
}

func (t *tripware) RoundTrip(req *http.Request) (*http.Response, error) {
preq, err := t.toParsedRequest(req)
if err != nil {
return nil, err
}

resp, err := httpext.MakeRequest(t.vu.Context(), t.vu.State(), preq)
if err != nil {
return nil, err
}

return t.toResponse(req, resp)
}
40 changes: 40 additions & 0 deletions g0/bootstrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-FileCopyrightText: 2023 Iván Szkiba
//
// SPDX-License-Identifier: MIT

package g0

import (
"github.com/szkiba/xk6-g0/internal/builtin/gjson"
"github.com/szkiba/xk6-g0/internal/builtin/gofakeit"
"github.com/szkiba/xk6-g0/internal/builtin/goquery"
"github.com/szkiba/xk6-g0/internal/builtin/jsonpath"
"github.com/szkiba/xk6-g0/internal/builtin/jsonschema"
"github.com/szkiba/xk6-g0/internal/builtin/logrus"
"github.com/szkiba/xk6-g0/internal/builtin/resty"
"github.com/szkiba/xk6-g0/internal/builtin/stdlib"
"github.com/szkiba/xk6-g0/internal/builtin/testify"
"go.k6.io/k6/js/modules"
)

func registerBuiltins() {
RegisterExports(stdlib.Exports)
RegisterExports(logrus.Exports)
RegisterExports(resty.Exports)
RegisterExports(testify.Exports)
RegisterExports(goquery.Exports)
RegisterExports(gjson.Exports)
RegisterExports(jsonpath.Exports)
RegisterExports(jsonschema.Exports)
RegisterExports(gofakeit.Exports)
}

func registerExtension() {
modules.Register("k6/x/g0", New())
}

func Bootstrap() {
registerBuiltins()
redirectStdin()
registerExtension()
}
2 changes: 1 addition & 1 deletion g0/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func isRunCommand(args []string) (bool, int) {
return true, scriptIndex
}

func RedirectStdin() {
func redirectStdin() {
isRun, scriptIndex := isRunCommand(os.Args)
if !isRun {
return
Expand Down
21 changes: 0 additions & 21 deletions g0/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ import (
"sync"

"github.com/imdario/mergo"
"github.com/szkiba/xk6-g0/internal/builtin/gjson"
"github.com/szkiba/xk6-g0/internal/builtin/gofakeit"
"github.com/szkiba/xk6-g0/internal/builtin/goquery"
"github.com/szkiba/xk6-g0/internal/builtin/jsonpath"
jsonschema "github.com/szkiba/xk6-g0/internal/builtin/jsonscema"
"github.com/szkiba/xk6-g0/internal/builtin/logrus"
"github.com/szkiba/xk6-g0/internal/builtin/resty"
"github.com/szkiba/xk6-g0/internal/builtin/stdlib"
"github.com/szkiba/xk6-g0/internal/builtin/testify"
"github.com/traefik/yaegi/interp"
"go.k6.io/k6/js/modules"
)
Expand Down Expand Up @@ -55,15 +46,3 @@ func (r *exportsRegistry) merge(vu modules.VU) (interp.Exports, error) { //nolin

return symbols, nil
}

func init() {
RegisterExports(stdlib.Exports)
RegisterExports(logrus.Exports)
RegisterExports(resty.Exports)
RegisterExports(testify.Exports)
RegisterExports(goquery.Exports)
RegisterExports(gjson.Exports)
RegisterExports(jsonpath.Exports)
RegisterExports(jsonschema.Exports)
RegisterExports(gofakeit.Exports)
}
5 changes: 3 additions & 2 deletions g0/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package g0
import (
"os"

"github.com/szkiba/xk6-g0/g0/addon"
"github.com/szkiba/xk6-g0/internal/builtin/testify/assertions"
"github.com/szkiba/xk6-g0/internal/builtin/testify/requirements"
"github.com/traefik/yaegi/interp"
Expand All @@ -27,8 +28,8 @@ func (root *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { // n
mod := &Module{ //nolint:exhaustruct
vu: vu,
yaegi: yaegi,
assert: assertions.New(&checker{fail: false, vu: vu}),
require: requirements.New(&checker{fail: true, vu: vu}),
assert: assertions.New(addon.NewTestingT(vu, false)),
require: requirements.New(addon.NewTestingT(vu, true)),
}

if err := mod.init(os.Getenv(envScript)); err != nil {
Expand Down
8 changes: 1 addition & 7 deletions register.go → init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@ package g0

import (
"github.com/szkiba/xk6-g0/g0"
"go.k6.io/k6/js/modules"
)

func init() { //nolint:gochecknoinits
register()
g0.RedirectStdin()
}

func register() {
modules.Register("k6/x/g0", g0.New())
g0.Bootstrap()
}
4 changes: 2 additions & 2 deletions internal/builtin/goquery/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/PuerkitoBio/goquery"
"github.com/imdario/mergo"
"github.com/szkiba/xk6-g0/internal/builtin/stdlib"
"github.com/szkiba/xk6-g0/g0/addon"
"github.com/traefik/yaegi/interp"
"go.k6.io/k6/js/modules"
)
Expand All @@ -21,7 +21,7 @@ var Symbols = interp.Exports{}

func Exports(vu modules.VU) interp.Exports {
newDocument := func(url string) (*goquery.Document, error) {
client := &http.Client{Transport: stdlib.NewTransport(vu)}
client := &http.Client{Transport: addon.NewTransport(vu)}

resp, err := client.Get(url)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/builtin/resty/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/go-resty/resty/v2"
"github.com/imdario/mergo"
"github.com/szkiba/xk6-g0/internal/builtin/stdlib"
"github.com/szkiba/xk6-g0/g0/addon"
"github.com/traefik/yaegi/interp"
"go.k6.io/k6/js/modules"
)
Expand All @@ -19,7 +19,7 @@ var Symbols = interp.Exports{}
//go:generate yaegi extract -name resty github.com/go-resty/resty/v2

func Exports(vu modules.VU) interp.Exports {
transport := stdlib.NewTransport(vu)
transport := addon.NewTransport(vu)
newClient := func() *resty.Client {
client := resty.New()

Expand Down
Loading

0 comments on commit b788376

Please sign in to comment.