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

     1  package containerv2
     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("Ingress Secrets", func() {
    18  	var server *ghttp.Server
    19  	AfterEach(func() {
    20  		server.Close()
    21  	})
    22  
    23  	//Enable
    24  	Describe("Create", func() {
    25  		Context("When creating ingress secret successful", func() {
    26  			BeforeEach(func() {
    27  				server = ghttp.NewServer()
    28  				server.AppendHandlers(
    29  					ghttp.CombineHandlers(
    30  						ghttp.VerifyRequest(http.MethodPost, "/ingress/v2/secret/createSecret"),
    31  						ghttp.VerifyJSON(`{"cluster":"bugi52rf0rtfgadjfso0","name":"testabc2","namespace":"","crn":"crn:v1:bluemix:public:cloudcerts:us-south:a/883079c85357a1f3f85d968780e56518:b65b5b7f-e904-4d2b-bd87-f0ccd57e76ba:certificate:333d8673f4d03c148ff81192b9edaafc","persistence":true,"type":"","add":null}`),
    32  						ghttp.RespondWith(http.StatusCreated, `{}`),
    33  					),
    34  				)
    35  			})
    36  
    37  			It("should create Ingress Secret in a cluster", func() {
    38  
    39  				params := SecretCreateConfig{
    40  					Cluster:     "bugi52rf0rtfgadjfso0",
    41  					Name:        "testabc2",
    42  					CRN:         "crn:v1:bluemix:public:cloudcerts:us-south:a/883079c85357a1f3f85d968780e56518:b65b5b7f-e904-4d2b-bd87-f0ccd57e76ba:certificate:333d8673f4d03c148ff81192b9edaafc",
    43  					Persistence: true,
    44  				}
    45  				_, err := newIngresses(server.URL()).CreateIngressSecret(params)
    46  				Expect(err).NotTo(HaveOccurred())
    47  			})
    48  		})
    49  		Context("When add ingress secret field successful", func() {
    50  			BeforeEach(func() {
    51  				server = ghttp.NewServer()
    52  				server.AppendHandlers(
    53  					ghttp.CombineHandlers(
    54  						ghttp.VerifyRequest(http.MethodPost, "/ingress/v2/secret/addField"),
    55  						ghttp.VerifyJSON(`{"cluster":"bugi52rf0rtfgadjfso0","name":"testabc2","namespace":"default","crn":"","add":[{"name":"","crn":"crn:v1:bluemix:public:cloudcerts:us-south:a/883079c85357a1f3f85d968780e56518:b65b5b7f-e904-4d2b-bd87-f0ccd57e76ba:certificate:333d8673f4d03c148ff81192b9edaafc","append_prefix": false}],"remove":null}`),
    56  						ghttp.RespondWith(http.StatusCreated, `{}`),
    57  					),
    58  				)
    59  			})
    60  
    61  			It("should create Ingress Secret in a cluster", func() {
    62  
    63  				params := SecretUpdateConfig{
    64  					Cluster:   "bugi52rf0rtfgadjfso0",
    65  					Name:      "testabc2",
    66  					Namespace: "default",
    67  					FieldsToAdd: []FieldAdd{
    68  						{
    69  							CRN: "crn:v1:bluemix:public:cloudcerts:us-south:a/883079c85357a1f3f85d968780e56518:b65b5b7f-e904-4d2b-bd87-f0ccd57e76ba:certificate:333d8673f4d03c148ff81192b9edaafc",
    70  						},
    71  					},
    72  				}
    73  				_, err := newIngresses(server.URL()).AddIngressSecretField(params)
    74  				Expect(err).NotTo(HaveOccurred())
    75  			})
    76  		})
    77  		Context("When creating is unsuccessful", func() {
    78  			BeforeEach(func() {
    79  				server = ghttp.NewServer()
    80  				server.SetAllowUnhandledRequests(true)
    81  				server.AppendHandlers(
    82  					ghttp.CombineHandlers(
    83  						ghttp.VerifyRequest(http.MethodPost, "/ingress/v2/secret/createSecret"),
    84  						ghttp.VerifyJSON(`{"cluster":"bugi52rf0rtfgadjfso0","name":"testabc2","namespace":"","crn":"crn:v1:bluemix:public:cloudcerts:us-south:a/883079c85357a1f3f85d968780e56518:b65b5b7f-e904-4d2b-bd87-f0ccd57e76ba:certificate:333d8673f4d03c148ff81192b9edaafc","persistence":true,"type":"","add":null}`),
    85  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to enable ingress`),
    86  					),
    87  				)
    88  			})
    89  
    90  			It("should return error during creating ingress secret", func() {
    91  				params := SecretCreateConfig{
    92  					Cluster:     "bugi52rf0rtfgadjfso0",
    93  					Name:        "testabc2",
    94  					CRN:         "crn:v1:bluemix:public:cloudcerts:us-south:a/883079c85357a1f3f85d968780e56518:b65b5b7f-e904-4d2b-bd87-f0ccd57e76ba:certificate:333d8673f4d03c148ff81192b9edaafc",
    95  					Persistence: true,
    96  				}
    97  
    98  				_, err := newIngresses(server.URL()).CreateIngressSecret(params)
    99  				Expect(err).To(HaveOccurred())
   100  			})
   101  		})
   102  	})
   103  
   104  	//Disable
   105  	Describe("Destroy", func() {
   106  		Context("When deleting ingress secret successful", func() {
   107  			BeforeEach(func() {
   108  				server = ghttp.NewServer()
   109  				server.AppendHandlers(
   110  					ghttp.CombineHandlers(
   111  						ghttp.VerifyRequest(http.MethodPost, "/ingress/v2/secret/deleteSecret"),
   112  						ghttp.VerifyJSON(`{"cluster":"bugi52rf0rtfgadjfso0","name":"testabc2","namespace":"default"}`),
   113  						ghttp.RespondWith(http.StatusCreated, `{}`),
   114  					),
   115  				)
   116  			})
   117  
   118  			It("should destroy Ingress Secret in a cluster", func() {
   119  
   120  				params := SecretDeleteConfig{
   121  					Cluster:   "bugi52rf0rtfgadjfso0",
   122  					Name:      "testabc2",
   123  					Namespace: "default",
   124  				}
   125  				err := newIngresses(server.URL()).DeleteIngressSecret(params)
   126  				Expect(err).NotTo(HaveOccurred())
   127  			})
   128  		})
   129  		Context("When Destroying is unsuccessful", func() {
   130  			BeforeEach(func() {
   131  				server = ghttp.NewServer()
   132  				server.SetAllowUnhandledRequests(true)
   133  				server.AppendHandlers(
   134  					ghttp.CombineHandlers(
   135  						ghttp.VerifyRequest(http.MethodPost, "/ingress/v2/secret/deleteSecret"),
   136  						ghttp.VerifyJSON(`{"cluster":"bugi52rf0rtfgadjfso0","name":"testabc2","namespace":"default"}`),
   137  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to disable ingress`),
   138  					),
   139  				)
   140  			})
   141  
   142  			It("should return error during destroying ingress", func() {
   143  				params := SecretDeleteConfig{
   144  					Cluster:   "bugi52rf0rtfgadjfso0",
   145  					Name:      "testabc2",
   146  					Namespace: "default",
   147  				}
   148  
   149  				err := newIngresses(server.URL()).DeleteIngressSecret(params)
   150  				Expect(err).To(HaveOccurred())
   151  			})
   152  		})
   153  	})
   154  
   155  	//GetIngress Secrets
   156  	Describe("Get", func() {
   157  		Context("When Get Ingress Secret is successful", func() {
   158  			BeforeEach(func() {
   159  				server = ghttp.NewServer()
   160  				server.AppendHandlers(
   161  					ghttp.CombineHandlers(
   162  						ghttp.VerifyRequest(http.MethodGet, "/ingress/v2/secret/getSecret"),
   163  						ghttp.RespondWith(http.StatusCreated, `{"cluster":"bugi52rf0rtfgadjfso0","name":"testabc2","namespace":"default","domain":"*.mytestclustercb8f-dce1dcf4a47f9ff42332256e6c4eb998-0000.us-south.containers.appdomain.cloud","crn":"crn:v1:bluemix:public:cloudcerts:us-south:a/883079c85357a1f3f85d968780e56518:b65b5b7f-e904-4d2b-bd87-f0ccd57e76ba:certificate:333d8673f4d03c148ff81192b9edaafc","expiresOn":"2021-01-27T00:18:56+0000","status":"created","userManaged":true,"persistence":true}`),
   164  					),
   165  				)
   166  			})
   167  
   168  			It("should get Ingress Secret in a cluster", func() {
   169  
   170  				_, err := newIngresses(server.URL()).GetIngressSecret("bugi52rf0rtfgadjfso0", "testabc2", "default")
   171  				Expect(err).NotTo(HaveOccurred())
   172  			})
   173  		})
   174  		Context("When get ingress secret unsuccessful", func() {
   175  			BeforeEach(func() {
   176  				server = ghttp.NewServer()
   177  				server.SetAllowUnhandledRequests(true)
   178  				server.AppendHandlers(
   179  					ghttp.CombineHandlers(
   180  						ghttp.VerifyRequest(http.MethodGet, "/ingress/v2/secret/getSecret"),
   181  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to get ingress`),
   182  					),
   183  				)
   184  			})
   185  
   186  			It("should return error during get ingress", func() {
   187  
   188  				_, err := newIngresses(server.URL()).GetIngressSecret("bugi52rf0rtfgadjfso0", "testabc2", "default")
   189  				Expect(err).To(HaveOccurred())
   190  			})
   191  		})
   192  	})
   193  
   194  	//Register
   195  	Describe("Register Instance", func() {
   196  		Context("When registering ingress instance successful", func() {
   197  			BeforeEach(func() {
   198  				server = ghttp.NewServer()
   199  				server.AppendHandlers(
   200  					ghttp.CombineHandlers(
   201  						ghttp.VerifyRequest(http.MethodPost, "/ingress/v2/secret/registerInstance"),
   202  						ghttp.VerifyJSON(`{"cluster":"bugi52rf0rtfgadjfso0","crn":"crn:v1:bluemix:public:secrets-manager:us-south:a/883079c85357a1f3f85d968780e56518:b65b5b7f-e904-4d2b-bd87-f0ccd57e76ba::","isDefault":true,"secretGroupID":"b65b5b7f-e904-4d2b-bd87-f0ccd57e76ba"}`),
   203  						ghttp.RespondWith(http.StatusCreated, `{}`),
   204  					),
   205  				)
   206  			})
   207  
   208  			It("should register an Ingress instance with a cluster", func() {
   209  
   210  				params := InstanceRegisterConfig{
   211  					Cluster:       "bugi52rf0rtfgadjfso0",
   212  					IsDefault:     true,
   213  					CRN:           "crn:v1:bluemix:public:secrets-manager:us-south:a/883079c85357a1f3f85d968780e56518:b65b5b7f-e904-4d2b-bd87-f0ccd57e76ba::",
   214  					SecretGroupID: "b65b5b7f-e904-4d2b-bd87-f0ccd57e76ba",
   215  				}
   216  				_, err := newIngresses(server.URL()).RegisterIngressInstance(params)
   217  				Expect(err).NotTo(HaveOccurred())
   218  			})
   219  		})
   220  		Context("When registering is unsuccessful", func() {
   221  			BeforeEach(func() {
   222  				server = ghttp.NewServer()
   223  				server.SetAllowUnhandledRequests(true)
   224  				server.AppendHandlers(
   225  					ghttp.CombineHandlers(
   226  						ghttp.VerifyRequest(http.MethodPost, "/ingress/v2/secret/registerInstance"),
   227  						ghttp.VerifyJSON(`{"cluster":"bugi52rf0rtfgadjfso0","crn":"crn:v1:bluemix:public:secrets-manager:us-south:a/883079c85357a1f3f85d968780e56518:b65b5b7f-e904-4d2b-bd87-f0ccd57e76ba::","isDefault":true,"secretGroupID":"b65b5b7f-e904-4d2b-bd87-f0ccd57e76ba"}`),
   228  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to enable ingress`),
   229  					),
   230  				)
   231  			})
   232  
   233  			It("should return error during registering ingress instance", func() {
   234  				params := InstanceRegisterConfig{
   235  					Cluster:       "bugi52rf0rtfgadjfso0",
   236  					IsDefault:     true,
   237  					CRN:           "crn:v1:bluemix:public:secrets-manager:us-south:a/883079c85357a1f3f85d968780e56518:b65b5b7f-e904-4d2b-bd87-f0ccd57e76ba::",
   238  					SecretGroupID: "b65b5b7f-e904-4d2b-bd87-f0ccd57e76ba",
   239  				}
   240  				_, err := newIngresses(server.URL()).RegisterIngressInstance(params)
   241  				Expect(err).To(HaveOccurred())
   242  			})
   243  		})
   244  	})
   245  
   246  	//Delete Ingress Instance
   247  	Describe("Delete Instance", func() {
   248  		Context("When deleting ingress instance successful", func() {
   249  			BeforeEach(func() {
   250  				server = ghttp.NewServer()
   251  				server.AppendHandlers(
   252  					ghttp.CombineHandlers(
   253  						ghttp.VerifyRequest(http.MethodPost, "/ingress/v2/secret/unregisterInstance"),
   254  						ghttp.VerifyJSON(`{"cluster":"bugi52rf0rtfgadjfso0","name":"testabc2"}`),
   255  						ghttp.RespondWith(http.StatusOK, `{}`),
   256  					),
   257  				)
   258  			})
   259  
   260  			It("should remove Ingress instance from a cluster", func() {
   261  
   262  				params := InstanceDeleteConfig{
   263  					Cluster: "bugi52rf0rtfgadjfso0",
   264  					Name:    "testabc2",
   265  				}
   266  				err := newIngresses(server.URL()).DeleteIngressInstance(params)
   267  				Expect(err).NotTo(HaveOccurred())
   268  			})
   269  		})
   270  		Context("When deleting is unsuccessful", func() {
   271  			BeforeEach(func() {
   272  				server = ghttp.NewServer()
   273  				server.SetAllowUnhandledRequests(true)
   274  				server.AppendHandlers(
   275  					ghttp.CombineHandlers(
   276  						ghttp.VerifyRequest(http.MethodPost, "/ingress/v2/secret/unregisterInstance"),
   277  						ghttp.VerifyJSON(`{"cluster":"bugi52rf0rtfgadjfso0","name":"testabc2"}`),
   278  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to unregister instance`),
   279  					),
   280  				)
   281  			})
   282  
   283  			It("should return error during destroying ingress", func() {
   284  				params := InstanceDeleteConfig{
   285  					Cluster: "bugi52rf0rtfgadjfso0",
   286  					Name:    "testabc2",
   287  				}
   288  				err := newIngresses(server.URL()).DeleteIngressInstance(params)
   289  				Expect(err).To(HaveOccurred())
   290  			})
   291  		})
   292  	})
   293  
   294  	//GetIngress Instance
   295  	Describe("Get Instance", func() {
   296  		Context("When Get Ingress Instance is successful", func() {
   297  			BeforeEach(func() {
   298  				server = ghttp.NewServer()
   299  				server.AppendHandlers(
   300  					ghttp.CombineHandlers(
   301  						ghttp.VerifyRequest(http.MethodGet, "/ingress/v2/secret/getInstance"),
   302  						ghttp.RespondWith(http.StatusCreated, `{"cluster":"bugi52rf0rtfgadjfso0","name":"testabc2","namespace":"default","domain":"*.mytestclustercb8f-dce1dcf4a47f9ff42332256e6c4eb998-0000.us-south.containers.appdomain.cloud","crn":"crn:v1:bluemix:public:cloudcerts:us-south:a/883079c85357a1f3f85d968780e56518:b65b5b7f-e904-4d2b-bd87-f0ccd57e76ba:certificate:333d8673f4d03c148ff81192b9edaafc","expiresOn":"2021-01-27T00:18:56+0000","status":"created","userManaged":true,"persistence":true}`),
   303  					),
   304  				)
   305  			})
   306  
   307  			It("should get Ingress instance for a cluster", func() {
   308  
   309  				_, err := newIngresses(server.URL()).GetIngressInstance("bugi52rf0rtfgadjfso0", "testabc2")
   310  				Expect(err).NotTo(HaveOccurred())
   311  			})
   312  		})
   313  		Context("When get ingress instance unsuccessful", func() {
   314  			BeforeEach(func() {
   315  				server = ghttp.NewServer()
   316  				server.SetAllowUnhandledRequests(true)
   317  				server.AppendHandlers(
   318  					ghttp.CombineHandlers(
   319  						ghttp.VerifyRequest(http.MethodGet, "/ingress/v2/secret/getInstance"),
   320  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to get ingress instance`),
   321  					),
   322  				)
   323  			})
   324  
   325  			It("should return error during get ingress", func() {
   326  
   327  				_, err := newIngresses(server.URL()).GetIngressInstance("bugi52rf0rtfgadjfso0", "testabc2")
   328  				Expect(err).To(HaveOccurred())
   329  			})
   330  		})
   331  	})
   332  
   333  	//GetIngress Instance List
   334  	Describe("List Instances", func() {
   335  		Context("When List Ingress Instance is successful", func() {
   336  			BeforeEach(func() {
   337  				server = ghttp.NewServer()
   338  				server.AppendHandlers(
   339  					ghttp.CombineHandlers(
   340  						ghttp.VerifyRequest(http.MethodGet, "/ingress/v2/secret/getInstances"),
   341  						ghttp.RespondWith(http.StatusCreated, `[
   342  							{
   343  								"cluster": "bugi52rf0rtfgadjfso0",
   344  								"name": "kube-bugi52rf0rtfgadjfso0",
   345  								"crn": "crn:v1:bluemix:public:secrets-manager:us-south:a/f8ea34ae7f494076a9f5ad6a763b91f0:c19eaa85-328e-4ee9-93b6-a6d118097e59::",
   346  								"secretGroupID": "",
   347  								"secretGroupName": "",
   348  								"callbackChannel": "",
   349  								"userManaged": false,
   350  								"isDefault": true,
   351  								"type": "secrets-manager",
   352  								"status": "created"
   353  							}
   354  						]`),
   355  					),
   356  				)
   357  			})
   358  
   359  			It("should get Ingress instance list for a cluster", func() {
   360  
   361  				_, err := newIngresses(server.URL()).GetIngressInstanceList("bugi52rf0rtfgadjfso0", false)
   362  				Expect(err).NotTo(HaveOccurred())
   363  			})
   364  		})
   365  		Context("When get ingress instance list unsuccessful", func() {
   366  			BeforeEach(func() {
   367  				server = ghttp.NewServer()
   368  				server.SetAllowUnhandledRequests(true)
   369  				server.AppendHandlers(
   370  					ghttp.CombineHandlers(
   371  						ghttp.VerifyRequest(http.MethodGet, "/ingress/v2/secret/getInstances"),
   372  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to list ingress instances`),
   373  					),
   374  				)
   375  			})
   376  
   377  			It("should return error during get ingress", func() {
   378  
   379  				_, err := newIngresses(server.URL()).GetIngressInstanceList("bugi52rf0rtfgadjfso0", false)
   380  				Expect(err).To(HaveOccurred())
   381  			})
   382  		})
   383  	})
   384  
   385  })
   386  
   387  func newIngresses(url string) Ingress {
   388  
   389  	sess, err := session.New()
   390  	if err != nil {
   391  		log.Fatal(err)
   392  	}
   393  	conf := sess.Config.Copy()
   394  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
   395  	conf.Endpoint = &url
   396  
   397  	client := client.Client{
   398  		Config:      conf,
   399  		ServiceName: bluemix.VpcContainerService,
   400  	}
   401  	return newIngressAPI(&client)
   402  }