fix local file stating (#9660)

This commit is contained in:
Megan Marsh 2020-07-29 01:26:09 -07:00 committed by GitHub
parent b40490c3c1
commit b695615d7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import (
"context"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
@ -194,10 +195,19 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
if b.config.GlobalID != "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("You may either set global_id or source_path but not both"))
}
// We're about to open up an actual boxfile. If the file is local to the
// filesystem, let's make sure it exists before we get too far into the
// build.
if strings.HasSuffix(b.config.SourceBox, ".box") {
if _, err := os.Stat(b.config.SourceBox); err != nil {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Source box '%s' needs to exist at time of config validation! %v", b.config.SourceBox, err))
// If scheme is "file" or empty, then we need to check the
// filesystem to make sure the box is present locally.
u, err := url.Parse(b.config.SourceBox)
if err == nil && (u.Scheme == "" || u.Scheme == "file") {
if _, err := os.Stat(b.config.SourceBox); err != nil {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("Source box '%s' needs to exist at time of"+
" config validation! %v", b.config.SourceBox, err))
}
}
}
}

View File

@ -79,6 +79,46 @@ func TestBuilder_Prepare_ValidateSource(t *testing.T) {
errExpected: true,
reason: "Inalid argument for teardown method",
},
{
config: map[string]interface{}{
"communicator": "ssh",
"source_path": "./my.box",
},
errExpected: true,
reason: "Should fail because path does not exist",
},
{
config: map[string]interface{}{
"communicator": "ssh",
"source_path": "file://my.box",
},
errExpected: true,
reason: "Should fail because path does not exist",
},
{
config: map[string]interface{}{
"communicator": "ssh",
"source_path": "http://my.box",
},
errExpected: false,
reason: "Should pass because path is not local",
},
{
config: map[string]interface{}{
"communicator": "ssh",
"source_path": "https://my.box",
},
errExpected: false,
reason: "Should pass because path is not local",
},
{
config: map[string]interface{}{
"communicator": "ssh",
"source_path": "smb://my.box",
},
errExpected: false,
reason: "Should pass because path is not local",
},
}
for _, tc := range cases {