github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/fault/fault_io_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 IO", 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 io", func() { 50 51 It("fault io latency", func() { 52 inputs := [][]string{ 53 {"--mode=one", "--delay=10s", "--volume-path=/data", "--dry-run=client"}, 54 {"--mode=fixed", "--value=2", "--delay=10s", "--volume-path=/data", "--dry-run=client"}, 55 {"--mode=fixed-percent", "--value=50", "--delay=10s", "--volume-path=/data", "--dry-run=client"}, 56 {"--mode=random-max-percent", "--value=50", "--delay=10s", "--volume-path=/data", "--dry-run=client"}, 57 {"--ns-fault=kb-system", "--delay=10s", "--volume-path=/data", "--dry-run=client"}, 58 {"--node=minikube-m02", "--delay=10s", "--volume-path=/data", "--dry-run=client"}, 59 {"--label=app.kubernetes.io/component=mysql", "--delay=10s", "--volume-path=/data", "--dry-run=client"}, 60 {"--node-label=kubernetes.io/arch=arm64", "--delay=10s", "--volume-path=/data", "--dry-run=client"}, 61 {"--annotation=example-annotation=group-a", "--delay=10s", "--volume-path=/data", "--dry-run=client"}, 62 {"--delay=10s", "--volume-path=/data", "--dry-run=client"}, 63 {"--delay=10s", "--volume-path=/data", "--path=test.txt", "--percent=50", "--method=READ", "-c=mysql", "--dry-run=client"}, 64 } 65 o := NewIOChaosOptions(tf, streams, string(v1alpha1.IoLatency)) 66 cmd := o.NewCobraCommand(Latency, LatencyShort) 67 o.AddCommonFlag(cmd, tf) 68 cmd.Flags().StringVar(&o.Delay, "delay", "", `Specific delay time.`) 69 70 for _, input := range inputs { 71 Expect(cmd.Flags().Parse(input)).Should(Succeed()) 72 Expect(o.CreateOptions.Complete()) 73 Expect(o.Complete()).Should(Succeed()) 74 Expect(o.Validate()).Should(Succeed()) 75 Expect(o.Run()).Should(Succeed()) 76 } 77 }) 78 79 It("fault io errno", func() { 80 inputs := [][]string{ 81 {"--errno=22", "--volume-path=/data", "--dry-run=client"}, 82 {"--errno=22", "--volume-path=/data", "--path=test.txt", "--percent=50", "--method=READ", "--dry-run=client"}, 83 } 84 o := NewIOChaosOptions(tf, streams, string(v1alpha1.IoFaults)) 85 cmd := o.NewCobraCommand(Errno, ErrnoShort) 86 o.AddCommonFlag(cmd, tf) 87 cmd.Flags().IntVar(&o.Errno, "errno", 0, `The returned error number.`) 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 io attribute", func() { 99 inputs := [][]string{ 100 {"--perm=72", "--size=72", "--blocks=72", "--nlink=72", "--ino=72", 101 "--uid=72", "--gid=72", "--volume-path=/data", "--dry-run=client"}, 102 } 103 o := NewIOChaosOptions(tf, streams, string(v1alpha1.IoAttrOverride)) 104 cmd := o.NewCobraCommand(Attribute, AttributeShort) 105 o.AddCommonFlag(cmd, tf) 106 cmd.Flags().Uint64Var(&o.Ino, "ino", 0, `ino number.`) 107 cmd.Flags().Uint64Var(&o.Size, "size", 0, `File size.`) 108 cmd.Flags().Uint64Var(&o.Blocks, "blocks", 0, `The number of blocks the file occupies.`) 109 cmd.Flags().Uint16Var(&o.Perm, "perm", 0, `Decimal representation of file permissions.`) 110 cmd.Flags().Uint32Var(&o.Nlink, "nlink", 0, `The number of hard links.`) 111 cmd.Flags().Uint32Var(&o.UID, "uid", 0, `Owner's user ID.`) 112 cmd.Flags().Uint32Var(&o.GID, "gid", 0, `The owner's group ID.`) 113 114 for _, input := range inputs { 115 Expect(cmd.Flags().Parse(input)).Should(Succeed()) 116 Expect(o.CreateOptions.Complete()) 117 Expect(o.Complete()).Should(Succeed()) 118 Expect(o.Validate()).Should(Succeed()) 119 Expect(o.Run()).Should(Succeed()) 120 } 121 }) 122 123 It("fault io mistake", func() { 124 inputs := [][]string{ 125 {"--volume-path=/data", "--filling=zero", "--max-occurrences=10", "--max-length=1", "--dry-run=client"}, 126 {"--volume-path=/data", "--filling=random", "--max-occurrences=10", "--max-length=1", "--dry-run=client"}, 127 {"--volume-path=/data", "--filling=zero", "--max-occurrences=10", "--max-length=1", "--path=test.txt", "--percent=50", "--method=READ", "--dry-run=client"}, 128 } 129 o := NewIOChaosOptions(tf, streams, string(v1alpha1.IoMistake)) 130 cmd := o.NewCobraCommand(Mistake, MistakeShort) 131 o.AddCommonFlag(cmd, tf) 132 cmd.Flags().StringVar(&o.Filling, "filling", "", `The filling content of the error data can only be zero (filling with 0) or random (filling with random bytes).`) 133 cmd.Flags().IntVar(&o.MaxOccurrences, "max-occurrences", 1, `The maximum number of times an error can occur per operation.`) 134 cmd.Flags().IntVar(&o.MaxLength, "max-length", 1, `The maximum length (in bytes) of each error.`) 135 136 for _, input := range inputs { 137 Expect(cmd.Flags().Parse(input)).Should(Succeed()) 138 Expect(o.CreateOptions.Complete()) 139 Expect(o.Complete()).Should(Succeed()) 140 Expect(o.Validate()).Should(Succeed()) 141 Expect(o.Run()).Should(Succeed()) 142 } 143 }) 144 145 }) 146 })