github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/cluster/list_logs_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 cluster
    21  
    22  import (
    23  	"net/http"
    24  	"os"
    25  
    26  	. "github.com/onsi/ginkgo/v2"
    27  	. "github.com/onsi/gomega"
    28  
    29  	corev1 "k8s.io/api/core/v1"
    30  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    31  	"k8s.io/apimachinery/pkg/runtime/schema"
    32  	"k8s.io/cli-runtime/pkg/genericiooptions"
    33  	"k8s.io/client-go/kubernetes/scheme"
    34  	restclient "k8s.io/client-go/rest"
    35  	"k8s.io/client-go/rest/fake"
    36  	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
    37  
    38  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    39  	"github.com/1aal/kubeblocks/pkg/cli/cluster"
    40  	"github.com/1aal/kubeblocks/pkg/cli/exec"
    41  	"github.com/1aal/kubeblocks/pkg/constant"
    42  )
    43  
    44  var _ = Describe("listLogs test", func() {
    45  	It("listLogs", func() {
    46  		tf := cmdtesting.NewTestFactory().WithNamespace("test")
    47  		defer tf.Cleanup()
    48  		codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
    49  		ns := scheme.Codecs.WithoutConversion()
    50  		tf.Client = &fake.RESTClient{
    51  			GroupVersion:         schema.GroupVersion{Group: "", Version: "v1"},
    52  			NegotiatedSerializer: ns,
    53  			Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
    54  				body := cmdtesting.ObjBody(codec, mockPod())
    55  				return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil
    56  			}),
    57  		}
    58  		tf.ClientConfigVal = &restclient.Config{APIPath: "/api", ContentConfig: restclient.ContentConfig{NegotiatedSerializer: scheme.Codecs, GroupVersion: &schema.GroupVersion{Version: "v1"}}}
    59  
    60  		stream := genericiooptions.NewTestIOStreamsDiscard()
    61  		o := &ListLogsOptions{
    62  			factory:   tf,
    63  			IOStreams: stream,
    64  		}
    65  
    66  		// validate without args
    67  		Expect(o.Validate([]string{})).Should(MatchError("must specify the cluster name"))
    68  
    69  		// validate with args
    70  		Expect(o.Validate([]string{"cluster-name"})).Should(BeNil())
    71  		Expect(o.Complete(o.factory, []string{"cluster-name"})).Should(BeNil())
    72  		Expect(o.clusterName).Should(Equal("cluster-name"))
    73  	})
    74  	It("printContext test", func() {
    75  		dataObj := &cluster.ClusterObjects{
    76  			Cluster: &appsv1alpha1.Cluster{
    77  				Spec: appsv1alpha1.ClusterSpec{
    78  					ComponentSpecs: []appsv1alpha1.ClusterComponentSpec{
    79  						{
    80  							Name:            "component-name",
    81  							ComponentDefRef: "component-type",
    82  							EnabledLogs:     []string{"slow"},
    83  						},
    84  					},
    85  				},
    86  			},
    87  			ClusterDef: &appsv1alpha1.ClusterDefinition{
    88  				Spec: appsv1alpha1.ClusterDefinitionSpec{
    89  					ComponentDefs: []appsv1alpha1.ClusterComponentDefinition{
    90  						{
    91  							Name: "component-type",
    92  							LogConfigs: []appsv1alpha1.LogConfig{
    93  								{
    94  									Name:            "slow",
    95  									FilePathPattern: "",
    96  								},
    97  							},
    98  						},
    99  					},
   100  				},
   101  			},
   102  			Pods: &corev1.PodList{},
   103  		}
   104  		pod := corev1.Pod{
   105  			ObjectMeta: metav1.ObjectMeta{
   106  				Name:            "foo",
   107  				Namespace:       "test",
   108  				ResourceVersion: "10",
   109  				Labels: map[string]string{
   110  					"app.kubernetes.io/name":        "mysql-apecloud-mysql",
   111  					constant.KBAppComponentLabelKey: "component-name",
   112  				},
   113  			},
   114  		}
   115  		dataObj.Pods.Items = append(dataObj.Pods.Items, pod)
   116  		o := &ListLogsOptions{
   117  			exec: &exec.ExecOptions{},
   118  			IOStreams: genericiooptions.IOStreams{
   119  				Out:    os.Stdout,
   120  				ErrOut: os.Stdout,
   121  			},
   122  		}
   123  		Expect(o.printListLogs(dataObj)).Should(BeNil())
   124  	})
   125  
   126  	It("convertToLogFileInfo test", func() {
   127  		// empty case
   128  		fileInfo := ""
   129  		logFileList := convertToLogFileInfo(fileInfo, "type", "inst", "component")
   130  		Expect(len(logFileList)).Should(Equal(0))
   131  		// normal case, and size is 1
   132  		fileInfo1 := "-rw-r----- 1 mysql mysql 6.1K Nov 23, 2022 21:37 (UTC+08:00) mysqld.err\n"
   133  		logFileList1 := convertToLogFileInfo(fileInfo1, "type", "inst", "component")
   134  		Expect(len(logFileList1)).Should(Equal(1))
   135  		// normal case, and size is 2
   136  		fileInfo2 := "-rw-r----- 1 mysql mysql 6.1K Nov 23, 2022 21:37 (UTC+08:00) mysqld.err\n-rw-r----- 1 root  root  1.7M Dec 08, 2022 10:07 (UTC+08:00) mysqld.err.1"
   137  		logFileList2 := convertToLogFileInfo(fileInfo2, "type", "inst", "component")
   138  		Expect(len(logFileList2)).Should(Equal(2))
   139  	})
   140  })