aboutsummaryrefslogtreecommitdiff
path: root/weed/shell/command_s3_circuitbreaker_test.go
diff options
context:
space:
mode:
author石昌林 <changlin.shi@ly.com>2022-06-17 17:11:18 +0800
committer石昌林 <changlin.shi@ly.com>2022-06-17 17:11:18 +0800
commit37df209195995d619fe2fc9ae7b9798cd745e55d (patch)
tree66cd9f4382a3c1bf01c3baffcc1be3f57b5f876f /weed/shell/command_s3_circuitbreaker_test.go
parent78b372816935c663f7f56b92e0e79e37e99cd4d2 (diff)
downloadseaweedfs-37df209195995d619fe2fc9ae7b9798cd745e55d.tar.xz
seaweedfs-37df209195995d619fe2fc9ae7b9798cd745e55d.zip
add some unit tests and some code optimizes
Diffstat (limited to 'weed/shell/command_s3_circuitbreaker_test.go')
-rw-r--r--weed/shell/command_s3_circuitbreaker_test.go69
1 files changed, 68 insertions, 1 deletions
diff --git a/weed/shell/command_s3_circuitbreaker_test.go b/weed/shell/command_s3_circuitbreaker_test.go
index 93e9fa414..214256b5c 100644
--- a/weed/shell/command_s3_circuitbreaker_test.go
+++ b/weed/shell/command_s3_circuitbreaker_test.go
@@ -1,7 +1,74 @@
package shell
-import "testing"
+import (
+ "bytes"
+ "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: "{\n \"global\": {\n \"enabled\": true,\n \"actions\": {\n \"Read:count\": \"500\",\n \"Write:count\": \"200\"\n }\n }\n}\n",
+ },
+ //disable global config
+ {
+ args: strings.Split("-global -disable", " "),
+ result: "{\n \"global\": {\n \"actions\": {\n \"Read:count\": \"500\",\n \"Write:count\": \"200\"\n }\n }\n}\n",
+ },
+ //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: "{\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",
+ },
+ //disable circuit breaker config of x
+ {
+ args: strings.Split("-buckets x -disable", " "),
+ 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",
+ },
+ //delete circuit breaker config of x
+ {
+ args: strings.Split("-buckets x -delete", " "),
+ 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",
+ },
+ //clear all circuit breaker config
+ {
+ args: strings.Split("-delete", " "),
+ result: "{\n\n}\n",
+ },
+ }
+)
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()
+ if result != tc.result {
+ t.Fatal("result of s3 circuit breaker shell command is unexpect!")
+ }
+ }
+ }
}