-
Notifications
You must be signed in to change notification settings - Fork 244
/
Copy pathdetect_s3_test.go
147 lines (139 loc) · 3.05 KB
/
detect_s3_test.go
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package getter
import (
"testing"
)
func TestS3Detector(t *testing.T) {
cases := []struct {
Input string
Output string
}{
// Virtual hosted style
{
"bucket.s3.amazonaws.com/foo",
"s3::https://s3.amazonaws.com/bucket/foo",
},
{
"bucket.s3.amazonaws.com/foo/bar",
"s3::https://s3.amazonaws.com/bucket/foo/bar",
},
{
"bucket.s3.amazonaws.com/foo/bar.baz",
"s3::https://s3.amazonaws.com/bucket/foo/bar.baz",
},
{
"bucket.s3-eu-west-1.amazonaws.com/foo",
"s3::https://s3-eu-west-1.amazonaws.com/bucket/foo",
},
{
"bucket.s3-eu-west-1.amazonaws.com/foo/bar",
"s3::https://s3-eu-west-1.amazonaws.com/bucket/foo/bar",
},
{
"bucket.s3-eu-west-1.amazonaws.com/foo/bar.baz",
"s3::https://s3-eu-west-1.amazonaws.com/bucket/foo/bar.baz",
},
// 5 parts Virtual hosted-style
{
"bucket.s3.eu-west-1.amazonaws.com/foo/bar.baz",
"s3::https://s3.eu-west-1.amazonaws.com/bucket/foo/bar.baz",
},
// Path style
{
"s3.amazonaws.com/bucket/foo",
"s3::https://s3.amazonaws.com/bucket/foo",
},
{
"s3.amazonaws.com/bucket/foo/bar",
"s3::https://s3.amazonaws.com/bucket/foo/bar",
},
{
"s3.amazonaws.com/bucket/foo/bar.baz",
"s3::https://s3.amazonaws.com/bucket/foo/bar.baz",
},
{
"s3-eu-west-1.amazonaws.com/bucket/foo",
"s3::https://s3-eu-west-1.amazonaws.com/bucket/foo",
},
{
"s3-eu-west-1.amazonaws.com/bucket/foo/bar",
"s3::https://s3-eu-west-1.amazonaws.com/bucket/foo/bar",
},
{
"s3-eu-west-1.amazonaws.com/bucket/foo/bar.baz",
"s3::https://s3-eu-west-1.amazonaws.com/bucket/foo/bar.baz",
},
// Misc tests
{
"s3-eu-west-1.amazonaws.com/bucket/foo/bar.baz?version=1234",
"s3::https://s3-eu-west-1.amazonaws.com/bucket/foo/bar.baz?version=1234",
},
}
pwd := "/pwd"
f := new(S3Detector)
for i, tc := range cases {
output, ok, err := f.Detect(tc.Input, pwd)
if err != nil {
t.Fatalf("err: %s", err)
}
if !ok {
t.Fatal("not ok")
}
if output != tc.Output {
t.Fatalf("%d: bad: %#v", i, output)
}
}
}
func TestS3Detector_MalformedDetectHTTP(t *testing.T) {
cases := []struct {
Name string
Input string
Expected string
Output string
}{
{
"valid url",
"s3.amazonaws.com/bucket/foo/bar",
"",
"s3::https://s3.amazonaws.com/bucket/foo/bar",
},
{
"empty url",
"",
"",
"",
},
{
"not valid url",
"bucket/foo/bar",
"error parsing S3 URL",
"",
},
{
"not valid url domain",
"s3.amazonaws.com.invalid/bucket/foo/bar",
"error parsing S3 URL",
"",
},
{
"not valid url lenght",
"http://s3.amazonaws.com",
"URL is not a valid S3 URL",
"",
},
}
pwd := "/pwd"
f := new(S3Detector)
for _, tc := range cases {
output, _, err := f.Detect(tc.Input, pwd)
if err != nil {
if err.Error() != tc.Expected {
t.Fatalf("expected error %s, got %s for %s", tc.Expected, err.Error(), tc.Name)
}
}
if output != tc.Output {
t.Fatalf("expected %s, got %s for %s", tc.Output, output, tc.Name)
}
}
}