runn

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2022 License: MIT Imports: 29 Imported by: 3

README

runn

runn ( means "Run N" ) is a package/tool for running operations following a scenario.

Key features of runn are:

  • As a tool for scenario testing.
  • As a test helper package for the Go language.
  • As a tool for automation.

Usage

runn can run a multi-step scenario following a runbook written in YAML format.

As a tool for scenario testing / As a tool for automation.

runn can run one or more runbooks.

$ runn list path/to/**/*.yml
  Desc                     Path
-----------------------------------------------------
  Login and get projects.  path/to/books/login.yml
  Login and logout.        path/to/books/logout.yml
  New project.             path/to/books/new.yml
$ runn run path/to/**/*.yml
Login and get projects. ... ok
Login and logout. ... ok
New project. ... ok

3 scenarios, 0 failures
As a test helper package for the Go language.

runn can also behave as a test helper for the Go language.

Run N runbooks
package main

import (
	"context"
	"database/sql"
	"log"
	"net/http/httptest"
	"testing"

	"github.com/k1LoW/runn"
	_ "github.com/lib/pq"
)

func TestRouter(t *testing.T) {
	ctx := context.Background()
	db, err := sql.Open("postgres", "user=root password=root host=localhost dbname=test sslmode=disable")
	if err != nil {
		log.Fatal(err)
	}
	ts := httptest.NewServer(NewRouter(db))
	t.Cleanup(func() {
		ts.Close()
		db.Close()
	})
	o, err := runn.Load("testdata/books/**/*.yml", runn.T(t), runn.Runner("req", ts.URL), runn.DBRunner("db", db))
	if err != nil {
		t.Fatal(err)
	}
	if err := o.RunN(ctx); err != nil {
		t.Fatal(err)
	}
}
Run single runbook
func TestRouter(t *testing.T) {
	ctx := context.Background()
	db, err := sql.Open("postgres", "user=root password=root host=localhost dbname=test sslmode=disable")
	if err != nil {
		log.Fatal(err)
	}
	ts := httptest.NewServer(NewRouter(db))
	t.Cleanup(func() {
		ts.Close()
		db.Close()
	})
	o, err := runn.New(runn.T(t), runn.Book("testdata/books/login.yml"), runn.Runner("req", ts.URL), runn.DBRunner("db", db))
	if err != nil {
		t.Fatal(err)
	}
	if err := o.Run(ctx); err != nil {
		t.Fatal(err)
	}
}

Runbook

The runbook file has the following format.

desc: Login and get projects.
runners:
  req: https://example.com/api/v1
  db: mysql://root:mypass@localhost:3306/testdb
vars:
  username: alice
steps:
  -
    db:
      query: SELECT * FROM users WHERE name = '{{ vars.username }}'
  -
    req:
      /login:
        post:
          body:
            application/json:
              email: "{{ steps[0].rows[0].email }}"
              password: "{{ steps[0].rows[0].password }}"
  -
    test: steps[1].res.status == 200
  -
    req:
      /projects:
        get:
          headers:
            Authorization: "token {{ steps[1].res.body.session_token }}"
          body: nil
  -
    test: steps[3].res.status == 200
  -
    test: len(steps[3].res.body.projects) > 0

color

Documentation is WIP

Alternatives

Documentation

Index

Constants

View Source
const (
	MediaTypeApplicationJSON = "application/json"
)

Variables

View Source
var (
	T       = AsTestHelper
	Runbook = Book
)

Functions

func GetDesc added in v0.4.0

func GetDesc(opt Option) string

func Load added in v0.2.0

func Load(pathp string, opts ...Option) (operators, error)

func LoadBook

func LoadBook(path string) (*book, error)

func New

func New(opts ...Option) (*operator, error)

func Paths added in v0.4.0

func Paths(pathp string) ([]string, error)

Types

type Option

type Option func(*book) error

func AsTestHelper

func AsTestHelper(t *testing.T) Option

func Book

func Book(path string) Option

func Books added in v0.4.0

func Books(pathp string) ([]Option, error)

func DBRunner

func DBRunner(name string, client *sql.DB) Option

func Debug added in v0.3.0

func Debug(debug bool) Option

func Desc

func Desc(desc string) Option

func HTTPRunner

func HTTPRunner(name, endpoint string, client *http.Client) Option

func Runner

func Runner(name, dsn string) Option

func Var added in v0.4.0

func Var(k string, v interface{}) Option

Directories

Path Synopsis
cmd
runn command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL