mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-06-13 23:36:45 +03:00
test(iam): add comprehensive ARN extraction tests
Add unit tests for ExtractRoleNameFromArn and ExtractRoleNameFromPrincipal to verify support for both AWS standard and legacy IAM role ARN formats: TestExtractRoleNameFromArn (14 test cases): - Legacy format without account ID: arn:aws:iam::role/RoleName - Standard format with account ID: arn:aws:iam::ACCOUNT:role/RoleName - With role paths: arn:aws:iam::role/Path/RoleName - Invalid ARNs and edge cases TestExtractRoleNameFromPrincipal (11 test cases): - STS assumed role format (legacy and standard) - IAM role format (legacy and standard) - With and without account ID - Invalid principals and edge cases All tests pass with 100% coverage for both functions.
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
package utils
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestExtractRoleNameFromArn(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
roleArn string
|
||||
expected string
|
||||
}{
|
||||
// Legacy format (without account ID)
|
||||
{
|
||||
name: "legacy_format_simple_role_name",
|
||||
roleArn: "arn:aws:iam::role/default",
|
||||
expected: "default",
|
||||
},
|
||||
{
|
||||
name: "legacy_format_custom_role_name",
|
||||
roleArn: "arn:aws:iam::role/MyRole",
|
||||
expected: "MyRole",
|
||||
},
|
||||
{
|
||||
name: "legacy_format_with_path",
|
||||
roleArn: "arn:aws:iam::role/Path/MyRole",
|
||||
expected: "Path/MyRole",
|
||||
},
|
||||
{
|
||||
name: "legacy_format_with_nested_path",
|
||||
roleArn: "arn:aws:iam::role/Division/Team/Role",
|
||||
expected: "Division/Team/Role",
|
||||
},
|
||||
// Standard AWS format (with account ID)
|
||||
{
|
||||
name: "standard_format_simple_role_name",
|
||||
roleArn: "arn:aws:iam::123456789012:role/default",
|
||||
expected: "default",
|
||||
},
|
||||
{
|
||||
name: "standard_format_custom_role_name",
|
||||
roleArn: "arn:aws:iam::999999999999:role/MyRole",
|
||||
expected: "MyRole",
|
||||
},
|
||||
{
|
||||
name: "standard_format_with_path",
|
||||
roleArn: "arn:aws:iam::123456789012:role/Path/MyRole",
|
||||
expected: "Path/MyRole",
|
||||
},
|
||||
{
|
||||
name: "standard_format_with_nested_path",
|
||||
roleArn: "arn:aws:iam::123456789012:role/Division/Team/Role",
|
||||
expected: "Division/Team/Role",
|
||||
},
|
||||
// Edge cases and invalid formats
|
||||
{
|
||||
name: "invalid_arn_missing_prefix",
|
||||
roleArn: "invalid-arn",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
name: "invalid_arn_incomplete",
|
||||
roleArn: "arn:aws:iam::",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
name: "invalid_arn_no_role_marker",
|
||||
roleArn: "arn:aws:iam::123456789012:user/username",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
name: "invalid_arn_wrong_service",
|
||||
roleArn: "arn:aws:sts::assumed-role/Role/Session",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
name: "empty_string",
|
||||
roleArn: "",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
name: "role_marker_no_name",
|
||||
roleArn: "arn:aws:iam::role/",
|
||||
expected: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := ExtractRoleNameFromArn(tc.roleArn)
|
||||
if result != tc.expected {
|
||||
t.Errorf("ExtractRoleNameFromArn(%q) = %q, want %q", tc.roleArn, result, tc.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractRoleNameFromPrincipal(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
principal string
|
||||
expected string
|
||||
}{
|
||||
// STS assumed role format (legacy)
|
||||
{
|
||||
name: "sts_assumed_role_legacy",
|
||||
principal: "arn:aws:sts::assumed-role/RoleName/SessionName",
|
||||
expected: "RoleName",
|
||||
},
|
||||
{
|
||||
name: "sts_assumed_role_legacy_no_session",
|
||||
principal: "arn:aws:sts::assumed-role/RoleName",
|
||||
expected: "RoleName",
|
||||
},
|
||||
// STS assumed role format (standard with account ID)
|
||||
{
|
||||
name: "sts_assumed_role_standard",
|
||||
principal: "arn:aws:sts::123456789012:assumed-role/RoleName/SessionName",
|
||||
expected: "RoleName",
|
||||
},
|
||||
{
|
||||
name: "sts_assumed_role_standard_no_session",
|
||||
principal: "arn:aws:sts::123456789012:assumed-role/RoleName",
|
||||
expected: "RoleName",
|
||||
},
|
||||
// IAM role format (legacy)
|
||||
{
|
||||
name: "iam_role_legacy",
|
||||
principal: "arn:aws:iam::role/RoleName",
|
||||
expected: "RoleName",
|
||||
},
|
||||
{
|
||||
name: "iam_role_legacy_with_path",
|
||||
principal: "arn:aws:iam::role/Path/RoleName",
|
||||
expected: "Path/RoleName",
|
||||
},
|
||||
// IAM role format (standard)
|
||||
{
|
||||
name: "iam_role_standard",
|
||||
principal: "arn:aws:iam::123456789012:role/RoleName",
|
||||
expected: "RoleName",
|
||||
},
|
||||
{
|
||||
name: "iam_role_standard_with_path",
|
||||
principal: "arn:aws:iam::123456789012:role/Path/RoleName",
|
||||
expected: "Path/RoleName",
|
||||
},
|
||||
// Invalid formats
|
||||
{
|
||||
name: "invalid_principal",
|
||||
principal: "invalid-arn",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
name: "empty_string",
|
||||
principal: "",
|
||||
expected: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result := ExtractRoleNameFromPrincipal(tc.principal)
|
||||
if result != tc.expected {
|
||||
t.Errorf("ExtractRoleNameFromPrincipal(%q) = %q, want %q", tc.principal, result, tc.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user