forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_storage_client_test.go
172 lines (159 loc) · 4.21 KB
/
local_storage_client_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
// Copyright (c) 2023 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"bytes"
"compress/gzip"
"context"
"os"
"path"
"path/filepath"
"testing"
)
func TestLocalUpload(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "local_put")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(tmpDir)
fname := filepath.Join(tmpDir, "test_put_get.txt.gz")
originalContents := "123,test1\n456,test2\n"
var b bytes.Buffer
gzw := gzip.NewWriter(&b)
gzw.Write([]byte(originalContents))
gzw.Close()
if err := os.WriteFile(fname, b.Bytes(), readWriteFileMode); err != nil {
t.Fatal("could not write to gzip file")
}
putDir, err := os.MkdirTemp("", "put")
if err != nil {
t.Error(err)
}
info := execResponseStageInfo{
Location: putDir,
LocationType: "LOCAL_FS",
}
localUtil := new(localUtil)
localCli, err := localUtil.createClient(&info, false)
if err != nil {
t.Error(err)
}
uploadMeta := fileMetadata{
name: "data1.txt.gz",
stageLocationType: "LOCAL_FS",
noSleepingTime: true,
parallel: 4,
client: localCli,
stageInfo: &info,
dstFileName: "data1.txt.gz",
srcFileName: path.Join(tmpDir, "/test_put_get.txt.gz"),
overwrite: true,
options: &SnowflakeFileTransferOptions{
MultiPartThreshold: dataSizeThreshold,
},
}
uploadMeta.realSrcFileName = uploadMeta.srcFileName
err = localUtil.uploadOneFileWithRetry(&uploadMeta)
if err != nil {
t.Error(err)
}
if uploadMeta.resStatus != uploaded {
t.Fatalf("failed to upload file")
}
uploadMeta.overwrite = false
err = localUtil.uploadOneFileWithRetry(&uploadMeta)
if err != nil {
t.Error(err)
}
if uploadMeta.resStatus != skipped {
t.Fatal("overwrite is false. should have skipped")
}
fileStream, _ := os.Open(fname)
ctx := WithFileStream(context.Background(), fileStream)
uploadMeta.srcStream = getFileStream(ctx)
err = localUtil.uploadOneFileWithRetry(&uploadMeta)
if err != nil {
t.Error(err)
}
if uploadMeta.resStatus != skipped {
t.Fatalf("overwrite is false. should have skipped")
}
uploadMeta.overwrite = true
err = localUtil.uploadOneFileWithRetry(&uploadMeta)
if err != nil {
t.Error(err)
}
if uploadMeta.resStatus != uploaded {
t.Fatalf("failed to upload file")
}
uploadMeta.realSrcStream = uploadMeta.srcStream
err = localUtil.uploadOneFileWithRetry(&uploadMeta)
if err != nil {
t.Error(err)
}
if uploadMeta.resStatus != uploaded {
t.Fatalf("failed to upload file")
}
}
func TestDownloadLocalFile(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "local_put")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(tmpDir)
fname := filepath.Join(tmpDir, "test_put_get.txt.gz")
originalContents := "123,test1\n456,test2\n"
var b bytes.Buffer
gzw := gzip.NewWriter(&b)
gzw.Write([]byte(originalContents))
gzw.Close()
if err := os.WriteFile(fname, b.Bytes(), readWriteFileMode); err != nil {
t.Fatal("could not write to gzip file")
}
putDir, err := os.MkdirTemp("", "put")
if err != nil {
t.Error(err)
}
info := execResponseStageInfo{
Location: tmpDir,
LocationType: "LOCAL_FS",
}
localUtil := new(localUtil)
localCli, err := localUtil.createClient(&info, false)
if err != nil {
t.Error(err)
}
downloadMeta := fileMetadata{
name: "test_put_get.txt.gz",
stageLocationType: "LOCAL_FS",
noSleepingTime: true,
client: localCli,
stageInfo: &info,
dstFileName: "test_put_get.txt.gz",
overwrite: true,
srcFileName: "test_put_get.txt.gz",
localLocation: putDir,
options: &SnowflakeFileTransferOptions{
MultiPartThreshold: dataSizeThreshold,
},
}
err = localUtil.downloadOneFile(&downloadMeta)
if err != nil {
t.Error(err)
}
if downloadMeta.resStatus != downloaded {
t.Fatalf("failed to get file in local storage")
}
downloadMeta.srcFileName = "test_put_get.txt.gz"
err = localUtil.downloadOneFile(&downloadMeta)
if err != nil {
t.Error(err)
}
if downloadMeta.resStatus != downloaded {
t.Fatalf("failed to get file in local storage")
}
downloadMeta.srcFileName = "local://test_put_get.txt.gz"
err = localUtil.downloadOneFile(&downloadMeta)
if err == nil {
t.Error("file name is invalid. should have returned an error")
}
}