github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/container/containerv1/workers_test.go (about) 1 package containerv1 2 3 import ( 4 "log" 5 "net/http" 6 7 bluemix "github.com/IBM-Cloud/bluemix-go" 8 "github.com/IBM-Cloud/bluemix-go/client" 9 bluemixHttp "github.com/IBM-Cloud/bluemix-go/http" 10 "github.com/IBM-Cloud/bluemix-go/session" 11 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 "github.com/onsi/gomega/ghttp" 15 ) 16 17 var _ = Describe("Workers", func() { 18 var server *ghttp.Server 19 Describe("Add", func() { 20 Context("When adding a worker is successful", func() { 21 BeforeEach(func() { 22 server = ghttp.NewServer() 23 server.AppendHandlers( 24 ghttp.CombineHandlers( 25 ghttp.VerifyRequest(http.MethodPost, "/v1/clusters/test/workers"), 26 ghttp.RespondWith(http.StatusCreated, `{}`), 27 ), 28 ) 29 }) 30 31 It("should return worker added to cluster", func() { 32 target := ClusterTargetHeader{ 33 OrgID: "abc", 34 SpaceID: "def", 35 AccountID: "ghi", 36 } 37 params := WorkerParam{ 38 Isolation: "public", MachineType: "u2c.2x4", Prefix: "test", PrivateVlan: "1764491", PublicVlan: "1764435", WorkerNum: 2, 39 } 40 err := newWorker(server.URL()).Add("test", params, target) 41 Expect(err).NotTo(HaveOccurred()) 42 }) 43 }) 44 Context("When adding worker is unsuccessful", func() { 45 BeforeEach(func() { 46 server = ghttp.NewServer() 47 server.SetAllowUnhandledRequests(true) 48 server.AppendHandlers( 49 ghttp.CombineHandlers( 50 ghttp.VerifyRequest(http.MethodPost, "/v1/clusters/test/workers"), 51 ghttp.RespondWith(http.StatusInternalServerError, `Failed to add worker to cluster`), 52 ), 53 ) 54 }) 55 56 It("should return error during add webhook to cluster", func() { 57 target := ClusterTargetHeader{ 58 OrgID: "abc", 59 SpaceID: "def", 60 AccountID: "ghi", 61 } 62 params := WorkerParam{ 63 Isolation: "public", MachineType: "u2c.2x4", Prefix: "test", PrivateVlan: "1764491", PublicVlan: "1764435", WorkerNum: 2, 64 } 65 err := newWorker(server.URL()).Add("test", params, target) 66 Expect(err).To(HaveOccurred()) 67 }) 68 }) 69 }) 70 //Get 71 Describe("Get", func() { 72 Context("When retrieving worker is successful", func() { 73 BeforeEach(func() { 74 server = ghttp.NewServer() 75 server.AppendHandlers( 76 ghttp.CombineHandlers( 77 ghttp.VerifyRequest(http.MethodGet, "/v1/workers/abc-123-def-ghi"), 78 ghttp.RespondWith(http.StatusOK, `{"ErrorMessage":"","Isolation":"","MachineType":"u2c.2x4","KubeVersion":"","PrivateIP":"","PublicIP":"","PrivateVlan":"vlan","PublicVlan":"vlan","state":"normal","status":"ready"}`), 79 ), 80 ) 81 }) 82 83 It("should return available workers ", func() { 84 target := ClusterTargetHeader{ 85 OrgID: "abc", 86 SpaceID: "def", 87 AccountID: "ghi", 88 } 89 worker, err := newWorker(server.URL()).Get("abc-123-def-ghi", target) 90 Expect(err).NotTo(HaveOccurred()) 91 Expect(worker).ShouldNot(BeNil()) 92 Expect(worker.State).Should(Equal("normal")) 93 }) 94 }) 95 Context("When retrieving worker is unsuccessful", func() { 96 BeforeEach(func() { 97 server = ghttp.NewServer() 98 server.SetAllowUnhandledRequests(true) 99 server.AppendHandlers( 100 ghttp.CombineHandlers( 101 ghttp.VerifyRequest(http.MethodGet, "/v1/workers/abc-123-def-ghi"), 102 ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve workers`), 103 ), 104 ) 105 }) 106 107 It("should return error during retrieveing workers", func() { 108 target := ClusterTargetHeader{ 109 OrgID: "abc", 110 SpaceID: "def", 111 AccountID: "ghi", 112 } 113 worker, err := newWorker(server.URL()).Get("abc-123-def-ghi", target) 114 Expect(err).To(HaveOccurred()) 115 Expect(worker.ID).Should(Equal("")) 116 Expect(worker.State).Should(Equal("")) 117 }) 118 }) 119 }) 120 //List 121 Describe("List", func() { 122 Context("When retrieving available workers of a cluster is successful", func() { 123 BeforeEach(func() { 124 server = ghttp.NewServer() 125 server.AppendHandlers( 126 ghttp.CombineHandlers( 127 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/myCluster/workers"), 128 ghttp.RespondWith(http.StatusOK, `[{"ErrorMessage":"","Isolation":"","MachineType":"u2c.2x4","KubeVersion":"","PrivateIP":"","PublicIP":"","PrivateVlan":"vlan","PublicVlan":"vlan","state":"normal","status":"ready"}]`), 129 ), 130 ) 131 }) 132 133 It("should return available workers ", func() { 134 target := ClusterTargetHeader{ 135 OrgID: "abc", 136 SpaceID: "def", 137 AccountID: "ghi", 138 } 139 worker, err := newWorker(server.URL()).List("myCluster", target) 140 Expect(err).NotTo(HaveOccurred()) 141 Expect(worker).ShouldNot(BeNil()) 142 for _, wObj := range worker { 143 Expect(wObj).ShouldNot(BeNil()) 144 Expect(wObj.State).Should(Equal("normal")) 145 } 146 }) 147 }) 148 Context("When retrieving available workers is unsuccessful", func() { 149 BeforeEach(func() { 150 server = ghttp.NewServer() 151 server.SetAllowUnhandledRequests(true) 152 server.AppendHandlers( 153 ghttp.CombineHandlers( 154 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/myCluster/workers"), 155 ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve workers`), 156 ), 157 ) 158 }) 159 160 It("should return error during retrieveing workers", func() { 161 target := ClusterTargetHeader{ 162 OrgID: "abc", 163 SpaceID: "def", 164 AccountID: "ghi", 165 } 166 worker, err := newWorker(server.URL()).List("myCluster", target) 167 Expect(err).To(HaveOccurred()) 168 Expect(worker).Should(BeNil()) 169 Expect(len(worker)).Should(Equal(0)) 170 }) 171 }) 172 }) 173 //ListByWorkerPool 174 Describe("ListByWorkerPool", func() { 175 Context("When retrieving available workers belong to a worker pool of a cluster is successful", func() { 176 BeforeEach(func() { 177 server = ghttp.NewServer() 178 server.AppendHandlers( 179 ghttp.CombineHandlers( 180 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/myCluster/workers"), 181 ghttp.RespondWith(http.StatusOK, `[{"ErrorMessage":"","Isolation":"","MachineType":"u2c.2x4","KubeVersion":"","PrivateIP":"","PublicIP":"","PrivateVlan":"vlan","PublicVlan":"vlan","state":"normal","status":"ready"}]`), 182 ), 183 ) 184 }) 185 186 It("should return available workers ", func() { 187 target := ClusterTargetHeader{ 188 OrgID: "abc", 189 SpaceID: "def", 190 AccountID: "ghi", 191 Region: "eu-de", 192 } 193 194 worker, err := newWorker(server.URL()).ListByWorkerPool("myCluster", "test", false, target) 195 Expect(err).NotTo(HaveOccurred()) 196 Expect(worker).ShouldNot(BeNil()) 197 for _, wObj := range worker { 198 Expect(wObj).ShouldNot(BeNil()) 199 Expect(wObj.State).Should(Equal("normal")) 200 } 201 }) 202 }) 203 Context("When retrieving available workers is unsuccessful", func() { 204 BeforeEach(func() { 205 server = ghttp.NewServer() 206 server.SetAllowUnhandledRequests(true) 207 server.AppendHandlers( 208 ghttp.CombineHandlers( 209 ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/myCluster/workers"), 210 ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve workers`), 211 ), 212 ) 213 }) 214 215 It("should return error during retrieveing workers", func() { 216 target := ClusterTargetHeader{ 217 OrgID: "abc", 218 SpaceID: "def", 219 AccountID: "ghi", 220 Region: "eu-de", 221 } 222 223 worker, err := newWorker(server.URL()).ListByWorkerPool("myCluster", "test", false, target) 224 Expect(err).To(HaveOccurred()) 225 Expect(worker).Should(BeNil()) 226 Expect(len(worker)).Should(Equal(0)) 227 }) 228 }) 229 }) 230 //Delete 231 Describe("Delete", func() { 232 Context("When delete of worker is successful", func() { 233 BeforeEach(func() { 234 server = ghttp.NewServer() 235 server.AppendHandlers( 236 ghttp.CombineHandlers( 237 ghttp.VerifyRequest(http.MethodDelete, "/v1/clusters/test/workers/abc-123-def-ghi"), 238 ghttp.RespondWith(http.StatusOK, `{ 239 }`), 240 ), 241 ) 242 }) 243 244 It("should delete cluster", func() { 245 target := ClusterTargetHeader{ 246 OrgID: "abc", 247 SpaceID: "def", 248 AccountID: "ghi", 249 } 250 err := newWorker(server.URL()).Delete("test", "abc-123-def-ghi", target) 251 Expect(err).NotTo(HaveOccurred()) 252 }) 253 }) 254 Context("When cluster delete is failed", func() { 255 BeforeEach(func() { 256 server = ghttp.NewServer() 257 server.SetAllowUnhandledRequests(true) 258 server.AppendHandlers( 259 ghttp.CombineHandlers( 260 ghttp.VerifyRequest(http.MethodDelete, "/v1/clusters/test/workers/abc-123-def-ghi"), 261 ghttp.RespondWith(http.StatusInternalServerError, `Failed to delete worker`), 262 ), 263 ) 264 }) 265 266 It("should return error service key delete", func() { 267 target := ClusterTargetHeader{ 268 OrgID: "abc", 269 SpaceID: "def", 270 AccountID: "ghi", 271 } 272 err := newWorker(server.URL()).Delete("test", "abc-123-def-ghi", target) 273 Expect(err).To(HaveOccurred()) 274 }) 275 }) 276 }) 277 //Update 278 Describe("Update", func() { 279 Context("When update worker is successful", func() { 280 BeforeEach(func() { 281 server = ghttp.NewServer() 282 server.AppendHandlers( 283 ghttp.CombineHandlers( 284 ghttp.VerifyRequest(http.MethodPut, "/v1/clusters/test/workers/abc-123-def-ghi"), 285 ghttp.RespondWith(http.StatusCreated, `{}`), 286 ), 287 ) 288 }) 289 290 It("should return worker updated", func() { 291 target := ClusterTargetHeader{ 292 OrgID: "abc", 293 SpaceID: "def", 294 AccountID: "ghi", 295 } 296 params := WorkerUpdateParam{ 297 Action: "reload", 298 } 299 err := newWorker(server.URL()).Update("test", "abc-123-def-ghi", params, target) 300 Expect(err).NotTo(HaveOccurred()) 301 }) 302 }) 303 Context("When updating worker is unsuccessful", func() { 304 BeforeEach(func() { 305 server = ghttp.NewServer() 306 server.SetAllowUnhandledRequests(true) 307 server.AppendHandlers( 308 ghttp.CombineHandlers( 309 ghttp.VerifyRequest(http.MethodPut, "/v1/clusters/test/workers/abc-123-def-ghi"), 310 ghttp.RespondWith(http.StatusInternalServerError, `Failed to add worker to cluster`), 311 ), 312 ) 313 }) 314 315 It("should return error during updating worker", func() { 316 target := ClusterTargetHeader{ 317 OrgID: "abc", 318 SpaceID: "def", 319 AccountID: "ghi", 320 } 321 params := WorkerUpdateParam{ 322 Action: "reload", 323 } 324 err := newWorker(server.URL()).Update("test", "abc-123-def-ghi", params, target) 325 Expect(err).To(HaveOccurred()) 326 }) 327 }) 328 }) 329 }) 330 331 func newWorker(url string) Workers { 332 333 sess, err := session.New() 334 if err != nil { 335 log.Fatal(err) 336 } 337 conf := sess.Config.Copy() 338 conf.HTTPClient = bluemixHttp.NewHTTPClient(conf) 339 conf.Endpoint = &url 340 341 client := client.Client{ 342 Config: conf, 343 ServiceName: bluemix.MccpService, 344 } 345 return newWorkerAPI(&client) 346 }