Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Go instead of Node.js app in tests #1375

Merged
merged 3 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ test-changeloglockcleaner: init init-db test-certs
@make -C src/changeloglockcleaner test DBURL="${DBURL}"
test-acceptance-unit:
@make -C src/acceptance test-unit
@make -C src/acceptance/assets/app/go_app test


.PHONY: start-db
Expand Down Expand Up @@ -278,8 +279,9 @@ target/vendor-app:
&& npm prune --omit=dev
@touch $@


.PHONY: acceptance-release
acceptance-release: clean-acceptance mod-tidy vendor vendor-app
acceptance-release: clean-acceptance mod-tidy vendor vendor-app build-test-app
@echo " - building acceptance test release '${VERSION}' to dir: '${DEST}' "
@mkdir -p ${DEST}
@tar --create --auto-compress --directory="src" --file="${ACCEPTANCE_TESTS_FILE}" 'acceptance'
Expand Down Expand Up @@ -340,8 +342,13 @@ deploy-prometheus:
export BBL_STATE_PATH=$${BBL_STATE_PATH:-$(shell realpath "../app-autoscaler-env-bbl-state/bbl-state/")};\
${CI_DIR}/infrastructure/scripts/deploy-prometheus.sh;


.PHONY: build-test-app
build-test-app:
@make -C src/acceptance/assets/app/go_app build

.PHONY: acceptance-tests
acceptance-tests: vendor-app
acceptance-tests: vendor-app build-test-app
${CI_DIR}/autoscaler/scripts/run-acceptance-tests.sh;
acceptance-cleanup:
${CI_DIR}/autoscaler/scripts/cleanup-acceptance.sh;
Expand Down
104 changes: 104 additions & 0 deletions api/custom-metrics-api.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
openapi: 3.1.0
info:
title: app-autoscaler custom metrics API
description: |
app-autoscaler lets you automatically increase or decrease the number of your application instances based on the policies you have defined. Your applications can call the app-autoscaler custom metrics API to submit custom metrics defined and used in a dynamic scaling policy to enable automatic scaling based on application-specific metrics.
version: 1.0.0
security:
- mtls: []
- basicAuthentication: []
tags:
- name: Custom Metrics API V1
description: Work with application-specific custom metrics to scale your application.

paths:
/v1/apps/{appGuid}/metrics:
parameters:
- in: path
name: appGuid
description: The GUID identifying the application the custom metrics are submitted for. Can be found in the `application_id` property of the JSON object stored in the `VCAP_APPLICATION` environment variable.
example: 58d162ad-87d5-4f52-8abf-e56a63a67292
required: true
schema:
type: string
post:
summary: Submits application-specific custom metrics to app-autoscaler.
description: |
Use to submit custom metrics to app-autoscaler which are defined and used in a dynamic scaling policy to enable automatic scaling based on application-specific metrics. We recommend submitting the custom metrics every 40 seconds from every application instance.
tags:
- Custom Metrics API V1
requestBody:
description: Custom metrics to be submitted to app-autoscaler.
content:
application/json:
schema:
$ref: '#/components/schemas/Metrics'
required: true
responses:
'200':
description: Metrics were successfully submitted.
default:
$ref: '#/components/responses/Error'
security:
- mtls: []
x-codegen-request-body-name: body
components:
responses:
Error:
description: |
Error, the response body may contain further information.
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
schemas:
Metrics:
required:
- instance_index
- metrics
type: object
properties:
instance_index:
type: integer
format: int64
description: The index of the application instance the custom metrics are submitted for. Can be found in the `CF_INSTANCE_INDEX` environment variable.
example: 0
metrics:
type: array
items:
$ref: '#/components/schemas/Metric'
description: The custom metrics to be submitted.
Metric:
required:
- name
- value
type: object
properties:
name:
type: string
description: The name of the custom metric, must have been defined by using it in the scaling policy for the application before submitting.
example: queue_length
value:
type: number
description: The value of the custom metric.
example: 1337
unit:
type: string
description: An optional unit for the metric, for display purposes only.
example: jobs
ErrorResponse:
type: object
properties:
code:
type: string
description: The textual error code.
message:
type: string
description: The message describing the cause of the error.
securitySchemes:
mtls:
type: mutualTLS
description: Use the Instance Identity Credentials provided by the Cloud Foundry runtime
basicAuthentication:
type: http
scheme: basic
2 changes: 1 addition & 1 deletion src/acceptance/app/custom_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var _ = Describe("AutoScaler custom metrics policy", func() {
By("adding policy so test_metric is allowed")
policy = GenerateDynamicScaleOutAndInPolicy(1, 2, "test_metric", 500, 500)
By("sending metric via mtls endpoint")
SendMetric(cfg, appName, 10)
SendMetricMTLS(cfg, appName, 10)
GinkgoWriter.Println("")
})
})
Expand Down
9 changes: 4 additions & 5 deletions src/acceptance/app/dynamic_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ var _ = Describe("AutoScaler dynamic policy", func() {
doneAcceptChan <- true
return
case <-ticker.C:
cfh.CurlApp(cfg, appName, "/slow/3000", "-f")
cfh.CurlApp(cfg, appName, "/responsetime/slow/3000", "-f")
}
}
}(doneChan)
Expand Down Expand Up @@ -146,7 +146,7 @@ var _ = Describe("AutoScaler dynamic policy", func() {
doneAcceptChan <- true
return
case <-ticker.C:
cfh.CurlApp(cfg, appName, "/fast", "-f")
cfh.CurlApp(cfg, appName, "/responsetime/fast", "-f")
}
}
}(doneChan)
Expand Down Expand Up @@ -189,7 +189,7 @@ var _ = Describe("AutoScaler dynamic policy", func() {
doneAcceptChan <- true
return
case <-ticker.C:
cfh.CurlApp(cfg, appName, "/fast", "-f")
cfh.CurlApp(cfg, appName, "/responsetime/fast", "-f")
}
}
}(doneChan)
Expand Down Expand Up @@ -219,7 +219,7 @@ var _ = Describe("AutoScaler dynamic policy", func() {
doneAcceptChan <- true
return
case <-ticker.C:
cfh.CurlApp(cfg, appName, "/fast", "-f")
cfh.CurlApp(cfg, appName, "/responsetime/fast", "-f")
}
}
}(doneChan)
Expand All @@ -232,7 +232,6 @@ var _ = Describe("AutoScaler dynamic policy", func() {
})

// To check existing aggregated cpu metrics do: cf asm APP_NAME cpu
// See: https://www.ibm.com/docs/de/cloud-private/3.2.0?topic=SSBS6K_3.2.0/cloud_foundry/integrating/cfee_autoscaler.html
Context("when scaling by cpu", func() {

BeforeEach(func() {
Expand Down
13 changes: 4 additions & 9 deletions src/acceptance/assets/app/go_app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,20 @@ test:
@echo "Running tests"
ginkgo run ${GINKGO_OPTS} ./...

.PHONY: buildtools
buildtools:
@echo "# Installing build tools"
@go mod download
@which certstrap >/dev/null || go install github.com/square/certstrap
@which ginkgo >/dev/null || go install github.com/onsi/ginkgo/v2/ginkgo
@which counterfeiter >/dev/null || go install github.com/maxbrunsfeld/counterfeiter/v6

lint:
@cd ../../../../../; make lint_acceptance OPTS=${OPTS}

.PHONY: start
start: build
docker run -it --name go_app -v $$PWD/build/:/cf/bin -p 8080:8080 --rm cloudfoundry/cflinuxfs3 /cf/bin/app
docker run -it --name go_app -v $$PWD/build/:/cf/bin -p 8080:8080 --rm cloudfoundry/cflinuxfs4 /cf/bin/app

.PHONY: deploy
deploy: build
./deploy.sh

generate: go.mod ../../../../../api/custom-metrics-api.yaml
go generate internal/generate.go

.PHONY: clean
clean:
@echo "# cleaning autoscaler"
Expand Down
9 changes: 2 additions & 7 deletions src/acceptance/assets/app/go_app/app_manifest.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
---
applications:
- name: ((app_name))
env:
SERVICE_NAME: ((service_name))
NODE_ENV: production
MEMORY_MAX: ((memory_mb))
buildpacks:
- ((buildpack))
stack: cflinuxfs3
stack: cflinuxfs4
command: ./app
routes:
- route: ((app_name)).((app_domain))
protocol: http1
processes:
- type: web
instances: ((instances))
Expand All @@ -20,4 +15,4 @@ applications:
health-check-type: http
health-check-invocation-timeout: 10
health-check-http-endpoint: "/health"
timeout: 120
timeout: 120
10 changes: 8 additions & 2 deletions src/acceptance/assets/app/go_app/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,23 @@ fi

script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
app_dir="$( realpath -e "${script_dir}/build")"
service_name=$(getConfItem service_name)

cf create-org test
cf target -o test
cf create-space test_app
cf target -s test_app

cf enable-service-access "$service_name" -b "$(getConfItem service_broker)" -p "$(getConfItem service_plan)" -o test
cf create-service "$service_name" "$(getConfItem service_plan)" "$service_name" -b "$(getConfItem service_broker)" --wait
#cf unbind-service test_app "$service_name" --wait

pushd "$app_dir" >/dev/null
cf push\
--var app_name=test_app\
--var app_domain="$(getConfItem apps_domain)"\
--var service_name="$(getConfItem service_name)"\
--var service_name="$service_name"\
silvestre marked this conversation as resolved.
Show resolved Hide resolved
--var instances=1\
--var node_tls_reject_unauthorized=0\
--var memory_mb="$(getConfItem node_memory_limit||echo 128)"\
--var buildpack="binary_buildpack"\
-f "manifest.yml"\
Expand Down
68 changes: 68 additions & 0 deletions src/acceptance/assets/app/go_app/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module code.cloudfoundry.org/app-autoscaler-release/src/acceptance/assets/app/go_app

go 1.19

require (
github.com/cloudfoundry-community/go-cfenv v1.18.0
github.com/fgrosse/zaptest v1.1.0
github.com/gin-contrib/zap v0.1.0
github.com/gin-gonic/gin v1.8.2
github.com/go-faster/errors v0.6.1
github.com/go-faster/jx v1.0.0
github.com/go-logr/logr v1.2.4
github.com/go-logr/zapr v1.2.3
github.com/mitchellh/mapstructure v1.5.0
github.com/ogen-go/ogen v0.61.0
github.com/onsi/ginkgo/v2 v2.9.2
github.com/onsi/gomega v1.27.6
github.com/prometheus/procfs v0.9.0
github.com/steinfletcher/apitest v1.5.14
go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.39.0
go.opentelemetry.io/contrib/propagators/b3 v1.14.0
go.opentelemetry.io/otel v1.14.0
go.opentelemetry.io/otel/metric v0.37.0
go.opentelemetry.io/otel/trace v1.14.0
go.uber.org/zap v1.24.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.8.1 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-faster/yaml v0.4.3 // indirect
github.com/go-faster/yamlx v0.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.11.2 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/pprof v0.0.0-20230323073829-e72429f035bd // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/ugorji/go/codec v1.2.9 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/tools v0.7.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading