From f9f29ea80ca82bcf85c0dc7d88df1dae2a53d145 Mon Sep 17 00:00:00 2001 From: Arsalan Khan Date: Fri, 18 Oct 2024 12:18:56 +0200 Subject: [PATCH] test for go_app --- src/acceptance/assets/app/go_app/Makefile | 2 +- .../app/go_app/internal/app/custom_metrics.go | 34 +++---- .../internal/app/custom_metrics_test.go | 90 ++++++++++++++++--- 3 files changed, 94 insertions(+), 32 deletions(-) diff --git a/src/acceptance/assets/app/go_app/Makefile b/src/acceptance/assets/app/go_app/Makefile index e4194da52b..ab06d99869 100644 --- a/src/acceptance/assets/app/go_app/Makefile +++ b/src/acceptance/assets/app/go_app/Makefile @@ -119,7 +119,7 @@ deploy: build .PHONY: clean clean: - @echo "# cleaning autoscaler" + @echo "# cleaning go_app" @go clean -cache -testcache @rm --force --recursive './build' @rm --force --recursive './internal/app/appfakes' diff --git a/src/acceptance/assets/app/go_app/internal/app/custom_metrics.go b/src/acceptance/assets/app/go_app/internal/app/custom_metrics.go index 7e0d37bdf4..0fd629a730 100644 --- a/src/acceptance/assets/app/go_app/internal/app/custom_metrics.go +++ b/src/acceptance/assets/app/go_app/internal/app/custom_metrics.go @@ -26,6 +26,8 @@ type CustomMetricAPIClient struct{} var _ CustomMetricClient = &CustomMetricAPIClient{} +var CfenvCurrent = cfenv.Current + func CustomMetricsTests(logger logr.Logger, r *gin.RouterGroup, customMetricTest CustomMetricClient) *gin.RouterGroup { r.GET("/mtls/:name/:value", handleCustomMetricsEndpoint(logger, customMetricTest, true)) r.GET("/:name/:value", handleCustomMetricsEndpoint(logger, customMetricTest, false)) @@ -52,16 +54,10 @@ func handleCustomMetricsEndpoint(logger logr.Logger, customMetricTest CustomMetr Error(c, http.StatusBadRequest, "invalid metric value: %s", err.Error()) return } - // required if neighbour app is sending metric for appToScaleGuid - appConfig := &cfenv.App{} + // required if producer app is sending metric for appToScaleGuid appToScaleGuid := c.Query("appToScaleGuid") - if appToScaleGuid != "" { - logger.Info("neighbour-app-relationship-found", "appToScaleGuid", appToScaleGuid) - appConfig.AppID = appToScaleGuid - //assuming the neighbour app has the same autoscaler service as the appToScale - currentApp, _ := cfenv.Current() - appConfig.Services = currentApp.Services - } + appConfig := &cfenv.App{AppID: appToScaleGuid} + err = customMetricTest.PostCustomMetric(c, logger, appConfig, float64(metricValue), metricName, useMtls) if err != nil { logger.Error(err, "failed to submit custom metric") @@ -73,15 +69,19 @@ func handleCustomMetricsEndpoint(logger logr.Logger, customMetricTest CustomMetr } func (*CustomMetricAPIClient) PostCustomMetric(ctx context.Context, logger logr.Logger, appConfig *cfenv.App, metricValue float64, metricName string, useMtls bool) error { - var err error - logger.Info("sending custom metric", "appConfig", appConfig) - if appConfig == nil || appConfig.AppID == "" { - appConfig, err = cfenv.Current() - if err != nil { - return fmt.Errorf("cloud foundry environment not found %w", err) - } + currentApp, err := CfenvCurrent() + if err != nil { + return fmt.Errorf("cloud foundry environment not found %w", err) + } + // appToScale is provided i.e. producer and consumer app relationship + if appConfig != nil && appConfig.AppID != "" { + logger.Info("producer-app-relationship-found", "appToScaleGuid", appConfig.AppID) + //assuming the producer app has the same autoscaler service credentials as appToScale + appConfig.Services = currentApp.Services + } + if appConfig.AppID == "" { + appConfig = currentApp } - appId := api.GUID(appConfig.AppID) autoscalerCredentials, err := getAutoscalerCredentials(appConfig) if err != nil { diff --git a/src/acceptance/assets/app/go_app/internal/app/custom_metrics_test.go b/src/acceptance/assets/app/go_app/internal/app/custom_metrics_test.go index ca5ce89e09..c619985d1e 100644 --- a/src/acceptance/assets/app/go_app/internal/app/custom_metrics_test.go +++ b/src/acceptance/assets/app/go_app/internal/app/custom_metrics_test.go @@ -2,6 +2,7 @@ package app_test import ( "context" + "errors" "net/http" "github.com/go-logr/logr" @@ -16,9 +17,8 @@ import ( ) var _ = Describe("custom metrics tests", func() { - + var fakeCustomMetricClient *appfakes.FakeCustomMetricClient Context("custom metrics handler", func() { - fakeCustomMetricClient := &appfakes.FakeCustomMetricClient{} It("should err if value out of bounds", func() { apiTest(nil, nil, nil, nil). @@ -37,6 +37,7 @@ var _ = Describe("custom metrics tests", func() { End() }) It("should post the custom metric", func() { + fakeCustomMetricClient = &appfakes.FakeCustomMetricClient{} apiTest(nil, nil, nil, fakeCustomMetricClient). Get("/custom-metrics/test/4"). Expect(GinkgoT()). @@ -44,17 +45,43 @@ var _ = Describe("custom metrics tests", func() { Body(`{"mtls":false}`). End() Expect(fakeCustomMetricClient.PostCustomMetricCallCount()).To(Equal(1)) - _, _, _, sentValue, sentMetric, mtlsUsed := fakeCustomMetricClient.PostCustomMetricArgsForCall(0) + _, _, appConfig, sentValue, sentMetric, mtlsUsed := fakeCustomMetricClient.PostCustomMetricArgsForCall(0) + Expect(appConfig.AppID).Should(Equal("")) Expect(sentMetric).Should(Equal("test")) Expect(sentValue).Should(Equal(4.0)) Expect(mtlsUsed).Should(Equal(false)) }) + When("appToScaleGuid is provided in a producer-consumer relationship", func() { + fakeCustomMetricClient = &appfakes.FakeCustomMetricClient{} + It("should post the custom metric with appToScaleGuid", func() { + fakeCustomMetricClient := &appfakes.FakeCustomMetricClient{} + apiTest(nil, nil, nil, fakeCustomMetricClient). + Get("/custom-metrics/test/5"). + QueryParams(map[string]string{"appToScaleGuid": "test-app-id"}). + Expect(GinkgoT()). + Status(http.StatusOK). + Body(`{"mtls":false}`). + End() + Expect(fakeCustomMetricClient.PostCustomMetricCallCount()).To(Equal(1)) + _, _, appConfig, sentValue, sentMetric, mtlsUsed := fakeCustomMetricClient.PostCustomMetricArgsForCall(0) + Expect(appConfig.AppID).Should(Equal("test-app-id")) + Expect(sentMetric).Should(Equal("test")) + Expect(sentValue).Should(Equal(5.0)) + Expect(mtlsUsed).Should(Equal(false)) + }) + }) + }) Context("PostCustomMetrics", func() { - It("should post a custom metric", func() { + var ( + service cfenv.Service + testAppId string + fakeServer *ghttp.Server + ) + BeforeEach(func() { - testAppId := "test-app-id" - fakeServer := ghttp.NewServer() + testAppId = "test-app-id" + fakeServer = ghttp.NewServer() username := "test-user" password := "test-pass" fakeServer.AppendHandlers( @@ -79,24 +106,59 @@ var _ = Describe("custom metrics tests", func() { Password: password, URL: fakeServer.URL(), } - service := cfenv.Service{ + service = cfenv.Service{ Name: "test", Tags: []string{"app-autoscaler"}, Credentials: map[string]interface{}{"custom_metrics": customMetricsCredentials}, } - - appEnv := cfenv.App{ - AppID: testAppId, - Index: 0, - Services: map[string][]cfenv.Service{"autoscaler": {service}}, + }) + It("should post a custom metric", func() { + app.CfenvCurrent = func() (*cfenv.App, error) { + return &cfenv.App{ + AppID: testAppId, + Index: 0, + Services: map[string][]cfenv.Service{"autoscaler": {service}}, + }, nil } - + appEnv, _ := app.CfenvCurrent() client := &app.CustomMetricAPIClient{} - err := client.PostCustomMetric(context.TODO(), logr.Logger{}, &appEnv, 42, "test", false) + err := client.PostCustomMetric(context.TODO(), logr.Logger{}, appEnv, 42, "test", false) Expect(err).ToNot(HaveOccurred()) Expect(len(fakeServer.ReceivedRequests())).To(Equal(1)) fakeServer.Close() }) + Context("verify configs", func() { + When("cloud foundry environment is not found", func() { + It("should return error if cloud foundry environment is not found", func() { + app.CfenvCurrent = func() (*cfenv.App, error) { + return nil, errors.New("cloud foundry environment not found") + } + client := &app.CustomMetricAPIClient{} + err := client.PostCustomMetric(context.TODO(), logr.Logger{}, &cfenv.App{}, 42, "test", false) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("cloud foundry environment not found")) + }) + }) + When("appToScaleGuid is provided in a producer-consumer relationship", func() { + It("should set appConfig with appToScaleGuid and services", func() { + + app.CfenvCurrent = func() (*cfenv.App, error) { + return &cfenv.App{AppID: testAppId, Services: map[string][]cfenv.Service{"autoscaler": {service}}}, nil + } + appConfig, _ := app.CfenvCurrent() + client := &app.CustomMetricAPIClient{} + err := client.PostCustomMetric(context.TODO(), logr.Logger{}, appConfig, 42, "test", false) + Expect(err).ToNot(HaveOccurred()) + Expect(appConfig.Services).NotTo(BeNil()) + Expect(appConfig.AppID).To(Equal(testAppId)) + Expect(len(fakeServer.ReceivedRequests())).To(Equal(1)) + fakeServer.Close() + }) + }) + + }) + }) + })