github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/container/containerv2/vpcs_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("VPCs", func() {
    19  	var server *ghttp.Server
    20  	AfterEach(func() {
    21  		server.Close()
    22  	})
    23  
    24  	// ListVPCs
    25  	Describe("List", func() {
    26  		Context("When List VPCs is successful", func() {
    27  			BeforeEach(func() {
    28  				server = ghttp.NewServer()
    29  				server.AppendHandlers(
    30  					ghttp.CombineHandlers(
    31  						ghttp.VerifyRequest(http.MethodGet, "/v2/vpc/getVPCs"),
    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 VPCs in a cluster", func() {
    50  				target := ClusterTargetHeader{}
    51  
    52  				_, err := newVPCs(server.URL()).ListVPCs(target)
    53  				Expect(err).NotTo(HaveOccurred())
    54  			})
    55  		})
    56  		Context("When list VPCs 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/getVPCs"),
    63  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to list VPCs`),
    64  					),
    65  				)
    66  			})
    67  
    68  			It("should return error during get VPCs", func() {
    69  				target := ClusterTargetHeader{}
    70  				_, err := newVPCs(server.URL()).ListVPCs(target)
    71  				Expect(err).To(HaveOccurred())
    72  			})
    73  		})
    74  	})
    75  
    76  	// SetOutboundTrafficProtection
    77  	Describe("SetOutboundTrafficProtection", func() {
    78  		Context("When SetOutboundTrafficProtection is successful", func() {
    79  			BeforeEach(func() {
    80  				server = ghttp.NewServer()
    81  				server.AppendHandlers(
    82  					ghttp.CombineHandlers(
    83  						ghttp.VerifyRequest(http.MethodPost, "/network/v2/outbound-traffic-protection"),
    84  						ghttp.VerifyJSON(`{"cluster":"testCluster","operation":"enable-outbound-protection"}`),
    85  						ghttp.RespondWith(http.StatusOK, ""),
    86  					),
    87  				)
    88  			})
    89  
    90  			It("should return with 200 OK", func() {
    91  				target := ClusterTargetHeader{}
    92  
    93  				err := newVPCs(server.URL()).SetOutboundTrafficProtection("testCluster", true, target)
    94  				Expect(err).NotTo(HaveOccurred())
    95  			})
    96  		})
    97  		Context("When SetOutboundTrafficProtection is unsuccessful", func() {
    98  			BeforeEach(func() {
    99  				server = ghttp.NewServer()
   100  				server.SetAllowUnhandledRequests(true)
   101  				server.AppendHandlers(
   102  					ghttp.CombineHandlers(
   103  						ghttp.VerifyRequest(http.MethodPost, "/network/v2/outbound-traffic-protection"),
   104  						ghttp.VerifyJSON(`{"cluster":"testCluster","operation":"disable-outbound-protection"}`),
   105  						ghttp.RespondWith(http.StatusInternalServerError, ""),
   106  					),
   107  				)
   108  			})
   109  
   110  			It("should return with 500 Internal server error", func() {
   111  				target := ClusterTargetHeader{}
   112  				err := newVPCs(server.URL()).SetOutboundTrafficProtection("testCluster", false, target)
   113  				Expect(err).To(HaveOccurred())
   114  			})
   115  		})
   116  	})
   117  
   118  })
   119  
   120  func newVPCs(url string) VPCs {
   121  	sess, err := session.New()
   122  	if err != nil {
   123  		log.Fatal(err)
   124  	}
   125  	conf := sess.Config.Copy()
   126  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
   127  	conf.Endpoint = &url
   128  
   129  	client := client.Client{
   130  		Config:      conf,
   131  		ServiceName: bluemix.VpcContainerService,
   132  	}
   133  	return newVPCsAPI(&client)
   134  }