github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/preflight/collect_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 preflight 21 22 import ( 23 "context" 24 "net/http" 25 "time" 26 27 . "github.com/onsi/ginkgo/v2" 28 . "github.com/onsi/gomega" 29 30 troubleshoot "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" 31 corev1 "k8s.io/api/core/v1" 32 "k8s.io/apimachinery/pkg/runtime" 33 "k8s.io/apimachinery/pkg/runtime/schema" 34 "k8s.io/cli-runtime/pkg/genericiooptions" 35 "k8s.io/cli-runtime/pkg/resource" 36 "k8s.io/client-go/kubernetes/scheme" 37 clientfake "k8s.io/client-go/rest/fake" 38 cmdtesting "k8s.io/kubectl/pkg/cmd/testing" 39 40 preflightv1beta2 "github.com/1aal/kubeblocks/externalapis/preflight/v1beta2" 41 preflightTesting "github.com/1aal/kubeblocks/pkg/cli/preflight/testing" 42 "github.com/1aal/kubeblocks/pkg/cli/testing" 43 "github.com/1aal/kubeblocks/pkg/cli/types" 44 ) 45 46 var _ = Describe("collect_test", func() { 47 var ( 48 timeOut = 10 * time.Second 49 namespace = "test" 50 clusterName = "test" 51 tf *cmdtesting.TestFactory 52 cluster = testing.FakeCluster(clusterName, namespace) 53 pods = testing.FakePods(3, namespace, clusterName) 54 preflight *preflightv1beta2.Preflight 55 hostPreflight *preflightv1beta2.HostPreflight 56 ) 57 58 BeforeEach(func() { 59 _, _, _, _ = genericiooptions.NewTestIOStreams() 60 tf = testing.NewTestFactory(namespace) 61 codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...) 62 httpResp := func(obj runtime.Object) *http.Response { 63 return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, obj)} 64 } 65 66 tf.UnstructuredClient = &clientfake.RESTClient{ 67 GroupVersion: schema.GroupVersion{Group: types.AppsAPIGroup, Version: types.AppsAPIVersion}, 68 NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer, 69 Client: clientfake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { 70 if req.Method != "GET" { 71 return nil, nil 72 } 73 urlPrefix := "/api/v1/namespaces/" + namespace 74 mapping := map[string]*http.Response{ 75 "/api/v1/nodes/" + testing.NodeName: httpResp(testing.FakeNode()), 76 urlPrefix + "/services": httpResp(&corev1.ServiceList{}), 77 urlPrefix + "/events": httpResp(&corev1.EventList{}), 78 urlPrefix + "/pods": httpResp(pods), 79 } 80 return mapping[req.URL.Path], nil 81 }), 82 } 83 84 tf.Client = tf.UnstructuredClient 85 tf.FakeDynamicClient = testing.FakeDynamicClient(cluster, testing.FakeClusterDef(), testing.FakeClusterVersion()) 86 87 preflight = preflightTesting.FakeKbPreflight() 88 hostPreflight = preflightTesting.FakeKbHostPreflight() 89 }) 90 91 AfterEach(func() { 92 tf.Cleanup() 93 }) 94 95 It("CollectPreflight test, and expect success ", func() { 96 Eventually(func(g Gomega) { 97 progressCh := make(chan interface{}) 98 go func() { 99 for { 100 g.Expect(<-progressCh).NotTo(BeNil()) 101 } 102 }() 103 results, err := CollectPreflight(tf, nil, context.TODO(), preflight, hostPreflight, progressCh) 104 g.Expect(err).NotTo(HaveOccurred()) 105 g.Expect(len(results)).Should(BeNumerically(">=", 3)) 106 }).WithTimeout(timeOut).Should(Succeed()) 107 }) 108 109 It("CollectHostData Test, and expect success", func() { 110 Eventually(func(g Gomega) { 111 progressCh := make(chan interface{}) 112 go func() { 113 for { 114 g.Expect(<-progressCh).NotTo(BeNil()) 115 } 116 }() 117 results, err := CollectHostData(context.TODO(), hostPreflight, progressCh) 118 g.Expect(err).NotTo(HaveOccurred()) 119 _, ok := (*results).(KBHostCollectResult) 120 g.Expect(ok).Should(BeTrue()) 121 }).WithTimeout(timeOut).Should(Succeed()) 122 }) 123 124 It("CollectRemoteData test, and expect success", func() { 125 Eventually(func(g Gomega) { 126 progressCh := make(chan interface{}) 127 go func() { 128 for { 129 g.Expect(<-progressCh).NotTo(BeNil()) 130 } 131 }() 132 collectResult, err := CollectRemoteData(context.TODO(), &preflightv1beta2.HostPreflight{}, tf, progressCh) 133 g.Expect(err).NotTo(HaveOccurred()) 134 g.Expect(collectResult).NotTo(BeNil()) 135 }).WithTimeout(timeOut).Should(Succeed()) 136 }) 137 138 It("ParseTimeFlags test, and expect success", func() { 139 sinceStr := "5m" 140 sinceTimeStr := "2023-01-09T15:18:46+08:00" 141 Expect(ParseTimeFlags(sinceStr, sinceTimeStr, []*troubleshoot.Collect{})).Should(HaveOccurred()) 142 Expect(ParseTimeFlags(sinceTimeStr, "", []*troubleshoot.Collect{})).Should(Succeed()) 143 Expect(ParseTimeFlags("", sinceStr, []*troubleshoot.Collect{})).Should(Succeed()) 144 }) 145 146 It("ParseTimeFlags test, and expect error", func() { 147 sinceStr := "5error-m" 148 sinceTimeStr := "2023-01-09T15:46+:00" 149 Expect(ParseTimeFlags("", "", []*troubleshoot.Collect{})).Should(HaveOccurred()) 150 Expect(ParseTimeFlags(sinceStr, "", []*troubleshoot.Collect{})).Should(HaveOccurred()) 151 Expect(ParseTimeFlags("", sinceTimeStr, []*troubleshoot.Collect{})).Should(HaveOccurred()) 152 153 }) 154 })