File: README.md

package info (click to toggle)
golang-github-etcd-io-gofail 0.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 316 kB
  • sloc: makefile: 24; sh: 3
file content (112 lines) | stat: -rw-r--r-- 2,531 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# gofail

[![Build Status](https://travis-ci.com/etcd-io/gofail.svg?branch=master)](https://travis-ci.com/etcd-io/gofail)

An implementation of [failpoints][failpoint] for golang. Please read [design.md](doc/design.md) for a deeper understanding.

[failpoint]: http://www.freebsd.org/cgi/man.cgi?query=fail

## Add a failpoint

Failpoints are special comments that include a failpoint variable declaration and some trigger code,

```go
func someFunc() string {
	// gofail: var SomeFuncString string
	// // this is called when the failpoint is triggered
	// return SomeFuncString
	return "default"
}
```

## Build with failpoints

Building with failpoints will translate gofail comments in place to code that accesses the gofail runtime.

Call gofail in the directory with failpoints to generate gofail runtime bindings, then build as usual,

```sh
gofail enable
go build cmd/
```

The translated code looks something like,

```go
func someFunc() string {
        if vSomeFuncString, __fpErr := __fp_SomeFuncString.Acquire(); __fpErr == nil { SomeFuncString, __fpTypeOK := vSomeFuncString.(string); if !__fpTypeOK { goto __badTypeSomeFuncString}
		// this is called when the failpoint is triggered
		return SomeFuncString; __badTypeSomeFuncString: __fp_SomeFuncString.BadType(vSomeFuncString, "string"); };
        return "default"
}
```

To disable failpoints and revert to the original code,

```sh
gofail disable
```

## Triggering a failpoint

After building with failpoints enabled, the program's failpoints can be activated so they may trigger when evaluated.

### Command line

From the command line, trigger the failpoint to set SomeFuncString to `hello`,

```sh
GOFAIL_FAILPOINTS='SomeFuncString=return("hello")' ./cmd
```

Multiple failpoints are set by using ';' for a delimiter,

```sh
GOFAIL_FAILPOINTS='failpoint1=return("hello");failpoint2=sleep(10)' ./cmd
```

### HTTP endpoint

First, enable the HTTP server from the command line,

```sh
GOFAIL_HTTP="127.0.0.1:1234" ./cmd
```


Activate a failpoint with curl,

```sh
$ curl http://127.0.0.1:1234/SomeFuncString -XPUT -d'return("hello")'
```

List the failpoints,

```sh
$ curl http://127.0.0.1:1234/SomeFuncString=return("hello")
```

Deactivate a failpoint,

```sh
$ curl http://127.0.0.1:1234/SomeFuncString -XDELETE
```

### Unit tests

From a unit test,

```go
import (
	"testing"

	gofail "go.etcd.io/gofail/runtime"
)

func TestWhatever(t *testing.T) {
	gofail.Enable("SomeFuncString", `return("hello")`)
	defer gofail.Disable("SomeFuncString")
	...
}
```