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.

74 lines
3.3 KiB

  1. package shell
  2. import (
  3. "bytes"
  4. "strings"
  5. "testing"
  6. )
  7. type Case struct {
  8. args []string
  9. result string
  10. }
  11. var (
  12. TestCases = []*Case{
  13. //add circuit breaker config for global
  14. {
  15. args: strings.Split("-global -type count -actions Read,Write -values 500,200", " "),
  16. result: "{\n \"global\": {\n \"enabled\": true,\n \"actions\": {\n \"Read:count\": \"500\",\n \"Write:count\": \"200\"\n }\n }\n}\n",
  17. },
  18. //disable global config
  19. {
  20. args: strings.Split("-global -disable", " "),
  21. result: "{\n \"global\": {\n \"actions\": {\n \"Read:count\": \"500\",\n \"Write:count\": \"200\"\n }\n }\n}\n",
  22. },
  23. //add circuit breaker config for buckets x,y,z
  24. {
  25. args: strings.Split("-buckets x,y,z -type count -actions Read,Write -values 200,100", " "),
  26. result: "{\n \"global\": {\n \"actions\": {\n \"Read:count\": \"500\",\n \"Write:count\": \"200\"\n }\n },\n \"buckets\": {\n \"x\": {\n \"enabled\": true,\n \"actions\": {\n \"Read:count\": \"200\",\n \"Write:count\": \"100\"\n }\n },\n \"y\": {\n \"enabled\": true,\n \"actions\": {\n \"Read:count\": \"200\",\n \"Write:count\": \"100\"\n }\n },\n \"z\": {\n \"enabled\": true,\n \"actions\": {\n \"Read:count\": \"200\",\n \"Write:count\": \"100\"\n }\n }\n }\n}\n",
  27. },
  28. //disable circuit breaker config of x
  29. {
  30. args: strings.Split("-buckets x -disable", " "),
  31. result: "{\n \"global\": {\n \"actions\": {\n \"Read:count\": \"500\",\n \"Write:count\": \"200\"\n }\n },\n \"buckets\": {\n \"x\": {\n \"actions\": {\n \"Read:count\": \"200\",\n \"Write:count\": \"100\"\n }\n },\n \"y\": {\n \"enabled\": true,\n \"actions\": {\n \"Read:count\": \"200\",\n \"Write:count\": \"100\"\n }\n },\n \"z\": {\n \"enabled\": true,\n \"actions\": {\n \"Read:count\": \"200\",\n \"Write:count\": \"100\"\n }\n }\n }\n}\n",
  32. },
  33. //delete circuit breaker config of x
  34. {
  35. args: strings.Split("-buckets x -delete", " "),
  36. result: "{\n \"global\": {\n \"actions\": {\n \"Read:count\": \"500\",\n \"Write:count\": \"200\"\n }\n },\n \"buckets\": {\n \"y\": {\n \"enabled\": true,\n \"actions\": {\n \"Read:count\": \"200\",\n \"Write:count\": \"100\"\n }\n },\n \"z\": {\n \"enabled\": true,\n \"actions\": {\n \"Read:count\": \"200\",\n \"Write:count\": \"100\"\n }\n }\n }\n}\n",
  37. },
  38. //clear all circuit breaker config
  39. {
  40. args: strings.Split("-delete", " "),
  41. result: "{\n\n}\n",
  42. },
  43. }
  44. )
  45. func TestCircuitBreakerShell(t *testing.T) {
  46. var writeBuf bytes.Buffer
  47. cmd := &commandS3CircuitBreaker{}
  48. LoadConfig = func(commandEnv *CommandEnv, dir string, file string, buf *bytes.Buffer) error {
  49. _, err := buf.Write(writeBuf.Bytes())
  50. if err != nil {
  51. return err
  52. }
  53. writeBuf.Reset()
  54. return nil
  55. }
  56. for i, tc := range TestCases {
  57. err := cmd.Do(tc.args, nil, &writeBuf)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. if i != 0 {
  62. result := writeBuf.String()
  63. if result != tc.result {
  64. t.Fatal("result of s3 circuit breaker shell command is unexpect!")
  65. }
  66. }
  67. }
  68. }