seaweedfs/weed/shell/command_s3_circuitbreaker_test.go

293 lines
5.1 KiB
Go
Raw Normal View History

package shell
import (
"bytes"
2022-06-17 19:07:39 +08:00
"encoding/json"
"reflect"
"strings"
"testing"
)
type Case struct {
args []string
result string
}
var (
TestCases = []*Case{
//add circuit breaker config for global
{
2022-06-20 12:35:29 +08:00
args: strings.Split("-global -type Count -actions Read,Write -values 500,200", " "),
2022-06-17 19:07:39 +08:00
result: `{
"global": {
"enabled": true,
"actions": {
2022-06-20 12:35:29 +08:00
"Read:Count": "500",
"Write:Count": "200"
2022-06-17 19:07:39 +08:00
}
}
}`,
},
2022-06-17 19:07:39 +08:00
//disable global config
{
2022-06-17 19:07:39 +08:00
args: strings.Split("-global -disable", " "),
result: `{
"global": {
"actions": {
2022-06-20 12:35:29 +08:00
"Read:Count": "500",
"Write:Count": "200"
2022-06-17 19:07:39 +08:00
}
}
}`,
},
2022-06-17 19:07:39 +08:00
//add circuit breaker config for buckets x,y,z
{
2022-06-20 12:35:29 +08:00
args: strings.Split("-buckets x,y,z -type Count -actions Read,Write -values 200,100", " "),
2022-06-17 19:07:39 +08:00
result: `{
"global": {
"actions": {
2022-06-20 12:35:29 +08:00
"Read:Count": "500",
"Write:Count": "200"
2022-06-17 19:07:39 +08:00
}
},
"buckets": {
"x": {
"enabled": true,
"actions": {
2022-06-20 12:35:29 +08:00
"Read:Count": "200",
"Write:Count": "100"
2022-06-17 19:07:39 +08:00
}
},
"y": {
"enabled": true,
"actions": {
2022-06-20 12:35:29 +08:00
"Read:Count": "200",
"Write:Count": "100"
2022-06-17 19:07:39 +08:00
}
},
"z": {
"enabled": true,
"actions": {
2022-06-20 12:35:29 +08:00
"Read:Count": "200",
"Write:Count": "100"
2022-06-17 19:07:39 +08:00
}
}
}
}`,
},
2022-06-17 19:07:39 +08:00
//disable circuit breaker config of x
{
2022-06-17 19:07:39 +08:00
args: strings.Split("-buckets x -disable", " "),
result: `{
"global": {
"actions": {
2022-06-20 12:35:29 +08:00
"Read:Count": "500",
"Write:Count": "200"
2022-06-17 19:07:39 +08:00
}
},
"buckets": {
"x": {
"actions": {
2022-06-20 12:35:29 +08:00
"Read:Count": "200",
"Write:Count": "100"
2022-06-17 19:07:39 +08:00
}
},
"y": {
"enabled": true,
"actions": {
2022-06-20 12:35:29 +08:00
"Read:Count": "200",
"Write:Count": "100"
2022-06-17 19:07:39 +08:00
}
},
"z": {
"enabled": true,
"actions": {
2022-06-20 12:35:29 +08:00
"Read:Count": "200",
"Write:Count": "100"
2022-06-17 19:07:39 +08:00
}
}
}
}`,
},
2022-06-17 19:07:39 +08:00
//delete circuit breaker config of x
{
2022-06-17 19:07:39 +08:00
args: strings.Split("-buckets x -delete", " "),
result: `{
"global": {
"actions": {
2022-06-20 12:35:29 +08:00
"Read:Count": "500",
"Write:Count": "200"
2022-06-17 19:07:39 +08:00
}
},
"buckets": {
"y": {
"enabled": true,
"actions": {
2022-06-20 12:35:29 +08:00
"Read:Count": "200",
"Write:Count": "100"
2022-06-17 19:07:39 +08:00
}
},
"z": {
"enabled": true,
"actions": {
2022-06-20 12:35:29 +08:00
"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"
2022-06-17 19:07:39 +08:00
}
}
}
}`,
},
2022-06-17 19:07:39 +08:00
//clear all circuit breaker config
{
2022-06-17 19:07:39 +08:00
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()
2022-06-17 19:07:39 +08:00
actual := make(map[string]interface{})
err := json.Unmarshal([]byte(result), &actual)
if err != nil {
t.Error(err)
}
2022-06-17 19:07:39 +08:00
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 unexpect!")
}
}
}
}