github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/fault/fault_node_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 Node", 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 node", func() {
    49  		It("fault node stop", func() {
    50  			inputs := [][]string{
    51  				{"-c=aws", "--region=cn-northwest-1", "--dry-run=client"},
    52  				{"-c=aws", "--region=cn-northwest-1", "--secret=test-secret", "--dry-run=client"},
    53  				{"-c=gcp", "--region=us-central1-c", "--project=apecloud-platform-engineering", "--dry-run=client"},
    54  			}
    55  			o := NewNodeOptions(tf, streams)
    56  			cmd := o.NewCobraCommand(Stop, StopShort)
    57  			o.AddCommonFlag(cmd)
    58  
    59  			o.Args = []string{"node1", "node2"}
    60  			for _, input := range inputs {
    61  				Expect(cmd.Flags().Parse(input)).Should(Succeed())
    62  				Expect(o.Execute(Stop, o.Args, true)).Should(Succeed())
    63  			}
    64  		})
    65  
    66  		It("fault node restart", func() {
    67  			inputs := [][]string{
    68  				{"-c=aws", "--region=cn-northwest-1", "--dry-run=client"},
    69  				{"-c=aws", "--region=cn-northwest-1", "--secret=test-secret", "--dry-run=client"},
    70  				{"-c=gcp", "--region=us-central1-c", "--project=apecloud-platform-engineering", "--dry-run=client"},
    71  			}
    72  			o := NewNodeOptions(tf, streams)
    73  			cmd := o.NewCobraCommand(Restart, RestartShort)
    74  			o.AddCommonFlag(cmd)
    75  
    76  			o.Args = []string{"node1", "node2"}
    77  			for _, input := range inputs {
    78  				Expect(cmd.Flags().Parse(input)).Should(Succeed())
    79  				Expect(o.Execute(Restart, o.Args, true)).Should(Succeed())
    80  			}
    81  		})
    82  
    83  		It("fault node detach-volume", func() {
    84  			inputs := [][]string{
    85  				{"-c=aws", "--region=cn-northwest-1", "--volume-id=v1,v2", "--device-name=/d1,/d2", "--dry-run=client"},
    86  				{"-c=aws", "--region=cn-northwest-1", "--volume-id=v1,v2", "--device-name=/d1,/d2", "--secret=test-secret", "--dry-run=client"},
    87  				{"-c=gcp", "--region=us-central1-c", "--project=apecloud-platform-engineering", "--device-name=/d1,/d2", "--dry-run=client"},
    88  			}
    89  			o := NewNodeOptions(tf, streams)
    90  			cmd := o.NewCobraCommand(DetachVolume, DetachVolumeShort)
    91  			o.AddCommonFlag(cmd)
    92  			cmd.Flags().StringSliceVar(&o.VolumeIDs, "volume-id", nil, "The volume id of the ec2.")
    93  			cmd.Flags().StringSliceVar(&o.DeviceNames, "device-name", nil, "The device name of the volume.")
    94  
    95  			o.Args = []string{"node1", "node2"}
    96  			for _, input := range inputs {
    97  				Expect(cmd.Flags().Parse(input)).Should(Succeed())
    98  				Expect(o.Execute(DetachVolume, o.Args, true)).Should(Succeed())
    99  				o.VolumeIDs = nil
   100  				o.DeviceNames = nil
   101  			}
   102  		})
   103  	})
   104  })