-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathzt_list_test.go
162 lines (136 loc) · 4.95 KB
/
zt_list_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package cmd
import (
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
"github.com/Azure/azure-storage-azcopy/v10/common"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
func TestListVersions(t *testing.T) {
a := assert.New(t)
bsc := getSecondaryBlobServiceClient()
// set up the container with single blob with 2 versions
containerClient, containerName := createNewContainer(a, bsc)
defer deleteContainer(a, containerClient)
blobsToInclude := []string{"AzURE2021.jpeg", "sub1/dir2/HELLO-4.txt", "sub1/test/testing.txt"}
scenarioHelper{}.generateBlobsFromList(a, containerClient, blobsToInclude, blockBlobDefaultData)
a.NotNil(containerClient)
// get dictionary/map of blob: version id
versions := make(map[string]string)
for _, blob := range blobsToInclude {
props, err := containerClient.NewBlockBlobClient(blob).GetProperties(ctx, nil)
a.NoError(err)
versions[blob] = *props.VersionID
}
// confirm that container has 3 blobs
pager := containerClient.NewListBlobsFlatPager(nil)
list, err := pager.NextPage(ctx)
a.NoError(err)
a.NotNil(list.Segment.BlobItems)
a.Equal(3, len(list.Segment.BlobItems))
// set up interceptor
mockedRPC := interceptor{}
Rpc = mockedRPC.intercept
mockedRPC.init()
mockedLcm := mockedLifecycleManager{infoLog: make(chan string, 50)}
mockedLcm.SetOutputFormat(common.EOutputFormat.Text()) // text format
glcm = &mockedLcm
// construct the raw input to simulate user input
rawContainerURLWithSAS := scenarioHelper{}.getSecondaryRawContainerURLWithSAS(a, containerName)
raw := getDefaultListRawInput(rawContainerURLWithSAS.String())
raw.Properties = "VersionId"
raw.RunningTally = true
runListAndVerify(a, raw, func(err error) {
a.Nil(err)
// validate that the no transfers were scheduled
a.Nil(mockedRPC.transfers)
// check if info logs contain the correct version id for each blob
msg := mockedLcm.GatherAllLogs(mockedLcm.infoLog)
for i, m := range msg {
if i < 3 { // 0-2 will be blob names + version id
a.True(strings.Contains(m, blobsToInclude[i]))
a.True(strings.Contains(m, "VersionId: "+versions[blobsToInclude[i]]))
}
if i == 4 { // 4 will be file count
a.True(strings.Contains(m, "File count: 3"))
}
if i == 5 { // 5 will be file size
a.True(strings.Contains(m, "Total file size: 69.00 B"))
}
}
})
}
func TestListVersionsMultiVersions(t *testing.T) {
a := assert.New(t)
bsc := getSecondaryBlobServiceClient()
// set up the container with single blob with 2 versions
containerClient, containerName := createNewContainer(a, bsc)
defer deleteContainer(a, containerClient)
// testing how running tally will handle foo.txt vs foo/foo.txt, test/foo.txt
blobsToInclude := []string{"foo.txt", "foo/foo.txt", "test/foo.txt", "sub1/test/baz.txt"}
scenarioHelper{}.generateBlobsFromList(a, containerClient, blobsToInclude, blockBlobDefaultData)
a.NotNil(containerClient)
// make first two blobs have 1 additional version
blobsToVersion := []string{blobsToInclude[0], blobsToInclude[1]}
scenarioHelper{}.generateVersionsForBlobsFromList(a, containerClient, blobsToVersion)
a.NotNil(containerClient)
// confirm that container has 6 blobs (4 blobs, 2 versions)
pager := containerClient.NewListBlobsFlatPager(&container.ListBlobsFlatOptions{
Include: container.ListBlobsInclude{Versions: true},
})
list, err := pager.NextPage(ctx)
a.NoError(err)
a.NotNil(list.Segment.BlobItems)
a.Equal(6, len(list.Segment.BlobItems))
var blobs []string
var versions []string
for _, item := range list.Segment.BlobItems {
blobs = append(blobs, *item.Name)
versions = append(versions, *item.VersionID)
}
// set up interceptor
mockedRPC := interceptor{}
Rpc = mockedRPC.intercept
mockedRPC.init()
mockedLcm := mockedLifecycleManager{infoLog: make(chan string, 50)}
mockedLcm.SetOutputFormat(common.EOutputFormat.Text()) // text format
glcm = &mockedLcm
// construct the raw input to simulate user input
rawContainerURLWithSAS := scenarioHelper{}.getSecondaryRawContainerURLWithSAS(a, containerName)
raw := getDefaultListRawInput(rawContainerURLWithSAS.String())
raw.Properties = "VersionId"
raw.RunningTally = true
runListAndVerify(a, raw, func(err error) {
a.Nil(err)
// validate that the no transfers were scheduled
a.Nil(mockedRPC.transfers)
// check if info logs contain the correct version id for each blob
msg := mockedLcm.GatherAllLogs(mockedLcm.infoLog)
for i, m := range msg {
if i < 6 { // 0-5 will be blob names + version id
a.True(contains(blobs, m, true))
a.True(contains(versions, m, false))
}
if i == 7 { // 7 will be file count
a.True(strings.Contains(m, "File count: 4"))
}
if i == 8 { // 8 will be file size
a.True(strings.Contains(m, "Total file size: 92.00 B"))
}
}
})
}
func contains(arr []string, msg string, isBlob bool) bool {
for _, a := range arr {
if isBlob {
if strings.HasPrefix(msg, a) {
return true
}
} else {
if strings.Contains(msg, a) {
return true
}
}
}
return false
}