Skip to content

Commit 04f7d2f

Browse files
committed
feat: integration tests with native framework
1 parent 4493fde commit 04f7d2f

File tree

5 files changed

+222
-72
lines changed

5 files changed

+222
-72
lines changed

tests/cpu-compatibility.tftest.hcl

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
variables {
2+
vpc_id = "vpc-12345678"
3+
subnet_ids = ["subnet-12345678", "subnet-87654321"]
4+
stage = "test"
5+
namespace = "mp"
6+
name = "ssm-agent"
7+
region = "us-east-1"
8+
availability_zones = ["us-east-1a"]
9+
nat_gateway_enabled = true
10+
ipv6_enabled = true
11+
}
12+
13+
### TESTING INSTANCE and ARCHITECTURE COMPATIBILITY ###
14+
# https://docs.aws.amazon.com/ec2/latest/instancetypes/instance-type-names.html
15+
# https://aws.amazon.com/ec2/instance-types/
16+
17+
# Test valid x86_64 instance type
18+
run "valid_x86_64_instance" {
19+
command = plan
20+
21+
variables {
22+
instance_type = "t3.micro"
23+
architecture = "x86_64"
24+
}
25+
26+
assert {
27+
condition = local.is_instance_compatible
28+
error_message = "Expected instance type t3.micro to be compatible with x86_64 architecture"
29+
}
30+
}
31+
32+
# Test valid arm64 instance type
33+
run "valid_arm64_instance" {
34+
command = plan
35+
36+
variables {
37+
instance_type = "t4g.micro"
38+
architecture = "arm64"
39+
}
40+
41+
assert {
42+
condition = local.is_instance_compatible
43+
error_message = "Expected instance type t4g.micro to be compatible with arm64 architecture"
44+
}
45+
}
46+
47+
# Test invalid x86_64 instance type (using arm64 instance type)
48+
run "invalid_x86_64_instance" {
49+
command = plan
50+
51+
variables {
52+
instance_type = "t4g.micro"
53+
architecture = "x86_64"
54+
}
55+
56+
expect_failures = [
57+
null_resource.validate_instance_type
58+
]
59+
}
60+
61+
# Test invalid arm64 instance type (using x86_64 instance type)
62+
run "invalid_arm64_instance" {
63+
command = plan
64+
65+
variables {
66+
instance_type = "t3.micro"
67+
architecture = "arm64"
68+
}
69+
70+
expect_failures = [
71+
null_resource.validate_instance_type
72+
]
73+
}
74+
75+
# Test edge case, where the 'g' is defined as the instance family rather than the processor family
76+
# It has 'g' in the name, but it's still an x86_64 instance type because the 'g' is the instance family
77+
run "graphics_instance_arm_incompatiblity_edge_case" {
78+
command = plan
79+
80+
variables {
81+
instance_type = "g3s.xlarge"
82+
architecture = "arm64"
83+
}
84+
85+
expect_failures = [
86+
null_resource.validate_instance_type
87+
]
88+
}
89+
90+
# Test edge case, where the 'g' is defined as the instance family rather than the processor family
91+
# It has 'g' in the name, but it still is compatible with x86_64 since the 'g' is the instance family
92+
run "graphics_instance_x86_compatibility_edge_case" {
93+
command = plan
94+
95+
variables {
96+
instance_type = "g4dn.xlarge"
97+
architecture = "x86_64"
98+
}
99+
100+
assert {
101+
condition = local.is_instance_compatible
102+
error_message = "Expected instance type g3s.xlarge to be compatible with x86_64 architecture"
103+
}
104+
}

tests/main.tftest.hcl

+64-72
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,96 @@
1-
variables {
2-
vpc_id = "vpc-12345678"
3-
subnet_ids = ["subnet-12345678", "subnet-87654321"]
4-
stage = "test"
5-
namespace = "mp"
6-
name = "ssm-agent"
7-
region = "us-east-1"
8-
availability_zones = ["us-east-1a"]
9-
nat_gateway_enabled = true
10-
ipv6_enabled = true
11-
}
1+
### Integration Tests for the SSM Agent Module
2+
### This test suite will create the SSM Agent module
3+
### and validate the resources created by the module,
4+
### then destroy it.
5+
6+
### `/test-harness/` module is used as a helper to validate resources that aren't in the Terraform state, for example the EC2 instances created from the ASG.
127

13-
### TESTING INSTANCE and ARCHITECTURE COMPATIBILITY ###
14-
# https://docs.aws.amazon.com/ec2/latest/instancetypes/instance-type-names.html
15-
# https://aws.amazon.com/ec2/instance-types/
8+
run "setup" {
9+
module {
10+
source = "./tests/setup"
11+
}
12+
}
1613

17-
# Test valid x86_64 instance type
18-
run "valid_x86_64_instance" {
19-
command = plan
14+
run "create_ssm_agent" {
15+
command = apply
2016

2117
variables {
22-
instance_type = "t3.micro"
23-
architecture = "x86_64"
18+
namespace = "mp"
19+
stage = "terraform-test${run.setup.random_number}"
2420
}
2521

26-
assert {
27-
condition = local.is_instance_compatible
28-
error_message = "Expected instance type t3.micro to be compatible with x86_64 architecture"
22+
module {
23+
source = "./examples/complete"
2924
}
30-
}
3125

32-
# Test valid arm64 instance type
33-
run "valid_arm64_instance" {
34-
command = plan
35-
36-
variables {
37-
instance_type = "t4g.micro"
38-
architecture = "arm64"
26+
assert {
27+
condition = module.ssm_agent.security_group_id != ""
28+
error_message = "The ID of the SSM Agent Security Group is empty, possibly not created."
3929
}
4030

4131
assert {
42-
condition = local.is_instance_compatible
43-
error_message = "Expected instance type t4g.micro to be compatible with arm64 architecture"
32+
condition = module.ssm_agent.launch_template_id != ""
33+
error_message = "The ID of the SSM Agent Launch Template is empty, possibly not created."
4434
}
45-
}
4635

47-
# Test invalid x86_64 instance type (using arm64 instance type)
48-
run "invalid_x86_64_instance" {
49-
command = plan
36+
assert {
37+
condition = module.ssm_agent.autoscaling_group_id != ""
38+
error_message = "The ID of the SSM Agent Autoscaling Group is empty, possibly not created."
39+
}
5040

51-
variables {
52-
instance_type = "t4g.micro"
53-
architecture = "x86_64"
41+
assert {
42+
condition = module.ssm_agent.role_id != ""
43+
error_message = "The ID of the SSM Agent Role is empty, possibly not created."
5444
}
5545

56-
expect_failures = [
57-
null_resource.validate_instance_type
58-
]
5946
}
6047

61-
# Test invalid arm64 instance type (using x86_64 instance type)
62-
run "invalid_arm64_instance" {
63-
command = plan
48+
run "validate_ssm_agent_data" {
49+
module {
50+
source = "./tests/test-harness"
51+
}
6452

6553
variables {
66-
instance_type = "t3.micro"
67-
architecture = "arm64"
54+
# These variables are based on using the values from `./examples/complete` module since we are using that for the integration tests.
55+
instance_name = "mp-terraform-test${run.setup.random_number}"
56+
ssm_document_name_from_test = "SSM-SessionManagerRunShell"
57+
iam_role_name_from_test = run.create_ssm_agent.role_id
6858
}
6959

70-
expect_failures = [
71-
null_resource.validate_instance_type
72-
]
73-
}
74-
75-
# Test edge case, where the 'g' is defined as the instance family rather than the processor family
76-
# It has 'g' in the name, but it's still an x86_64 instance type because the 'g' is the instance family
77-
run "graphics_instance_arm_incompatiblity_edge_case" {
78-
command = plan
60+
# The EC2 Instance is not directly created since it is managed by the ASG + Launch Template.
61+
# Check that the EC2 instance is actually spun up after this integration test.
62+
assert {
63+
condition = data.aws_instance.from_test.arn != ""
64+
error_message = "The SSM Agent EC2 instance does not exist."
65+
}
66+
assert {
67+
condition = contains(["running", "pending"], data.aws_instance.from_test.instance_state)
68+
error_message = "The SSM Agent EC2 instance is not running or pending."
69+
}
7970

80-
variables {
81-
instance_type = "g3s.xlarge"
82-
architecture = "arm64"
71+
assert {
72+
condition = tolist(data.aws_instance.from_test.root_block_device)[0].encrypted == true
73+
error_message = "The root block device of the SSM Agent EC2 instance is not encrypted."
8374
}
8475

85-
expect_failures = [
86-
null_resource.validate_instance_type
87-
]
88-
}
8976

90-
# Test edge case, where the 'g' is defined as the instance family rather than the processor family
91-
# It has 'g' in the name, but it still is compatible with x86_64 since the 'g' is the instance family
92-
run "graphics_instance_x86_compatibility_edge_case" {
93-
command = plan
77+
assert {
78+
condition = data.aws_ssm_document.from_test.content != ""
79+
error_message = "The created SSM document content is empty."
80+
}
9481

95-
variables {
96-
instance_type = "g4dn.xlarge"
97-
architecture = "x86_64"
82+
assert {
83+
condition = can(regex("\"Effect\"\\s*:\\s*\"Allow\"", data.aws_iam_role.from_test.assume_role_policy))
84+
error_message = "The created IAM role policy must contain Effect: Allow"
85+
}
86+
87+
assert {
88+
condition = can(regex("\"Service\"\\s*:\\s*\"ec2\\.amazonaws\\.com\"", data.aws_iam_role.from_test.assume_role_policy))
89+
error_message = "The created IAM role policy must contain Service: ec2.amazonaws.com"
9890
}
9991

10092
assert {
101-
condition = local.is_instance_compatible
102-
error_message = "Expected instance type g3s.xlarge to be compatible with x86_64 architecture"
93+
condition = can(regex("\"Action\"\\s*:\\s*\"sts:AssumeRole\"", data.aws_iam_role.from_test.assume_role_policy))
94+
error_message = "The created IAM role policy must contain Action: sts:AssumeRole"
10395
}
10496
}

tests/setup/main.tf

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
terraform {
2+
required_providers {
3+
random = {
4+
source = "hashicorp/random"
5+
version = "~> 3.0"
6+
}
7+
}
8+
}
9+
10+
resource "random_integer" "random_number" {
11+
min = 1
12+
max = 9999
13+
}
14+
15+
output "random_number" {
16+
value = random_integer.random_number.result
17+
description = "Random number between 1 and 9999"
18+
}

tests/test-harness/main.tf

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
terraform {
2+
required_version = ">= 1.0"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 5.0"
8+
}
9+
}
10+
}
11+
12+
data "aws_ssm_document" "from_test" {
13+
name = var.ssm_document_name_from_test
14+
}
15+
16+
data "aws_iam_role" "from_test" {
17+
name = var.iam_role_name_from_test
18+
}
19+
20+
data "aws_instance" "from_test" {
21+
filter {
22+
name = "tag:Name"
23+
values = [var.instance_name]
24+
}
25+
}

tests/test-harness/variable.tf

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
variable "ssm_document_name_from_test" {
2+
type = string
3+
}
4+
5+
variable "iam_role_name_from_test" {
6+
type = string
7+
}
8+
9+
variable "instance_name" {
10+
type = string
11+
}

0 commit comments

Comments
 (0)