Failpoints are getting executed while running tests using `go test -v`
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 45/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- go
- 领域
- testing-qa
调研方向
使用 samplestruct.go、main.go 和 sample_test.go,通过比较 go test -v 与构建出的 ./modv1 可执行文件来复现该报告。检查在 TestFailpoint 期间如何设置和使用 GO_FAILPOINTS,并验证 failpoint 是否会在测试二进制文件中运行。完成意味着已解释该行为,并且通过可复现的测试或清晰的文档覆盖了测试时的预期行为。
由索引模型根据 Issue 内容生成。
描述
Bug Report
- What did you do? If possible, provide a recipe for reproducing the error.
I have the following setup (all in the same directory):
go.mod
module example.com/modv1
go 1.20
require (
github.com/pingcap/failpoint v0.0.0-20220801062533-2eaa32854a6c
github.com/stretchr/testify v1.8.4
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pingcap/errors v0.11.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
samplestruct.go
package main
import (
"log"
"github.com/pingcap/failpoint"
)
type SampleStruct struct {
cnt int
}
func (s *SampleStruct) increment() {
if _, _err_ := failpoint.Eval(_curpkg_("testPanic")); _err_ == nil {
log.Printf("[SampleStruct] SUCCESS!!! Executed testPanic FP: The count for s is %v", s.cnt)
}
s.cnt++
}
func (s *SampleStruct) decrement() {
s.cnt--
}
func (s *SampleStruct) incrementFP() string {
log.Printf("[SampleStruct] Executing incrementFP with old cnt %v", s.cnt)
s.cnt++
log.Printf("[SampleStruct] Executing incrementFP with new cnt %v", s.cnt)
if _, _err_ := failpoint.Eval(_curpkg_("doubleIncrement")); _err_ == nil {
s.cnt++
log.Printf("[SampleStruct] SUCCESS!!! Executed doubleIncrement FP. The count for s is %v", s.cnt)
}
return "single"
}
main.go
package main
import (
"fmt"
"log"
"os"
)
func main() {
log.Printf(fmt.Sprintf("[main] This is main function being executed in an executable at path %v", os.Args[0]))
//os.Setenv("Go_FAILPOINTS", "main/doubleIncrement=return(true);main/testPanic=return(true)")
log.Printf(fmt.Sprintf("[main] After FP code in main function"))
sampleStruct := SampleStruct{cnt: 1}
log.Printf("[main] sampleStruct cnt before increment is %v", sampleStruct.cnt)
sampleStruct.increment()
log.Printf("[main] sampleStruct cnt after increment is %v", sampleStruct.cnt)
log.Printf("[main] sampleStruct cnt before double increment is %v", sampleStruct.cnt)
sampleStruct.incrementFP()
log.Printf("[main] sampleStruct cnt after double increment is %v", sampleStruct.cnt)
}
sample_test.go
package main
import (
"log"
"testing"
"os/exec"
"os"
"fmt"
"bytes"
"github.com/stretchr/testify/assert"
)
func TestFailpoint(t *testing.T) {
log.Printf("[TestFailpoint] Executing TestFailpoint")
mySampleStruct := SampleStruct{cnt: 2}
assert.Equal(t, mySampleStruct.cnt, 2)
// fpsToEnable := map[string]struct{}{
// "doubleIncrement": {},
// }
// cmd := exec.Command("export", "GO_FAILPOINTS='main/doubleIncrement=return(true);main/testPanic=return(true)'")
// if err := cmd.Run(); err != nil {
// log.Fatal(fmt.Sprintf("Encountered error while setting up the GO_FAILPOINTS environment variable. Error: %v", err))
// } else {
// log.Printf("Successfully set up the GO_FAILPOINTS environment variable")
// }
os.Setenv("GO_FAILPOINTS","'main/doubleIncrement=return(true);main/testPanic=return(true)'")
log.Printf("[TestFailpoint] Successfully set up the GO_FAILPOINTS environment variable")
cmd := exec.Command("./modv1")
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
if err := cmd.Start(); err != nil {
log.Printf(fmt.Sprintf("[TestFailpoint] Error while starting modv1 executable. Error: %v", err))
log.Printf("[TestFailpoint] Command error is: " + stderr.String())
} else {
log.Printf("[TestFailpoint] Successfully started modv1 executable")
log.Printf("[TestFailpoint] Result: " + out.String() + ",Error: " + stderr.String())
}
if err := cmd.Wait(); err != nil {
log.Printf(fmt.Sprintf("[TestFailpoint] Error while waiting for modv1 executable. Error: %v", err))
log.Printf("[TestFailpoint] Command error is: " + stderr.String())
} else {
log.Printf("[TestFailpoint] Successfully waited for modv1 executable")
log.Printf("[TestFailpoint] Result: " + out.String() + ",Error: " + stderr.String())
}
//failpoint.Enable("doubleIncrement", "return(1)")
log.Printf("[TestFailpoint] Calling increment")
mySampleStruct.increment()
log.Printf("[TestFailpoint] Called increment")
log.Printf("[TestFailpoint] Calling incrementFP")
result := mySampleStruct.incrementFP()
log.Printf("[TestFailpoint] Called incrementFP")
assert.Equal(t, mySampleStruct.cnt, 4)
assert.Equal(t, result, "single")
os.Unsetenv("GO_FAILPOINTS")
log.Printf("[TestFailpoint] Successfully unset the GO_FAILPOINTS environment variable")
}
-
What did you expect to see?
When I rungo buildand set the environment variableexport GO_FAILPOINTS=main/doubleIncrement=return(true);main/testPanic=return(true)and then execute the./modv1executable I see that the failpoints insamplestruct.goare hit and the failpoint closure is executed. I expected same behavior when I rungo test -vi.e. when theTestFailpointtest is executed. -
What did you see instead?
I did not see the failpoints insamplestruct.gogetting executed while running the testTestFalipoint. Is this expected behavior? Is the failpoint code executed only in the compiled binarymodv1and not during test execution even though the environment variableGO_FAILPOINTSis kept around (and additionally also set/unset during) test execution? -
Versions of the failpoint
$ ./failpoint-ctl -V
ReleaseVersion ed9079f
BuildTS 2023-09-01 02:53:58
GitHash ed9079f47f3761cbbeadaded49d48e3264c23427
GitBranch fp-testing
GoVersion go version go1.21.0 darwin/arm64
- 主要语言
- Go
- 星标
- 896
- 派生
- 69
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
pingcap/failpoint 的其他 Issue
-
难度 4/5 3-5 天 新手友好度 30/100
-
expect `Enable()` inTerm:'off' does not trigger failpoint code, but it's not as expect可能已有人在做 @1fanwang 于 28 天前认领。 未关闭bug
难度 3/5 1-2 天 新手友好度 45/100
-
question
难度 3/5 1-2 天 新手友好度 35/100
-
bug
难度 3/5 1-2 天 新手友好度 35/100
-
难度 4/5 3-5 天 新手友好度 35/100
查看 pingcap/failpoint 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 82/100
dataiku/kiji-proxy#635 ·
-
难度 2/5 1-3 小时 新手友好度 67/100
hyperledger-firefly/signer#126 ·
-
agent-review-finding chore
难度 2/5 1-3 小时 新手友好度 73/100
jordansmall/spindrift#5748 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 74/100
-
Resource links written as host:port without a scheme are broken可能已有人在做 @RaphaelFakhri 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 1 天内回复