Skip to content

Commit

Permalink
SNOW-1760222 Add test for raising PUT errors (#1230)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pfus authored Oct 29, 2024
1 parent a5b6b78 commit 3d3e1a5
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
95 changes: 94 additions & 1 deletion file_transfer_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"path"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -646,7 +647,99 @@ func TestUploadWhenFilesystemReadOnlyError(t *testing.T) {
}
}

func TestUnitUpdateProgess(t *testing.T) {
func TestUploadWhenErrorWithResultIsReturned(t *testing.T) {
if isWindows {
t.Skip("permission model is different")
}

for _, tc := range []struct {
shouldRaiseError bool
resultCondition func(t *testing.T, err error)
}{
{
shouldRaiseError: false,
resultCondition: func(t *testing.T, err error) {
assertNilE(t, err)
},
},
{
shouldRaiseError: true,
resultCondition: func(t *testing.T, err error) {
assertNotNilE(t, err)
},
},
} {
t.Run(strconv.FormatBool(tc.shouldRaiseError), func(t *testing.T) {
var err error

dir, err := os.Getwd()
assertNilF(t, err)
err = createWriteonlyFile(path.Join(dir, "test_data"), "writeonly.csv")
assertNilF(t, err)

uploadMeta := fileMetadata{
name: "data1.txt.gz",
stageLocationType: "GCS",
noSleepingTime: true,
client: local,
sha256Digest: "123456789abcdef",
stageInfo: &execResponseStageInfo{
Location: dir,
LocationType: "local",
},
dstFileName: "data1.txt.gz",
srcFileName: path.Join(dir, "test_data/writeonly.csv"),
overwrite: true,
}

sfa := &snowflakeFileTransferAgent{
ctx: context.Background(),
sc: &snowflakeConn{
cfg: &Config{
TmpDirPath: dir,
},
},
data: &execResponseData{
SrcLocations: []string{path.Join(dir, "/test_data/writeonly.csv")},
Command: "UPLOAD",
SourceCompression: "none",
StageInfo: execResponseStageInfo{
LocationType: "LOCAL_FS",
Location: dir,
},
},
commandType: uploadCommand,
command: fmt.Sprintf("put file://%v/test_data/data1.txt @~", dir),
stageLocationType: local,
fileMetadata: []*fileMetadata{&uploadMeta},
parallel: 1,
options: &SnowflakeFileTransferOptions{
RaisePutGetError: tc.shouldRaiseError,
},
}

err = sfa.execute()
assertNilF(t, err) // execute should not propagate errors, it should be returned by sfa.result only
_, err = sfa.result()
tc.resultCondition(t, err)
})
}
}

func createWriteonlyFile(dir, filename string) error {
path := path.Join(dir, filename)
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
if _, err := os.Create(path); err != nil {
return err
}
}
if err := os.Chmod(path, 0222); err != nil {
return err
}
return nil
}

func TestUnitUpdateProgress(t *testing.T) {
var b bytes.Buffer
buf := io.Writer(&b)
buf.Write([]byte("testing"))
Expand Down
1 change: 1 addition & 0 deletions test_data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
writeonly.csv

0 comments on commit 3d3e1a5

Please sign in to comment.