Skip to content

Commit

Permalink
wip: refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bonzofenix committed May 29, 2024
1 parent 27cf26d commit 945a66a
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 166 deletions.
26 changes: 14 additions & 12 deletions src/autoscaler/metricsforwarder/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,7 @@ PR_NUMBER := $(shell gh pr view --json number --jq '.number')
DEPLOYMENT_NAME ?= autoscaler-$(PR_NUMBER)
VM_NAME := $(shell bosh -d $(DEPLOYMENT_NAME) vms --json | jq ".Tables | .[0] | .Rows | .[] | .instance" -r | grep metricsforwarder)





set-security-group:
$(eval ORG := $(shell cf target |grep "org\:" |cut -d':' -f2 | xargs))
$(eval SPACE := $(shell cf target |grep "space\:" |cut -d':' -f2 | xargs))

cf create-security-group metricsforwarder security-group.json
cf bind-security-group metricsforwarder $(ORG)

fetch-config:
fetch-config: cf-login
# how to define variables in deployment name
mkdir -p assets/certs/policy_db assets/certs/storedprocedure_db assets/certs/syslog_client

Expand Down Expand Up @@ -49,5 +38,18 @@ fetch-config:
sed -i '' 's|$(DEPLOYMENT_NAME).autoscalerpostgres.service.cf.internal|$(POSTGRES_IP)|g' metricsforwarder.yml
sed -i '' 's|log-cache.service.cf.internal|$(LOG_CACHE_IP)|g' metricsforwarder.yml

set-security-group:
$(eval ORG := $(shell cf target |grep "org\:" |cut -d':' -f2 | xargs))
$(eval SPACE := $(shell cf target |grep "space\:" |cut -d':' -f2 | xargs))

cf create-security-group metricsforwarder security-group.json
cf bind-security-group metricsforwarder $(ORG)

build-cf:
@cd ../; make build-cf-metricsforwarder



cf-login:
@cd ../../../; make cf-login

Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package server_test

import (
"bytes"
"encoding/json"
"fmt"
"path/filepath"
"time"

"code.cloudfoundry.org/app-autoscaler/src/autoscaler/fakes"
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/helpers"
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/metricsforwarder/config"
. "code.cloudfoundry.org/app-autoscaler/src/autoscaler/metricsforwarder/server"
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/models"

"code.cloudfoundry.org/lager/v3"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/tedsuo/ifrit/ginkgomon_v2"

"net/http"
"net/http/httptest"
Expand All @@ -20,7 +24,6 @@ import (
)

var _ = Describe("MetricHandler", func() {

var (
handler *CustomMetricsHandler

Expand All @@ -42,26 +45,76 @@ var _ = Describe("MetricHandler", func() {

scalingPolicy *models.ScalingPolicy
)

BeforeEach(func() {
testCertDir := "../../../../test-certs"
loggregatorConfig := config.LoggregatorConfig{
TLS: models.TLSCerts{
KeyFile: filepath.Join(testCertDir, "metron.key"),
CertFile: filepath.Join(testCertDir, "metron.crt"),
CACertFile: filepath.Join(testCertDir, "loggregator-ca.crt"),
},
MetronAddress: "invalid-host-name-blah:12345",
}
serverConfig := helpers.ServerConfig{
Port: 2223 + GinkgoParallelProcess(),
}

loggerConfig := helpers.LoggingConfig{
Level: "debug",
}

conf := &config.Config{
Server: serverConfig,
Logging: loggerConfig,
LoggregatorConfig: loggregatorConfig,
}
policyDB = &fakes.FakePolicyDB{}
allowedMetricCache = *cache.New(10*time.Minute, -1)
httpStatusCollector := &fakes.FakeHTTPStatusCollector{}
rateLimiter = &fakes.FakeLimiter{}
fakeCredentials = &fakes.FakeCredentials{}

logger := lager.NewLogger("metrichandler-test")
logger.RegisterSink(lager.NewWriterSink(GinkgoWriter, lager.DEBUG))

httpServer, err = NewServer(logger, conf, policyDB,
fakeCredentials, allowedMetricCache, httpStatusCollector, rateLimiter)
Expect(err).NotTo(HaveOccurred())
serverUrl = fmt.Sprintf("http://127.0.0.1:%d", conf.Server.Port)
serverProcess = ginkgomon_v2.Invoke(httpServer)

policyDB = &fakes.FakePolicyDB{}
metricsforwarder = &fakes.FakeMetricForwarder{}
allowedMetricCache = *cache.New(10*time.Minute, -1)
allowedMetricTypeSet = make(map[string]struct{})
vars = make(map[string]string)
resp = httptest.NewRecorder()
handler = NewCustomMetricsHandler(logger, metricsforwarder, policyDB, allowedMetricCache)
serverUrl = fmt.Sprintf("http://127.0.0.1:%d", conf.Server.Port)
allowedMetricCache.Flush()
})

JustBeforeEach(func() {

})
AfterEach(func() {
ginkgomon_v2.Interrupt(serverProcess)
// wait for the server to shutdown
// this is necessary because the server is running in a separate goroutine
// and the test can exit before the server has a chance to shutdown

Eventually(serverProcess.Wait(), 5).Should(Receive())

})

JustBeforeEach(func() {
req = CreateRequest(body, "/v1/apps/an-app-id/metrics")
req.SetBasicAuth("username", "password")
vars["appid"] = "an-app-id"
handler.VerifyCredentialsAndPublishMetrics(resp, req, vars)
})

Describe("PublishMetrics", func() {
JustBeforeEach(func() {
req = CreateRequest(body)
Expect(err).ToNot(HaveOccurred())
vars["appid"] = "an-app-id"
handler.VerifyCredentialsAndPublishMetrics(resp, req, vars)
})

Context("when a request to publish custom metrics comes with malformed request body", func() {
BeforeEach(func() {
Expand All @@ -71,7 +124,7 @@ var _ = Describe("MetricHandler", func() {
}, nil)
body = []byte(`{
"instance_index":0,
"test" :
"test" :
"metrics":[
{
"name":"custom_metric1",
Expand Down Expand Up @@ -268,10 +321,3 @@ var _ = Describe("MetricHandler", func() {
})

})

func CreateRequest(body []byte) *http.Request {
req, err := http.NewRequest(http.MethodPost, serverUrl+"/v1/apps/an-app-id/metrics", bytes.NewReader(body))
Expect(err).ToNot(HaveOccurred())
req.Header.Add("Content-Type", "application/json")
return req
}
85 changes: 21 additions & 64 deletions src/autoscaler/metricsforwarder/server/server_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,38 @@
package server_test

import (
"fmt"
"bytes"
"net/http"
"os"
"path/filepath"
"time"

"code.cloudfoundry.org/app-autoscaler/src/autoscaler/fakes"
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/helpers"
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/metricsforwarder/config"
. "code.cloudfoundry.org/app-autoscaler/src/autoscaler/metricsforwarder/server"
"code.cloudfoundry.org/app-autoscaler/src/autoscaler/models"

"code.cloudfoundry.org/lager/v3"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/patrickmn/go-cache"
"github.com/tedsuo/ifrit"
"github.com/tedsuo/ifrit/ginkgomon_v2"

"testing"
)

func TestServer(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Server Suite")
}

var (
serverProcess ifrit.Process
serverUrl string
policyDB *fakes.FakePolicyDB
rateLimiter *fakes.FakeLimiter
fakeCredentials *fakes.FakeCredentials

allowedMetricCache cache.Cache
)

func TestServer(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Server Suite")
}
httpStatusCollector *fakes.FakeHTTPStatusCollector
serverUrl string
httpServer ifrit.Runner
serverProcess ifrit.Process

var _ = SynchronizedBeforeSuite(func() []byte {
conf *config.Config
)

var _ = BeforeSuite(func() {
_, err := os.ReadFile("../../../../test-certs/metron.key")
Expect(err).NotTo(HaveOccurred())

Expand All @@ -47,49 +41,12 @@ var _ = SynchronizedBeforeSuite(func() []byte {

_, err = os.ReadFile("../../../../test-certs/loggregator-ca.crt")
Expect(err).NotTo(HaveOccurred())

return nil
}, func(_ []byte) {

testCertDir := "../../../../test-certs"
loggregatorConfig := config.LoggregatorConfig{
TLS: models.TLSCerts{
KeyFile: filepath.Join(testCertDir, "metron.key"),
CertFile: filepath.Join(testCertDir, "metron.crt"),
CACertFile: filepath.Join(testCertDir, "loggregator-ca.crt"),
},
MetronAddress: "invalid-host-name-blah:12345",
}
serverConfig := helpers.ServerConfig{
Port: 2222 + GinkgoParallelProcess(),
}

loggerConfig := helpers.LoggingConfig{
Level: "debug",
}

conf := &config.Config{
Server: serverConfig,
Logging: loggerConfig,
LoggregatorConfig: loggregatorConfig,
}
policyDB = &fakes.FakePolicyDB{}
allowedMetricCache = *cache.New(10*time.Minute, -1)
httpStatusCollector := &fakes.FakeHTTPStatusCollector{}
rateLimiter = &fakes.FakeLimiter{}
fakeCredentials = &fakes.FakeCredentials{}

logger := lager.NewLogger("server_suite_test")
logger.RegisterSink(lager.NewWriterSink(GinkgoWriter, lager.DEBUG))

httpServer, err := NewServer(logger, conf, policyDB,
fakeCredentials, allowedMetricCache, httpStatusCollector, rateLimiter)
Expect(err).NotTo(HaveOccurred())
serverUrl = fmt.Sprintf("http://127.0.0.1:%d", conf.Server.Port)
serverProcess = ginkgomon_v2.Invoke(httpServer)
})

var _ = SynchronizedAfterSuite(func() {
ginkgomon_v2.Interrupt(serverProcess)
}, func() {
})
func CreateRequest(body []byte, path string) *http.Request {
req, err := http.NewRequest(http.MethodPost, serverUrl+path, bytes.NewReader(body))
Expect(err).ToNot(HaveOccurred())
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Basic dXNlcm5hbWU6cGFzc3dvcmQ=")
return req
}
Loading

0 comments on commit 945a66a

Please sign in to comment.