github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/report/zipwritter_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 report 21 22 import ( 23 "context" 24 "os" 25 26 . "github.com/onsi/ginkgo/v2" 27 . "github.com/onsi/gomega" 28 corev1 "k8s.io/api/core/v1" 29 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 30 "k8s.io/apimachinery/pkg/runtime" 31 "k8s.io/cli-runtime/pkg/printers" 32 33 "github.com/1aal/kubeblocks/pkg/cli/testing" 34 ) 35 36 var _ = Describe("zipwritter", func() { 37 var printer printers.ResourcePrinter 38 const fileName = "test.zip" 39 40 Context("zipwritter", func() { 41 BeforeEach(func() { 42 printer = &printers.JSONPrinter{} 43 }) 44 AfterEach(func() { 45 os.Remove(fileName) 46 }) 47 48 It("should succeed to new zipwritter", func() { 49 zipwritter := NewReportWritter() 50 printer = &printers.JSONPrinter{} 51 err := zipwritter.Init(fileName, printer.PrintObj) 52 Expect(err).Should(Succeed()) 53 err = zipwritter.Init(fileName, printer.PrintObj) 54 Expect(err).Should(HaveOccurred()) 55 Expect(err.Error()).Should(ContainSubstring("already exists")) 56 }) 57 58 It("should succeed to close zipwritter", func() { 59 zipwritter := NewReportWritter() 60 printer = &printers.JSONPrinter{} 61 err := zipwritter.Init(fileName, printer.PrintObj) 62 Expect(err).Should(Succeed()) 63 err = zipwritter.Close() 64 Expect(err).Should(Succeed()) 65 }) 66 67 It("should succeed to write kbversion", func() { 68 zipwritter := NewReportWritter() 69 printer = &printers.JSONPrinter{} 70 err := zipwritter.Init(fileName, printer.PrintObj) 71 Expect(err).Should(Succeed()) 72 73 client := testing.FakeClientSet(testing.FakeKBDeploy("0.5.23")) 74 err = zipwritter.WriteKubeBlocksVersion("versions.txt", client) 75 Expect(err).Should(Succeed()) 76 err = zipwritter.Close() 77 Expect(err).Should(Succeed()) 78 }) 79 80 It("should succeed to write objects", func() { 81 zipwritter := NewReportWritter() 82 printer = &printers.JSONPrinter{} 83 err := zipwritter.Init(fileName, printer.PrintObj) 84 Expect(err).Should(Succeed()) 85 86 deploy := testing.FakeKBDeploy("0.5.23") 87 unstructuredDeploy, err := runtime.DefaultUnstructuredConverter.ToUnstructured(deploy) 88 Expect(err).Should(Succeed()) 89 unstructuredList := unstructured.UnstructuredList{} 90 unstructuredList.Items = []unstructured.Unstructured{{Object: unstructuredDeploy}} 91 92 err = zipwritter.WriteObjects("objects", []*unstructured.UnstructuredList{&unstructuredList}, "json") 93 Expect(err).Should(Succeed()) 94 95 err = zipwritter.WriteSingleObject("single-object", deploy.Kind, deploy.Name, deploy, "json") 96 Expect(err).Should(Succeed()) 97 98 err = zipwritter.Close() 99 Expect(err).Should(Succeed()) 100 }) 101 102 It("should succeed to write events", func() { 103 zipwritter := NewReportWritter() 104 printer = &printers.JSONPrinter{} 105 err := zipwritter.Init(fileName, printer.PrintObj) 106 Expect(err).Should(Succeed()) 107 108 deploy := testing.FakeKBDeploy("0.5.23") 109 event := testing.FakeEventForObject("test-events", deploy.Namespace, deploy.Name) 110 events := map[string][]corev1.Event{"pod": {*event}} 111 112 err = zipwritter.WriteEvents("events", events, "json") 113 Expect(err).Should(Succeed()) 114 115 err = zipwritter.Close() 116 Expect(err).Should(Succeed()) 117 }) 118 119 It("should succeed to write logs", func() { 120 zipwritter := NewReportWritter() 121 printer = &printers.JSONPrinter{} 122 err := zipwritter.Init(fileName, printer.PrintObj) 123 Expect(err).Should(Succeed()) 124 125 ctx := context.Background() 126 pods := testing.FakePods(1, "test", "test-cluster") 127 client := testing.FakeClientSet(&pods.Items[0]) 128 129 logOption := corev1.PodLogOptions{} 130 err = zipwritter.WriteLogs("logs", ctx, client, pods, logOption, true) 131 Expect(err).Should(Succeed()) 132 133 err = zipwritter.Close() 134 Expect(err).Should(Succeed()) 135 }) 136 }) 137 })