From 3d3e1a5d4175c2cece68bf7c2c79d4ef35bc1d09 Mon Sep 17 00:00:00 2001 From: Piotr Fus Date: Tue, 29 Oct 2024 11:57:12 +0100 Subject: [PATCH] SNOW-1760222 Add test for raising PUT errors (#1230) --- file_transfer_agent_test.go | 95 ++++++++++++++++++++++++++++++++++++- test_data/.gitignore | 1 + 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 test_data/.gitignore diff --git a/file_transfer_agent_test.go b/file_transfer_agent_test.go index 69a10b393..8b2ea90d9 100644 --- a/file_transfer_agent_test.go +++ b/file_transfer_agent_test.go @@ -13,6 +13,7 @@ import ( "os" "path" "path/filepath" + "strconv" "strings" "testing" "time" @@ -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")) diff --git a/test_data/.gitignore b/test_data/.gitignore new file mode 100644 index 000000000..d5329f891 --- /dev/null +++ b/test_data/.gitignore @@ -0,0 +1 @@ +writeonly.csv \ No newline at end of file