|
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. |
12 | 7 |
|
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 | +} |
16 | 13 |
|
17 |
| -# Test valid x86_64 instance type |
18 |
| -run "valid_x86_64_instance" { |
19 |
| - command = plan |
| 14 | +run "create_ssm_agent" { |
| 15 | + command = apply |
20 | 16 |
|
21 | 17 | variables {
|
22 |
| - instance_type = "t3.micro" |
23 |
| - architecture = "x86_64" |
| 18 | + namespace = "mp" |
| 19 | + stage = "terraform-test${run.setup.random_number}" |
24 | 20 | }
|
25 | 21 |
|
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" |
29 | 24 | }
|
30 |
| -} |
31 | 25 |
|
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." |
39 | 29 | }
|
40 | 30 |
|
41 | 31 | 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." |
44 | 34 | }
|
45 |
| -} |
46 | 35 |
|
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 | + } |
50 | 40 |
|
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." |
54 | 44 | }
|
55 | 45 |
|
56 |
| - expect_failures = [ |
57 |
| - null_resource.validate_instance_type |
58 |
| - ] |
59 | 46 | }
|
60 | 47 |
|
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 | + } |
64 | 52 |
|
65 | 53 | 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 |
68 | 58 | }
|
69 | 59 |
|
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 | + } |
79 | 70 |
|
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." |
83 | 74 | }
|
84 | 75 |
|
85 |
| - expect_failures = [ |
86 |
| - null_resource.validate_instance_type |
87 |
| - ] |
88 |
| -} |
89 | 76 |
|
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 | + } |
94 | 81 |
|
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" |
98 | 90 | }
|
99 | 91 |
|
100 | 92 | 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" |
103 | 95 | }
|
104 | 96 | }
|
0 commit comments