scenari

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2018 License: MIT Imports: 2 Imported by: 0

README

scenari: Simple scenario execution

GoDoc

This package provides a simple library for writing arbitrary scenarios, which consist of steps executed sequentially. It can be useful for performing unit/functional testing.

Example usage

This examples illustrates a chaos engineering scenario, where we describe different steps injecting various chaotic behavior into a web application using the Chaos HTTP middleware:

package main

import (
	"fmt"
	"math/rand"
	"net/http"
	"time"

	"github.com/falzm/chaos"
	"github.com/falzm/scenari"
)

var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))

func main() {
	chaoscli := chaos.NewClient("")

	if err := scenari.NewScenario("chaos").
		// Add a 1-second delay at a 0.5 probability to route POST /api/a
		Step(scenari.NewStep(func() error {
			return chaoscli.AddRouteChaos("POST", "/api/a", chaos.NewSpec().Delay(1000, 0.5))
		})).

		// Wait between 0 and 60 seconds
		Pause(time.Duration(rnd.Intn(60))*time.Second).

		// Add a 3-second delay at a 0.75 probability and return 504 status code error
		// at a 0.5 probability to route POST /api/a
		Step(scenari.NewStep(func() error {
			return chaoscli.AddRouteChaos("POST", "/api/a", chaos.NewSpec().
				Delay(3000, 0.75).
				Error(http.StatusGatewayTimeout, "Whoopsie!", 0.5))
		})).

		// Wait between 0 and 60 seconds
		Pause(time.Duration(rnd.Intn(60))*time.Second).

		// Repeat above scenario 2 more times, waiting 1 minute between each iteration
		Repeat(3, 1*time.Minute).
		Rollout(); err != nil {
		fmt.Printf("scenario rollout failed: %s\n", err)
	}

	// Once the scenario rollout is over, reset chaos effects to route POST /api/a
	chaoscli.DeleteRouteChaos("POST", "/api/a")
}

Documentation

Overview

Package scenari provides a simple interface to describe and execute arbitrary scenarios.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Scenario

type Scenario struct {
	// contains filtered or unexported fields
}

Scenario represents a scenario instance.

func NewScenario

func NewScenario(name string) *Scenario

NewScenario returns an new scenario instance.

func (*Scenario) CarryOn

func (s *Scenario) CarryOn() *Scenario

CarryOn sets the scenario rollout to continue even if one ore more steps return an error.

func (*Scenario) Pause

func (s *Scenario) Pause(d time.Duration) *Scenario

Pause injects a pause of duration d after the latest step added to the scenario.

func (*Scenario) Repeat

func (s *Scenario) Repeat(n int, delay time.Duration) *Scenario

Repeat sets the scenario to be repeated n times with a delay of duration d between each iteration.

func (*Scenario) Rollout

func (s *Scenario) Rollout() error

Rollout executes the scenario.

func (*Scenario) Step

func (s *Scenario) Step(step *Step) *Scenario

Step adds a new step in the scenario.

type Step

type Step struct {
	// contains filtered or unexported fields
}

Step reprensents a scenario step instance.

func NewStep

func NewStep(f func() error) *Step

NewStep returns a new scenario step instance.

func (*Step) PostExec

func (s *Step) PostExec(f func() error) *Step

PostExec defines a function f to be executed after the step function.

func (*Step) PreExec

func (s *Step) PreExec(f func() error) *Step

PreExec defines a function f to be executed before the step function.

Jump to

Keyboard shortcuts

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