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

     1  package containerv1
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  	"github.com/onsi/gomega/ghttp"
    10  
    11  	bluemix "github.com/IBM-Cloud/bluemix-go"
    12  	"github.com/IBM-Cloud/bluemix-go/client"
    13  	bluemixHttp "github.com/IBM-Cloud/bluemix-go/http"
    14  	"github.com/IBM-Cloud/bluemix-go/session"
    15  )
    16  
    17  var _ = Describe("AddOns", func() {
    18  	var server *ghttp.Server
    19  	AfterEach(func() {
    20  		server.Close()
    21  	})
    22  	//Configure
    23  	Describe("Configure", func() {
    24  		Context("When configuring addons is successful", func() {
    25  			BeforeEach(func() {
    26  				server = ghttp.NewServer()
    27  				server.AppendHandlers(
    28  					ghttp.CombineHandlers(
    29  						ghttp.VerifyRequest(http.MethodPatch, "/v1/clusters/testcluster/addons"),
    30  						ghttp.VerifyJSON(`{
    31  							"addons": [
    32  							  {
    33  								"deprecated": false,
    34  								"name": "istio",
    35  								"vlan_spanning_required": false
    36  							  }
    37  							],
    38  							"enable": false,
    39  							"update": false
    40  						  }`),
    41  						ghttp.RespondWith(http.StatusCreated, `{}`),
    42  					),
    43  				)
    44  			})
    45  
    46  			It("should configure addon to a cluster", func() {
    47  				target := ClusterTargetHeader{
    48  					OrgID:     "abc",
    49  					SpaceID:   "def",
    50  					AccountID: "ghi",
    51  					Region:    "eu-de",
    52  				}
    53  				clustername := "testcluster"
    54  				params := ConfigureAddOns{
    55  					AddonsList: []AddOn{},
    56  					Enable:     false,
    57  					Update:     false,
    58  				}
    59  				var addOn = AddOn{
    60  					Name: "istio",
    61  				}
    62  				params.AddonsList = append(params.AddonsList, addOn)
    63  				_, err := newAddOns(server.URL()).ConfigureAddons(clustername, &params, target)
    64  				Expect(err).NotTo(HaveOccurred())
    65  			})
    66  		})
    67  		Context("When configuring addons is unsuccessful", func() {
    68  			BeforeEach(func() {
    69  				server = ghttp.NewServer()
    70  				server.SetAllowUnhandledRequests(true)
    71  				server.AppendHandlers(
    72  					ghttp.CombineHandlers(
    73  						ghttp.VerifyRequest(http.MethodPatch, "/v1/clusters/testcluster/addons"),
    74  						ghttp.VerifyJSON(`{
    75  							"addons": [
    76  							  {
    77  								"deprecated": false,
    78  								"name": "istio",
    79  								"vlan_spanning_required": false
    80  							  }
    81  							],
    82  							"enable": false,
    83  							"update": false
    84  						  }`),
    85  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to configure addons`),
    86  					),
    87  				)
    88  			})
    89  
    90  			It("should return error during configuring addons", func() {
    91  
    92  				params := ConfigureAddOns{
    93  					AddonsList: []AddOn{},
    94  					Enable:     false,
    95  					Update:     false,
    96  				}
    97  				var addOn = AddOn{
    98  					Name: "istio",
    99  				}
   100  				params.AddonsList = append(params.AddonsList, addOn)
   101  				clustername := "testcluster"
   102  				target := ClusterTargetHeader{
   103  					OrgID:     "abc",
   104  					SpaceID:   "def",
   105  					AccountID: "ghi",
   106  					Region:    "eu-de",
   107  				}
   108  				_, err := newAddOns(server.URL()).ConfigureAddons(clustername, &params, target)
   109  				Expect(err).To(HaveOccurred())
   110  			})
   111  		})
   112  	})
   113  
   114  	//GetAlb
   115  	Describe("Get cluster addons", func() {
   116  		Context("When read of cluster addons is successful", func() {
   117  			BeforeEach(func() {
   118  				server = ghttp.NewServer()
   119  				server.AppendHandlers(
   120  					ghttp.CombineHandlers(
   121  						ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/testcluster/addons"),
   122  						ghttp.RespondWith(http.StatusOK, `[
   123  							{
   124  							  "name": "istio",
   125  							  "version": "1.6",
   126  							  "targetVersion": "1.6",
   127  							  "healthStatus": "Enabling",
   128  							  "allowed_upgrade_versions": [
   129  								"1.7"
   130  							  ]
   131  							}
   132  						  ]
   133  						  `),
   134  					),
   135  				)
   136  			})
   137  
   138  			It("should return addons", func() {
   139  				target := ClusterTargetHeader{
   140  					OrgID:     "abc",
   141  					SpaceID:   "def",
   142  					AccountID: "ghi",
   143  					Region:    "eu-de",
   144  				}
   145  				addons, err := newAddOns(server.URL()).GetAddons("testcluster", target)
   146  				Expect(addons).ShouldNot(BeNil())
   147  				Expect(err).NotTo(HaveOccurred())
   148  			})
   149  		})
   150  		Context("When read of cluster addon is unsuccessful", func() {
   151  			BeforeEach(func() {
   152  				server = ghttp.NewServer()
   153  				server.SetAllowUnhandledRequests(true)
   154  				server.AppendHandlers(
   155  					ghttp.CombineHandlers(
   156  						ghttp.VerifyRequest(http.MethodGet, "/v1/clusters/testcluster/addons"),
   157  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve addons.`),
   158  					),
   159  				)
   160  			})
   161  
   162  			It("should return error when addons are retrieved", func() {
   163  				target := ClusterTargetHeader{
   164  					OrgID:     "abc",
   165  					SpaceID:   "def",
   166  					AccountID: "ghi",
   167  					Region:    "eu-de",
   168  				}
   169  				_, err := newAddOns(server.URL()).GetAddons("testcluster", target)
   170  				Expect(err).To(HaveOccurred())
   171  			})
   172  		})
   173  	})
   174  
   175  })
   176  
   177  func newAddOns(url string) AddOns {
   178  
   179  	sess, err := session.New()
   180  	if err != nil {
   181  		log.Fatal(err)
   182  	}
   183  	conf := sess.Config.Copy()
   184  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
   185  	conf.Endpoint = &url
   186  
   187  	client := client.Client{
   188  		Config:      conf,
   189  		ServiceName: bluemix.MccpService,
   190  	}
   191  	return newAddOnsAPI(&client)
   192  }