Skip to content

Commit

Permalink
fix linters
Browse files Browse the repository at this point in the history
  • Loading branch information
asalan316 committed Oct 17, 2024
1 parent 6e95c3e commit 2e9256a
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 91 deletions.
12 changes: 6 additions & 6 deletions src/acceptance/app/app_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ var (
appToScaleName string
appToScaleGUID string

neighbourAppName string
metricProducerAppName string

neighbourAppGUID string
metricProducerAppGUID string
)

const componentName = "Application Scale Suite"
Expand Down Expand Up @@ -68,10 +68,10 @@ func AppAfterEach() {
DeleteService(cfg, instanceName, appToScaleName)
DeleteTestApp(appToScaleName, cfg.DefaultTimeoutDuration())
}
if neighbourAppName != "" {
DebugInfo(cfg, setup, neighbourAppName)
DeleteService(cfg, instanceName, neighbourAppName)
DeleteTestApp(neighbourAppName, cfg.DefaultTimeoutDuration())
if metricProducerAppName != "" {
DebugInfo(cfg, setup, metricProducerAppName)
DeleteService(cfg, instanceName, metricProducerAppName)
DeleteTestApp(metricProducerAppName, cfg.DefaultTimeoutDuration())
}
}
}
Expand Down
81 changes: 23 additions & 58 deletions src/acceptance/app/custom_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,101 +79,66 @@ var _ = Describe("AutoScaler custom metrics", func() {
})
})

Describe("Custom metrics with neighbour app", func() {
Describe("Custom metrics with producer app", func() {
BeforeEach(func() {
// attach policy to appToScale B
policy = GenerateBindingsWithScalingPolicy("bound_app", 1, 2, "test_metric", 100, 500)
instanceName = CreatePolicy(cfg, appToScaleName, appToScaleGUID, policy)
StartApp(appToScaleName, cfg.CfPushTimeoutDuration())

// push neighbour app
neighbourAppName = CreateTestApp(cfg, "go-custom_metric_producer-app", 1)
neighbourAppGUID, err = GetAppGuid(cfg, neighbourAppName)
// push producer app without policy
metricProducerAppName = CreateTestApp(cfg, "go-custom_metric_producer-app", 1)
metricProducerAppGUID, err = GetAppGuid(cfg, metricProducerAppName)
Expect(err).NotTo(HaveOccurred())
err := BindServiceToAppWithPolicy(cfg, neighbourAppName, instanceName, "")
err := BindServiceToAppWithPolicy(cfg, metricProducerAppName, instanceName, "")
Expect(err).NotTo(HaveOccurred())
StartApp(neighbourAppName, cfg.CfPushTimeoutDuration())
StartApp(metricProducerAppName, cfg.CfPushTimeoutDuration())

})
Context("neighbour app sends custom metrics for app B via mtls", func() {
When("policy is attached with the appToScale with a bound_app mentioned", func() {
Context("producer app A sends custom metrics for appToScale B via mtls", func() {
When("policy is attached with the appToScale B with bound_app", func() {
BeforeEach(func() {
policy = GenerateBindingsWithScalingPolicy("bound_app", 1, 2, "test_metric", 100, 500)
})
It("should scale out and scale in app B", Label(acceptance.LabelSmokeTests), func() {
By(fmt.Sprintf("Scale out %s to 2 instance", appToScaleName))
scaleOut := sendMetricToAutoscaler(cfg, appToScaleGUID, neighbourAppName, 550, true)
scaleOut := sendMetricToAutoscaler(cfg, appToScaleGUID, metricProducerAppName, 550, true)
Eventually(scaleOut).
WithTimeout(5 * time.Minute).
WithPolling(15 * time.Second).
Should(Equal(2))

By(fmt.Sprintf("Scale in %s to 1 instance", appToScaleName))
scaleIn := sendMetricToAutoscaler(cfg, appToScaleGUID, neighbourAppName, 80, true)
scaleIn := sendMetricToAutoscaler(cfg, appToScaleGUID, metricProducerAppName, 80, true)
Eventually(scaleIn).
WithTimeout(5 * time.Minute).
WithPolling(15 * time.Second).
Should(Equal(1))

})
})
//FixME ? Is the following valid?
/*
cf bind-service autoscaler-3-go-neighbour-app-25a4dc3fb9e6ea00
autoscaler-3-service-64a8ea1ff7d7f3f6 -c
{"configuration":{"custom_metrics":{"auth":{"credential_type":""},
"metric_submission_strategy":{"allow_from":"bound_app"}}},
"instance_min_count":0,"instance_max_count":0}
Command
cf bind-service autoscaler-3-go-neighbour-app-25a4dc3fb9e6ea00 autoscaler-3-service-64a8ea1ff7d7f3f6 -c {"configuration":{"custom_metrics":{"auth":{"credential_type":""},"metric_submission_strategy":{"allow_from":"bound_app"}}},"instance_min_count":0,"instance_max_count":0}
Binding service instance autoscaler-3-service-64a8ea1ff7d7f3f6 to app autoscaler-3-go-neighbour-app-25a4dc3fb9e6ea00 in org arsalan / space test-space as autoscaler-3211-TESTS-3-USER-c41b39e65d2ce188...
Job (e3fee92d-1062-4853-a6d4-d017f6b43157) failed: bind could not be completed: Service broker error: invalid policy provided: [{"context":"(root)","description":"Must validate at least one schema (anyOf)"},{"context":"(root)","description":"scaling_rules is required"},{"context":"(root).instance_min_count","description":"Must be greater than or equal to 1"}]
*/
/*When("policy is not attached with the neighbour app", func() {
BeforeEach(func() {
policy = GenerateBindingConfiguration("bound_app")
})
It("should scale out and scale in app B", func() {
By(fmt.Sprintf("Scale out %s to 2 instance", appToScaleName))
scaleOut := sendMetricToAutoscaler(cfg, appToScaleGUID, neighbourAppName, 550, true)
Eventually(scaleOut).
WithTimeout(5 * time.Minute).
WithPolling(15 * time.Second).
Should(Equal(2))
By(fmt.Sprintf("Scale in %s to 1 instance", appToScaleName))
scaleIn := sendMetricToAutoscaler(cfg, appToScaleGUID, neighbourAppName, 80, true)
Eventually(scaleIn).
WithTimeout(5 * time.Minute).
WithPolling(15 * time.Second).
Should(Equal(1))
})
})*/

When("app B tries to send metrics for neighbour app with strategy same_app", func() {
BeforeEach(func() {
policy = GenerateBindingsWithScalingPolicy("bound_app", 1, 2, "test_metric", 100, 500)
})
It("should not scale neighbour app", func() {
By(fmt.Sprintf("Fail Scale %s ", neighbourAppName))
sendMetricToAutoscaler(cfg, neighbourAppGUID, appToScaleName, 550, true)
WaitForNInstancesRunning(neighbourAppGUID, 1, 5*time.Second, "expected 1 instance running")
})
})
Context("appToScale B tries to send metrics for producer app A with strategy same_app", func() {
BeforeEach(func() {
policy = GenerateBindingsWithScalingPolicy("same_app", 1, 2, "test_metric", 100, 500)
})
It("should not scale producer app", func() {
By(fmt.Sprintf("Fail Scale %s ", metricProducerAppName))
sendMetricToAutoscaler(cfg, metricProducerAppGUID, appToScaleName, 550, true)
WaitForNInstancesRunning(metricProducerAppGUID, 1, 5*time.Second, "expected 1 instance running")
})
})
})
})

func sendMetricToAutoscaler(config *config.Config, appToScaleGUID string, neighbourAppName string, metricThreshold int, mtls bool) func() (int, error) {
func sendMetricToAutoscaler(config *config.Config, appToScaleGUID string, metricProducerAppName string, metricThreshold int, mtls bool) func() (int, error) {
return func() (int, error) {
if mtls {
SendMetricMTLS(config, appToScaleGUID, neighbourAppName, metricThreshold)
SendMetricMTLS(config, appToScaleGUID, metricProducerAppName, metricThreshold)
} else {
SendMetric(config, neighbourAppName, metricThreshold)
SendMetric(config, metricProducerAppName, metricThreshold)
}
fmt.Println("INVOKED====================")
return RunningInstances(appToScaleGUID, 5*time.Second)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package app_test

import (
"context"
"github.com/go-logr/logr"
"net/http"

"github.com/go-logr/logr"

"code.cloudfoundry.org/app-autoscaler-release/src/acceptance/assets/app/go_app/internal/app"
"code.cloudfoundry.org/app-autoscaler-release/src/acceptance/assets/app/go_app/internal/app/appfakes"
api "code.cloudfoundry.org/app-autoscaler-release/src/acceptance/assets/app/go_app/internal/custommetrics"
Expand Down
1 change: 0 additions & 1 deletion src/acceptance/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ func GenerateBindingsWithScalingPolicy(allowFrom string, instanceMin, instanceMa
}

func GenerateDynamicScaleOutPolicy(instanceMin, instanceMax int, metricName string, threshold int64) string {

policy := buildScalingPolicy(instanceMin, instanceMax, metricName, threshold)
marshaled, err := MarshalWithoutHTMLEscape(policy)
Expect(err).NotTo(HaveOccurred())
Expand Down
3 changes: 3 additions & 0 deletions src/autoscaler/api/db/servicebroker.db.changelog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ databaseChangeLog:
- column:
name: custom_metrics_strategy
type: varchar(40)
constraints:
nullable: false
- changeSet:
id: 3
author: Arsalan
Expand All @@ -96,6 +98,7 @@ databaseChangeLog:
type: varchar(40)
constraints:
primaryKey: true
nullable: false
- insert:
tableName: metrics_submission
columns:
Expand Down
16 changes: 8 additions & 8 deletions src/autoscaler/db/sqldb/binding_sqldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,10 @@ func (bdb *BindingSQLDB) DeleteServiceInstance(ctx context.Context, serviceInsta
}

func (bdb *BindingSQLDB) CreateServiceBinding(ctx context.Context, bindingId string, serviceInstanceId string, appId string, customMetricsStrategy string) error {

err := bdb.isBindingExists(ctx, bindingId, serviceInstanceId, appId)
if err != nil {
return err
}

query := bdb.sqldb.Rebind("INSERT INTO binding" +
"(binding_id, service_instance_id, app_id, created_at, custom_metrics_strategy) " +
"VALUES(?, ?, ?, ?,?)")
Expand Down Expand Up @@ -390,7 +388,6 @@ func (bdb *BindingSQLDB) GetBindingIdsByInstanceId(ctx context.Context, instance
}

func (bdb *BindingSQLDB) IsAppBoundToSameAutoscaler(ctx context.Context, metricSubmitterAppId string, appToScaleId string) (bool, error) {

serviceInstanceId, err := bdb.GetServiceInstanceIdByAppId(metricSubmitterAppId)
if err != nil {
bdb.logger.Error("get-service-instance-by-appId", err, lager.Data{"appId": metricSubmitterAppId})
Expand Down Expand Up @@ -439,13 +436,16 @@ func (bdb *BindingSQLDB) fetchCustomMetricStrategyByAppId(ctx context.Context, a
}
defer func() { _ = rows.Close() }()

for rows.Next() {
if rows.Next() {
if err = rows.Scan(&customMetricsStrategy); err != nil {
bdb.logger.Error("scan-customMetricsStrategy-from-binding-table", err)
bdb.logger.Error("error-finding-customMetricsStrategy-in-binding-table", err)
return "", err
}
return customMetricsStrategy.String, nil

}
return customMetricsStrategy.String, rows.Err()
err = rows.Err()
if err != nil {
bdb.logger.Error("error-finding-customMetricsStrategy-in-binding-table", err)
return "", err
}
return customMetricsStrategy.String, nil
}
18 changes: 9 additions & 9 deletions src/autoscaler/db/sqldb/binding_sqldb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ var _ = Describe("BindingSqldb", func() {
"adjustment":"+1"
}]
}`
policyGuid2 = addProcessIdTo("test-policy-guid-2")
testInstanceId2 = testInstanceId + "2"
testInstanceId3 = testInstanceId + "3"
testAppId2 = testAppId + "2"
testAppId3 = testAppId + "3"
testBindingId3 = testBindingId + "3"
testBindingId2 = testBindingId + "2"
policyGuid2 = addProcessIdTo("test-policy-guid-2")
testInstanceId2 = testInstanceId + "2"
testInstanceId3 = testInstanceId + "3"
testAppId2 = testAppId + "2"
testAppId3 = testAppId + "3"
testBindingId3 = testBindingId + "3"
testBindingId2 = testBindingId + "2"
customMetricsStrategy = "same_app"
)

dbUrl := testhelpers.GetDbUrl()
Expand Down Expand Up @@ -352,7 +353,6 @@ var _ = Describe("BindingSqldb", func() {
})

Describe("CreateServiceBinding", func() {
var customMetricsStrategy = "same_app"

JustBeforeEach(func() {
err = bdb.CreateServiceBinding(context.Background(), testBindingId, testInstanceId, testAppId, customMetricsStrategy)
Expand All @@ -378,7 +378,7 @@ var _ = Describe("BindingSqldb", func() {
})
Context("When service binding already exists", func() {
It("should error", func() {
err := bdb.CreateServiceBinding(context.Background(), testBindingId, testInstanceId, testAppId, "same_app")
err = bdb.CreateServiceBinding(context.Background(), testBindingId, testInstanceId, testAppId, "same_app")
Expect(err).To(HaveOccurred())
Expect(err).To(Equal(db.ErrAlreadyExists))
})
Expand Down
6 changes: 3 additions & 3 deletions src/autoscaler/metricsforwarder/server/auth/authenticator.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package auth

import (
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/db"
"errors"
"net/http"

"code.cloudfoundry.org/app-autoscaler/src/autoscaler/db"

"code.cloudfoundry.org/app-autoscaler/src/autoscaler/cred_helper"
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/metricsforwarder/server/common"
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/models"
Expand Down Expand Up @@ -55,11 +56,10 @@ func (a *Auth) AuthenticateHandler(next http.Handler) func(w http.ResponseWriter

func (a *Auth) CheckAuth(r *http.Request, appID string) error {
var errAuth error
a.logger.Info("checking authentication for app", lager.Data{"app_id": appID})
errAuth = a.XFCCAuth(r, a.bindingDB, appID)
if errAuth != nil {
if errors.Is(errAuth, ErrXFCCHeaderNotFound) {
a.logger.Info("Trying basic auth")
a.logger.Info("Trying basic auth", lager.Data{"app_id": appID})
errAuth = a.BasicAuth(r, appID)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package auth

import (
"net/http"

"code.cloudfoundry.org/app-autoscaler/src/autoscaler/db"
"code.cloudfoundry.org/lager/v3"
"net/http"
)

type MetricsSubmissionStrategy interface {
Expand Down Expand Up @@ -32,7 +33,6 @@ func (c *BoundedMetricsSubmissionStrategy) validate(appId string, submitterAppId
}

func (c *BoundedMetricsSubmissionStrategy) verifyMetricSubmissionStrategy(r *http.Request, logger lager.Logger, bindingDB db.BindingDB, submitterAppCert string, appID string) error {

isAppBound, err := bindingDB.IsAppBoundToSameAutoscaler(r.Context(), submitterAppCert, appID)
if err != nil {
logger.Error("error-checking-app-bound-to-same-service", err, lager.Data{"metric-submitter-app-id": submitterAppCert})
Expand Down
7 changes: 4 additions & 3 deletions src/autoscaler/metricsforwarder/server/auth/xfcc_auth.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package auth

import (
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/db"
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/models"
"code.cloudfoundry.org/lager/v3"
"crypto/x509"
"encoding/base64"
"errors"
"fmt"
"net/http"
"strings"

"code.cloudfoundry.org/app-autoscaler/src/autoscaler/db"
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/models"
"code.cloudfoundry.org/lager/v3"
)

var ErrXFCCHeaderNotFound = errors.New("mTLS authentication method not found")
Expand Down

0 comments on commit 2e9256a

Please sign in to comment.