Skip to content

Commit

Permalink
Merge pull request #32206 from hashicorp/jbardin/communicator-size
Browse files Browse the repository at this point in the history
fix typo in scp upload size check
  • Loading branch information
jbardin committed Nov 14, 2022
2 parents 0c7fda1 + 8ba8d5a commit 242b8a7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
8 changes: 7 additions & 1 deletion internal/communicator/ssh/communicator.go
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
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

0 comments on commit 242b8a7

Please sign in to comment.