github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/fault/list_and_delete_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  	"net/http"
    24  
    25  	. "github.com/onsi/ginkgo/v2"
    26  	. "github.com/onsi/gomega"
    27  
    28  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    29  	"k8s.io/apimachinery/pkg/runtime"
    30  	"k8s.io/apimachinery/pkg/runtime/schema"
    31  	"k8s.io/cli-runtime/pkg/genericiooptions"
    32  	"k8s.io/cli-runtime/pkg/resource"
    33  	"k8s.io/client-go/dynamic/fake"
    34  	"k8s.io/client-go/kubernetes/scheme"
    35  	clientfake "k8s.io/client-go/rest/fake"
    36  	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
    37  
    38  	"github.com/1aal/kubeblocks/pkg/cli/testing"
    39  )
    40  
    41  var _ = Describe("Chaos resources list and delete", func() {
    42  	var (
    43  		tf           *cmdtesting.TestFactory
    44  		streams      genericiooptions.IOStreams
    45  		namespace    = "test"
    46  		podChaosName = "testPodChaos"
    47  		podChaos     = testing.FakePodChaos(podChaosName, namespace)
    48  	)
    49  
    50  	BeforeEach(func() {
    51  		streams, _, _, _ = genericiooptions.NewTestIOStreams()
    52  		tf = testing.NewTestFactory(namespace)
    53  		codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
    54  		httpResp := func(obj runtime.Object) *http.Response {
    55  			return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, obj)}
    56  		}
    57  		tf.UnstructuredClient = &clientfake.RESTClient{
    58  			GroupVersion:         schema.GroupVersion{Group: Group, Version: Version},
    59  			NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
    60  			Client: clientfake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
    61  				urlPrefix := "/apis/" + GroupVersion + "/namespaces/" + namespace
    62  				mapping := map[string]*http.Response{
    63  					urlPrefix + "/podchaos/" + podChaos.Name: httpResp(podChaos),
    64  				}
    65  				return mapping[req.URL.Path], nil
    66  			}),
    67  		}
    68  
    69  		tf.Client = tf.UnstructuredClient
    70  		_ = v1alpha1.AddToScheme(scheme.Scheme)
    71  		tf.FakeDynamicClient = fake.NewSimpleDynamicClient(scheme.Scheme, podChaos)
    72  	})
    73  
    74  	AfterEach(func() {
    75  		tf.Cleanup()
    76  	})
    77  
    78  	Context("test list and delete chaos resources", func() {
    79  		It("test fault list", func() {
    80  			args := []string{"podchaoses"}
    81  			o := &ListAndDeleteOptions{Factory: tf, IOStreams: streams}
    82  			Expect(o.Complete(args)).Should(Succeed())
    83  			Expect(o.RunList()).Should(Succeed())
    84  		})
    85  
    86  		It("test fault delete", func() {
    87  			args := []string{"podchaoses"}
    88  			o := &ListAndDeleteOptions{Factory: tf, IOStreams: streams}
    89  			Expect(o.Complete(args)).Should(Succeed())
    90  			Expect(o.RunDelete()).Should(Succeed())
    91  		})
    92  	})
    93  })