fix bug with copying a few files in order

This commit is contained in:
Maxim Lobanov 2020-08-07 09:38:30 +03:00
parent cf999e07db
commit dd2927f871
2 changed files with 61 additions and 2 deletions

View File

@ -196,15 +196,16 @@ func (p *Provisioner) ProvisionUpload(ui packer.Ui, comm packer.Communicator) er
return err return err
} }
filedst := dst
if strings.HasSuffix(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) pf := ui.TrackProgress(filepath.Base(src), 0, info.Size(), f)
defer pf.Close() defer pf.Close()
// Upload the file // 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") { if strings.Contains(err.Error(), "Error restoring file") {
ui.Error(fmt.Sprintf("Upload failed: %s; this can occur when "+ ui.Error(fmt.Sprintf("Upload failed: %s; this can occur when "+
"your file destination is a folder without a trailing "+ "your file destination is a folder without a trailing "+

View File

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
"testing" "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) { func TestProvisionDownloadMkdirAll(t *testing.T) {
tests := []struct { tests := []struct {
path string path string