Merge pull request #9725 from maxim-lobanov/file-provisioner-sources

Bug: "file" provisioner only uses first entry from Sources if processing directories
This commit is contained in:
Megan Marsh 2020-08-07 08:06:35 -07:00 committed by GitHub
commit 97acdf2374
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 185 additions and 3 deletions

View File

@ -177,7 +177,11 @@ func (p *Provisioner) ProvisionUpload(ui packer.Ui, comm packer.Communicator) er
// If we're uploading a directory, short circuit and do that
if info.IsDir() {
return comm.UploadDir(dst, src, nil)
if err = comm.UploadDir(dst, src, nil); err != nil {
ui.Error(fmt.Sprintf("Upload failed: %s", err))
return err
}
continue
}
// We're uploading a file...
@ -192,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 "+

View File

@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
@ -149,6 +150,182 @@ func TestProvisionerProvision_SendsFile(t *testing.T) {
}
}
func TestProvisionerProvision_SendsFileMultipleFiles(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")
}
}
func TestProvisionerProvision_SendsFileMultipleDirs(t *testing.T) {
var p Provisioner
// Prepare the first directory
td1, err := ioutil.TempDir("", "packerdir")
if err != nil {
t.Fatalf("error temp folder 1: %s", err)
}
defer os.Remove(td1)
tf1, err := ioutil.TempFile(td1, "packer")
if err != nil {
t.Fatalf("error tempfile: %s", err)
}
if _, err = tf1.Write([]byte("hello")); err != nil {
t.Fatalf("error writing tempfile: %s", err)
}
// Prepare the second directory
td2, err := ioutil.TempDir("", "packerdir")
if err != nil {
t.Fatalf("error temp folder 1: %s", err)
}
defer os.Remove(td2)
tf2, err := ioutil.TempFile(td2, "packer")
if err != nil {
t.Fatalf("error tempfile: %s", err)
}
if _, err = tf2.Write([]byte("hello")); err != nil {
t.Fatalf("error writing tempfile: %s", err)
}
if _, err = tf1.Write([]byte("hello")); err != nil {
t.Fatalf("error writing tempfile: %s", err)
}
// Run Provision
config := map[string]interface{}{
"sources": []string{td1, td2},
"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(), td1) {
t.Fatalf("should print first directory")
}
if !strings.Contains(b.String(), td2) {
t.Fatalf("should print second directory")
}
}
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