Skip to content

Commit

Permalink
feat: user executor
Browse files Browse the repository at this point in the history
close #320

Signed-off-by: Yvonnick Esnault <[email protected]>
  • Loading branch information
yesnault committed Nov 22, 2020
1 parent 3825176 commit f71f03a
Show file tree
Hide file tree
Showing 26 changed files with 333 additions and 241 deletions.
4 changes: 2 additions & 2 deletions .build/go.mk
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ mk_go_build:
$(info *** mk_go_build)

mk_go_build_plugin:
@mkdir -p dist/lib && \
go build -buildmode=plugin -o dist/lib/$(TARGET_NAME).so
@mkdir -p dist/executors && \
go build -buildmode=plugin -o dist/executors/$(TARGET_NAME).so

mk_go_build_clean:
@rm -rf dist
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ profile.coverprofile
tests.log
*.dump.json
*.dump.html
*.jpg
*.jpg
test_results.xml
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ plugins: ## build all components and push them into dist directory
$(info Building plugin)
$(MAKE) build -C executors/plugins
$(MAKE) dist -C executors/plugins
@mkdir -p dist/lib && \
mv executors/plugins/dist/lib/* dist/lib;
@mkdir -p dist/executors && \
mv executors/plugins/dist/executors/* dist/executors;

dist: $(ALL_DIST_TARGETS)

Expand All @@ -70,6 +70,7 @@ clean: mk_go_clean ## delete directories dist and results and all temp files (co
@rm -rf ${RESULTS_DIR}
$(MAKE) clean -C cmd/venom
$(MAKE) clean -C executors/plugins
$(MAKE) clean -C tests

test-results: $(ALL_RESULTS_TARGETS)

Expand Down
15 changes: 8 additions & 7 deletions executors/dbfixtures/dbfixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

fixtures "github.com/go-testfixtures/testfixtures/v3"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
migrate "github.com/rubenv/sql-migrate"

// SQL drivers.
Expand Down Expand Up @@ -56,12 +57,12 @@ func (e Executor) Run(ctx context.Context, step venom.TestStep, workdir string)

db, err := sql.Open(e.Database, e.DSN)
if err != nil {
return nil, fmt.Errorf("failed to connect to database: %v", err)
return nil, errors.Wrapf(err, "failed to connect to database")
}
defer db.Close()

if err = db.Ping(); err != nil {
return nil, fmt.Errorf("failed to ping database: %v", err)
return nil, errors.Wrapf(err, "failed to ping database")
}
// Load and import the schemas in the database
// if the argument is specified.
Expand All @@ -74,7 +75,7 @@ func (e Executor) Run(ctx context.Context, step venom.TestStep, workdir string)
return nil, errs
}
if _, err = db.Exec(string(sbytes)); err != nil {
return nil, fmt.Errorf("failed to exec schema from file %s : %v", s, err)
return nil, errors.Wrapf(err, "failed to exec schema from file %q", s)
}
}
} else if e.Migrations != "" {
Expand Down Expand Up @@ -129,10 +130,10 @@ func loadFixtures(ctx context.Context, db *sql.DB, files []string, folder string
dialect)

if err != nil {
return fmt.Errorf("failed to create folder loader: %v", err)
return errors.Wrapf(err, "failed to create folder loader")
}
if err = loader.Load(); err != nil {
return fmt.Errorf("failed to load fixtures from folder %s: %v", path.Join(workdir, folder), err)
return errors.Wrapf(err, "failed to load fixtures from folder %q", path.Join(workdir, folder))
}
return nil
}
Expand All @@ -150,10 +151,10 @@ func loadFixtures(ctx context.Context, db *sql.DB, files []string, folder string
dialect)

if err != nil {
return fmt.Errorf("failed to create files loader: %v", err)
return errors.Wrapf(err, "failed to create files loader")
}
if err = loader.Load(); err != nil {
return fmt.Errorf("failed to load fixtures from files: %v", err)
return errors.Wrapf(err, "failed to load fixtures from files")
}
return nil
}
Expand Down
18 changes: 9 additions & 9 deletions executors/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,17 @@ func (Executor) Run(ctx context.Context, step venom.TestStep, workdir string) (i
}

// Create a tmp file
tmpscript, errt := ioutil.TempFile(os.TempDir(), "venom-")
if errt != nil {
return nil, fmt.Errorf("cannot create tmp file: %s", errt)
tmpscript, err := ioutil.TempFile(os.TempDir(), "venom-")
if err != nil {
return nil, fmt.Errorf("cannot create tmp file: %s", err)
}

// Put script in file
venom.Debug(ctx, "work with tmp file %s", tmpscript.Name())
n, errw := tmpscript.Write([]byte(scriptContent))
if errw != nil || n != len(scriptContent) {
if errw != nil {
return nil, fmt.Errorf("cannot write script: %s", errw)
n, err := tmpscript.Write([]byte(scriptContent))
if err != nil || n != len(scriptContent) {
if err != nil {
return nil, fmt.Errorf("cannot write script: %s", err)
}
return nil, fmt.Errorf("cannot write all script: %d/%d", n, len(scriptContent))
}
Expand All @@ -122,8 +122,8 @@ func (Executor) Run(ctx context.Context, step venom.TestStep, workdir string) (i
defer os.Remove(scriptPath)

// Chmod file
if errc := os.Chmod(scriptPath, 0755); errc != nil {
return nil, fmt.Errorf("cannot chmod script %s: %s", scriptPath, errc)
if err := os.Chmod(scriptPath, 0750); err != nil {
return nil, fmt.Errorf("cannot chmod script %s: %s", scriptPath, err)
}

start := time.Now()
Expand Down
8 changes: 4 additions & 4 deletions executors/imap/imap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package imap

import (
"context"
"errors"
"fmt"
"regexp"
"strings"
"time"

"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/yesnault/go-imap/imap"

"github.com/ovh/venom"
Expand Down Expand Up @@ -101,7 +101,7 @@ func (e *Executor) getMail(ctx context.Context) (*Mail, error) {

c, errc := connect(e.IMAPHost, e.IMAPPort, e.IMAPUser, e.IMAPPassword)
if errc != nil {
return nil, fmt.Errorf("error while connecting:%s", errc.Error())
return nil, errors.Wrapf(errc, "error while connecting")
}
defer c.Logout(5 * time.Second) // nolint

Expand All @@ -115,7 +115,7 @@ func (e *Executor) getMail(ctx context.Context) (*Mail, error) {

count, err := queryCount(c, box)
if err != nil {
return nil, fmt.Errorf("error while queryCount:%v", err)
return nil, errors.Wrapf(err, "error while queryCount")
}

venom.Debug(ctx, "count messages:%d", count)
Expand All @@ -126,7 +126,7 @@ func (e *Executor) getMail(ctx context.Context) (*Mail, error) {

messages, err := fetch(ctx, c, box, count)
if err != nil {
return nil, fmt.Errorf("Error while feching messages:%v", err)
return nil, errors.Wrapf(err, "Error while feching messages")
}
defer c.Close(false)

Expand Down
8 changes: 4 additions & 4 deletions executors/plugins/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

DIRS := `ls -d */ | cut -f1 -d'/'|grep -v dist`
ALL_PLUGINS := $(if ${TARGET_NAME},${TARGET_NAME}, $(filter-out $(TARGET_DIST), $(shell for plugin in $(DIRS); do echo $(basename "$$plugin"); done)))
TARGET_PLUGINS := $(foreach PLUGIN, $(ALL_PLUGINS), dist/lib/$(PLUGIN).so)
TARGET_PLUGINS := $(foreach PLUGIN, $(ALL_PLUGINS), dist/executors/$(PLUGIN).so)
TARGET_DIST := ./dist

define get_plugin_name
$(strip $(patsubst dist/lib/%.so, %,$(shell echo $(1))))
$(strip $(patsubst dist/executors/%.so, %,$(shell echo $(1))))
endef

$(TARGET_PLUGINS):
Expand All @@ -21,9 +21,9 @@ build: $(TARGET_PLUGINS)

.PHONY: dist
dist:
@mkdir -p dist/lib
@mkdir -p dist/executors
@for PLU in $(ALL_PLUGINS); do \
mv $$PLU/dist/lib/* dist/lib; \
mv $$PLU/dist/executors/* dist/executors; \
done;

.PHONY: clean
Expand Down
1 change: 1 addition & 0 deletions executors/plugins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Build venom and plugin:

```bash
$ make build
$ make plugins
```

Create a file `test.yml` testsuite:
Expand Down
2 changes: 1 addition & 1 deletion executors/plugins/odbc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ $ cd venom
$ make build
$ make plugins
$ # venom binary is generated into dist directory
$ # plugin binary is generated into dist/libs directory
$ # plugin binary is generated into dist/executors directory
$ cd dist
$ ./venom run ...
```
Expand Down
12 changes: 6 additions & 6 deletions executors/plugins/odbc/odbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"C"
"context"
"fmt"
"io/ioutil"
"path"

Expand All @@ -15,6 +14,7 @@ import (

"github.com/ovh/venom"
)
import "github.com/pkg/errors"

// Name of the executor.
const Name = "odbc"
Expand Down Expand Up @@ -55,7 +55,7 @@ func (e Executor) Run(ctx context.Context, step venom.TestStep, workdir string)
venom.Debug(ctx, "connecting to database odbc, %s\n", e.DSN)
db, err := sqlx.Connect("odbc", e.DSN)
if err != nil {
return nil, fmt.Errorf("failed to connect to database: %v", err)
return nil, errors.Wrapf(err, "failed to connect to database")
}
defer db.Close()

Expand All @@ -67,11 +67,11 @@ func (e Executor) Run(ctx context.Context, step venom.TestStep, workdir string)
venom.Debug(ctx, "Executing command number %d\n", i)
rows, err := db.Queryx(s)
if err != nil {
return nil, fmt.Errorf("failed to exec command number %d : %v", i, err)
return nil, errors.Wrapf(err, "failed to exec command number %d", i)
}
r, err := handleRows(rows)
if err != nil {
return nil, fmt.Errorf("failed to parse SQL rows for command number %d : %v", i, err)
return nil, errors.Wrapf(err, "failed to parse SQL rows for command number %d", i)
}
results = append(results, QueryResult{Rows: r})
}
Expand All @@ -84,11 +84,11 @@ func (e Executor) Run(ctx context.Context, step venom.TestStep, workdir string)
}
rows, err := db.Queryx(string(sbytes))
if err != nil {
return nil, fmt.Errorf("failed to exec SQL file %s : %v", file, err)
return nil, errors.Wrapf(err, "failed to exec SQL file %q", file)
}
r, err := handleRows(rows)
if err != nil {
return nil, fmt.Errorf("failed to parse SQL rows for SQL file %s : %v", file, err)
return nil, errors.Wrapf(err, "failed to parse SQL rows for SQL file %q", file)
}
results = append(results, QueryResult{Rows: r})
}
Expand Down
3 changes: 2 additions & 1 deletion executors/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
shellwords "github.com/mattn/go-shellwords"
"github.com/mitchellh/mapstructure"
"github.com/ovh/venom"
"github.com/pkg/errors"
)

// Name of executor
Expand Down Expand Up @@ -68,7 +69,7 @@ func (Executor) Run(ctx context.Context, step venom.TestStep, workdir string) (i
if e.FilePath != "" {
commands, err = file2lines(path.Join(workdir, e.FilePath))
if err != nil {
return nil, fmt.Errorf("Failed to load file %v", err)
return nil, errors.Wrapf(err, "Failed to load file")
}
} else {
commands = e.Commands
Expand Down
5 changes: 3 additions & 2 deletions executors/smtp/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"

"github.com/ovh/venom"
)
Expand Down Expand Up @@ -117,7 +118,7 @@ func (e *Executor) sendEmail(ctx context.Context) error {
if e.WithTLS {
conn, err := tls.Dial("tcp", servername, tlsconfig)
if err != nil {
return fmt.Errorf("tls dial error: %v", err)
return errors.Wrapf(err, "tls dial error")
}

c, err = smtp.NewClient(conn, e.Host)
Expand All @@ -128,7 +129,7 @@ func (e *Executor) sendEmail(ctx context.Context) error {
var err error
c, err = smtp.Dial(servername)
if err != nil {
return fmt.Errorf("tls dial error: %v", err)
return errors.Wrapf(err, "tls dial error")
}
defer c.Close()
}
Expand Down
12 changes: 6 additions & 6 deletions executors/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package sql

import (
"context"
"fmt"
"io/ioutil"
"path"

"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"

// MySQL drivers
_ "github.com/go-sql-driver/mysql"
Expand Down Expand Up @@ -63,7 +63,7 @@ func (e Executor) Run(ctx context.Context, step venom.TestStep, workdir string)
venom.Debug(ctx, "connecting to database %s, %s\n", e.Driver, e.DSN)
db, err := sqlx.Connect(e.Driver, e.DSN)
if err != nil {
return nil, fmt.Errorf("failed to connect to database: %v", err)
return nil, errors.Wrapf(err, "failed to connect to database")
}
defer db.Close()

Expand All @@ -75,11 +75,11 @@ func (e Executor) Run(ctx context.Context, step venom.TestStep, workdir string)
venom.Debug(ctx, "Executing command number %d\n", i)
rows, err := db.Queryx(s)
if err != nil {
return nil, fmt.Errorf("failed to exec command number %d : %v", i, err)
return nil, errors.Wrapf(err, "failed to exec command number %d", i)
}
r, err := handleRows(rows)
if err != nil {
return nil, fmt.Errorf("failed to parse SQL rows for command number %d : %v", i, err)
return nil, errors.Wrapf(err, "failed to parse SQL rows for command number %d", i)
}
results = append(results, QueryResult{Rows: r})
}
Expand All @@ -92,11 +92,11 @@ func (e Executor) Run(ctx context.Context, step venom.TestStep, workdir string)
}
rows, err := db.Queryx(string(sbytes))
if err != nil {
return nil, fmt.Errorf("failed to exec SQL file %s : %v", file, err)
return nil, errors.Wrapf(err, "failed to exec SQL file %q", file)
}
r, err := handleRows(rows)
if err != nil {
return nil, fmt.Errorf("failed to parse SQL rows for SQL file %s : %v", file, err)
return nil, errors.Wrapf(err, "failed to parse SQL rows for SQL file %q", file)
}
results = append(results, QueryResult{Rows: r})
}
Expand Down
5 changes: 3 additions & 2 deletions executors/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/gosimple/slug"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/sclevine/agouti"

"github.com/ovh/venom"
Expand Down Expand Up @@ -85,14 +86,14 @@ func (Executor) Setup(ctx context.Context, vars venom.H) (context.Context, error
webCtx.wd.Debug = venom.BoolVarFromCtx(ctx, "web.debug")

if err := webCtx.wd.Start(); err != nil {
return ctx, fmt.Errorf("Unable start web driver %v", err)
return ctx, errors.Wrapf(err, "Unable start web driver")
}

// Init Page
var err error
webCtx.Page, err = webCtx.wd.NewPage()
if err != nil {
return ctx, fmt.Errorf("Unable create new page: %v", err)
return ctx, errors.Wrapf(err, "Unable create new page")
}

var resizePage bool
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
Expand Down
Loading

0 comments on commit f71f03a

Please sign in to comment.