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

feat: add smoke testing #296

Merged
merged 14 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions testdata/function/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exports.handler = async (event) => {
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};

4 changes: 4 additions & 0 deletions tf/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.terraform/

*.tfstate
*.tfstate.*
52 changes: 52 additions & 0 deletions tf/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
provider "aws" {
region = var.aws_region
}

module "ec_deployment" {
source = "github.com/elastic/apm-server//testing/infra/terraform/modules/ec_deployment?depth=1"
deployment_name_prefix = "apm-aws-lambda-smoke-testing"
integrations_server = true
apm_server_expvar = false
apm_server_pprof = false
region = var.ess_region
deployment_template = var.ess_deployment_template
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, we'll end up using the latest SNAPSHOT versions, they may be broken and cause the smoke tests to fail. If we want to avoid that, then we need to provide an expression for a released version, such as:

Suggested change
deployment_template = var.ess_deployment_template
deployment_template = var.ess_deployment_template
stack_version = "\\d.\\d.\\d?(\\d)$" // use the latest released version.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++ I've opened #307 to track this

}

module "lambda_function" {
source = "terraform-aws-modules/lambda/aws"

function_name = "smoke-testing-test"
description = "Example Lambda function for smoke testing"
handler = "index.handler"
runtime = "nodejs16.x"

source_path = "../testdata/function/"

layers = [
module.lambda_layer_local.lambda_layer_arn,
"arn:aws:lambda:${var.aws_region}:267093732750:layer:elastic-apm-node-ver-3-38-0:1",
]

environment_variables = {
NODE_OPTIONS = "-r elastic-apm-node/start"
ELASTIC_APM_LOG_LEVEL = var.log_level
ELASTIC_APM_LAMBDA_APM_SERVER = module.ec_deployment.apm_url
ELASTIC_APM_SECRET_TOKEN = module.ec_deployment.apm_secret_token
}

tags = {
Name = "my-lambda"
}
}

module "lambda_layer_local" {
source = "terraform-aws-modules/lambda/aws"

create_layer = true

layer_name = "apm-lambda-extension-smoke-testing"
description = "AWS Lambda Extension Layer for Elastic APM - smoke testing"
compatible_runtimes = ["nodejs16.x"]

source_path = "../bin/"
}
16 changes: 16 additions & 0 deletions tf/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
output "elasticsearch_url" {
value = module.ec_deployment.elasticsearch_url
description = "The secure Elasticsearch URL"
}

output "elasticsearch_username" {
value = module.ec_deployment.elasticsearch_username
description = "The Elasticsearch username"
sensitive = true
}

output "elasticsearch_password" {
value = module.ec_deployment.elasticsearch_password
description = "The Elasticsearch password"
sensitive = true
}
12 changes: 12 additions & 0 deletions tf/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">=4.28.0"
}
ec = {
source = "elastic/ec"
version = ">=0.4.1"
}
}
}
48 changes: 48 additions & 0 deletions tf/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

set -e

export TF_IN_AUTOMATION=1
export TF_CLI_ARGS=-no-color

cleanup() {
terraform destroy -auto-approve >> tf.log
}

trap "cleanup" EXIT

echo "-> Creating the underlying infrastructure..."
terraform init | tee tf.log
terraform apply -auto-approve | tee -a tf.log

echo "-> Calling the lambda function..."
aws lambda invoke --region=eu-central-1 --function-name smoke-testing-test response.json
aws lambda invoke --region=eu-central-1 --function-name smoke-testing-test response.json

echo "-> Waiting for the agent documents to be indexed in Elasticsearch..."

ES_HOST=$(terraform output -raw elasticsearch_url)
ES_USER=$(terraform output -raw elasticsearch_username)
ES_PASS=$(terraform output -raw elasticsearch_password)

hits=0
attempts=0
while [ ${hits} -eq 0 ]; do
# Check that ES has transaction documents on the GET endpoint.
resp=$(curl -s -H 'Content-Type: application/json' -u ${ES_USER}:${ES_PASS} "${ES_HOST}/traces-apm-*/_search" -d '{
"query": {
"match": {
"agent.name": "nodejs"
}
}
}')
hits=$(echo ${resp} | jq '.hits.total.value')
if [[ ${attempts} -ge 30 ]]; then
echo "-> Didn't find any traces for the NodeJS agents after ${attempts} attempts"
exit 1
fi
let "attempts+=1"
sleep 1
done

echo "-> Smoke tests passed!"
23 changes: 23 additions & 0 deletions tf/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
variable "aws_region" {
type = string
description = "aws region"
default = "eu-central-1"
}

variable "log_level" {
type = string
description = "lambda extension log level"
default = "trace"
}

variable "ess_region" {
type = string
description = "ess region"
default = "gcp-us-west2"
}

variable "ess_deployment_template" {
type = string
description = "Elastic Cloud deployment template"
default = "gcp-compute-optimized"
}