Skip to content

Commit

Permalink
Add end-to-end tests (#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwilkie committed Jul 12, 2024
1 parent 67fc2bf commit 57495c0
Show file tree
Hide file tree
Showing 12 changed files with 365 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ examples.txt
node_modules
nohup.out
.raw
*.zip
1 change: 1 addition & 0 deletions components/testing/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.tool-versions
Dockerfile
infrastructure
**/node_modules
**/nohup.out
**/.prettierrc
62 changes: 62 additions & 0 deletions components/testing/infrastructure/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions components/testing/infrastructure/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM public.ecr.aws/docker/library/alpine:latest

RUN apk update && apk upgrade && apk add --update --no-cache jq sipp curl

COPY --link tests tests

WORKDIR /tests
14 changes: 14 additions & 0 deletions components/testing/infrastructure/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Somleng E2E Integration Tests

This directory contains infrastructure and end-to-end tests for testing Somleng.

## Setup

1. Run `terraform apply`
2. Open a SSM session to `somleng-switch-testing`
3. Run `sudo docker ps`
4. Run `sudo docker exec -it <docker-id> /bin/sh`

## Cleanup

1. Run `terraform destroy`
96 changes: 96 additions & 0 deletions components/testing/infrastructure/ec2.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
data "aws_ssm_parameter" "arm64_ami" {
name = "/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-arm64"
}

data "archive_file" "test_files" {
type = "zip"
source_dir = "${path.module}/tests"
output_path = "${path.module}/tests_files.zip"
}

resource "aws_security_group" "this" {
name = "somleng-switch-testing"
vpc_id = data.terraform_remote_state.core_infrastructure.outputs.vpc.vpc_id
}

resource "aws_security_group_rule" "egress" {
type = "egress"
to_port = 0
protocol = "-1"
from_port = 0
security_group_id = aws_security_group.this.id
cidr_blocks = ["0.0.0.0/0"]
}

resource "aws_instance" "this" {
ami = data.aws_ssm_parameter.arm64_ami.value
instance_type = "t4g.small"
security_groups = [
aws_security_group.this.id,
]
subnet_id = element(data.terraform_remote_state.core_infrastructure.outputs.vpc.public_subnets, 0)
associate_public_ip_address = true
iam_instance_profile = aws_iam_instance_profile.this.id

root_block_device {
volume_size = 100
volume_type = "gp3"
}

tags = {
Name = "somleng-switch-testing"
}

user_data = base64encode(join("\n", [
"#cloud-config",
yamlencode({
# https://cloudinit.readthedocs.io/en/latest/topics/modules.html
write_files : [
{
path : "/opt/testing/setup.sh",
content : file("${path.module}/setup.sh"),
permissions : "0755",
},
{
path : "/opt/testing/Dockerfile",
content : file("${path.module}/Dockerfile"),
},
{
encoding : "b64",
path : "/opt/testing/test_files.zip",
content : filebase64(data.archive_file.test_files.output_path),
},
],
runcmd : [
["/opt/testing/setup.sh"]
],
})
]))
}

resource "aws_iam_role" "this" {
name = "somleng-switch-testing"

assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ec2.amazonaws.com"
}
}
]
})
}

resource "aws_iam_instance_profile" "this" {
name = aws_iam_role.this.name
role = aws_iam_role.this.name
}

resource "aws_iam_role_policy_attachment" "ssm" {
role = aws_iam_role.this.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM"
}
15 changes: 15 additions & 0 deletions components/testing/infrastructure/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

# Get this data from inside EC2 instance
# TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
# curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/user-data

set -e

dnf update
dnf install -y docker
service docker start

unzip /opt/testing/test_files.zip -d /opt/testing/tests
docker build -t "testing:latest" /opt/testing
docker run --rm -d testing:latest tail -f /dev/null
22 changes: 22 additions & 0 deletions components/testing/infrastructure/terraform.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
terraform {
backend "s3" {
bucket = "infrastructure.somleng.org"
key = "somleng_switch_testing.tfstate"
encrypt = true
region = "ap-southeast-1"
}
}

provider "aws" {
region = var.aws_region
}

data "terraform_remote_state" "core_infrastructure" {
backend = "s3"

config = {
bucket = "infrastructure.somleng.org"
key = "core.tfstate"
region = var.aws_region
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

set -e

TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
AWS_PUBLIC_IP=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/public-ipv4)
DESTINATION_NUMBER="1234"
HOSTNAME=$(cat /etc/hostname)

read -p "1. Add (or modify) a SIP trunk on Somleng with the following Source IP for Inbound Dialing: $AWS_PUBLIC_IP. Press any key when done."
read -p "2. Configure the number: $DESTINATION_NUMBER on Somleng: Press any key when done."
read -p "3. Start TCP dump. In another terminal run the following: sudo docker run -it --rm --net container:$HOSTNAME nicolaka/netshoot followed by tcpdump -Xvv -i eth0 -s0 -w capture.pcap. Press any key when done."

log_file="uac_*_messages.log"
rm -f $log_file

sipp -sf scenarios/uac.xml 15.197.218.231:5080 -key username "+855715100850" -s 1234 -m 1 -trace_msg > /dev/null
119 changes: 119 additions & 0 deletions components/testing/infrastructure/tests/scenarios/uac.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE scenario SYSTEM "sipp.dtd">

<!-- This program is free software; you can redistribute it and/or -->
<!-- modify it under the terms of the GNU General Public License as -->
<!-- published by the Free Software Foundation; either version 2 of the -->
<!-- License, or (at your option) any later version. -->
<!-- -->
<!-- This program is distributed in the hope that it will be useful, -->
<!-- but WITHOUT ANY WARRANTY; without even the implied warranty of -->
<!-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -->
<!-- GNU General Public License for more details. -->
<!-- -->
<!-- You should have received a copy of the GNU General Public License -->
<!-- along with this program; if not, write to the -->
<!-- Free Software Foundation, Inc., -->
<!-- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -->
<!-- -->
<!-- Sipp default 'uac' scenario. -->
<!-- -->

<scenario name="Basic Sipstone UAC">
<!-- In client mode (sipp placing calls), the Call-ID MUST be -->
<!-- generated by sipp. To do so, use [call_id] keyword. -->
<send retrans="500">
<![CDATA[
INVITE sip:[service]@[remote_ip]:[remote_port] SIP/2.0
Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
From: sipp <sip:[username]@[local_ip]:[local_port]>;tag=[pid]SIPpTag00[call_number]
To: [service] <sip:[service]@[remote_ip]:[remote_port]>
Call-ID: [call_id]
CSeq: 1 INVITE
Contact: sip:[username]@[local_ip]:[local_port]
Max-Forwards: 70
Subject: Performance Test
Content-Type: application/sdp
Content-Length: [len]
v=0
o=user1 53655765 2353687637 IN IP[local_ip_type] [local_ip]
s=-
c=IN IP[media_ip_type] [media_ip]
t=0 0
m=audio [media_port] RTP/AVP 0
a=rtpmap:0 PCMU/8000
]]>
</send>

<recv response="100"
optional="true">
</recv>

<recv response="180" optional="true">
</recv>

<recv response="183" optional="true">
</recv>

<!-- By adding rrs="true" (Record Route Sets), the route sets -->
<!-- are saved and used for following messages sent. Useful to test -->
<!-- against stateful SIP proxies/B2BUAs. -->
<recv response="200" rtd="true" rrs="true">
</recv>

<!-- Packet lost can be simulated in any send/recv message by -->
<!-- by adding the 'lost = "10"'. Value can be [1-100] percent. -->
<send>
<![CDATA[
ACK sip:[service]@[remote_ip]:[remote_port] SIP/2.0
Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
From: sipp <sip:[username]@[local_ip]:[local_port]>;tag=[pid]SIPpTag00[call_number]
To: [service] <sip:[service]@[remote_ip]:[remote_port]>[peer_tag_param]
Call-ID: [call_id]
CSeq: 1 ACK
Contact: sip:[username]@[local_ip]:[local_port]
Max-Forwards: 70
Subject: Performance Test
Content-Length: 0
[routes]
]]>
</send>

<!-- This delay can be customized by the -d command-line option -->
<!-- or by adding a 'milliseconds = "value"' option here. -->
<pause/>

<!-- The 'crlf' option inserts a blank line in the statistics report. -->
<send retrans="500">
<![CDATA[
BYE sip:[service]@[remote_ip]:[remote_port] SIP/2.0
Via: SIP/2.0/[transport] [local_ip]:[local_port];branch=[branch]
From: sipp <sip:[username]@[local_ip]:[local_port]>;tag=[pid]SIPpTag00[call_number]
To: [service] <sip:[service]@[remote_ip]:[remote_port]>[peer_tag_param]
Call-ID: [call_id]
CSeq: 2 BYE
Contact: sip:[username]@[local_ip]:[local_port]
Max-Forwards: 70
Subject: Performance Test
Content-Length: 0
[routes]
]]>
</send>

<recv response="200" crlf="true">
</recv>

<!-- definition of the response time repartition table (unit is ms) -->
<ResponseTimeRepartition value="10, 20, 30, 40, 50, 100, 150, 200"/>

<!-- definition of the call length repartition table (unit is ms) -->
<CallLengthRepartition value="10, 50, 100, 500, 1000, 5000, 10000"/>

</scenario>
3 changes: 3 additions & 0 deletions components/testing/infrastructure/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
variable "aws_region" {
default = "ap-southeast-1"
}
8 changes: 8 additions & 0 deletions components/testing/infrastructure/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
required_version = ">= 0.13"
}

0 comments on commit 57495c0

Please sign in to comment.