1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
package arn
import (
"reflect"
"strings"
"testing"
"github.com/aws/aws-sdk-go-v2/aws/arn"
)
func TestParseResource(t *testing.T) {
cases := map[string]struct {
Input string
MappedResources map[string]func(arn.ARN, []string) (Resource, error)
Expect Resource
ExpectErr string
}{
"Empty ARN": {
Input: "",
ExpectErr: "arn: invalid prefix",
},
"No Partition": {
Input: "arn::sqs:us-west-2:012345678901:accesspoint",
ExpectErr: "partition not set",
},
"Not S3 ARN": {
Input: "arn:aws:sqs:us-west-2:012345678901:accesspoint",
ExpectErr: "service is not supported",
},
"No Resource": {
Input: "arn:aws:s3:us-west-2:012345678901:",
ExpectErr: "resource not set",
},
"Unknown Resource Type": {
Input: "arn:aws:s3:us-west-2:012345678901:myresource",
ExpectErr: "unknown resource type",
},
"Unknown BucketARN Resource Type": {
Input: "arn:aws:s3:us-west-2:012345678901:bucket_name:mybucket",
ExpectErr: "unknown resource type",
},
"Unknown Resource Type with Resource and Sub-Resource": {
Input: "arn:aws:s3:us-west-2:012345678901:somethingnew:myresource/subresource",
ExpectErr: "unknown resource type",
},
"Access Point with sub resource": {
Input: "arn:aws:s3:us-west-2:012345678901:accesspoint:myresource/subresource",
MappedResources: map[string]func(arn.ARN, []string) (Resource, error){
"accesspoint": func(a arn.ARN, parts []string) (Resource, error) {
return ParseAccessPointResource(a, parts)
},
},
ExpectErr: "resource not supported",
},
"AccessPoint Resource Type": {
Input: "arn:aws:s3:us-west-2:012345678901:accesspoint:myendpoint",
MappedResources: map[string]func(arn.ARN, []string) (Resource, error){
"accesspoint": func(a arn.ARN, parts []string) (Resource, error) {
return ParseAccessPointResource(a, parts)
},
},
Expect: AccessPointARN{
ARN: arn.ARN{
Partition: "aws",
Service: "s3",
Region: "us-west-2",
AccountID: "012345678901",
Resource: "accesspoint:myendpoint",
},
AccessPointName: "myendpoint",
},
},
"AccessPoint Resource Type With Path Syntax": {
Input: "arn:aws:s3:us-west-2:012345678901:accesspoint/myendpoint",
MappedResources: map[string]func(arn.ARN, []string) (Resource, error){
"accesspoint": func(a arn.ARN, parts []string) (Resource, error) {
return ParseAccessPointResource(a, parts)
},
},
Expect: AccessPointARN{
ARN: arn.ARN{
Partition: "aws",
Service: "s3",
Region: "us-west-2",
AccountID: "012345678901",
Resource: "accesspoint/myendpoint",
},
AccessPointName: "myendpoint",
},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
var parsed Resource
arn, err := arn.Parse(c.Input)
if err == nil {
parsed, err = ParseResource(arn, mappedResourceParser(c.MappedResources))
}
if len(c.ExpectErr) == 0 && err != nil {
t.Fatalf("expect no error but got %v", err)
} else if len(c.ExpectErr) != 0 && err == nil {
t.Fatalf("expect error but got nil")
} else if len(c.ExpectErr) != 0 && err != nil {
if e, a := c.ExpectErr, err.Error(); !strings.Contains(a, e) {
t.Fatalf("expect error %q, got %q", e, a)
}
return
}
if e, a := c.Expect, parsed; !reflect.DeepEqual(e, a) {
t.Errorf("Expect %v, got %v", e, a)
}
})
}
}
func mappedResourceParser(kinds map[string]func(arn.ARN, []string) (Resource, error)) ResourceParser {
return func(a arn.ARN) (Resource, error) {
parts := SplitResource(a.Resource)
fn, ok := kinds[parts[0]]
if !ok {
return nil, InvalidARNError{ARN: a, Reason: "unknown resource type"}
}
return fn(a, parts[1:])
}
}
func TestSplitResource(t *testing.T) {
cases := []struct {
Input string
Expect []string
}{
{
Input: "accesspoint:myendpoint",
Expect: []string{"accesspoint", "myendpoint"},
},
{
Input: "accesspoint/myendpoint",
Expect: []string{"accesspoint", "myendpoint"},
},
{
Input: "accesspoint",
Expect: []string{"accesspoint"},
},
{
Input: "accesspoint:",
Expect: []string{"accesspoint", ""},
},
{
Input: "accesspoint: ",
Expect: []string{"accesspoint", " "},
},
{
Input: "accesspoint:endpoint/object/key",
Expect: []string{"accesspoint", "endpoint", "object", "key"},
},
}
for _, c := range cases {
t.Run(c.Input, func(t *testing.T) {
parts := SplitResource(c.Input)
if e, a := c.Expect, parts; !reflect.DeepEqual(e, a) {
t.Errorf("expect %v, got %v", e, a)
}
})
}
}
|