github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/fault/fault_http_test.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package fault 21 22 import ( 23 . "github.com/onsi/ginkgo/v2" 24 . "github.com/onsi/gomega" 25 26 "k8s.io/cli-runtime/pkg/genericiooptions" 27 clientfake "k8s.io/client-go/rest/fake" 28 cmdtesting "k8s.io/kubectl/pkg/cmd/testing" 29 30 "github.com/1aal/kubeblocks/pkg/cli/testing" 31 ) 32 33 var _ = Describe("Fault Network HTPP", func() { 34 var ( 35 tf *cmdtesting.TestFactory 36 streams genericiooptions.IOStreams 37 ) 38 BeforeEach(func() { 39 streams, _, _, _ = genericiooptions.NewTestIOStreams() 40 tf = cmdtesting.NewTestFactory().WithNamespace(testing.Namespace) 41 tf.Client = &clientfake.RESTClient{} 42 }) 43 44 AfterEach(func() { 45 tf.Cleanup() 46 }) 47 48 Context("test fault network http", func() { 49 It("fault network http abort", func() { 50 inputs := [][]string{ 51 {"--dry-run=client"}, 52 {"--mode=one", "--dry-run=client"}, 53 {"--mode=fixed", "--value=2", "--dry-run=client"}, 54 {"--mode=fixed-percent", "--value=50", "--dry-run=client"}, 55 {"--mode=random-max-percent", "--value=50", "--dry-run=client"}, 56 {"--ns-fault=kb-system", "--dry-run=client"}, 57 {"--node=minikube-m02", "--dry-run=client"}, 58 {"--label=app.kubernetes.io/component=mysql", "--dry-run=client"}, 59 {"--node-label=kubernetes.io/arch=arm64", "--dry-run=client"}, 60 {"--annotation=example-annotation=group-a", "--dry-run=client"}, 61 {"--abort=true", "--dry-run=client"}, 62 } 63 o := NewHTTPChaosOptions(tf, streams, "") 64 cmd := o.NewCobraCommand(Abort, AbortShort) 65 o.AddCommonFlag(cmd) 66 cmd.Flags().BoolVar(&o.Abort, "abort", true, `Indicates whether to inject the fault that interrupts the connection.`) 67 68 for _, input := range inputs { 69 Expect(cmd.Flags().Parse(input)).Should(Succeed()) 70 Expect(o.CreateOptions.Complete()) 71 Expect(o.Complete()).Should(Succeed()) 72 Expect(o.Validate()).Should(Succeed()) 73 Expect(o.Run()).Should(Succeed()) 74 } 75 }) 76 77 It("fault network http delay", func() { 78 inputs := [][]string{ 79 {"--dry-run=client"}, 80 {"--delay=50s", "--dry-run=client"}, 81 } 82 o := NewHTTPChaosOptions(tf, streams, "") 83 cmd := o.NewCobraCommand(Delay, DelayShort) 84 o.AddCommonFlag(cmd) 85 cmd.Flags().StringVar(&o.Delay, "delay", "10s", `The time for delay.`) 86 87 for _, input := range inputs { 88 Expect(cmd.Flags().Parse(input)).Should(Succeed()) 89 Expect(o.CreateOptions.Complete()) 90 Expect(o.Complete()).Should(Succeed()) 91 Expect(o.Validate()).Should(Succeed()) 92 Expect(o.Run()).Should(Succeed()) 93 } 94 }) 95 96 It("fault network http replace", func() { 97 inputs := [][]string{ 98 {"--dry-run=client"}, 99 {"--replace-method=PUT", "--body=\"you are good luck\"", "--replace-path=/local/", "--duration=1m", "--dry-run=client"}, 100 {"--target=Response", "--replace-method=PUT", "--body=you", "--replace-path=/local/", "--duration=1m", "--dry-run=client"}, 101 } 102 o := NewHTTPChaosOptions(tf, streams, "") 103 cmd := o.NewCobraCommand(Replace, ReplaceShort) 104 o.AddCommonFlag(cmd) 105 cmd.Flags().StringVar(&o.InputReplaceBody, "body", "", `The content of the request body or response body to replace the failure.`) 106 cmd.Flags().StringVar(&o.ReplacePath, "replace-path", "", `The URI path used to replace content.`) 107 cmd.Flags().StringVar(&o.ReplaceMethod, "replace-method", "", `The replaced content of the HTTP request method.`) 108 109 for _, input := range inputs { 110 Expect(cmd.Flags().Parse(input)).Should(Succeed()) 111 Expect(o.CreateOptions.Complete()) 112 Expect(o.Complete()).Should(Succeed()) 113 Expect(o.Validate()).Should(Succeed()) 114 Expect(o.Run()).Should(Succeed()) 115 } 116 }) 117 118 It("fault network http patch", func() { 119 inputs := [][]string{ 120 {"--dry-run=client"}, 121 {"--body=\"you are good luck\"", "--type=JSON", "--duration=1m", "--dry-run=client"}, 122 {"--target=Response", "--body=\"you are good luck\"", "--type=JSON", "--duration=1m", "--dry-run=client"}, 123 } 124 o := NewHTTPChaosOptions(tf, streams, "") 125 cmd := o.NewCobraCommand(Patch, PatchShort) 126 o.AddCommonFlag(cmd) 127 cmd.Flags().StringVar(&o.PatchBodyValue, "body", "", `The fault of the request body or response body with patch faults.`) 128 cmd.Flags().StringVar(&o.PatchBodyType, "type", "", `The type of patch faults of the request body or response body. Currently, it only supports JSON.`) 129 130 for _, input := range inputs { 131 Expect(cmd.Flags().Parse(input)).Should(Succeed()) 132 Expect(o.CreateOptions.Complete()) 133 Expect(o.Complete()).Should(Succeed()) 134 Expect(o.Validate()).Should(Succeed()) 135 Expect(o.Run()).Should(Succeed()) 136 } 137 }) 138 }) 139 })