...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package backend
16
17 import (
18 "os"
19 "runtime/debug"
20 "strings"
21
22 "go.uber.org/zap"
23 )
24
25 const (
26 ENV_VERIFY = "ETCD_VERIFY"
27 ENV_VERIFY_ALL_VALUE = "all"
28 ENV_VERIFY_LOCK = "lock"
29 )
30
31 func ValidateCalledInsideApply(lg *zap.Logger) {
32 if !verifyLockEnabled() {
33 return
34 }
35 if !insideApply() {
36 lg.Panic("Called outside of APPLY!", zap.Stack("stacktrace"))
37 }
38 }
39
40 func ValidateCalledOutSideApply(lg *zap.Logger) {
41 if !verifyLockEnabled() {
42 return
43 }
44 if insideApply() {
45 lg.Panic("Called inside of APPLY!", zap.Stack("stacktrace"))
46 }
47 }
48
49 func ValidateCalledInsideUnittest(lg *zap.Logger) {
50 if !verifyLockEnabled() {
51 return
52 }
53 if !insideUnittest() {
54 lg.Fatal("Lock called outside of unit test!", zap.Stack("stacktrace"))
55 }
56 }
57
58 func verifyLockEnabled() bool {
59 return os.Getenv(ENV_VERIFY) == ENV_VERIFY_ALL_VALUE || os.Getenv(ENV_VERIFY) == ENV_VERIFY_LOCK
60 }
61
62 func insideApply() bool {
63 stackTraceStr := string(debug.Stack())
64 return strings.Contains(stackTraceStr, ".applyEntries")
65 }
66
67 func insideUnittest() bool {
68 stackTraceStr := string(debug.Stack())
69 return strings.Contains(stackTraceStr, "_test.go") && !strings.Contains(stackTraceStr, "tests/")
70 }
71
View as plain text