command_s3_circuitbreaker_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package shell
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "reflect"
  6. "strings"
  7. "testing"
  8. )
  9. type Case struct {
  10. args []string
  11. result string
  12. }
  13. var (
  14. TestCases = []*Case{
  15. //add circuit breaker config for global
  16. {
  17. args: strings.Split("-global -type Count -actions Read,Write -values 500,200", " "),
  18. result: `{
  19. "global": {
  20. "enabled": true,
  21. "actions": {
  22. "Read:Count": "500",
  23. "Write:Count": "200"
  24. }
  25. }
  26. }`,
  27. },
  28. //disable global config
  29. {
  30. args: strings.Split("-global -disable", " "),
  31. result: `{
  32. "global": {
  33. "actions": {
  34. "Read:Count": "500",
  35. "Write:Count": "200"
  36. }
  37. }
  38. }`,
  39. },
  40. //add circuit breaker config for buckets x,y,z
  41. {
  42. args: strings.Split("-buckets x,y,z -type Count -actions Read,Write -values 200,100", " "),
  43. result: `{
  44. "global": {
  45. "actions": {
  46. "Read:Count": "500",
  47. "Write:Count": "200"
  48. }
  49. },
  50. "buckets": {
  51. "x": {
  52. "enabled": true,
  53. "actions": {
  54. "Read:Count": "200",
  55. "Write:Count": "100"
  56. }
  57. },
  58. "y": {
  59. "enabled": true,
  60. "actions": {
  61. "Read:Count": "200",
  62. "Write:Count": "100"
  63. }
  64. },
  65. "z": {
  66. "enabled": true,
  67. "actions": {
  68. "Read:Count": "200",
  69. "Write:Count": "100"
  70. }
  71. }
  72. }
  73. }`,
  74. },
  75. //disable circuit breaker config of x
  76. {
  77. args: strings.Split("-buckets x -disable", " "),
  78. result: `{
  79. "global": {
  80. "actions": {
  81. "Read:Count": "500",
  82. "Write:Count": "200"
  83. }
  84. },
  85. "buckets": {
  86. "x": {
  87. "actions": {
  88. "Read:Count": "200",
  89. "Write:Count": "100"
  90. }
  91. },
  92. "y": {
  93. "enabled": true,
  94. "actions": {
  95. "Read:Count": "200",
  96. "Write:Count": "100"
  97. }
  98. },
  99. "z": {
  100. "enabled": true,
  101. "actions": {
  102. "Read:Count": "200",
  103. "Write:Count": "100"
  104. }
  105. }
  106. }
  107. }`,
  108. },
  109. //delete circuit breaker config of x
  110. {
  111. args: strings.Split("-buckets x -delete", " "),
  112. result: `{
  113. "global": {
  114. "actions": {
  115. "Read:Count": "500",
  116. "Write:Count": "200"
  117. }
  118. },
  119. "buckets": {
  120. "y": {
  121. "enabled": true,
  122. "actions": {
  123. "Read:Count": "200",
  124. "Write:Count": "100"
  125. }
  126. },
  127. "z": {
  128. "enabled": true,
  129. "actions": {
  130. "Read:Count": "200",
  131. "Write:Count": "100"
  132. }
  133. }
  134. }
  135. }`,
  136. },
  137. //configure the circuit breaker for the size of the uploaded file for bucket x,y
  138. {
  139. args: strings.Split("-buckets x,y -type MB -actions Write -values 1024", " "),
  140. result: `{
  141. "global": {
  142. "actions": {
  143. "Read:Count": "500",
  144. "Write:Count": "200"
  145. }
  146. },
  147. "buckets": {
  148. "x": {
  149. "enabled": true,
  150. "actions": {
  151. "Write:MB": "1073741824"
  152. }
  153. },
  154. "y": {
  155. "enabled": true,
  156. "actions": {
  157. "Read:Count": "200",
  158. "Write:Count": "100",
  159. "Write:MB": "1073741824"
  160. }
  161. },
  162. "z": {
  163. "enabled": true,
  164. "actions": {
  165. "Read:Count": "200",
  166. "Write:Count": "100"
  167. }
  168. }
  169. }
  170. }`,
  171. },
  172. //delete the circuit breaker configuration for the size of the uploaded file of bucket x,y
  173. {
  174. args: strings.Split("-buckets x,y -type MB -actions Write -delete", " "),
  175. result: `{
  176. "global": {
  177. "actions": {
  178. "Read:Count": "500",
  179. "Write:Count": "200"
  180. }
  181. },
  182. "buckets": {
  183. "x": {
  184. "enabled": true
  185. },
  186. "y": {
  187. "enabled": true,
  188. "actions": {
  189. "Read:Count": "200",
  190. "Write:Count": "100"
  191. }
  192. },
  193. "z": {
  194. "enabled": true,
  195. "actions": {
  196. "Read:Count": "200",
  197. "Write:Count": "100"
  198. }
  199. }
  200. }
  201. }`,
  202. },
  203. //enable global circuit breaker config (without -disable flag)
  204. {
  205. args: strings.Split("-global", " "),
  206. result: `{
  207. "global": {
  208. "enabled": true,
  209. "actions": {
  210. "Read:Count": "500",
  211. "Write:Count": "200"
  212. }
  213. },
  214. "buckets": {
  215. "x": {
  216. "enabled": true
  217. },
  218. "y": {
  219. "enabled": true,
  220. "actions": {
  221. "Read:Count": "200",
  222. "Write:Count": "100"
  223. }
  224. },
  225. "z": {
  226. "enabled": true,
  227. "actions": {
  228. "Read:Count": "200",
  229. "Write:Count": "100"
  230. }
  231. }
  232. }
  233. }`,
  234. },
  235. //clear all circuit breaker config
  236. {
  237. args: strings.Split("-delete", " "),
  238. result: `{
  239. }`,
  240. },
  241. }
  242. )
  243. func TestCircuitBreakerShell(t *testing.T) {
  244. var writeBuf bytes.Buffer
  245. cmd := &commandS3CircuitBreaker{}
  246. LoadConfig = func(commandEnv *CommandEnv, dir string, file string, buf *bytes.Buffer) error {
  247. _, err := buf.Write(writeBuf.Bytes())
  248. if err != nil {
  249. return err
  250. }
  251. writeBuf.Reset()
  252. return nil
  253. }
  254. for i, tc := range TestCases {
  255. err := cmd.Do(tc.args, nil, &writeBuf)
  256. if err != nil {
  257. t.Fatal(err)
  258. }
  259. if i != 0 {
  260. result := writeBuf.String()
  261. actual := make(map[string]interface{})
  262. err := json.Unmarshal([]byte(result), &actual)
  263. if err != nil {
  264. t.Error(err)
  265. }
  266. expect := make(map[string]interface{})
  267. err = json.Unmarshal([]byte(result), &expect)
  268. if err != nil {
  269. t.Error(err)
  270. }
  271. if !reflect.DeepEqual(actual, expect) {
  272. t.Fatal("result of s3 circuit breaker shell command is unexpected!")
  273. }
  274. }
  275. }
  276. }