From dd2927f8717b6f9b6edf1f92db46a36baebbad8f Mon Sep 17 00:00:00 2001 From: Maxim Lobanov Date: Fri, 7 Aug 2020 09:38:30 +0300 Subject: [PATCH] fix bug with copying a few files in order --- provisioner/file/provisioner.go | 5 ++- provisioner/file/provisioner_test.go | 58 ++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/provisioner/file/provisioner.go b/provisioner/file/provisioner.go index 96819b9bc..f8fcc403a 100644 --- a/provisioner/file/provisioner.go +++ b/provisioner/file/provisioner.go @@ -196,15 +196,16 @@ func (p *Provisioner) ProvisionUpload(ui packer.Ui, comm packer.Communicator) er return err } + filedst := dst if strings.HasSuffix(dst, "/") { - dst = dst + filepath.Base(src) + filedst = dst + filepath.Base(src) } pf := ui.TrackProgress(filepath.Base(src), 0, info.Size(), f) defer pf.Close() // Upload the file - if err = comm.Upload(dst, pf, &fi); err != nil { + if err = comm.Upload(filedst, pf, &fi); err != nil { if strings.Contains(err.Error(), "Error restoring file") { ui.Error(fmt.Sprintf("Upload failed: %s; this can occur when "+ "your file destination is a folder without a trailing "+ diff --git a/provisioner/file/provisioner_test.go b/provisioner/file/provisioner_test.go index 7239c709c..0986e1e05 100644 --- a/provisioner/file/provisioner_test.go +++ b/provisioner/file/provisioner_test.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" "path/filepath" + "regexp" "strings" "testing" @@ -268,6 +269,63 @@ func TestProvisionerProvision_SendsFileMultipleDirs(t *testing.T) { } } +func TestProvisionerProvision_SendsFileMultipleFilesToFolder(t *testing.T) { + var p Provisioner + + tf1, err := ioutil.TempFile("", "packer") + if err != nil { + t.Fatalf("error tempfile: %s", err) + } + defer os.Remove(tf1.Name()) + + if _, err = tf1.Write([]byte("hello")); err != nil { + t.Fatalf("error writing tempfile: %s", err) + } + + tf2, err := ioutil.TempFile("", "packer") + if err != nil { + t.Fatalf("error tempfile: %s", err) + } + defer os.Remove(tf2.Name()) + + if _, err = tf2.Write([]byte("hello")); err != nil { + t.Fatalf("error writing tempfile: %s", err) + } + + config := map[string]interface{}{ + "sources": []string{tf1.Name(), tf2.Name()}, + "destination": "something/", + } + + if err := p.Prepare(config); err != nil { + t.Fatalf("err: %s", err) + } + + b := bytes.NewBuffer(nil) + ui := &packer.BasicUi{ + Writer: b, + } + comm := &packer.MockCommunicator{} + err = p.Provision(context.Background(), ui, comm, make(map[string]interface{})) + if err != nil { + t.Fatalf("should successfully provision: %s", err) + } + + if !strings.Contains(b.String(), tf1.Name()) { + t.Fatalf("should print first source filename") + } + + if !strings.Contains(b.String(), tf2.Name()) { + t.Fatalf("should print second source filename") + } + + dstRegex := regexp.MustCompile("something/\n") + allDst := dstRegex.FindAllString(b.String(), -1) + if len(allDst) != 2 { + t.Fatalf("some destinations are broken; output: \n%s", b.String()) + } +} + func TestProvisionDownloadMkdirAll(t *testing.T) { tests := []struct { path string