-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
145 lines (118 loc) Β· 3.61 KB
/
client.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package chaos
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"strings"
)
// Client represents a chaos controller management client.
type Client struct {
http http.Client
}
// NewClient returns a client for managing chaos controller on address controllerAddr.
func NewClient(controllerAddr string) *Client {
var (
client Client
controllerProto = "tcp"
)
if controllerAddr == "" {
controllerAddr = DefaultBindAddr
}
if strings.HasPrefix(controllerAddr, "unix:") {
controllerProto = "unix"
controllerAddr = strings.TrimPrefix(controllerAddr, "unix:")
}
client.http = http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial(controllerProto, controllerAddr)
},
},
}
return &client
}
// Spec represents a chaos route specification.
type Spec struct {
s map[string]interface{}
}
// NewSpec returns an empty chaos route specification.
func NewSpec() *Spec {
return &Spec{s: make(map[string]interface{})}
}
// Delay sets a chaos delay injection of d milliseconds at a p probability (0 < p < 1) to chaos spec.
func (s *Spec) Delay(d int, p float64) *Spec {
s.s["delay"] = map[string]interface{}{
"duration": d,
"p": p,
}
return s
}
// Delay sets a chaos error injection with HTTP status code sc with an optional message msg at a p probability
// (0 < p < 1) to chaos spec.
func (s *Spec) Error(sc int, msg string, p float64) *Spec {
s.s["error"] = map[string]interface{}{
"status_code": sc,
"message": msg,
"p": p,
}
return s
}
// During specifies that the route chaos spec effects must be enforced for a duration d
// (value must be expressed using time.ParseDuration() format).
func (s *Spec) During(d string) *Spec {
s.s["duration"] = d
return s
}
// AddRouteChaos adds chaos effects specified by spec to the route defined by method method (e.g. "POST")
// and URL path path (e.g. "/api/foo"), and returns an error if it failed.
func (c *Client) AddRouteChaos(method, path string, spec *Spec) error {
js, err := json.Marshal(spec.s)
if err != nil {
return fmt.Errorf("unable to marshal spec to JSON: %s", err)
}
req, err := http.NewRequest("PUT",
fmt.Sprintf("http://controller/?method=%s&path=%s", method, path),
bytes.NewBuffer(js))
if err != nil {
return fmt.Errorf("error creating HTTP request: %s\n", err)
}
req.Header.Add("Content-Type", "application/json")
res, err := c.http.Do(req)
if err != nil {
return fmt.Errorf("error sending HTTP request: %s\n", err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("unable to read response body: %s", err)
}
if res.StatusCode != http.StatusNoContent {
return fmt.Errorf("controller error: %s: %s", res.Status, body)
}
return nil
}
// DeleteRouteChaos delete route chaos specification applied to the route defined by method method (e.g. "POST")
// and URL path path (e.g. "/api/foo"), and returns an error if it failed.
func (c *Client) DeleteRouteChaos(method, path string) error {
req, err := http.NewRequest("DELETE", fmt.Sprintf("http://controller/?method=%s&path=%s", method, path), nil)
if err != nil {
return fmt.Errorf("error creating HTTP request: %s\n", err)
}
res, err := c.http.Do(req)
if err != nil {
return fmt.Errorf("error sending HTTP request: %s\n", err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("unable to read response body: %s", err)
}
if res.StatusCode != http.StatusNoContent {
return fmt.Errorf("controller error: %s: %s", res.Status, body)
}
return nil
}