github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/dashboard/dashboard_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 dashboard
    21  
    22  import (
    23  	"net/http"
    24  	"time"
    25  
    26  	. "github.com/onsi/ginkgo/v2"
    27  	. "github.com/onsi/gomega"
    28  
    29  	corev1 "k8s.io/api/core/v1"
    30  	"k8s.io/cli-runtime/pkg/genericiooptions"
    31  	"k8s.io/cli-runtime/pkg/resource"
    32  	"k8s.io/client-go/kubernetes/scheme"
    33  	"k8s.io/client-go/rest/fake"
    34  	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
    35  )
    36  
    37  const namespace = "test"
    38  
    39  var _ = Describe("kubeblocks", func() {
    40  	var streams genericiooptions.IOStreams
    41  	var tf *cmdtesting.TestFactory
    42  
    43  	fakeSvcs := func() *corev1.ServiceList {
    44  		svcs := &corev1.ServiceList{}
    45  		svc := corev1.Service{}
    46  		svc.SetName("kubeblocks-grafana")
    47  		svc.SetNamespace(namespace)
    48  		svc.SetLabels(map[string]string{
    49  			"app.kubernetes.io/instance": "kubeblocks",
    50  			"app.kubernetes.io/name":     "grafana",
    51  		})
    52  		svcs.Items = append(svcs.Items, svc)
    53  		return svcs
    54  	}
    55  
    56  	BeforeEach(func() {
    57  		streams, _, _, _ = genericiooptions.NewTestIOStreams()
    58  		tf = cmdtesting.NewTestFactory().WithNamespace(namespace)
    59  		codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
    60  		tf.UnstructuredClient = &fake.RESTClient{
    61  			NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
    62  			Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
    63  				switch p, m := req.URL.Path, req.Method; {
    64  				case p == "/api/v1/services" && m == "GET":
    65  					return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, fakeSvcs())}, nil
    66  				default:
    67  					return nil, nil
    68  				}
    69  			}),
    70  		}
    71  		tf.Client = tf.UnstructuredClient
    72  	})
    73  
    74  	AfterEach(func() {
    75  		tf.Cleanup()
    76  	})
    77  
    78  	It("dashboard cmd", func() {
    79  		cmd := NewDashboardCmd(tf, streams)
    80  		Expect(cmd).ShouldNot(BeNil())
    81  		Expect(cmd.HasSubCommands()).Should(BeTrue())
    82  	})
    83  
    84  	It("list", func() {
    85  		cmd := newListCmd(tf, streams)
    86  		Expect(cmd).ShouldNot(BeNil())
    87  
    88  		By("list options")
    89  
    90  		o := newListOptions(tf, streams)
    91  		Expect(o.complete()).Should(Succeed())
    92  		Expect(o.run()).Should(Succeed())
    93  	})
    94  
    95  	It("open", func() {
    96  		cmd := newOpenCmd(tf, streams)
    97  		Expect(cmd).ShouldNot(BeNil())
    98  
    99  		By("open options")
   100  		o := newOpenOptions(tf, streams)
   101  		Expect(cmd.Flags().Set(podRunningTimeoutFlag, time.Second.String())).Should(Succeed())
   102  		Expect(o).ShouldNot(BeNil())
   103  		Expect(o.complete(cmd, []string{})).Should(HaveOccurred())
   104  		Expect(o.complete(cmd, []string{"kubeblocks-grafana"})).Should(HaveOccurred())
   105  
   106  		clientSet, err := tf.KubernetesClientSet()
   107  		Expect(err).Should(Succeed())
   108  		o.portForwardOptions.PodClient = clientSet.CoreV1()
   109  		Expect(o.run()).Should(HaveOccurred())
   110  	})
   111  })