Merge pull request #6848 from mayn/issue-6831

builder/googlecompute: validate startup_script_file exists
This commit is contained in:
Adrien Delorme 2018-10-16 17:39:36 +02:00 committed by GitHub
commit d8345a5bde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package googlecompute
import (
"errors"
"fmt"
"os"
"regexp"
"time"
@ -231,6 +232,13 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
errs = packer.MultiErrorAppend(fmt.Errorf("you may not specify a 'service_account_email' when 'disable_default_service_account' is true"))
}
if c.StartupScriptFile != "" {
if _, err := os.Stat(c.StartupScriptFile); err != nil {
errs = packer.MultiErrorAppend(
errs, fmt.Errorf("startup_script_file: %v", err))
}
}
// Check for any errors.
if errs != nil && len(errs.Errors) > 0 {
return nil, nil, errs

View File

@ -329,6 +329,22 @@ func TestConfigPrepareServiceAccount(t *testing.T) {
}
}
func TestConfigPrepareStartupScriptFile(t *testing.T) {
config := map[string]interface{}{
"project_id": "project",
"source_image": "foo",
"ssh_username": "packer",
"startup_script_file": "no-such-file",
"zone": "us-central1-a",
}
_, _, errs := NewConfig(config)
if errs == nil || !strings.Contains(errs.Error(), "startup_script_file") {
t.Fatalf("should error: startup_script_file")
}
}
func TestConfigDefaults(t *testing.T) {
cases := []struct {
Read func(c *Config) interface{}