Skip to content

Commit

Permalink
test for go_app
Browse files Browse the repository at this point in the history
  • Loading branch information
asalan316 committed Oct 18, 2024
1 parent a7ee6e7 commit f9f29ea
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/acceptance/assets/app/go_app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
34 changes: 17 additions & 17 deletions src/acceptance/assets/app/go_app/internal/app/custom_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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")
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app_test

import (
"context"
"errors"
"net/http"

"github.com/go-logr/logr"
Expand All @@ -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).
Expand All @@ -37,24 +37,51 @@ 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()).
Status(http.StatusOK).
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(
Expand All @@ -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()
})
})

})

})

})

0 comments on commit f9f29ea

Please sign in to comment.