github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/context/cloud_context_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 context 21 22 import ( 23 ginkgo_context "github.com/onsi/ginkgo/v2" 24 . "github.com/onsi/gomega" 25 26 "encoding/json" 27 "net/http" 28 "net/http/httptest" 29 30 "github.com/1aal/kubeblocks/pkg/cli/cmd/organization" 31 ) 32 33 func mockServer() *httptest.Server { 34 return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 35 switch r.URL.Path { 36 case "/api/v1/organizations/test_org/contexts/test_context": 37 switch r.Method { 38 case http.MethodGet: 39 cloudContextResponse := CloudContextResponse{ 40 APIVersion: "test_api_version", 41 Kind: "test_kind", 42 Metadata: Metadata{ 43 Name: "test_context", 44 }, 45 } 46 jsonData, err := json.Marshal(cloudContextResponse) 47 if err != nil { 48 w.WriteHeader(http.StatusInternalServerError) 49 return 50 } 51 w.Header().Set("Content-Type", "application/json") 52 w.WriteHeader(http.StatusOK) 53 _, _ = w.Write(jsonData) 54 case http.MethodDelete: 55 w.WriteHeader(http.StatusOK) 56 case http.MethodPost: 57 w.WriteHeader(http.StatusCreated) 58 default: 59 w.WriteHeader(http.StatusNotFound) 60 } 61 62 case "/api/v1/organizations/test_org/contexts": 63 cloudContextsResponse := CloudContextsResponse{ 64 APIVersion: "test_api_version", 65 Kind: "test_kind", 66 Items: []ClusterItem{ 67 { 68 Metadata: Metadata{ 69 Name: "test_context", 70 }, 71 }, 72 }, 73 } 74 jsonData, err := json.Marshal(cloudContextsResponse) 75 if err != nil { 76 w.WriteHeader(http.StatusInternalServerError) 77 return 78 } 79 80 w.Header().Set("Content-Type", "application/json") 81 w.WriteHeader(http.StatusOK) 82 _, _ = w.Write(jsonData) 83 } 84 })) 85 } 86 87 var _ = ginkgo_context.Describe("Test Cloud Context", func() { 88 var ( 89 o *CloudContext 90 ) 91 ginkgo_context.BeforeEach(func() { 92 server := mockServer() 93 o = &CloudContext{ 94 ContextName: "test_context", 95 OrgName: "test_org", 96 APIURL: server.URL, 97 APIPath: organization.APIPath, 98 } 99 }) 100 101 ginkgo_context.AfterEach(func() { 102 }) 103 104 ginkgo_context.Context("test cloud context", func() { 105 Expect(organization.SetCurrentOrgAndContext(&organization.CurrentOrgAndContext{ 106 CurrentOrganization: "test_org", 107 CurrentContext: "test_context", 108 })).Should(BeNil()) 109 110 ginkgo_context.It("test GetContext ", func() { 111 ExpectWithOffset(1, func() error { 112 _, err := o.GetContext() 113 return err 114 }()).To(BeNil()) 115 }) 116 117 ginkgo_context.It("test GetContexts ", func() { 118 ExpectWithOffset(1, func() error { 119 _, err := o.GetContexts() 120 return err 121 }()).To(BeNil()) 122 }) 123 124 ginkgo_context.It("test getCurrentContext ", func() { 125 ExpectWithOffset(1, func() error { 126 _, err := o.getCurrentContext() 127 return err 128 }()).To(BeNil()) 129 }) 130 131 ginkgo_context.It("test useContext ", func() { 132 ExpectWithOffset(1, func() error { 133 _, err := o.useContext("test_context") 134 return err 135 }()).To(BeNil()) 136 }) 137 138 ginkgo_context.It("test deleteOrganization ", func() { 139 ExpectWithOffset(1, func() error { 140 err := o.removeContext() 141 return err 142 }()).To(BeNil()) 143 }) 144 }) 145 })