github.com/aavshr/aws-sdk-go@v1.41.3/internal/s3shared/arn/outpost_arn_test.go (about)

     1  //go:build go1.7
     2  // +build go1.7
     3  
     4  package arn
     5  
     6  import (
     7  	"reflect"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/aavshr/aws-sdk-go/aws/arn"
    12  )
    13  
    14  func TestParseOutpostAccessPointARNResource(t *testing.T) {
    15  	cases := map[string]struct {
    16  		ARN       arn.ARN
    17  		ExpectErr string
    18  		ExpectARN OutpostAccessPointARN
    19  	}{
    20  		"region not set": {
    21  			ARN: arn.ARN{
    22  				Partition: "aws",
    23  				Service:   "s3-outposts",
    24  				AccountID: "012345678901",
    25  				Resource:  "outpost/myoutpost/accesspoint/myendpoint",
    26  			},
    27  			ExpectErr: "region not set",
    28  		},
    29  		"account-id not set": {
    30  			ARN: arn.ARN{
    31  				Partition: "aws",
    32  				Service:   "s3-outposts",
    33  				Region:    "us-west-2",
    34  				Resource:  "outpost/myoutpost/accesspoint/myendpoint",
    35  			},
    36  			ExpectErr: "account-id not set",
    37  		},
    38  		"resource-id not set": {
    39  			ARN: arn.ARN{
    40  				Partition: "aws",
    41  				Service:   "s3-outposts",
    42  				Region:    "us-west-2",
    43  				AccountID: "012345678901",
    44  				Resource:  "myoutpost",
    45  			},
    46  			ExpectErr: "resource-id not set",
    47  		},
    48  		"resource-id empty": {
    49  			ARN: arn.ARN{
    50  				Partition: "aws",
    51  				Service:   "s3-outposts",
    52  				Region:    "us-west-2",
    53  				AccountID: "012345678901",
    54  				Resource:  "outpost:",
    55  			},
    56  			ExpectErr: "resource-id not set",
    57  		},
    58  		"resource not supported": {
    59  			ARN: arn.ARN{
    60  				Partition: "aws",
    61  				Service:   "s3-outposts",
    62  				Region:    "us-west-2",
    63  				AccountID: "012345678901",
    64  				Resource:  "outpost/myoutpost/accesspoint/endpoint/object/key",
    65  			},
    66  			ExpectErr: "sub resource not supported",
    67  		},
    68  		"access-point not defined": {
    69  			ARN: arn.ARN{
    70  				Partition: "aws",
    71  				Service:   "s3-outposts",
    72  				Region:    "us-west-2",
    73  				AccountID: "012345678901",
    74  				Resource:  "outpost/myoutpost/endpoint/object/key",
    75  			},
    76  			ExpectErr: "unknown resource set for outpost ARN",
    77  		},
    78  		"valid resource-id": {
    79  			ARN: arn.ARN{
    80  				Partition: "aws",
    81  				Service:   "s3-outposts",
    82  				Region:    "us-west-2",
    83  				AccountID: "012345678901",
    84  				Resource:  "outpost/myoutpost/accesspoint/myaccesspoint",
    85  			},
    86  			ExpectARN: OutpostAccessPointARN{
    87  				AccessPointARN: AccessPointARN{
    88  					ARN: arn.ARN{
    89  						Partition: "aws",
    90  						Service:   "s3-outposts",
    91  						Region:    "us-west-2",
    92  						AccountID: "012345678901",
    93  						Resource:  "outpost/myoutpost/accesspoint/myaccesspoint",
    94  					},
    95  					AccessPointName: "myaccesspoint",
    96  				},
    97  				OutpostID: "myoutpost",
    98  			},
    99  		},
   100  	}
   101  
   102  	for name, c := range cases {
   103  		t.Run(name, func(t *testing.T) {
   104  			resParts := SplitResource(c.ARN.Resource)
   105  			a, err := ParseOutpostARNResource(c.ARN, resParts[1:])
   106  
   107  			if len(c.ExpectErr) == 0 && err != nil {
   108  				t.Fatalf("expect no error but got %v", err)
   109  			} else if len(c.ExpectErr) != 0 && err == nil {
   110  				t.Fatalf("expect error %q, but got nil", c.ExpectErr)
   111  			} else if len(c.ExpectErr) != 0 && err != nil {
   112  				if e, a := c.ExpectErr, err.Error(); !strings.Contains(a, e) {
   113  					t.Fatalf("expect error %q, got %q", e, a)
   114  				}
   115  				return
   116  			}
   117  
   118  			if e, a := c.ExpectARN, a; !reflect.DeepEqual(e, a) {
   119  				t.Errorf("expect %v, got %v", e, a)
   120  			}
   121  		})
   122  	}
   123  }
   124  
   125  func TestParseOutpostBucketARNResource(t *testing.T) {
   126  	cases := map[string]struct {
   127  		ARN       arn.ARN
   128  		ExpectErr string
   129  		ExpectARN OutpostBucketARN
   130  	}{
   131  		"region not set": {
   132  			ARN: arn.ARN{
   133  				Partition: "aws",
   134  				Service:   "s3-outposts",
   135  				AccountID: "012345678901",
   136  				Resource:  "outpost/myoutpost/bucket/mybucket",
   137  			},
   138  			ExpectErr: "region not set",
   139  		},
   140  		"resource-id empty": {
   141  			ARN: arn.ARN{
   142  				Partition: "aws",
   143  				Service:   "s3-outposts",
   144  				Region:    "us-west-2",
   145  				AccountID: "012345678901",
   146  				Resource:  "outpost:",
   147  			},
   148  			ExpectErr: "resource-id not set",
   149  		},
   150  		"resource not supported": {
   151  			ARN: arn.ARN{
   152  				Partition: "aws",
   153  				Service:   "s3-outposts",
   154  				Region:    "us-west-2",
   155  				AccountID: "012345678901",
   156  				Resource:  "outpost/myoutpost/bucket/mybucket/object/key",
   157  			},
   158  			ExpectErr: "sub resource not supported",
   159  		},
   160  		"bucket not defined": {
   161  			ARN: arn.ARN{
   162  				Partition: "aws",
   163  				Service:   "s3-outposts",
   164  				Region:    "us-west-2",
   165  				AccountID: "012345678901",
   166  				Resource:  "outpost/myoutpost/endpoint/object/key",
   167  			},
   168  			ExpectErr: "unknown resource set for outpost ARN",
   169  		},
   170  		"valid resource-id": {
   171  			ARN: arn.ARN{
   172  				Partition: "aws",
   173  				Service:   "s3-outposts",
   174  				Region:    "us-west-2",
   175  				AccountID: "012345678901",
   176  				Resource:  "outpost/myoutpost/bucket/mybucket",
   177  			},
   178  			ExpectARN: OutpostBucketARN{
   179  				ARN: arn.ARN{
   180  					Partition: "aws",
   181  					Service:   "s3-outposts",
   182  					Region:    "us-west-2",
   183  					AccountID: "012345678901",
   184  					Resource:  "outpost/myoutpost/bucket/mybucket",
   185  				},
   186  				BucketName: "mybucket",
   187  				OutpostID:  "myoutpost",
   188  			},
   189  		},
   190  	}
   191  
   192  	for name, c := range cases {
   193  		t.Run(name, func(t *testing.T) {
   194  			resParts := SplitResource(c.ARN.Resource)
   195  			a, err := ParseOutpostARNResource(c.ARN, resParts[1:])
   196  
   197  			if len(c.ExpectErr) == 0 && err != nil {
   198  				t.Fatalf("expect no error but got %v", err)
   199  			} else if len(c.ExpectErr) != 0 && err == nil {
   200  				t.Fatalf("expect error %q, but got nil", c.ExpectErr)
   201  			} else if len(c.ExpectErr) != 0 && err != nil {
   202  				if e, a := c.ExpectErr, err.Error(); !strings.Contains(a, e) {
   203  					t.Fatalf("expect error %q, got %q", e, a)
   204  				}
   205  				return
   206  			}
   207  
   208  			if e, a := c.ExpectARN, a; !reflect.DeepEqual(e, a) {
   209  				t.Errorf("expect %v, got %v", e, a)
   210  			}
   211  		})
   212  	}
   213  }
   214  
   215  func TestParseBucketResource(t *testing.T) {
   216  	cases := map[string]struct {
   217  		ARN              arn.ARN
   218  		ExpectErr        string
   219  		ExpectBucketName string
   220  	}{
   221  		"resource-id empty": {
   222  			ARN: arn.ARN{
   223  				Partition: "aws",
   224  				Service:   "s3",
   225  				Region:    "us-west-2",
   226  				AccountID: "012345678901",
   227  				Resource:  "bucket:",
   228  			},
   229  			ExpectErr: "bucket resource-id not set",
   230  		},
   231  		"resource not supported": {
   232  			ARN: arn.ARN{
   233  				Partition: "aws",
   234  				Service:   "s3",
   235  				Region:    "us-west-2",
   236  				AccountID: "012345678901",
   237  				Resource:  "bucket/mybucket/object/key",
   238  			},
   239  			ExpectErr: "sub resource not supported",
   240  		},
   241  		"valid resource-id": {
   242  			ARN: arn.ARN{
   243  				Partition: "aws",
   244  				Service:   "s3",
   245  				Region:    "us-west-2",
   246  				AccountID: "012345678901",
   247  				Resource:  "bucket/mybucket",
   248  			},
   249  			ExpectBucketName: "mybucket",
   250  		},
   251  	}
   252  
   253  	for name, c := range cases {
   254  		t.Run(name, func(t *testing.T) {
   255  			resParts := SplitResource(c.ARN.Resource)
   256  			a, err := parseBucketResource(c.ARN, resParts[1:])
   257  
   258  			if len(c.ExpectErr) == 0 && err != nil {
   259  				t.Fatalf("expect no error but got %v", err)
   260  			} else if len(c.ExpectErr) != 0 && err == nil {
   261  				t.Fatalf("expect error %q, but got nil", c.ExpectErr)
   262  			} else if len(c.ExpectErr) != 0 && err != nil {
   263  				if e, a := c.ExpectErr, err.Error(); !strings.Contains(a, e) {
   264  					t.Fatalf("expect error %q, got %q", e, a)
   265  				}
   266  				return
   267  			}
   268  
   269  			if e, a := c.ExpectBucketName, a; !reflect.DeepEqual(e, a) {
   270  				t.Errorf("expect %v, got %v", e, a)
   271  			}
   272  		})
   273  	}
   274  }