/
factory.go
274 lines (229 loc) · 11 KB
/
factory.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright © Microsoft <wastore@microsoft.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package e2etest
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/Azure/azure-storage-azcopy/azbfs"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/Azure/azure-storage-file-go/azfile"
)
// provide convenient methods to get access to test resources such as accounts, containers/shares, directories
type TestResourceFactory struct{}
func (TestResourceFactory) GetBlobServiceURL(accountType AccountType) azblob.ServiceURL {
accountName, accountKey := GlobalInputManager{}.GetAccountAndKey(accountType)
u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net/", accountName))
credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
if err != nil {
panic(err)
}
pipeline := azblob.NewPipeline(credential, azblob.PipelineOptions{})
return azblob.NewServiceURL(*u, pipeline)
}
func (TestResourceFactory) GetFileServiceURL(accountType AccountType) azfile.ServiceURL {
accountName, accountKey := GlobalInputManager{}.GetAccountAndKey(accountType)
u, _ := url.Parse(fmt.Sprintf("https://%s.file.core.windows.net/", accountName))
credential, err := azfile.NewSharedKeyCredential(accountName, accountKey)
if err != nil {
panic(err)
}
pipeline := azfile.NewPipeline(credential, azfile.PipelineOptions{})
return azfile.NewServiceURL(*u, pipeline)
}
func (TestResourceFactory) GetDatalakeServiceURL(accountType AccountType) azbfs.ServiceURL {
accountName, accountKey := GlobalInputManager{}.GetAccountAndKey(accountType)
u, _ := url.Parse(fmt.Sprintf("https://%s.dfs.core.windows.net/", accountName))
cred := azbfs.NewSharedKeyCredential(accountName, accountKey)
pipeline := azbfs.NewPipeline(cred, azbfs.PipelineOptions{})
return azbfs.NewServiceURL(*u, pipeline)
}
func (TestResourceFactory) GetBlobServiceURLWithSAS(c asserter, accountType AccountType) azblob.ServiceURL {
accountName, accountKey := GlobalInputManager{}.GetAccountAndKey(accountType)
credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
c.AssertNoErr(err)
sasQueryParams, err := azblob.AccountSASSignatureValues{
Protocol: azblob.SASProtocolHTTPS,
ExpiryTime: time.Now().Add(48 * time.Hour),
Permissions: azblob.AccountSASPermissions{Read: true, List: true, Write: true, Delete: true, DeletePreviousVersion: true, Add: true, Create: true, Update: true, Process: true, Tag: true, FilterByTags: true}.String(),
Services: azblob.AccountSASServices{File: true, Blob: true, Queue: true}.String(),
ResourceTypes: azfile.AccountSASResourceTypes{Service: true, Container: true, Object: true}.String(),
}.NewSASQueryParameters(credential)
c.AssertNoErr(err)
// construct the url from scratch
qp := sasQueryParams.Encode()
rawURL := fmt.Sprintf("https://%s.blob.core.windows.net/?%s",
credential.AccountName(), qp)
// convert the raw url and validate it was parsed successfully
fullURL, err := url.Parse(rawURL)
c.AssertNoErr(err)
return azblob.NewServiceURL(*fullURL, azblob.NewPipeline(azblob.NewAnonymousCredential(), azblob.PipelineOptions{}))
}
func (TestResourceFactory) GetContainerURLWithSAS(c asserter, accountType AccountType, containerName string) azblob.ContainerURL {
accountName, accountKey := GlobalInputManager{}.GetAccountAndKey(accountType)
credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
c.AssertNoErr(err)
sasQueryParams, err := azblob.BlobSASSignatureValues{
Protocol: azblob.SASProtocolHTTPS,
ExpiryTime: time.Now().UTC().Add(48 * time.Hour),
ContainerName: containerName,
Permissions: azblob.ContainerSASPermissions{Read: true, Add: true, Write: true, Create: true, Delete: true, DeletePreviousVersion: true, List: true, Tag: true}.String(),
}.NewSASQueryParameters(credential)
c.AssertNoErr(err)
// construct the url from scratch
qp := sasQueryParams.Encode()
rawURL := fmt.Sprintf("https://%s.blob.core.windows.net/%s?%s",
credential.AccountName(), containerName, qp)
// convert the raw url and validate it was parsed successfully
fullURL, err := url.Parse(rawURL)
c.AssertNoErr(err)
return azblob.NewContainerURL(*fullURL, azblob.NewPipeline(azblob.NewAnonymousCredential(), azblob.PipelineOptions{}))
}
func (TestResourceFactory) GetFileShareULWithSAS(c asserter, accountType AccountType, containerName string) azfile.ShareURL {
accountName, accountKey := GlobalInputManager{}.GetAccountAndKey(accountType)
credential, err := azfile.NewSharedKeyCredential(accountName, accountKey)
c.AssertNoErr(err)
sasQueryParams, err := azfile.FileSASSignatureValues{
Protocol: azfile.SASProtocolHTTPS,
ExpiryTime: time.Now().UTC().Add(48 * time.Hour),
ShareName: containerName,
Permissions: azfile.ShareSASPermissions{Read: true, Write: true, Create: true, Delete: true, List: true}.String(),
}.NewSASQueryParameters(credential)
c.AssertNoErr(err)
// construct the url from scratch
qp := sasQueryParams.Encode()
rawURL := fmt.Sprintf("https://%s.file.core.windows.net/%s?%s",
credential.AccountName(), containerName, qp)
// convert the raw url and validate it was parsed successfully
fullURL, err := url.Parse(rawURL)
c.AssertNoErr(err)
return azfile.NewShareURL(*fullURL, azblob.NewPipeline(azblob.NewAnonymousCredential(), azblob.PipelineOptions{}))
}
func (TestResourceFactory) GetBlobURLWithSAS(c asserter, accountType AccountType, containerName string, blobName string) azblob.BlobURL {
containerURLWithSAS := TestResourceFactory{}.GetContainerURLWithSAS(c, accountType, containerName)
blobURLWithSAS := containerURLWithSAS.NewBlobURL(blobName)
return blobURLWithSAS
}
func (TestResourceFactory) CreateNewContainer(c asserter, accountType AccountType) (container azblob.ContainerURL, name string, rawURL url.URL) {
name = TestResourceNameGenerator{}.GenerateContainerName(c)
container = TestResourceFactory{}.GetBlobServiceURL(accountType).NewContainerURL(name)
cResp, err := container.Create(context.Background(), nil, azblob.PublicAccessNone)
c.AssertNoErr(err)
c.Assert(cResp.StatusCode(), equals(), 201)
return container, name, TestResourceFactory{}.GetContainerURLWithSAS(c, accountType, name).URL()
}
const defaultShareQuotaGB = 512
func (TestResourceFactory) CreateNewFileShare(c asserter, accountType AccountType) (fileShare azfile.ShareURL, name string, rawSasURL url.URL) {
name = TestResourceNameGenerator{}.GenerateContainerName(c)
fileShare = TestResourceFactory{}.GetFileServiceURL(accountType).NewShareURL(name)
cResp, err := fileShare.Create(context.Background(), nil, defaultShareQuotaGB)
c.AssertNoErr(err)
c.Assert(cResp.StatusCode(), equals(), 201)
return fileShare, name, TestResourceFactory{}.GetFileShareULWithSAS(c, accountType, name).URL()
}
func (TestResourceFactory) CreateNewFileShareSnapshot(c asserter, fileShare azfile.ShareURL) (snapshotID string) {
resp, err := fileShare.CreateSnapshot(context.TODO(), azfile.Metadata{})
c.AssertNoErr(err)
return resp.Snapshot()
}
func (TestResourceFactory) CreateLocalDirectory(c asserter) (dstDirName string) {
dstDirName, err := ioutil.TempDir("", "AzCopyLocalTest")
c.AssertNoErr(err)
return
}
type TestResourceNameGenerator struct{}
const (
containerPrefix = "e2e"
blobPrefix = "blob"
)
func getTestName(t *testing.T) (pseudoSuite, test string) {
removeUnderscores := func(s string) string {
return strings.Replace(s, "_", "-", -1) // necessary if using name as basis for blob container name
}
testName := t.Name()
// Look up the stack to find out more info about the test method
// Note: the way to do this changed in go 1.12, refer to release notes for more info
var pcs [10]uintptr
n := runtime.Callers(1, pcs[:])
frames := runtime.CallersFrames(pcs[:n])
fileName := ""
for {
frame, more := frames.Next()
if strings.HasSuffix(frame.Func.Name(), "."+testName) {
fileName = frame.File
break
} else if !more {
break
}
}
// When using the basic Testing package, we have adopted a convention that
// the test name should being with one of the words in the file name, followed by a _ .
// Try to extract a "pseudo suite" name from the test name according to that rule.
pseudoSuite = ""
testName = strings.Replace(testName, "Test", "", 1)
uscorePos := strings.Index(testName, "_")
if uscorePos >= 0 && uscorePos < len(testName)-1 {
beforeUnderscore := strings.ToLower(testName[:uscorePos])
fileWords := strings.Split(strings.Replace(strings.ToLower(filepath.Base(fileName)), "_test.go", "", -1), "_")
for _, w := range fileWords {
if beforeUnderscore == w {
pseudoSuite = beforeUnderscore
testName = testName[uscorePos+1:]
break
}
}
}
return pseudoSuite, removeUnderscores(testName)
}
// This function generates an entity name by concatenating the passed prefix,
// the name of the test requesting the entity name, and the minute, second, and nanoseconds of the call.
// This should make it easy to associate the entities with their test, uniquely identify
// them, and determine the order in which they were created.
// Will truncate the end of the test name, if there is not enough room for it, followed by the time-based suffix,
// with a non-zero maxLen.
func generateName(c asserter, prefix string, maxLen int) string {
name := c.CompactScenarioName() // don't want to just use test name here, because each test contains multiple scearios with the declarative runner
textualPortion := fmt.Sprintf("%s-%s", prefix, strings.ToLower(name))
currentTime := time.Now()
numericSuffix := fmt.Sprintf("%02d%02d%d", currentTime.Minute(), currentTime.Second(), currentTime.Nanosecond())
if maxLen > 0 {
maxTextLen := maxLen - len(numericSuffix)
if maxTextLen < 1 {
panic("max len too short")
}
if len(textualPortion) > maxTextLen {
textualPortion = textualPortion[:maxTextLen]
}
}
name = textualPortion + numericSuffix
return name
}
func (TestResourceNameGenerator) GenerateContainerName(c asserter) string {
return generateName(c, containerPrefix, 63)
}
func (TestResourceNameGenerator) generateBlobName(c asserter) string {
return generateName(c, blobPrefix, 0)
}