github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/icd/icdv4/cdb_test.go (about)

     1  package icdv4
     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/gomega/ghttp"
    13  
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("Deployments", func() {
    19  	var server *ghttp.Server
    20  	AfterEach(func() {
    21  		server.Close()
    22  	})
    23  	Describe("Get", func() {
    24  		Context("When get is successful", func() {
    25  			BeforeEach(func() {
    26  				server = ghttp.NewServer()
    27  				server.AppendHandlers(
    28  					ghttp.CombineHandlers(
    29  						ghttp.VerifyRequest(http.MethodGet, "/v4/ibm/deployments/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"),
    30  						ghttp.RespondWith(http.StatusOK, `
    31                             {
    32                                "deployment": {
    33                                  "id": "1f30581-54f8-41a4-8193-4a04cc022e9b-h",
    34                                  "name": "1f30581-54f8-41a4-8193-4a04cc022e9b-h",
    35                                  "type": "etcd",
    36                                  "version": "3.2.7",
    37                                  "admin_username": "admin"
    38                                }
    39                              }
    40                          `),
    41  					),
    42  				)
    43  			})
    44  
    45  			It("should return cdb", func() {
    46  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
    47  				cdb, err := newCdb(server.URL()).GetCdb(target1)
    48  				Expect(err).NotTo(HaveOccurred())
    49  				Expect(cdb).ShouldNot(BeNil())
    50  				Expect(cdb.Id).Should(Equal("1f30581-54f8-41a4-8193-4a04cc022e9b-h"))
    51  				Expect(cdb.Name).Should(Equal("1f30581-54f8-41a4-8193-4a04cc022e9b-h"))
    52  				Expect(cdb.Type).Should(Equal("etcd"))
    53  				Expect(cdb.Version).Should(Equal("3.2.7"))
    54  				Expect(cdb.AdminUser).Should(Equal("admin"))
    55  			})
    56  		})
    57  		Context("When get is unsuccessful", func() {
    58  			BeforeEach(func() {
    59  				server = ghttp.NewServer()
    60  				server.SetAllowUnhandledRequests(true)
    61  				server.AppendHandlers(
    62  					ghttp.CombineHandlers(
    63  						ghttp.VerifyRequest(http.MethodGet, "/v4/ibm/deployments/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"),
    64  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to get cdb`),
    65  					),
    66  				)
    67  			})
    68  
    69  			It("should return error during get cdb", func() {
    70  				target1 := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
    71  				cdb, err := newCdb(server.URL()).GetCdb(target1)
    72  				Expect(err).To(HaveOccurred())
    73  				Expect(cdb.Id).Should(Equal(""))
    74  			})
    75  		})
    76  	})
    77  })
    78  
    79  func newCdb(url string) Cdbs {
    80  
    81  	sess, err := session.New()
    82  	if err != nil {
    83  		log.Fatal(err)
    84  	}
    85  	conf := sess.Config.Copy()
    86  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
    87  	conf.Endpoint = &url
    88  
    89  	client := client.Client{
    90  		Config:      conf,
    91  		ServiceName: bluemix.ICDService,
    92  	}
    93  	return newCdbAPI(&client)
    94  }