livetest

package
v0.1.76 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package livetest implements a set of helpers that ease writing of a sidecar that tests the functions of a service.

Assuring the functional correctness of a service is an important task for a production grade system. This package aims to provide helpers that allow a go test like experience for building functional health tests in production.

Test functions need to be written similarly to the regular go test function format. Only difference is the use of the testing.TB interface.

If a test failed, all other tests are still executed. So tests should not build on each other. Sub tests should be used for that purpose.

The result for the tests is exposed via prometheus metrics.

The interval is configured using PACE_LIVETEST_INTERVAL (duration format).

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrFailNow = errors.New("failed test")

ErrFailNow is used as a panic if ErrFailNow is called on the test

View Source
var ErrSkipNow = errors.New("skipped test")

ErrSkipNow is used as a panic if ErrSkipNow is called on the test

Functions

func Test

func Test(ctx context.Context, tests []TestFunc) error

Test executes the passed tests in the given order (array order). The tests are executed in the configured interval until the given context is done.

Example
package main

import (
	"context"
	"log"
	"time"

	"github.com/pace/bricks/test/livetest"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
	defer cancel()

	err := livetest.Test(ctx, []livetest.TestFunc{
		func(t *livetest.T) {
			t.Logf("Executed test no %d", 1)
		},
		func(t *livetest.T) {
			t.Log("Executed test no 2")
		},
		func(t *livetest.T) {
			t.Fatal("Fail test no 3")
		},
		func(t *livetest.T) {
			t.Fatalf("Fail test no %d", 4)
		},
		func(t *livetest.T) {
			t.Skip("Skipping test no 5")
		},
		func(t *livetest.T) {
			t.Skipf("Skipping test no %d", 5)
		},
		func(t *livetest.T) {
			t.SkipNow()
		},
		func(t *livetest.T) {
			t.Fail()
		},
		func(t *livetest.T) {
			t.FailNow()
		},
		func(t *livetest.T) {
			t.Error("Some")
		},
		func(t *livetest.T) {
			t.Errorf("formatted")
		},
	})
	if err != context.DeadlineExceeded {
		log.Fatal(err)
	}
}
Output:

Types

type T

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

T implements a similar interface than testing.T

func NewTestProxy

func NewTestProxy(ctx context.Context, name string) *T

NewTestProxy creates a new text proxy using the given context and name.

func (*T) Context

func (t *T) Context() context.Context

Context returns the livetest context. Useful for passing timeout and/or logging constraints from the test executor to the individual case

func (*T) Error

func (t *T) Error(args ...interface{})

Error logs an error message with the test

func (*T) Errorf

func (t *T) Errorf(format string, args ...interface{})

Errorf logs an error message with the test

func (*T) Fail

func (t *T) Fail()

Fail marks the test as failed

func (*T) FailNow

func (t *T) FailNow()

FailNow marks the test as failed and skips further execution

func (*T) Failed

func (t *T) Failed() bool

Failed returns true if the test was marked as failed

func (*T) Fatal

func (t *T) Fatal(args ...interface{})

Fatal logs the passed message in the context of the test and fails the test

func (*T) Fatalf

func (t *T) Fatalf(format string, args ...interface{})

Fatalf logs the passed message in the context of the test and fails the test

func (*T) Log

func (t *T) Log(args ...interface{})

Log logs the passed message in the context of the test

func (*T) Logf

func (t *T) Logf(format string, args ...interface{})

Logf logs the passed message in the context of the test

func (*T) Name

func (t *T) Name() string

Name returns the name of the test

func (*T) Skip

func (t *T) Skip(args ...interface{})

Skip logs reason and marks the test as skipped

func (*T) SkipNow

func (t *T) SkipNow()

SkipNow skips the test immediately

func (*T) Skipf

func (t *T) Skipf(format string, args ...interface{})

Skipf marks the test as skippend and log a reason

func (*T) Skipped

func (t *T) Skipped() bool

Skipped returns true if the test was skipped

type TestFunc

type TestFunc func(t *T)

TestFunc represents a single test (possibly with sub tests)

type TestState

type TestState string

TestState represents the state of a test

var (
	// StateRunning first state
	StateRunning TestState = "running"
	// StateOK test was executed without failure
	StateOK TestState = "ok"
	// StateFailed test was executed with failure
	StateFailed TestState = "failed"
	// StateSkipped test was skipped
	StateSkipped TestState = "skipped"
)

Jump to

Keyboard shortcuts

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