@@ -130,14 +130,15 @@ func TestFromCredentials(t *testing.T) {
130130}
131131
132132func TestExtractCreds (t * testing.T ) {
133- tetCases := []struct {
133+ testCases := []struct {
134134 name string
135135 location string
136136 credsJSON []byte
137+ wantCreds * Credentials
137138 wantErr bool
138139 }{
139140 {
140- name : "valid credentials" ,
141+ name : "valid credentials without endpoint " ,
141142 location : "account/container" ,
142143 credsJSON : []byte (`{
143144 "storageAccountName": "test",
@@ -146,6 +147,33 @@ func TestExtractCreds(t *testing.T) {
146147 "clientID": "test",
147148 "clientSecret": "test"
148149 }` ),
150+ wantCreds : & Credentials {
151+ StorageAccountName : "account" ,
152+ Container : "container" ,
153+ TenantID : "test" ,
154+ ClientID : "test" ,
155+ ClientSecret : "test" ,
156+ Endpoint : "" ,
157+ },
158+ },
159+ {
160+ name : "valid credentials with custom endpoint" ,
161+ location : "blob.core.usgovcloudapi.net/account/container" ,
162+ credsJSON : []byte (`{
163+ "storageAccountName": "test",
164+ "container": "test",
165+ "tenantID": "test",
166+ "clientID": "test",
167+ "clientSecret": "test"
168+ }` ),
169+ wantCreds : & Credentials {
170+ StorageAccountName : "account" ,
171+ Container : "container" ,
172+ TenantID : "test" ,
173+ ClientID : "test" ,
174+ ClientSecret : "test" ,
175+ Endpoint : "blob.core.usgovcloudapi.net" ,
176+ },
149177 },
150178 {
151179 name : "invalid location, missing container" ,
@@ -173,20 +201,78 @@ func TestExtractCreds(t *testing.T) {
173201 },
174202 }
175203
176- for _ , tc := range tetCases {
204+ for _ , tc := range testCases {
177205 t .Run (tc .name , func (t * testing.T ) {
178206 creds , err := extractCreds (tc .location , tc .credsJSON )
179207 if tc .wantErr {
180208 assert .Error (t , err )
181209 } else {
182210 assert .NoError (t , err )
183- assert .Equal (t , & Credentials {
184- StorageAccountName : "account" ,
185- Container : "container" ,
186- TenantID : "test" ,
187- ClientID : "test" ,
188- ClientSecret : "test" ,
189- }, creds )
211+ assert .Equal (t , tc .wantCreds , creds )
212+ }
213+ })
214+ }
215+ }
216+
217+ func TestExtractLocationAndContainer (t * testing.T ) {
218+ testCases := []struct {
219+ name string
220+ location string
221+ wantEndpoint string
222+ wantAccount string
223+ wantContainer string
224+ wantErr bool
225+ }{
226+ {
227+ name : "simple location without endpoint" ,
228+ location : "myaccount/mycontainer" ,
229+ wantEndpoint : "" ,
230+ wantAccount : "myaccount" ,
231+ wantContainer : "mycontainer" ,
232+ },
233+ {
234+ name : "Azure Government Cloud endpoint" ,
235+ location : "blob.core.usgovcloudapi.net/myaccount/mycontainer" ,
236+ wantEndpoint : "blob.core.usgovcloudapi.net" ,
237+ wantAccount : "myaccount" ,
238+ wantContainer : "mycontainer" ,
239+ },
240+ {
241+ name : "Azure Stack Hub endpoint" ,
242+ location : "blob.local.azurestack.external/myaccount/mycontainer" ,
243+ wantEndpoint : "blob.local.azurestack.external" ,
244+ wantAccount : "myaccount" ,
245+ wantContainer : "mycontainer" ,
246+ },
247+ {
248+ name : "custom endpoint with path segments" ,
249+ location : "custom.endpoint.com/account/container" ,
250+ wantEndpoint : "custom.endpoint.com" ,
251+ wantAccount : "account" ,
252+ wantContainer : "container" ,
253+ },
254+ {
255+ name : "invalid simple location - missing container" ,
256+ location : "myaccount" ,
257+ wantErr : true ,
258+ },
259+ {
260+ name : "invalid location - too many segments" ,
261+ location : "endpoint/account/container/extra" ,
262+ wantErr : true ,
263+ },
264+ }
265+
266+ for _ , tc := range testCases {
267+ t .Run (tc .name , func (t * testing.T ) {
268+ endpoint , account , container , err := extractLocationAndContainer (tc .location )
269+ if tc .wantErr {
270+ assert .Error (t , err )
271+ } else {
272+ assert .NoError (t , err )
273+ assert .Equal (t , tc .wantEndpoint , endpoint )
274+ assert .Equal (t , tc .wantAccount , account )
275+ assert .Equal (t , tc .wantContainer , container )
190276 }
191277 })
192278 }
0 commit comments