fix local file stating (#9660)
This commit is contained in:
parent
b40490c3c1
commit
b695615d7d
|
@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue