github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/functions/functions_suite_test.go (about)

     1  package functions
     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  	"github.com/IBM-Cloud/bluemix-go/session"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	"github.com/onsi/gomega/ghttp"
    13  )
    14  
    15  var _ = Describe("Functions", func() {
    16  	var (
    17  		server *ghttp.Server
    18  	)
    19  
    20  	Describe("Remove()", func() {
    21  		Context("When namespace is deleted", func() {
    22  			BeforeEach(func() {
    23  				server = ghttp.NewServer()
    24  				server.AppendHandlers(
    25  					ghttp.CombineHandlers(
    26  						ghttp.VerifyRequest(http.MethodDelete, "/api/v1/namespaces/abc"),
    27  						ghttp.RespondWith(http.StatusNoContent, ""),
    28  					),
    29  				)
    30  			})
    31  
    32  			It("should return success", func() {
    33  				_, err := newTestNamespace(server.URL()).DeleteNamespace("/api/v1/namespaces/abc")
    34  
    35  				Expect(err).Should(Succeed())
    36  			})
    37  		})
    38  
    39  		Context("When namespace is not found", func() {
    40  			BeforeEach(func() {
    41  				server = ghttp.NewServer()
    42  				server.AppendHandlers(
    43  					ghttp.CombineHandlers(
    44  						ghttp.VerifyRequest(http.MethodDelete, "/api/v1/namespaces/abc"),
    45  						ghttp.RespondWith(http.StatusNotFound, `{
    46  							"StatusCode": 404,
    47  							"code": "not_found",
    48  							"message": "namespace abc is not found"
    49  						}`),
    50  					),
    51  				)
    52  			})
    53  
    54  			It("should return not found error", func() {
    55  				_, err := newTestNamespace(server.URL()).DeleteNamespace("abc")
    56  
    57  				Expect(err).Should(HaveOccurred())
    58  				Expect(err.Error()).Should(ContainSubstring("not_found"))
    59  			})
    60  		})
    61  	})
    62  
    63  })
    64  
    65  func newTestNamespace(url string) Functions {
    66  	sess, err := session.New()
    67  	if err != nil {
    68  		log.Fatal(err)
    69  	}
    70  	conf := sess.Config.Copy()
    71  	conf.Endpoint = &url
    72  	client := client.Client{
    73  		Config:      conf,
    74  		ServiceName: bluemix.FunctionsService,
    75  	}
    76  	return newFunctionsAPI(&client)
    77  }