|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2025-present Datadog, Inc. |
| 5 | + |
| 6 | +package test |
| 7 | + |
| 8 | +import ( |
| 9 | + "encoding/json" |
| 10 | + "log" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/aws/aws-sdk-go-v2/service/ecs/types" |
| 14 | + "github.com/gruntwork-io/terratest/modules/terraform" |
| 15 | +) |
| 16 | + |
| 17 | +// TestRoleParsingWithPath tests that the module correctly parses role names from ARNs with paths |
| 18 | +func (s *ECSFargateSuite) TestRoleParsingWithPath() { |
| 19 | + log.Println("TestRoleParsingWithPath: Running test...") |
| 20 | + |
| 21 | + var containers []types.ContainerDefinition |
| 22 | + task := terraform.OutputMap(s.T(), s.terraformOptions, "role-parsing-with-path") |
| 23 | + |
| 24 | + s.Equal(s.testPrefix+"-role-parsing-with-path", task["family"], "Unexpected task family name") |
| 25 | + |
| 26 | + err := json.Unmarshal([]byte(task["container_definitions"]), &containers) |
| 27 | + s.NoError(err, "Failed to parse container definitions") |
| 28 | + |
| 29 | + s.NotEmpty(task["arn"], "Task definition ARN should not be empty") |
| 30 | + s.NotEmpty(task["revision"], "Task definition revision should not be empty") |
| 31 | + |
| 32 | + taskRoleArn := task["task_role_arn"] |
| 33 | + s.NotEmpty(taskRoleArn, "Task role ARN should not be empty") |
| 34 | + s.Contains(taskRoleArn, "/terraform-test/", "Task role ARN should contain the path '/test-path/'") |
| 35 | + s.Contains(taskRoleArn, s.testPrefix+"-task-role-with-path", "Task role ARN should contain the expected role name") |
| 36 | + |
| 37 | + executionRoleArn := task["execution_role_arn"] |
| 38 | + s.NotEmpty(executionRoleArn, "Execution role ARN should not be empty") |
| 39 | + s.Contains(executionRoleArn, "/terraform-test/", "Execution role ARN should contain the path '/terraform-test/'") |
| 40 | + s.Contains(executionRoleArn, s.testPrefix+"-execution-role-with-path", "Execution role ARN should contain the expected role name") |
| 41 | +} |
| 42 | + |
| 43 | +// TestRoleParsingWithoutPath tests that the module correctly parses role names from ARNs without paths |
| 44 | +func (s *ECSFargateSuite) TestRoleParsingWithoutPath() { |
| 45 | + log.Println("TestRoleParsingWithoutPath: Running test...") |
| 46 | + |
| 47 | + var containers []types.ContainerDefinition |
| 48 | + task := terraform.OutputMap(s.T(), s.terraformOptions, "role-parsing-without-path") |
| 49 | + |
| 50 | + s.Equal(s.testPrefix+"-role-parsing-without-path", task["family"], "Unexpected task family name") |
| 51 | + |
| 52 | + err := json.Unmarshal([]byte(task["container_definitions"]), &containers) |
| 53 | + s.NoError(err, "Failed to parse container definitions") |
| 54 | + |
| 55 | + s.NotEmpty(task["arn"], "Task definition ARN should not be empty") |
| 56 | + s.NotEmpty(task["revision"], "Task definition revision should not be empty") |
| 57 | + |
| 58 | + taskRoleArn := task["task_role_arn"] |
| 59 | + s.NotEmpty(taskRoleArn, "Task role ARN should not be empty") |
| 60 | + s.Contains(taskRoleArn, s.testPrefix+"-task-role-without-path", "Task role ARN should contain the expected role name") |
| 61 | + |
| 62 | + roleArnParts := strings.Split(taskRoleArn, "/") |
| 63 | + s.Equal(2, len(roleArnParts), "Role ARN without path should have exactly 2 parts when split by '/'") |
| 64 | + s.Contains(roleArnParts[1], s.testPrefix+"-task-role-without-path", "Role name should be the second part after splitting by '/'") |
| 65 | + |
| 66 | + executionRoleArn := task["execution_role_arn"] |
| 67 | + s.NotEmpty(executionRoleArn, "Execution role ARN should not be empty") |
| 68 | + s.Contains(executionRoleArn, s.testPrefix+"-execution-role-without-path", "Execution role ARN should contain the expected role name") |
| 69 | + |
| 70 | + execRoleArnParts := strings.Split(executionRoleArn, "/") |
| 71 | + s.Equal(2, len(execRoleArnParts), "Execution role ARN without path should have exactly 2 parts when split by '/'") |
| 72 | + s.Contains(execRoleArnParts[1], s.testPrefix+"-execution-role-without-path", "Execution role name should be the second part after splitting by '/'") |
| 73 | +} |
0 commit comments