github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/container/containerv2/subnets_test.go (about)

     1  package containerv2
     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("Subnets", func() {
    19  	var server *ghttp.Server
    20  	AfterEach(func() {
    21  		server.Close()
    22  	})
    23  
    24  	//ListSubnets
    25  	Describe("List", func() {
    26  		Context("When List subnets is successful", func() {
    27  			BeforeEach(func() {
    28  				server = ghttp.NewServer()
    29  				server.AppendHandlers(
    30  					ghttp.CombineHandlers(
    31  						ghttp.VerifyRequest(http.MethodGet, "/v2/vpc/getSubnets"),
    32  						ghttp.RespondWith(http.StatusCreated, `[
    33  							{
    34  							  "availableIPv4AddressCount": 0,
    35  							  "id": "string",
    36  							  "ipv4CIDRBlock": "string",
    37  							  "name": "string",
    38  							  "publicGatewayID": "string",
    39  							  "publicGatewayName": "string",
    40  							  "vpcID": "string",
    41  							  "vpcName": "string",
    42  							  "zone": "string"
    43  							}
    44  						  ]`),
    45  					),
    46  				)
    47  			})
    48  
    49  			It("should list subnets in a cluster", func() {
    50  				target := ClusterTargetHeader{}
    51  
    52  				_, err := newSubnets(server.URL()).ListSubnets("aaa", "bbb", target)
    53  				Expect(err).NotTo(HaveOccurred())
    54  			})
    55  		})
    56  		Context("When list subnets is unsuccessful", func() {
    57  			BeforeEach(func() {
    58  				server = ghttp.NewServer()
    59  				server.SetAllowUnhandledRequests(true)
    60  				server.AppendHandlers(
    61  					ghttp.CombineHandlers(
    62  						ghttp.VerifyRequest(http.MethodGet, "/v2/vpc/getSubnets"),
    63  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to list subnets`),
    64  					),
    65  				)
    66  			})
    67  
    68  			It("should return error during get subnets", func() {
    69  				target := ClusterTargetHeader{}
    70  				_, err := newSubnets(server.URL()).ListSubnets("aaa", "bbb", target)
    71  				Expect(err).To(HaveOccurred())
    72  			})
    73  		})
    74  	})
    75  
    76  })
    77  
    78  func newSubnets(url string) Subnets {
    79  	sess, err := session.New()
    80  	if err != nil {
    81  		log.Fatal(err)
    82  	}
    83  	conf := sess.Config.Copy()
    84  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
    85  	conf.Endpoint = &url
    86  
    87  	client := client.Client{
    88  		Config:      conf,
    89  		ServiceName: bluemix.VpcContainerService,
    90  	}
    91  	return newSubnetsAPI(&client)
    92  }