github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/fault/fault_network_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 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1" 27 "k8s.io/cli-runtime/pkg/genericiooptions" 28 clientfake "k8s.io/client-go/rest/fake" 29 cmdtesting "k8s.io/kubectl/pkg/cmd/testing" 30 31 "github.com/1aal/kubeblocks/pkg/cli/testing" 32 ) 33 34 var _ = Describe("Fault Network", func() { 35 var ( 36 tf *cmdtesting.TestFactory 37 streams genericiooptions.IOStreams 38 ) 39 BeforeEach(func() { 40 streams, _, _, _ = genericiooptions.NewTestIOStreams() 41 tf = cmdtesting.NewTestFactory().WithNamespace(testing.Namespace) 42 tf.Client = &clientfake.RESTClient{} 43 }) 44 45 AfterEach(func() { 46 tf.Cleanup() 47 }) 48 49 Context("test fault network", func() { 50 It("fault network partition", func() { 51 inputs := [][]string{ 52 {"--dry-run=client"}, 53 {"--mode=one", "--dry-run=client"}, 54 {"--mode=fixed", "--value=2", "--dry-run=client"}, 55 {"--mode=fixed-percent", "--value=50", "--dry-run=client"}, 56 {"--mode=random-max-percent", "--value=50", "--dry-run=client"}, 57 {"--ns-fault=kb-system", "--dry-run=client"}, 58 {"--node=minikube-m02", "--dry-run=client"}, 59 {"--label=app.kubernetes.io/component=mysql", "--dry-run=client"}, 60 {"--node-label=kubernetes.io/arch=arm64", "--dry-run=client"}, 61 {"--annotation=example-annotation=group-a", "--dry-run=client"}, 62 {"--external-target=kubeblocks.io", "--dry-run=client"}, 63 {"--target-mode=one", "--target-label=statefulset.kubernetes.io/pod-name=mycluster-mysql-2", "--target-ns-fault=default", "--dry-run=client"}, 64 } 65 o := NewNetworkChaosOptions(tf, streams, string(v1alpha1.PartitionAction)) 66 cmd := o.NewCobraCommand(Partition, PartitionShort) 67 o.AddCommonFlag(cmd) 68 69 for _, input := range inputs { 70 Expect(cmd.Flags().Parse(input)).Should(Succeed()) 71 Expect(o.CreateOptions.Complete()) 72 Expect(o.Complete()).Should(Succeed()) 73 Expect(o.Validate()).Should(Succeed()) 74 Expect(o.Run()).Should(Succeed()) 75 } 76 }) 77 78 It("fault network loss", func() { 79 inputs := [][]string{ 80 {"--loss=50", "--dry-run=client"}, 81 {"--loss=50", "--correlation=100", "--dry-run=client"}, 82 } 83 o := NewNetworkChaosOptions(tf, streams, string(v1alpha1.LossAction)) 84 cmd := o.NewCobraCommand(Loss, LossShort) 85 o.AddCommonFlag(cmd) 86 cmd.Flags().StringVar(&o.Loss, "loss", "", `Indicates the probability of a packet error occurring. Value range: [0, 100].`) 87 cmd.Flags().StringVarP(&o.NetworkLoss.Correlation, "correlation", "c", "0", `Indicates the correlation between the probability of a packet error occurring and whether it occurred the previous time. Value range: [0, 100].`) 88 89 for _, input := range inputs { 90 Expect(cmd.Flags().Parse(input)).Should(Succeed()) 91 Expect(o.CreateOptions.Complete()) 92 Expect(o.Complete()).Should(Succeed()) 93 Expect(o.Validate()).Should(Succeed()) 94 Expect(o.Run()).Should(Succeed()) 95 } 96 }) 97 98 It("fault network delay", func() { 99 inputs := [][]string{ 100 {"--latency=50s", "--dry-run=client"}, 101 {"--latency=50s", "--jitter=10s", "--dry-run=client"}, 102 {"--latency=50s", "--correlation=100", "--dry-run=client"}, 103 {"--latency=50s", "--jitter=10s", "--correlation=100", "--dry-run=client"}, 104 } 105 o := NewNetworkChaosOptions(tf, streams, string(v1alpha1.DelayAction)) 106 cmd := o.NewCobraCommand(Delay, DelayShort) 107 o.AddCommonFlag(cmd) 108 cmd.Flags().StringVar(&o.Latency, "latency", "", `the length of time to delay.`) 109 cmd.Flags().StringVar(&o.Jitter, "jitter", "0ms", `the variation range of the delay time.`) 110 cmd.Flags().StringVarP(&o.NetworkDelay.Correlation, "correlation", "c", "0", `Indicates the probability of a packet error occurring. Value range: [0, 100].`) 111 112 for _, input := range inputs { 113 Expect(cmd.Flags().Parse(input)).Should(Succeed()) 114 Expect(o.CreateOptions.Complete()) 115 Expect(o.Complete()).Should(Succeed()) 116 Expect(o.Validate()).Should(Succeed()) 117 Expect(o.Run()).Should(Succeed()) 118 } 119 }) 120 121 It("fault network duplicate", func() { 122 inputs := [][]string{ 123 {"--duplicate=50", "--dry-run=client"}, 124 {"--duplicate=50", "--correlation=100", "--dry-run=client"}, 125 } 126 o := NewNetworkChaosOptions(tf, streams, string(v1alpha1.DuplicateAction)) 127 cmd := o.NewCobraCommand(Duplicate, DuplicateShort) 128 o.AddCommonFlag(cmd) 129 cmd.Flags().StringVar(&o.Duplicate, "duplicate", "", `the probability of a packet being repeated. Value range: [0, 100].`) 130 cmd.Flags().StringVarP(&o.NetworkDuplicate.Correlation, "correlation", "c", "0", `Indicates the correlation between the probability of a packet error occurring and whether it occurred the previous time. Value range: [0, 100].`) 131 132 for _, input := range inputs { 133 Expect(cmd.Flags().Parse(input)).Should(Succeed()) 134 Expect(o.CreateOptions.Complete()) 135 Expect(o.Complete()).Should(Succeed()) 136 Expect(o.Validate()).Should(Succeed()) 137 Expect(o.Run()).Should(Succeed()) 138 } 139 }) 140 141 It("fault network corrupt", func() { 142 inputs := [][]string{ 143 {"--corrupt=50", "--dry-run=client"}, 144 {"--corrupt=50", "--correlation=100", "--dry-run=client"}, 145 } 146 o := NewNetworkChaosOptions(tf, streams, string(v1alpha1.CorruptAction)) 147 cmd := o.NewCobraCommand(Corrupt, CorruptShort) 148 o.AddCommonFlag(cmd) 149 cmd.Flags().StringVar(&o.Corrupt, "corrupt", "", `Indicates the probability of a packet error occurring. Value range: [0, 100].`) 150 cmd.Flags().StringVarP(&o.NetworkCorrupt.Correlation, "correlation", "c", "0", `Indicates the correlation between the probability of a packet error occurring and whether it occurred the previous time. Value range: [0, 100].`) 151 152 for _, input := range inputs { 153 Expect(cmd.Flags().Parse(input)).Should(Succeed()) 154 Expect(o.CreateOptions.Complete()) 155 Expect(o.Complete()).Should(Succeed()) 156 Expect(o.Validate()).Should(Succeed()) 157 Expect(o.Run()).Should(Succeed()) 158 } 159 }) 160 161 It("fault network bandwidth", func() { 162 inputs := [][]string{ 163 {"--rate=10kbps", "--dry-run=client"}, 164 {"--rate=10kbps", "--limit=1000", "--buffer=100", "--peakrate=10", "--minburst=5", "--dry-run=client"}, 165 } 166 o := NewNetworkChaosOptions(tf, streams, string(v1alpha1.BandwidthAction)) 167 cmd := o.NewCobraCommand(Bandwidth, BandwidthShort) 168 o.AddCommonFlag(cmd) 169 cmd.Flags().StringVar(&o.Rate, "rate", "", `the rate at which the bandwidth is limited. For example : 10 bps/kbps/mbps/gbps.`) 170 cmd.Flags().Uint32Var(&o.Limit, "limit", 1, `the number of bytes waiting in the queue.`) 171 cmd.Flags().Uint32Var(&o.Buffer, "buffer", 1, `the maximum number of bytes that can be sent instantaneously.`) 172 cmd.Flags().Uint64Var(&o.Peakrate, "peakrate", 0, `the maximum consumption rate of the bucket.`) 173 cmd.Flags().Uint32Var(&o.Minburst, "minburst", 0, `the size of the peakrate bucket.`) 174 175 for _, input := range inputs { 176 Expect(cmd.Flags().Parse(input)).Should(Succeed()) 177 Expect(o.CreateOptions.Complete()) 178 Expect(o.Complete()).Should(Succeed()) 179 Expect(o.Validate()).Should(Succeed()) 180 Expect(o.Run()).Should(Succeed()) 181 } 182 }) 183 }) 184 })