github.com/aavshr/aws-sdk-go@v1.41.3/aws/arn/arn_test.go (about) 1 //go:build go1.7 2 // +build go1.7 3 4 package arn 5 6 import ( 7 "errors" 8 "testing" 9 ) 10 11 func TestParseARN(t *testing.T) { 12 cases := []struct { 13 input string 14 arn ARN 15 err error 16 }{ 17 { 18 input: "invalid", 19 err: errors.New(invalidPrefix), 20 }, 21 { 22 input: "arn:nope", 23 err: errors.New(invalidSections), 24 }, 25 { 26 input: "arn:aws:ecr:us-west-2:123456789012:repository/foo/bar", 27 arn: ARN{ 28 Partition: "aws", 29 Service: "ecr", 30 Region: "us-west-2", 31 AccountID: "123456789012", 32 Resource: "repository/foo/bar", 33 }, 34 }, 35 { 36 input: "arn:aws:elasticbeanstalk:us-east-1:123456789012:environment/My App/MyEnvironment", 37 arn: ARN{ 38 Partition: "aws", 39 Service: "elasticbeanstalk", 40 Region: "us-east-1", 41 AccountID: "123456789012", 42 Resource: "environment/My App/MyEnvironment", 43 }, 44 }, 45 { 46 input: "arn:aws:iam::123456789012:user/David", 47 arn: ARN{ 48 Partition: "aws", 49 Service: "iam", 50 Region: "", 51 AccountID: "123456789012", 52 Resource: "user/David", 53 }, 54 }, 55 { 56 input: "arn:aws:rds:eu-west-1:123456789012:db:mysql-db", 57 arn: ARN{ 58 Partition: "aws", 59 Service: "rds", 60 Region: "eu-west-1", 61 AccountID: "123456789012", 62 Resource: "db:mysql-db", 63 }, 64 }, 65 { 66 input: "arn:aws:s3:::my_corporate_bucket/exampleobject.png", 67 arn: ARN{ 68 Partition: "aws", 69 Service: "s3", 70 Region: "", 71 AccountID: "", 72 Resource: "my_corporate_bucket/exampleobject.png", 73 }, 74 }, 75 } 76 for _, tc := range cases { 77 t.Run(tc.input, func(t *testing.T) { 78 spec, err := Parse(tc.input) 79 if tc.arn != spec { 80 t.Errorf("Expected %q to parse as %v, but got %v", tc.input, tc.arn, spec) 81 } 82 if err == nil && tc.err != nil { 83 t.Errorf("Expected err to be %v, but got nil", tc.err) 84 } else if err != nil && tc.err == nil { 85 t.Errorf("Expected err to be nil, but got %v", err) 86 } else if err != nil && tc.err != nil && err.Error() != tc.err.Error() { 87 t.Errorf("Expected err to be %v, but got %v", tc.err, err) 88 } 89 }) 90 } 91 } 92 93 func TestIsARN(t *testing.T) { 94 95 cases := map[string]struct { 96 In string 97 Expect bool 98 // Params 99 }{ 100 "valid ARN slash resource": { 101 In: "arn:aws:service:us-west-2:123456789012:restype/resvalue", 102 Expect: true, 103 }, 104 "valid ARN colon resource": { 105 In: "arn:aws:service:us-west-2:123456789012:restype:resvalue", 106 Expect: true, 107 }, 108 "valid ARN resource": { 109 In: "arn:aws:service:us-west-2:123456789012:*", 110 Expect: true, 111 }, 112 "empty sections": { 113 In: "arn:::::", 114 Expect: true, 115 }, 116 "invalid ARN": { 117 In: "some random string", 118 }, 119 "invalid ARN missing resource": { 120 In: "arn:aws:service:us-west-2:123456789012", 121 }, 122 } 123 124 for name, c := range cases { 125 t.Run(name, func(t *testing.T) { 126 actual := IsARN(c.In) 127 if e, a := c.Expect, actual; e != a { 128 t.Errorf("expect %s valid %v, got %v", c.In, e, a) 129 } 130 }) 131 } 132 }