github.com/IBM-Cloud/bluemix-go@v0.0.0-20241117121028-a3be206688b3/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  	// Enable secure by default
   119  	Describe("Enable Secure by Default", func() {
   120  		Context("When EnableSecureByDefault is successful", func() {
   121  			BeforeEach(func() {
   122  				server = ghttp.NewServer()
   123  				server.AppendHandlers(
   124  					ghttp.CombineHandlers(
   125  						ghttp.VerifyRequest(http.MethodPost, "/network/v2/secure-by-default/enable"),
   126  						ghttp.VerifyJSON(`{"cluster":"testCluster","disableOutboundTrafficProtection":true}`),
   127  						ghttp.RespondWith(http.StatusOK, ""),
   128  					),
   129  				)
   130  			})
   131  
   132  			It("should return with 200 OK", func() {
   133  				target := ClusterTargetHeader{}
   134  
   135  				err := newVPCs(server.URL()).EnableSecureByDefault("testCluster", true, target)
   136  				Expect(err).NotTo(HaveOccurred())
   137  			})
   138  		})
   139  		Context("When EnableSecureByDefault is unsuccessful", func() {
   140  			BeforeEach(func() {
   141  				server = ghttp.NewServer()
   142  				server.SetAllowUnhandledRequests(true)
   143  				server.AppendHandlers(
   144  					ghttp.CombineHandlers(
   145  						ghttp.VerifyRequest(http.MethodPost, "/network/v2/secure-by-default/enable"),
   146  						ghttp.VerifyJSON(`{"cluster":"testCluster"}`),
   147  						ghttp.RespondWith(http.StatusInternalServerError, ""),
   148  					),
   149  				)
   150  			})
   151  
   152  			It("should return with 500 Internal server error", func() {
   153  				target := ClusterTargetHeader{}
   154  				err := newVPCs(server.URL()).EnableSecureByDefault("testCluster", false, target)
   155  				Expect(err).To(HaveOccurred())
   156  			})
   157  		})
   158  	})
   159  
   160  })
   161  
   162  func newVPCs(url string) VPCs {
   163  	sess, err := session.New()
   164  	if err != nil {
   165  		log.Fatal(err)
   166  	}
   167  	conf := sess.Config.Copy()
   168  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
   169  	conf.Endpoint = &url
   170  
   171  	client := client.Client{
   172  		Config:      conf,
   173  		ServiceName: bluemix.VpcContainerService,
   174  	}
   175  	return newVPCsAPI(&client)
   176  }