github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/container/containerv2/ob_logging_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("Logging", func() {
    19  	var server *ghttp.Server
    20  	AfterEach(func() {
    21  		server.Close()
    22  	})
    23  
    24  	//ListLoggingInstances
    25  	Describe("ListLoggingInstances", func() {
    26  		Context("When read of logging instances is successful", func() {
    27  			BeforeEach(func() {
    28  				server = ghttp.NewServer()
    29  				server.AppendHandlers(
    30  					ghttp.CombineHandlers(
    31  						ghttp.VerifyRequest(http.MethodGet, ContainSubstring("/v2/observe/logging/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 loggin isnatnces list", func() {
    48  				target := LoggingTargetHeader{}
    49  				myLogging, err := newLogging(server.URL()).ListLoggingInstances("clusterName", target)
    50  				Expect(myLogging).ShouldNot(BeNil())
    51  				Expect(err).NotTo(HaveOccurred())
    52  				Expect(len(myLogging)).Should(Equal(1))
    53  				Expect(myLogging[0].InstanceID).Should(Equal("ec4f0886-edc4-409e-8720-574035538f91"))
    54  				Expect(myLogging[0].CRN).Should(Equal("crn:v1:bluemix:public:sysdig-monitor:us-south:a/fcdb764102154c7ea8e1b79d3a64afe0:ec4f0886-edc4-409e-8720-574035538f91::"))
    55  				Expect(myLogging[0].PrivateEndpoint).Should(Equal(true))
    56  			})
    57  		})
    58  
    59  		Context("When read of logging instances 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 logging istances`),
    68  					),
    69  				)
    70  			})
    71  
    72  			It("should return error when ogging istances are retrieved", func() {
    73  				target := LoggingTargetHeader{}
    74  				myLogging, err := newLogging(server.URL()).ListLoggingInstances("DragonBoat-cluster", target)
    75  				Expect(err).To(HaveOccurred())
    76  				Expect(myLogging).Should(BeNil())
    77  			})
    78  		})
    79  	})
    80  
    81  	//Create
    82  	Describe("CreateLoggingConfig", 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/logging/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 Logging instances created", func() {
    98  				params := LoggingCreateRequest{
    99  					Cluster: "DragonBoat-cluster", LoggingInstance: "ec4f0886-edc4-409e-8720-574035538f91",
   100  				}
   101  				target := LoggingTargetHeader{}
   102  				myLogging, err := newLogging(server.URL()).CreateLoggingConfig(params, target)
   103  				Expect(err).NotTo(HaveOccurred())
   104  				Expect(myLogging).ShouldNot(BeNil())
   105  				Expect(myLogging.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/logging/createConfig"),
   115  						ghttp.VerifyJSON(`{"cluster": "DragonBoat-cluster", "instance": "ec4f0886-edc4-409e-8720-574035538f91"}`),
   116  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to create sysdig logging instance`),
   117  					),
   118  				)
   119  			})
   120  			It("should return error during logging instance creation", func() {
   121  
   122  				params := LoggingCreateRequest{
   123  					Cluster: "DragonBoat-cluster", LoggingInstance: "ec4f0886-edc4-409e-8720-574035538f91",
   124  				}
   125  				target := LoggingTargetHeader{}
   126  				_, err := newLogging(server.URL()).CreateLoggingConfig(params, target)
   127  				Expect(err).To(HaveOccurred())
   128  			})
   129  		})
   130  	})
   131  })
   132  
   133  func newLogging(url string) Logging {
   134  
   135  	sess, err := session.New()
   136  	if err != nil {
   137  		log.Fatal(err)
   138  	}
   139  	conf := sess.Config.Copy()
   140  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
   141  	conf.Endpoint = &url
   142  
   143  	client := client.Client{
   144  		Config:      conf,
   145  		ServiceName: bluemix.VpcContainerService,
   146  	}
   147  	return newLoggingAPI(&client)
   148  }