123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- package shell
- import (
- "bytes"
- "encoding/json"
- "reflect"
- "strings"
- "testing"
- )
- type Case struct {
- args []string
- result string
- }
- var (
- TestCases = []*Case{
- //add circuit breaker config for global
- {
- args: strings.Split("-global -type Count -actions Read,Write -values 500,200", " "),
- result: `{
- "global": {
- "enabled": true,
- "actions": {
- "Read:Count": "500",
- "Write:Count": "200"
- }
- }
- }`,
- },
- //disable global config
- {
- args: strings.Split("-global -disable", " "),
- result: `{
- "global": {
- "actions": {
- "Read:Count": "500",
- "Write:Count": "200"
- }
- }
- }`,
- },
- //add circuit breaker config for buckets x,y,z
- {
- args: strings.Split("-buckets x,y,z -type Count -actions Read,Write -values 200,100", " "),
- result: `{
- "global": {
- "actions": {
- "Read:Count": "500",
- "Write:Count": "200"
- }
- },
- "buckets": {
- "x": {
- "enabled": true,
- "actions": {
- "Read:Count": "200",
- "Write:Count": "100"
- }
- },
- "y": {
- "enabled": true,
- "actions": {
- "Read:Count": "200",
- "Write:Count": "100"
- }
- },
- "z": {
- "enabled": true,
- "actions": {
- "Read:Count": "200",
- "Write:Count": "100"
- }
- }
- }
- }`,
- },
- //disable circuit breaker config of x
- {
- args: strings.Split("-buckets x -disable", " "),
- result: `{
- "global": {
- "actions": {
- "Read:Count": "500",
- "Write:Count": "200"
- }
- },
- "buckets": {
- "x": {
- "actions": {
- "Read:Count": "200",
- "Write:Count": "100"
- }
- },
- "y": {
- "enabled": true,
- "actions": {
- "Read:Count": "200",
- "Write:Count": "100"
- }
- },
- "z": {
- "enabled": true,
- "actions": {
- "Read:Count": "200",
- "Write:Count": "100"
- }
- }
- }
- }`,
- },
- //delete circuit breaker config of x
- {
- args: strings.Split("-buckets x -delete", " "),
- result: `{
- "global": {
- "actions": {
- "Read:Count": "500",
- "Write:Count": "200"
- }
- },
- "buckets": {
- "y": {
- "enabled": true,
- "actions": {
- "Read:Count": "200",
- "Write:Count": "100"
- }
- },
- "z": {
- "enabled": true,
- "actions": {
- "Read:Count": "200",
- "Write:Count": "100"
- }
- }
- }
- }`,
- },
- //configure the circuit breaker for the size of the uploaded file for bucket x,y
- {
- args: strings.Split("-buckets x,y -type MB -actions Write -values 1024", " "),
- result: `{
- "global": {
- "actions": {
- "Read:Count": "500",
- "Write:Count": "200"
- }
- },
- "buckets": {
- "x": {
- "enabled": true,
- "actions": {
- "Write:MB": "1073741824"
- }
- },
- "y": {
- "enabled": true,
- "actions": {
- "Read:Count": "200",
- "Write:Count": "100",
- "Write:MB": "1073741824"
- }
- },
- "z": {
- "enabled": true,
- "actions": {
- "Read:Count": "200",
- "Write:Count": "100"
- }
- }
- }
- }`,
- },
- //delete the circuit breaker configuration for the size of the uploaded file of bucket x,y
- {
- args: strings.Split("-buckets x,y -type MB -actions Write -delete", " "),
- result: `{
- "global": {
- "actions": {
- "Read:Count": "500",
- "Write:Count": "200"
- }
- },
- "buckets": {
- "x": {
- "enabled": true
- },
- "y": {
- "enabled": true,
- "actions": {
- "Read:Count": "200",
- "Write:Count": "100"
- }
- },
- "z": {
- "enabled": true,
- "actions": {
- "Read:Count": "200",
- "Write:Count": "100"
- }
- }
- }
- }`,
- },
- //enable global circuit breaker config (without -disable flag)
- {
- args: strings.Split("-global", " "),
- result: `{
- "global": {
- "enabled": true,
- "actions": {
- "Read:Count": "500",
- "Write:Count": "200"
- }
- },
- "buckets": {
- "x": {
- "enabled": true
- },
- "y": {
- "enabled": true,
- "actions": {
- "Read:Count": "200",
- "Write:Count": "100"
- }
- },
- "z": {
- "enabled": true,
- "actions": {
- "Read:Count": "200",
- "Write:Count": "100"
- }
- }
- }
- }`,
- },
- //clear all circuit breaker config
- {
- args: strings.Split("-delete", " "),
- result: `{
-
- }`,
- },
- }
- )
- func TestCircuitBreakerShell(t *testing.T) {
- var writeBuf bytes.Buffer
- cmd := &commandS3CircuitBreaker{}
- LoadConfig = func(commandEnv *CommandEnv, dir string, file string, buf *bytes.Buffer) error {
- _, err := buf.Write(writeBuf.Bytes())
- if err != nil {
- return err
- }
- writeBuf.Reset()
- return nil
- }
- for i, tc := range TestCases {
- err := cmd.Do(tc.args, nil, &writeBuf)
- if err != nil {
- t.Fatal(err)
- }
- if i != 0 {
- result := writeBuf.String()
- actual := make(map[string]interface{})
- err := json.Unmarshal([]byte(result), &actual)
- if err != nil {
- t.Error(err)
- }
- expect := make(map[string]interface{})
- err = json.Unmarshal([]byte(result), &expect)
- if err != nil {
- t.Error(err)
- }
- if !reflect.DeepEqual(actual, expect) {
- t.Fatal("result of s3 circuit breaker shell command is unexpected!")
- }
- }
- }
- }
|