-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsqlchaos.go
126 lines (113 loc) · 2.9 KB
/
sqlchaos.go
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package sqlchaos
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/u2386/gorm-sqlchaos/pkg/callback"
"gorm.io/gorm"
)
var _ gorm.Option = (*Config)(nil)
type (
RuleProvider func(ctx context.Context, dbname, table string) ([]byte, error)
Config struct {
DBName string
RuleProvider RuleProvider
}
)
var (
ErrArgumentNotSpecified = errors.New("argument not specified")
)
func (f RuleProvider) Rule(ctx context.Context, dbname, table string) (*callback.ChaosRule, error) {
value, err := f(ctx, dbname, table)
if err != nil || value == nil {
return nil, err
}
r := &callback.ChaosRule{}
if err := json.Unmarshal(value, r); err != nil {
return nil, err
}
return r, nil
}
func (*Config) Apply(*gorm.Config) error {
return nil
}
func (c *Config) AfterInitialize(db *gorm.DB) error {
if c.DBName == "" {
fmt.Fprintln(os.Stderr, "SQLCHAOS:db not specified")
return ErrArgumentNotSpecified
}
if c.RuleProvider == nil {
fmt.Fprintln(os.Stderr, "SQLCHAOS:rule provider not specified")
return ErrArgumentNotSpecified
}
fmt.Fprintln(os.Stderr, "SQLCHAOS:SQLChaos enabled")
callback := &callback.Callback{
DBName: c.DBName,
RuleProvider: c.RuleProvider,
}
if err := db.Callback().Create().Before("gorm:create").Register("sqlchaos:before-create", callback.BeforeCreate()); err != nil {
return err
}
if err := db.Callback().Update().Before("gorm:update").Register("sqlchaos:before-update", callback.BeforeUpdate()); err != nil {
return err
}
return nil
}
func WithSimpleHTTPRuleProvider() RuleProvider {
address := os.Getenv("SQLCHAOS_HTTP")
if address == "" {
fmt.Fprintln(os.Stderr, "SQLCHAOS:http address not specified")
return nil
}
rules := sync.Map{}
handler := func(w http.ResponseWriter, r *http.Request) {
key := r.URL.Path[1:]
switch r.Method {
case http.MethodDelete:
rules.Delete(key)
fmt.Fprint(w, "ok")
case http.MethodPost:
defer r.Body.Close()
data, _ := ioutil.ReadAll(r.Body)
rules.Store(key, data)
fmt.Fprint(w, "ok")
case http.MethodGet:
var sb strings.Builder
rules.Range(func(key, value interface{}) bool {
sb.WriteString(key.(string))
sb.WriteByte(':')
sb.Write(value.([]byte))
sb.WriteByte(',')
return true
})
fmt.Fprint(w, sb.String())
default:
w.WriteHeader(401)
}
}
http.HandleFunc("/", handler)
echan := make(chan error)
defer close(echan)
var err error
go func() { err = http.ListenAndServe(address, nil) }()
<-time.After(time.Second)
if err != nil {
fmt.Fprintf(os.Stderr, "SQLCHAOS:http server starts failed:%s\n", err)
return nil
}
fmt.Fprintf(os.Stderr, "SQLCHAOS:http server listening on %s\n", address)
return func(ctx context.Context, dbname, table string) ([]byte, error) {
fmt.Println(dbname + "/" + table)
if v, ok := rules.Load(dbname + "/" + table); ok {
return v.([]byte), nil
}
return nil, nil
}
}