github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/container/containerv2/ob_monitoring_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("Monitoring", func() { 19 var server *ghttp.Server 20 AfterEach(func() { 21 server.Close() 22 }) 23 24 //ListAllMonitors 25 Describe("ListAllMonitors", func() { 26 Context("When read of monitors is successful", func() { 27 BeforeEach(func() { 28 server = ghttp.NewServer() 29 server.AppendHandlers( 30 ghttp.CombineHandlers( 31 ghttp.VerifyRequest(http.MethodGet, ContainSubstring("v2/observe/monitoring/getConfigs")), 32 ghttp.RespondWith(http.StatusOK, `[{ 33 "AgentKey": "", 34 "AgentNamespace": "", 35 "CRN": "crn:v1:bluemix:public:sysdig-monitor:us-south:a/fcdb764102154c7ea8e1b79d3a64afe0:ec4f0886-edc4-409e-8720-574035538f91::", 36 "DaemonsetName": "", 37 "DiscoveredAgent": true, 38 "InstanceID": "ec4f0886-edc4-409e-8720-574035538f91", 39 "InstanceName": "ns", 40 "Namespace": "", 41 "PrivateEndpoint": true 42 }]`), 43 ), 44 ) 45 }) 46 47 It("should return sysdig monitor list", func() { 48 target := MonitoringTargetHeader{} 49 myMonitor, err := newMonitoring(server.URL()).ListAllMonitors("clusterName", target) 50 Expect(myMonitor).ShouldNot(BeNil()) 51 Expect(err).NotTo(HaveOccurred()) 52 Expect(len(myMonitor)).Should(Equal(1)) 53 Expect(myMonitor[0].InstanceID).Should(Equal("ec4f0886-edc4-409e-8720-574035538f91")) 54 Expect(myMonitor[0].CRN).Should(Equal("crn:v1:bluemix:public:sysdig-monitor:us-south:a/fcdb764102154c7ea8e1b79d3a64afe0:ec4f0886-edc4-409e-8720-574035538f91::")) 55 Expect(myMonitor[0].PrivateEndpoint).Should(Equal(true)) 56 }) 57 }) 58 59 Context("When read of monitors is unsuccessful", func() { 60 BeforeEach(func() { 61 62 server = ghttp.NewServer() 63 server.SetAllowUnhandledRequests(true) 64 server.AppendHandlers( 65 ghttp.CombineHandlers( 66 ghttp.VerifyRequest(http.MethodGet, ContainSubstring("/v2/")), 67 ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve monitors`), 68 ), 69 ) 70 }) 71 72 It("should return error when monitors are retrieved", func() { 73 target := MonitoringTargetHeader{} 74 myMonitor, err := newMonitoring(server.URL()).ListAllMonitors("DragonBoat-cluster", target) 75 Expect(err).To(HaveOccurred()) 76 Expect(myMonitor).Should(BeNil()) 77 }) 78 }) 79 }) 80 81 //Create 82 Describe("CreateMonitorConfig", func() { 83 Context("When creation is successful", func() { 84 BeforeEach(func() { 85 server = ghttp.NewServer() 86 server.AppendHandlers( 87 ghttp.CombineHandlers( 88 ghttp.VerifyRequest(http.MethodPost, "/v2/observe/monitoring/createConfig"), 89 ghttp.VerifyJSON(`{"cluster": "DragonBoat-cluster", "instance": "ec4f0886-edc4-409e-8720-574035538f91"}`), 90 ghttp.RespondWith(http.StatusCreated, `{ 91 "instanceId": "ec4f0886-edc4-409e-8720-574035538f91" 92 }`), 93 ), 94 ) 95 }) 96 97 It("should return monitor created", func() { 98 params := MonitoringCreateRequest{ 99 Cluster: "DragonBoat-cluster", SysidigInstance: "ec4f0886-edc4-409e-8720-574035538f91", 100 } 101 target := MonitoringTargetHeader{} 102 myMonitor, err := newMonitoring(server.URL()).CreateMonitoringConfig(params, target) 103 Expect(err).NotTo(HaveOccurred()) 104 Expect(myMonitor).ShouldNot(BeNil()) 105 Expect(myMonitor.InstanceID).Should(Equal("ec4f0886-edc4-409e-8720-574035538f91")) 106 }) 107 }) 108 Context("When creation is unsuccessful", func() { 109 BeforeEach(func() { 110 server = ghttp.NewServer() 111 server.SetAllowUnhandledRequests(true) 112 server.AppendHandlers( 113 ghttp.CombineHandlers( 114 ghttp.VerifyRequest(http.MethodPost, "/v2/observe/monitoring/createConfig"), 115 ghttp.VerifyJSON(`{"cluster": "DragonBoat-cluster", "instance": "ec4f0886-edc4-409e-8720-574035538f91"}`), 116 ghttp.RespondWith(http.StatusInternalServerError, `Failed to create sysdig monitor`), 117 ), 118 ) 119 }) 120 It("should return error during monitor creation", func() { 121 122 params := MonitoringCreateRequest{ 123 Cluster: "DragonBoat-cluster", SysidigInstance: "ec4f0886-edc4-409e-8720-574035538f91", 124 } 125 target := MonitoringTargetHeader{} 126 _, err := newMonitoring(server.URL()).CreateMonitoringConfig(params, target) 127 Expect(err).To(HaveOccurred()) 128 //Expect(myMonitor.InstanceID).Should(Equal("ec4f0886-edc4-409e-8720-574035538f91")) 129 }) 130 }) 131 }) 132 }) 133 134 func newMonitoring(url string) Monitoring { 135 136 sess, err := session.New() 137 if err != nil { 138 log.Fatal(err) 139 } 140 conf := sess.Config.Copy() 141 conf.HTTPClient = bluemixHttp.NewHTTPClient(conf) 142 conf.Endpoint = &url 143 144 client := client.Client{ 145 Config: conf, 146 ServiceName: bluemix.VpcContainerService, 147 } 148 return newMonitoringAPI(&client) 149 }