You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
3.3 KiB

  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. //clear all circuit breaker config
  138. {
  139. args: strings.Split("-delete", " "),
  140. result: `{
  141. }`,
  142. },
  143. }
  144. )
  145. func TestCircuitBreakerShell(t *testing.T) {
  146. var writeBuf bytes.Buffer
  147. cmd := &commandS3CircuitBreaker{}
  148. LoadConfig = func(commandEnv *CommandEnv, dir string, file string, buf *bytes.Buffer) error {
  149. _, err := buf.Write(writeBuf.Bytes())
  150. if err != nil {
  151. return err
  152. }
  153. writeBuf.Reset()
  154. return nil
  155. }
  156. for i, tc := range TestCases {
  157. err := cmd.Do(tc.args, nil, &writeBuf)
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. if i != 0 {
  162. result := writeBuf.String()
  163. actual := make(map[string]interface{})
  164. err := json.Unmarshal([]byte(result), &actual)
  165. if err != nil {
  166. t.Error(err)
  167. }
  168. expect := make(map[string]interface{})
  169. err = json.Unmarshal([]byte(result), &expect)
  170. if err != nil {
  171. t.Error(err)
  172. }
  173. if !reflect.DeepEqual(actual, expect) {
  174. t.Fatal("result of s3 circuit breaker shell command is unexpect!")
  175. }
  176. }
  177. }
  178. }