Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix typo in scp upload size check #32206

Merged
merged 1 commit into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion internal/communicator/ssh/communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func (c *Communicator) Upload(path string, input io.Reader) error {
switch src := input.(type) {
case *os.File:
fi, err := src.Stat()
if err != nil {
if err == nil {
size = fi.Size()
}
case *bytes.Buffer:
Expand Down Expand Up @@ -641,7 +641,13 @@ func checkSCPStatus(r *bufio.Reader) error {
return nil
}

var testUploadSizeHook func(size int64)

func scpUploadFile(dst string, src io.Reader, w io.Writer, r *bufio.Reader, size int64) error {
if testUploadSizeHook != nil {
testUploadSizeHook(size)
}

if size == 0 {
// Create a temporary file where we can copy the contents of the src
// so that we can determine the length, since SCP is length-prefixed.
Expand Down
24 changes: 21 additions & 3 deletions internal/communicator/ssh/communicator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,10 +577,28 @@ func TestAccUploadFile(t *testing.T) {
}

tmpDir := t.TempDir()
source, err := os.CreateTemp(tmpDir, "tempfile.in")
if err != nil {
t.Fatal(err)
}

content := "this is the file content"
if _, err := source.WriteString(content); err != nil {
t.Fatal(err)
}
source.Seek(0, io.SeekStart)

content := []byte("this is the file content")
source := bytes.NewReader(content)
tmpFile := filepath.Join(tmpDir, "tempFile.out")

testUploadSizeHook = func(size int64) {
if size != int64(len(content)) {
t.Errorf("expected %d bytes, got %d\n", len(content), size)
}
}
defer func() {
testUploadSizeHook = nil
}()

err = c.Upload(tmpFile, source)
if err != nil {
t.Fatalf("error uploading file: %s", err)
Expand All @@ -591,7 +609,7 @@ func TestAccUploadFile(t *testing.T) {
t.Fatal(err)
}

if !bytes.Equal(data, content) {
if string(data) != content {
t.Fatalf("bad: %s", data)
}
}
Expand Down