-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathvalidator.go
145 lines (125 loc) · 5.48 KB
/
validator.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
// 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 (
"fmt"
"net/url"
"os"
"runtime"
"strings"
"github.com/Azure/azure-storage-azcopy/v10/common"
)
type Validator struct{}
// Use this to ensure that source and dest strings can be compared with each other
func makeSlashesComparable(s string) string {
return strings.Replace(s, "\\", "/", -1)
}
// Use this to ensure slashes are correct for the location, loc
func fixSlashes(s string, loc common.Location) string {
if loc == common.ELocation.Local() {
// replace all slashes with the one that right for the local OS
s = strings.Replace(s, "/", common.OS_PATH_SEPARATOR, -1)
s = strings.Replace(s, "\\", common.OS_PATH_SEPARATOR, -1)
} else {
// replace all backslashes with web-style forward slash
s = strings.Replace(s, "\\", common.AZCOPY_PATH_SEPARATOR_STRING, -1)
}
return s
}
func (Validator) ValidateRemoveTransfer(c asserter, isSrcEncoded bool, isDstEncoded bool,
sourcePrefix string, destinationPrefix string, expectedTransfers []*testObject, actualTransfers []common.TransferDetail, statusToTest common.TransferStatus) {
// TODO: Think of how to validate files in case of remove
}
func (Validator) ValidateCopyTransfersAreScheduled(c asserter, isSrcEncoded bool, isDstEncoded bool,
sourcePrefix string, destinationPrefix string, expectedTransfers []*testObject, actualTransfers []common.TransferDetail, statusToTest common.TransferStatus, expectFolders bool) {
sourcePrefix = makeSlashesComparable(sourcePrefix)
destinationPrefix = makeSlashesComparable(destinationPrefix)
snapshotID := ""
if isSrcEncoded {
// i.e. source is a URL
srcPrefixURL, err := url.Parse(sourcePrefix)
if err == nil {
snapshotID = srcPrefixURL.Query().Get("sharesnapshot")
if snapshotID != "" {
sourcePrefix = strings.TrimSuffix(sourcePrefix, "?sharesnapshot="+snapshotID)
}
}
}
// validate that the right number of transfers were scheduled
c.Assert(len(actualTransfers), equals(), len(expectedTransfers),
fmt.Sprintf("Number of actual and expected transfers should match, for status %s", statusToTest.String()))
// validate that the right transfers were sent
addFolderSuffix := func(s string) string {
if strings.HasSuffix(s, "/") {
panic("folder suffix already present")
}
return s + "/"
}
lookupMap := scenarioHelper{}.convertListToMap(expectedTransfers, func(to *testObject) string {
if to.isFolder() && expectFolders {
return addFolderSuffix(to.name)
} else {
return to.name
}
})
for _, transfer := range actualTransfers {
if snapshotID != "" {
c.Assert(strings.Contains(transfer.Src, snapshotID), equals(), true)
transfer.Src = strings.TrimSuffix(transfer.Src, "?sharesnapshot="+snapshotID)
}
srcRelativeFilePath := strings.Trim(strings.TrimPrefix(makeSlashesComparable(transfer.Src), sourcePrefix), "/")
dstRelativeFilePath := strings.Trim(strings.TrimPrefix(makeSlashesComparable(transfer.Dst), destinationPrefix), "/")
if isSrcEncoded {
srcRelativeFilePath, _ = url.PathUnescape(srcRelativeFilePath)
if runtime.GOOS == "windows" {
// Decode unsafe dst characters on windows
pathParts := strings.Split(dstRelativeFilePath, "/")
invalidChars := `<>\/:"|?*` + string(rune(0x00))
for _, c := range strings.Split(invalidChars, "") {
for k, p := range pathParts {
pathParts[k] = strings.ReplaceAll(p, url.PathEscape(c), c)
}
}
dstRelativeFilePath = strings.Join(pathParts, "/")
}
}
if isDstEncoded {
dstRelativeFilePath, _ = url.PathUnescape(dstRelativeFilePath)
}
if transfer.Dst != os.DevNull { // Don't check if the destination is NUL-- It won't be correct.
// the relative paths should be equal
c.Assert(dstRelativeFilePath, equals(), srcRelativeFilePath)
}
// look up the path from the expected transfers, make sure it exists
folderMessage := ""
lookupKey := srcRelativeFilePath
if transfer.IsFolderProperties {
lookupKey = addFolderSuffix(lookupKey)
folderMessage = ".\n The transfer was for a folder. Have you forgotten to include folders in your testFiles? (Use the folder() function)"
}
_, transferExist := lookupMap[lookupKey]
c.Assert(transferExist, equals(), true,
fmt.Sprintf("Transfer '%s' ended with status '%s' but was not expected to end in that status%s",
lookupKey,
statusToTest.String(),
folderMessage))
// TODO: do we also want to output specific filenames for ones that were expected to have that status, but did not get it?
}
}