forked from treeverse/lakeFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_test.go
181 lines (162 loc) · 5.55 KB
/
copy_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package esti
import (
"context"
"net/http"
"strings"
"sync"
"testing"
"time"
"github.com/go-test/deep"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"github.com/treeverse/lakefs/pkg/api"
"github.com/treeverse/lakefs/pkg/block"
"github.com/treeverse/lakefs/pkg/config"
)
const (
s3CopyDataPath = "s3://esti-system-testing-data/copy-test-data/"
gsCopyDataPath = "gs://esti-system-testing-data/copy-test-data/"
azureCopyDataPath = "https://esti.blob.core.windows.net/esti-system-testing-data/copy-test-data/"
azureAbortAccount = "esti4multipleaccounts"
ingestionBranch = "test-data"
largeObject = "squash.tar"
)
func TestCopyObject(t *testing.T) {
ctx, _, repo := setupTest(t)
defer tearDownTest(repo)
t.Run("copy_large_size_file", func(t *testing.T) {
importPath := getImportPath(t)
importTestData(t, ctx, client, repo, importPath)
res, err := client.StatObjectWithResponse(ctx, repo, ingestionBranch, &api.StatObjectParams{
Path: largeObject,
})
require.NoError(t, err)
require.Equal(t, http.StatusOK, res.StatusCode())
objStat := res.JSON200
destPath := "foo"
srcBranch := ingestionBranch
copyResp, err := client.CopyObjectWithResponse(ctx, repo, "main", &api.CopyObjectParams{
DestPath: destPath,
}, api.CopyObjectJSONRequestBody{
SrcPath: largeObject,
SrcRef: &srcBranch,
})
require.NoError(t, err, "failed to copy")
require.NotNil(t, copyResp.JSON201)
// Verify creation path, date and physical address are different
copyStat := copyResp.JSON201
require.NotEqual(t, objStat.PhysicalAddress, copyStat.PhysicalAddress)
require.GreaterOrEqual(t, copyStat.Mtime, objStat.Mtime)
require.Equal(t, destPath, copyStat.Path)
// Verify all else is equal
objStat.Mtime = copyStat.Mtime
objStat.Path = copyStat.Path
objStat.PhysicalAddress = copyStat.PhysicalAddress
require.Nil(t, deep.Equal(objStat, copyStat))
// get back info
statResp, err := client.StatObjectWithResponse(ctx, repo, "main", &api.StatObjectParams{Path: destPath})
require.NoError(t, err)
require.Equal(t, http.StatusOK, statResp.StatusCode())
require.Nil(t, deep.Equal(statResp.JSON200, copyStat))
})
// Copying different accounts takes more time and allows us to abort the copy in the middle
t.Run("copy_large_size_file_abort", func(t *testing.T) {
requireBlockstoreType(t, block.BlockstoreTypeAzure)
importPath := strings.Replace(azureCopyDataPath, "esti", azureAbortAccount, 1)
importTestData(t, ctx, client, repo, importPath)
res, err := client.StatObjectWithResponse(ctx, repo, ingestionBranch, &api.StatObjectParams{
Path: largeObject,
})
require.NoError(t, err)
require.NotNil(t, res.JSON200)
destPath := "bar"
srcBranch := ingestionBranch
cancelCtx, cancel := context.WithCancel(ctx)
defer cancel()
var (
wg sync.WaitGroup
copyResp *api.CopyObjectResponse
copyErr error
)
// Run copy object async and cancel context after 5 seconds
wg.Add(1)
go func() {
defer wg.Done()
copyResp, copyErr = client.CopyObjectWithResponse(cancelCtx, repo, "main", &api.CopyObjectParams{
DestPath: destPath,
}, api.CopyObjectJSONRequestBody{
SrcPath: largeObject,
SrcRef: &srcBranch,
})
}()
time.Sleep(5 * time.Second)
cancel()
wg.Wait()
require.ErrorIs(t, copyErr, context.Canceled)
require.Nil(t, copyResp)
// Verify object doesn't exist
statResp, err := client.StatObjectWithResponse(ctx, repo, "main", &api.StatObjectParams{Path: destPath})
require.NoError(t, err)
require.Equal(t, http.StatusNotFound, statResp.StatusCode())
})
}
func getImportPath(t *testing.T) string {
t.Helper()
importPath := ""
blockstoreType := viper.GetString(config.BlockstoreTypeKey)
switch blockstoreType {
case block.BlockstoreTypeS3:
importPath = s3CopyDataPath
case block.BlockstoreTypeGS:
importPath = gsCopyDataPath
case block.BlockstoreTypeAzure:
importPath = azureCopyDataPath
default:
t.Skip("import isn't supported for non-production block adapters")
}
return importPath
}
func importTestData(t *testing.T, ctx context.Context, client api.ClientWithResponsesInterface, repoName, importPath string) {
var (
after = ""
token *string
ranges []api.RangeMetadata
)
for {
resp, err := client.IngestRangeWithResponse(ctx, repoName, api.IngestRangeJSONRequestBody{
After: after,
ContinuationToken: token,
FromSourceURI: importPath,
})
require.NoError(t, err, "failed to ingest range")
require.Equal(t, http.StatusCreated, resp.StatusCode())
require.NotNil(t, resp.JSON201)
ranges = append(ranges, *resp.JSON201.Range)
if !resp.JSON201.Pagination.HasMore {
break
}
after = resp.JSON201.Pagination.LastKey
token = resp.JSON201.Pagination.ContinuationToken
}
metarangeResp, err := client.CreateMetaRangeWithResponse(ctx, repoName, api.CreateMetaRangeJSONRequestBody{
Ranges: ranges,
})
require.NoError(t, err, "failed to create metarange")
require.NotNil(t, metarangeResp.JSON201)
require.NotNil(t, metarangeResp.JSON201.Id)
_, err = client.CreateBranchWithResponse(ctx, repoName, api.CreateBranchJSONRequestBody{
Name: ingestionBranch,
Source: "main",
})
require.NoError(t, err, "failed to create branch")
commitResp, err := client.CommitWithResponse(ctx, repoName, ingestionBranch, &api.CommitParams{
SourceMetarange: metarangeResp.JSON201.Id,
}, api.CommitJSONRequestBody{
Message: "created by import",
Metadata: &api.CommitCreation_Metadata{
AdditionalProperties: map[string]string{"created_by": "import"},
},
})
require.NoError(t, err, "failed to commit")
require.NotNil(t, commitResp.JSON201)
}