Skip to content

Commit 59396a5

Browse files
authored
test: rework smoketests and improve usability (#508)
* test: rework smoketests and improve usability always tee to tf.log improve folder name and move smoketest under testing remove smokedir, we only have one smoketest move function under smoketest * feat: support multiple function runtime and fix cloudwatch logs
1 parent 95ada37 commit 59396a5

File tree

12 files changed

+118
-47
lines changed

12 files changed

+118
-47
lines changed

.github/workflows/smoke-tests.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ jobs:
5050
export_to_environment: true
5151
secrets: |-
5252
EC_API_KEY:elastic-observability/elastic-cloud-observability-team-pro-api-key
53-
- run: make smoketest/run TEST_DIR=./tf
53+
- run: make smoketest/run
5454
- if: always()
5555
name: Tear down
56-
run: make smoketest/all/cleanup TEST_DIR=./tf
56+
run: make smoketest/cleanup
5757

5858
- if: always()
5959
uses: elastic/oblt-actions/slack/[email protected]

.gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,4 @@ dist/
3131
.aws-*/
3232
*arn-file.md
3333

34-
tf/*.zip
35-
3634
build

Makefile

+5-21
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,13 @@ check-notice:
6262
##############################################################################
6363

6464
SMOKETEST_VERSIONS ?= latest
65-
SMOKETEST_DIRS = $$(find ./tf -mindepth 0 -maxdepth 0 -type d)
66-
67-
.PHONY: smoketest/discover
68-
smoketest/discover:
69-
@echo "$(SMOKETEST_DIRS)"
7065

7166
.PHONY: smoketest/run
7267
smoketest/run: zip
73-
@ for version in $(shell echo $(SMOKETEST_VERSIONS) | tr ',' ' '); do \
74-
echo "-> Running $(TEST_DIR) smoke tests for version $${version}..."; \
75-
cd $(TEST_DIR) && ./test.sh $${version}; \
76-
done
68+
@echo "-> Running smoke tests for version $${version}..."
69+
cd testing/smoketest && ./test.sh $${version}
7770

7871
.PHONY: smoketest/cleanup
79-
smoketest/cleanup:
80-
@ cd $(TEST_DIR); \
81-
if [ -f "./cleanup.sh" ]; then \
82-
./cleanup.sh; \
83-
fi
84-
85-
.PHONY: smoketest/all
86-
smoketest/all/cleanup:
87-
@ for test_dir in $(SMOKETEST_DIRS); do \
88-
echo "-> Cleanup $${test_dir} smoke tests..."; \
89-
$(MAKE) smoketest/cleanup TEST_DIR=$${test_dir}; \
90-
done
72+
smoketest/cleanup: zip
73+
@echo "-> Running cleanup"
74+
cd testing/smoketest && ./cleanup.sh

tf/.gitignore renamed to testing/smoketest/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ response.json
99
tf.log
1010

1111
builds/
12+
*.zip

tf/cleanup.sh renamed to testing/smoketest/cleanup.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ export TF_IN_AUTOMATION=1
66
export TF_CLI_ARGS=-no-color
77

88
echo "-> Tearing down the underlying infrastructure..."
9-
terraform destroy -auto-approve >> tf.log
9+
terraform destroy -auto-approve | tee -a tf.log
File renamed without changes.

testing/smoketest/function/main.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import json
2+
3+
coldstart = "true"
4+
def handler(event, context):
5+
global coldstart
6+
print("Example function log", context.aws_request_id)
7+
resp = {
8+
"statusCode": 200,
9+
"body": json.dumps("Hello from Lambda!"+context.aws_request_id),
10+
"headers": {
11+
"coldstart": coldstart,
12+
}
13+
}
14+
coldstart = "false"
15+
return resp
16+

tf/main.tf renamed to testing/smoketest/main.tf

+71-12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@ module "ec_deployment" {
2424
tags = module.tags.tags
2525
}
2626

27+
locals {
28+
runtimeVars = {
29+
"nodejs" = {
30+
"source_file" = "./function/index.js"
31+
"handler" = "index.handler"
32+
"runtime" = "nodejs18.x"
33+
"agent_layer" = "arn:aws:lambda:${var.aws_region}:267093732750:layer:elastic-apm-node-ver-4-3-0:1"
34+
"envvars" = {
35+
"NODE_OPTIONS" = "-r elastic-apm-node/start"
36+
}
37+
}
38+
"python" = {
39+
"source_file" = "./function/main.py"
40+
"handler" = "main.handler"
41+
"runtime" = "python3.9"
42+
"agent_layer" = "arn:aws:lambda:${var.aws_region}:267093732750:layer:elastic-apm-python-ver-6-22-3:1"
43+
"envvars" = {
44+
"AWS_LAMBDA_EXEC_WRAPPER" = "/opt/python/bin/elasticapm-lambda"
45+
}
46+
}
47+
}
48+
}
49+
50+
2751
data "aws_iam_policy_document" "assume_role" {
2852
statement {
2953
effect = "Allow"
@@ -44,35 +68,71 @@ resource "aws_iam_role" "lambda" {
4468

4569
data "archive_file" "lambda" {
4670
type = "zip"
47-
source_file = "../testdata/function/index.js"
71+
source_file = local.runtimeVars[var.function_runtime]["source_file"]
4872
output_path = "lambda_function_payload.zip"
4973
}
5074

5175
resource "aws_lambda_function" "test_lambda" {
5276
filename = "lambda_function_payload.zip"
5377
function_name = "${var.user_name}-smoke-testing-test"
5478
role = aws_iam_role.lambda.arn
55-
handler = "index.handler"
79+
handler = local.runtimeVars[var.function_runtime]["handler"]
5680

57-
source_code_hash = data.archive_file.lambda.output_base64sha256
81+
runtime = local.runtimeVars[var.function_runtime]["runtime"]
5882

59-
runtime = "nodejs16.x"
83+
source_code_hash = data.archive_file.lambda.output_base64sha256
6084

6185
layers = [
6286
aws_lambda_layer_version.lambda_layer.arn,
63-
"arn:aws:lambda:${var.aws_region}:267093732750:layer:elastic-apm-node-ver-4-3-0:1",
87+
local.runtimeVars[var.function_runtime]["agent_layer"]
6488
]
6589

6690
environment {
67-
variables = {
68-
NODE_OPTIONS = "-r elastic-apm-node/start"
91+
variables = merge({
6992
ELASTIC_APM_LOG_LEVEL = var.log_level
7093
ELASTIC_APM_LAMBDA_APM_SERVER = module.ec_deployment.apm_url
7194
ELASTIC_APM_SECRETS_MANAGER_SECRET_TOKEN_ID = aws_secretsmanager_secret.apm_secret_token.id
72-
}
95+
}, local.runtimeVars[var.function_runtime]["envvars"])
96+
}
97+
98+
depends_on = [
99+
aws_iam_role_policy_attachment.lambda_logs,
100+
aws_iam_role_policy_attachment.secrets_manager_elastic_apm_policy_attach,
101+
aws_cloudwatch_log_group.example,
102+
]
103+
}
104+
105+
resource "aws_cloudwatch_log_group" "example" {
106+
name = "/aws/lambda/${var.user_name}-smoke-testing-test"
107+
retention_in_days = 1
108+
}
109+
110+
data "aws_iam_policy_document" "lambda_logging" {
111+
statement {
112+
effect = "Allow"
113+
114+
actions = [
115+
"logs:CreateLogGroup",
116+
"logs:CreateLogStream",
117+
"logs:PutLogEvents",
118+
]
119+
120+
resources = ["arn:aws:logs:*:*:*"]
73121
}
74122
}
75123

124+
resource "aws_iam_policy" "lambda_logging" {
125+
name = "smoketest_extension_lambda_logging"
126+
path = "/"
127+
description = "IAM policy for logging during smoketest for apm aws lambda extension"
128+
policy = data.aws_iam_policy_document.lambda_logging.json
129+
}
130+
131+
resource "aws_iam_role_policy_attachment" "lambda_logs" {
132+
role = aws_iam_role.lambda.name
133+
policy_arn = aws_iam_policy.lambda_logging.arn
134+
}
135+
76136
resource "aws_secretsmanager_secret" "apm_secret_token" {
77137
name_prefix = "apm-aws-lambda-smoke-testing-secret"
78138
recovery_window_in_days = 0
@@ -103,13 +163,12 @@ resource "aws_iam_role_policy_attachment" "secrets_manager_elastic_apm_policy_at
103163
}
104164

105165
locals {
106-
zip_files = tolist(fileset("../dist/", "*-linux-amd64.zip"))
166+
zip_files = tolist(fileset("../../dist/", "*-linux-amd64.zip"))
107167
}
108168

109169
resource "aws_lambda_layer_version" "lambda_layer" {
110-
filename = "../dist/${local.zip_files[0]}"
170+
filename = "../../dist/${local.zip_files[0]}"
111171
layer_name = "apm-aws-lambda-smoke-testing-lambda_layer_name"
112172

113-
description = "AWS Lambda Extension Layer for Elastic APM - smoke testing"
114-
compatible_runtimes = ["nodejs16.x"]
173+
description = "AWS Lambda Extension Layer for Elastic APM - smoke testing"
115174
}

tf/output.tf renamed to testing/smoketest/output.tf

+4
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ output "aws_region" {
2323
output "user_name" {
2424
value = var.user_name
2525
}
26+
27+
output "agent_name" {
28+
value = var.function_runtime
29+
}
File renamed without changes.

tf/test.sh renamed to testing/smoketest/test.sh

+11-8
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ export TF_CLI_ARGS=-no-color
88
cleanup() {
99
if [ "$SKIP_DESTROY" != "1" ]; then
1010
echo "-> Tearing down the underlying infrastructure..."
11-
terraform destroy -auto-approve >> tf.log
11+
terraform destroy -auto-approve | tee -a tf.log
1212
fi
1313
}
1414

1515
trap "cleanup" EXIT
1616

17+
18+
1719
echo "-> Creating the underlying infrastructure..."
1820
terraform init | tee tf.log
19-
terraform apply -auto-approve | tee -a tf.log
21+
terraform apply -auto-approve -var "function_runtime=${FUNCTION_RUNTIME:-nodejs}" | tee -a tf.log
2022

2123
# https://github.com/hashicorp/setup-terraform/issues/167#issuecomment-1090760365
2224
TERRAFORM_WRAPPER=terraform-bin
@@ -36,21 +38,22 @@ echo "-> Waiting for the agent documents to be indexed in Elasticsearch..."
3638
ES_HOST=$($TERRAFORM_WRAPPER output -raw elasticsearch_url)
3739
ES_USER=$($TERRAFORM_WRAPPER output -raw elasticsearch_username)
3840
ES_PASS=$($TERRAFORM_WRAPPER output -raw elasticsearch_password)
41+
AGENT_NAME=$($TERRAFORM_WRAPPER output -raw agent_name)
3942

4043
hits=0
4144
attempts=0
4245
while [ ${hits} -eq 0 ]; do
4346
# Check that ES has transaction documents on the GET endpoint.
44-
resp=$(curl -s -H 'Content-Type: application/json' -u ${ES_USER}:${ES_PASS} "${ES_HOST}/traces-apm-*/_search" -d '{
45-
"query": {
46-
"match": {
47-
"agent.name": "nodejs"
47+
resp=$(curl -s -H 'Content-Type: application/json' -u ${ES_USER}:${ES_PASS} "${ES_HOST}/traces-apm-*/_search" -d "{
48+
\"query\": {
49+
\"match\": {
50+
\"agent.name\": \"${AGENT_NAME}\"
4851
}
4952
}
50-
}')
53+
}")
5154
hits=$(echo ${resp} | jq '.hits.total.value')
5255
if [[ ${attempts} -ge 30 ]]; then
53-
echo "-> Didn't find any traces for the NodeJS agents after ${attempts} attempts"
56+
echo "-> Didn't find any traces for the ${AGENT_NAME} agents after ${attempts} attempts"
5457
exit 1
5558
fi
5659
let "attempts+=1"

tf/variables.tf renamed to testing/smoketest/variables.tf

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ variable "user_name" {
99
type = string
1010
}
1111

12+
variable "function_runtime" {
13+
description = "function runtime and apm agent "
14+
type = string
15+
default = "nodejs"
16+
}
17+
1218
variable "log_level" {
1319
type = string
1420
description = "lambda extension log level"
@@ -24,7 +30,7 @@ variable "ess_region" {
2430
variable "ess_deployment_template" {
2531
type = string
2632
description = "Elastic Cloud deployment template"
27-
default = "gcp-compute-optimized-v3"
33+
default = "gcp-vector-search-optimized"
2834
}
2935

3036
variable "ess_version" {

0 commit comments

Comments
 (0)