An implementation of failpoints for golang. Please read design.md for a deeper understanding.
Failpoints are special comments that include a failpoint variable declaration and some trigger code,
func someFunc() string {
// gofail: var SomeFuncString string
// // this is called when the failpoint is triggered
// return SomeFuncString
return "default"
}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,
gofail enable
go build cmd/The translated code looks something like,
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; goto __nomockSomeFuncString; __badTypeSomeFuncString: __fp_SomeFuncString.BadType(vSomeFuncString, "string"); __nomockSomeFuncString: };
return "default"
}To disable failpoints and revert to the original code,
gofail disableAfter building with failpoints enabled, the program's failpoints can be activated so they may trigger when evaluated.
From the command line, trigger the failpoint to set SomeFuncString to hello,
GOFAIL_FAILPOINTS='SomeFuncString=return("hello")' ./cmdMultiple failpoints are set by using ';' for a delimiter,
GOFAIL_FAILPOINTS='failpoint1=return("hello");failpoint2=sleep(10)' ./cmdFirst, enable the HTTP server from the command line:
GOFAIL_HTTP="127.0.0.1:1234" ./cmdActivate a single failpoint with curl:
$ curl http://127.0.0.1:1234/SomeFuncString -XPUT -d'return("hello")'Activate multiple failpoints atomically with the special /failpoints endpoint. The payload is the same as for GOFAIL_FAILPOINTS above:
$ curl http://127.0.0.1:1234/failpoints -XPUT -d'failpoint1=return("hello");failpoint2=sleep(10)'List all failpoint configurations:
$ curl http://127.0.0.1:1234/List a single failpoint configuration:
$ curl http://127.0.0.1:1234/SomeFuncStringRetrieve the execution count of a failpoint:
$curl http://127.0.0.1:1234/SomeFuncString/count -XGETDeactivate a failpoint:
$ curl http://127.0.0.1:1234/SomeFuncString -XDELETEFrom a unit test,
import (
"testing"
gofail "go.etcd.io/gofail/runtime"
)
func TestWhatever(t *testing.T) {
gofail.Enable("SomeFuncString", `return("hello")`)
defer gofail.Disable("SomeFuncString")
...
}