github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/container/registryv1/namespaces_test.go (about) 1 package registryv1 2 3 import ( 4 "fmt" 5 "log" 6 "net/http" 7 8 ibmcloud "github.com/IBM-Cloud/bluemix-go" 9 "github.com/IBM-Cloud/bluemix-go/client" 10 ibmcloudHttp "github.com/IBM-Cloud/bluemix-go/http" 11 "github.com/IBM-Cloud/bluemix-go/session" 12 13 "github.com/onsi/gomega/ghttp" 14 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 ) 18 19 const ( 20 namespaceName = `gpfs` 21 namespaceList = `[ 22 "gpfs", 23 "devops_insights_dnt_dev", 24 "devops_insights_dnt_staging", 25 "bkuschel", 26 "othomann", 27 "bog-jx" 28 ]` 29 addNamespace = `{ 30 "namespace": "gpfs" 31 }` 32 namespaceForbidden = `{ 33 "code": "CRG0020E", 34 "message": "You are not authorized to access the specified resource.", 35 "request-id": "4185-1540334590.738-17873666" 36 }` 37 ) 38 39 var _ = Describe("Namespaces", func() { 40 var server *ghttp.Server 41 AfterEach(func() { 42 server.Close() 43 }) 44 Describe("GetNamespaces", func() { 45 Context("When get namespaces is completed", func() { 46 BeforeEach(func() { 47 server = ghttp.NewServer() 48 server.AppendHandlers( 49 ghttp.CombineHandlers( 50 ghttp.VerifyRequest(http.MethodGet, "/api/v1/namespaces"), 51 ghttp.RespondWith(http.StatusOK, namespaceList), 52 ), 53 ) 54 }) 55 56 It("should return get namespaces results", func() { 57 target := NamespaceTargetHeader{ 58 AccountID: "abc", 59 } 60 resparr, err := newNamespaces(server.URL()).GetNamespaces(target) 61 Expect(err).NotTo(HaveOccurred()) 62 Expect(resparr).NotTo(BeNil()) 63 Expect(resparr).To(HaveLen(6)) 64 Expect(resparr[0]).Should(Equal("gpfs")) 65 Expect(resparr[1]).Should(Equal("devops_insights_dnt_dev")) 66 Expect(resparr[2]).Should(Equal("devops_insights_dnt_staging")) 67 Expect(resparr[3]).Should(Equal("bkuschel")) 68 Expect(resparr[4]).Should(Equal("othomann")) 69 Expect(resparr[5]).Should(Equal("bog-jx")) 70 }) 71 }) 72 Context("When get namespace fails", func() { 73 BeforeEach(func() { 74 server = ghttp.NewServer() 75 server.SetAllowUnhandledRequests(true) 76 server.AppendHandlers( 77 ghttp.CombineHandlers( 78 ghttp.VerifyRequest(http.MethodGet, "/api/v1/namespaces"), 79 ghttp.RespondWith(http.StatusInternalServerError, `Internal Error`), 80 ), 81 ) 82 }) 83 84 It("should return error when namespaces are retrieved", func() { 85 target := NamespaceTargetHeader{ 86 AccountID: "abc", 87 } 88 resparr, err := newNamespaces(server.URL()).GetNamespaces(target) 89 Expect(err).To(HaveOccurred()) 90 Expect(resparr).Should(BeNil()) 91 }) 92 }) 93 }) 94 95 Describe("AddNamespace", func() { 96 Context("When add namespace is completed", func() { 97 BeforeEach(func() { 98 server = ghttp.NewServer() 99 server.AppendHandlers( 100 ghttp.CombineHandlers( 101 ghttp.VerifyRequest(http.MethodPut, fmt.Sprintf("/api/v1/namespaces/%s", namespaceName)), 102 ghttp.RespondWith(http.StatusOK, addNamespace), 103 ), 104 ) 105 }) 106 107 It("should return add namespace results", func() { 108 target := NamespaceTargetHeader{ 109 AccountID: "abc", 110 } 111 respptr, err := newNamespaces(server.URL()).AddNamespace(namespaceName, target) 112 Expect(err).NotTo(HaveOccurred()) 113 Expect(respptr).NotTo(BeNil()) 114 resp := *respptr 115 Expect(resp.Namespace).Should(Equal("gpfs")) 116 }) 117 }) 118 Context("When add namespace fails", func() { 119 BeforeEach(func() { 120 server = ghttp.NewServer() 121 server.AppendHandlers( 122 ghttp.CombineHandlers( 123 ghttp.VerifyRequest(http.MethodPut, fmt.Sprintf("/api/v1/namespaces/%s", namespaceName)), 124 ghttp.RespondWith(http.StatusForbidden, namespaceForbidden), 125 ), 126 ) 127 }) 128 129 It("should return error when namepsaces is added", func() { 130 target := NamespaceTargetHeader{ 131 AccountID: "abc", 132 } 133 resparr, err := newNamespaces(server.URL()).AddNamespace(namespaceName, target) 134 Expect(err).To(HaveOccurred()) 135 Expect(resparr).Should(BeNil()) 136 }) 137 }) 138 }) 139 140 Describe("DeleteNamespace", func() { 141 Context("When Delete namespace is completed", func() { 142 BeforeEach(func() { 143 server = ghttp.NewServer() 144 server.AppendHandlers( 145 ghttp.CombineHandlers( 146 ghttp.VerifyRequest(http.MethodDelete, fmt.Sprintf("/api/v1/namespaces/%s", namespaceName)), 147 ghttp.RespondWith(http.StatusNoContent, nil), 148 ), 149 ) 150 }) 151 152 It("should return delete namespace results", func() { 153 target := NamespaceTargetHeader{ 154 AccountID: "abc", 155 } 156 err := newNamespaces(server.URL()).DeleteNamespace(namespaceName, target) 157 Expect(err).NotTo(HaveOccurred()) 158 }) 159 }) 160 Context("When delete namespace fails", func() { 161 BeforeEach(func() { 162 server = ghttp.NewServer() 163 server.AppendHandlers( 164 ghttp.CombineHandlers( 165 ghttp.VerifyRequest(http.MethodDelete, fmt.Sprintf("/api/v1/namespaces/%s", namespaceName)), 166 ghttp.RespondWith(http.StatusForbidden, namespaceForbidden), 167 ), 168 ) 169 }) 170 171 It("should return error when namespace is deleted", func() { 172 target := NamespaceTargetHeader{ 173 AccountID: "abc", 174 } 175 err := newNamespaces(server.URL()).DeleteNamespace(namespaceName, target) 176 Expect(err).To(HaveOccurred()) 177 }) 178 }) 179 }) 180 }) 181 182 func newNamespaces(url string) Namespaces { 183 184 sess, err := session.New() 185 if err != nil { 186 log.Fatal(err) 187 } 188 conf := sess.Config.Copy() 189 conf.HTTPClient = ibmcloudHttp.NewHTTPClient(conf) 190 conf.Endpoint = &url 191 192 client := client.Client{ 193 Config: conf, 194 ServiceName: ibmcloud.ContainerRegistryService, 195 } 196 return newNamespaceAPI(&client) 197 }