github.com/IBM-Cloud/bluemix-go@v0.0.0-20240314082800-4e02a69b84b2/api/cis/cisv1/monitors_test.go (about)

     1  package cisv1
     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("Monitors", func() {
    19  	var server *ghttp.Server
    20  	AfterEach(func() {
    21  		server.Close()
    22  	})
    23  	Describe("Create", func() {
    24  		Context("When creation is successful", func() {
    25  			BeforeEach(func() {
    26  				server = ghttp.NewServer()
    27  				server.AppendHandlers(
    28  					ghttp.CombineHandlers(
    29  						ghttp.VerifyRequest(http.MethodPost, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/load_balancers/monitors"),
    30  						ghttp.RespondWith(http.StatusCreated, `
    31                             {
    32                                "result": {
    33                                  "description": "",
    34                                  "created_on": "2018-11-22T09:53:06.416054Z",
    35                                  "modified_on": "2018-11-22T09:53:06.416054Z",
    36                                  "id": "92859a0f6b4d3e55b953e0e29bb96338",
    37                                  "type": "http",
    38                                  "interval": 60,
    39                                  "retries": 2,
    40                                  "timeout": 5,
    41                                  "expected_body": "",
    42                                  "expected_codes": "200",
    43                                  "follow_redirects": true,
    44                                  "allow_insecure": false,
    45                                  "path": "/status",
    46                                  "header": {
    47                                    "Host": [
    48                                      "www.example.com"
    49                                    ],
    50                                    "X-App-Id": [
    51                                      "abc123"
    52                                    ]
    53                                  },
    54                                  "method": "GET"
    55                                },
    56                                "success": true,
    57                                "errors": [],
    58                                "messages": []
    59                              }
    60                          `),
    61  					),
    62  				)
    63  			})
    64  
    65  			It("should return monitor created", func() {
    66  				params := MonitorBody{
    67  					ExpCodes:        "200",
    68  					ExpBody:         "",
    69  					Path:            "/status",
    70  					MonType:         "http",
    71  					Method:          "GET",
    72  					Timeout:         5,
    73  					Retries:         2,
    74  					Interval:        60,
    75  					FollowRedirects: true,
    76  					AllowInsecure:   false,
    77  				}
    78  				target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
    79  				myMonitorPtr, err := newMonitor(server.URL()).CreateMonitor(target, params)
    80  				myMonitor := *myMonitorPtr
    81  				Expect(err).NotTo(HaveOccurred())
    82  				Expect(myMonitor).ShouldNot(BeNil())
    83  				Expect(myMonitor.Id).Should(Equal("92859a0f6b4d3e55b953e0e29bb96338"))
    84  			})
    85  		})
    86  		Context("When creation is unsuccessful", func() {
    87  			BeforeEach(func() {
    88  				server = ghttp.NewServer()
    89  				server.SetAllowUnhandledRequests(true)
    90  				server.AppendHandlers(
    91  					ghttp.CombineHandlers(
    92  						ghttp.VerifyRequest(http.MethodPost, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/load_balancers/monitors"),
    93  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to create Monitor`),
    94  					),
    95  				)
    96  			})
    97  			It("should return error during Monitor creation", func() {
    98  				params := MonitorBody{
    99  					ExpCodes:        "200",
   100  					ExpBody:         "",
   101  					Path:            "/status",
   102  					MonType:         "http",
   103  					Method:          "GET",
   104  					Timeout:         5,
   105  					Retries:         2,
   106  					Interval:        60,
   107  					FollowRedirects: true,
   108  					AllowInsecure:   false,
   109  				}
   110  				target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   111  				myMonitorPtr, err := newMonitor(server.URL()).CreateMonitor(target, params)
   112  				myMonitor := myMonitorPtr
   113  				Expect(err).To(HaveOccurred())
   114  				Expect(myMonitor).Should(BeNil())
   115  			})
   116  		})
   117  	})
   118  	//List
   119  	Describe("List", func() {
   120  		Context("When read of Monitors is successful", func() {
   121  			BeforeEach(func() {
   122  				server = ghttp.NewServer()
   123  				server.AppendHandlers(
   124  					ghttp.CombineHandlers(
   125  						ghttp.VerifyRequest(http.MethodGet, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/load_balancers/monitors"),
   126  						ghttp.RespondWith(http.StatusOK, `
   127                              {
   128                                "result": [
   129                                  {
   130                                    "description": "",
   131                                    "created_on": "2018-06-02T03:14:09.818402Z",
   132                                    "modified_on": "2018-11-22T08:54:25.126766Z",
   133                                    "id": "192c950172152639e21f549bc4a1cd6f",
   134                                    "type": "http",
   135                                    "interval": 60,
   136                                    "retries": 2,
   137                                    "timeout": 5,
   138                                    "expected_body": "",
   139                                    "expected_codes": "200",
   140                                    "follow_redirects": true,
   141                                    "allow_insecure": false,
   142                                    "path": "/status",
   143                                    "header": {},
   144                                    "method": "GET"
   145                                  }
   146                                ],
   147                                "success": true,
   148                                "errors": [],
   149                                "messages": []
   150                              }
   151                          `),
   152  					),
   153  				)
   154  			})
   155  
   156  			It("should return Monitor list", func() {
   157  				target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   158  				myMonitors, err := newMonitor(server.URL()).ListMonitors(target)
   159  				for _, Monitor := range myMonitors {
   160  					Expect(err).NotTo(HaveOccurred())
   161  					Expect(Monitor.Id).Should(Equal("192c950172152639e21f549bc4a1cd6f"))
   162  				}
   163  			})
   164  		})
   165  		Context("When read of Monitors is unsuccessful", func() {
   166  			BeforeEach(func() {
   167  				server = ghttp.NewServer()
   168  				server.SetAllowUnhandledRequests(true)
   169  				server.AppendHandlers(
   170  					ghttp.CombineHandlers(
   171  						ghttp.VerifyRequest(http.MethodGet, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/load_balancers/monitors"),
   172  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve Monitors`),
   173  					),
   174  				)
   175  			})
   176  
   177  			It("should return error when Monitor are retrieved", func() {
   178  				target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   179  				myMonitorPtr, err := newMonitor(server.URL()).ListMonitors(target)
   180  				myMonitor := myMonitorPtr
   181  				Expect(err).To(HaveOccurred())
   182  				Expect(myMonitor).Should(BeNil())
   183  			})
   184  		})
   185  	})
   186  	//Delete
   187  	Describe("Delete", func() {
   188  		Context("When delete of Monitor is successful", func() {
   189  			BeforeEach(func() {
   190  				server = ghttp.NewServer()
   191  				server.AppendHandlers(
   192  					ghttp.CombineHandlers(
   193  						ghttp.VerifyRequest(http.MethodDelete, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/load_balancers/monitors/92859a0f6b4d3e55b953e0e29bb96338"),
   194  						ghttp.RespondWith(http.StatusOK, `{                         
   195                          }`),
   196  					),
   197  				)
   198  			})
   199  
   200  			It("should delete Monitor", func() {
   201  				target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   202  				params := "92859a0f6b4d3e55b953e0e29bb96338"
   203  				err := newMonitor(server.URL()).DeleteMonitor(target, params)
   204  				Expect(err).NotTo(HaveOccurred())
   205  			})
   206  		})
   207  		Context("When Monitor delete has failed", func() {
   208  			BeforeEach(func() {
   209  				server = ghttp.NewServer()
   210  				server.SetAllowUnhandledRequests(true)
   211  				server.AppendHandlers(
   212  					ghttp.CombineHandlers(
   213  						ghttp.VerifyRequest(http.MethodDelete, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/load_balancers/monitors/92859a0f6b4d3e55b953e0e29bb96338"),
   214  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to delete service key`),
   215  					),
   216  				)
   217  			})
   218  
   219  			It("should return error zone delete", func() {
   220  				target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   221  				params := "92859a0f6b4d3e55b953e0e29bb96338"
   222  				err := newMonitor(server.URL()).DeleteMonitor(target, params)
   223  				Expect(err).To(HaveOccurred())
   224  			})
   225  		})
   226  	})
   227  	//Find
   228  	Describe("Get", func() {
   229  		Context("When read of Monitor is successful", func() {
   230  			BeforeEach(func() {
   231  				server = ghttp.NewServer()
   232  				server.AppendHandlers(
   233  					ghttp.CombineHandlers(
   234  						ghttp.VerifyRequest(http.MethodGet, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/load_balancers/monitors/92859a0f6b4d3e55b953e0e29bb96338"),
   235  						ghttp.RespondWith(http.StatusOK, `
   236                              {
   237                                "result": {
   238                                  "description": "",
   239                                  "created_on": "2018-06-02T03:14:09.818402Z",
   240                                  "modified_on": "2018-11-22T08:54:25.126766Z",
   241                                  "id": "192c950172152639e21f549bc4a1cd6f",
   242                                  "type": "http",
   243                                  "interval": 60,
   244                                  "retries": 2,
   245                                  "timeout": 5,
   246                                  "expected_body": "",
   247                                  "expected_codes": "200",
   248                                  "follow_redirects": true,
   249                                  "allow_insecure": false,
   250                                  "path": "/status",
   251                                  "header": {},
   252                                  "method": "GET"
   253                                },
   254                                "success": true,
   255                                "errors": [],
   256                                "messages": []
   257                              }
   258                          `),
   259  					),
   260  				)
   261  			})
   262  
   263  			It("should return Monitor", func() {
   264  				target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   265  				params := "92859a0f6b4d3e55b953e0e29bb96338"
   266  				myMonitorPtr, err := newMonitor(server.URL()).GetMonitor(target, params)
   267  				myMonitor := myMonitorPtr
   268  				Expect(err).NotTo(HaveOccurred())
   269  				Expect(myMonitor).ShouldNot(BeNil())
   270  				Expect(myMonitor.Id).Should(Equal("192c950172152639e21f549bc4a1cd6f"))
   271  			})
   272  		})
   273  		Context("When Monitor get has failed", func() {
   274  			BeforeEach(func() {
   275  				server = ghttp.NewServer()
   276  				server.SetAllowUnhandledRequests(true)
   277  				server.AppendHandlers(
   278  					ghttp.CombineHandlers(
   279  						ghttp.VerifyRequest(http.MethodGet, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/load_balancers/monitors/92859a0f6b4d3e55b953e0e29bb96338"),
   280  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve Monitor`),
   281  					),
   282  				)
   283  			})
   284  
   285  			It("should return error when Monitor is retrieved", func() {
   286  				target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a"
   287  				params := "92859a0f6b4d3e55b953e0e29bb96338"
   288  				myMonitorPtr, err := newMonitor(server.URL()).GetMonitor(target, params)
   289  				myMonitor := myMonitorPtr
   290  				Expect(err).To(HaveOccurred())
   291  				Expect(myMonitor).Should(BeNil())
   292  			})
   293  		})
   294  	})
   295  
   296  })
   297  
   298  func newMonitor(url string) Monitors {
   299  
   300  	sess, err := session.New()
   301  	if err != nil {
   302  		log.Fatal(err)
   303  	}
   304  	conf := sess.Config.Copy()
   305  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
   306  	conf.Endpoint = &url
   307  
   308  	client := client.Client{
   309  		Config:      conf,
   310  		ServiceName: bluemix.CisService,
   311  	}
   312  	return newMonitorAPI(&client)
   313  }