From 85b8a654b0f9e8e569cb0a5c90dcc3beb1c4253e Mon Sep 17 00:00:00 2001 From: Aaron Walker Date: Tue, 21 May 2019 15:26:07 +0200 Subject: [PATCH] only sets default value for chef license when installing chef and the install command is not set --- provisioner/chef-client/provisioner.go | 6 +++-- provisioner/chef-client/provisioner_test.go | 26 +++++++++++++++++++++ provisioner/chef-solo/provisioner.go | 6 +++-- provisioner/chef-solo/provisioner_test.go | 26 +++++++++++++++++++++ 4 files changed, 60 insertions(+), 4 deletions(-) diff --git a/provisioner/chef-client/provisioner.go b/provisioner/chef-client/provisioner.go index 2bbaa6cca..c08cff859 100644 --- a/provisioner/chef-client/provisioner.go +++ b/provisioner/chef-client/provisioner.go @@ -196,8 +196,10 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { errs, fmt.Errorf("server_url must be set")) } - if p.config.ChefLicense == "" { - p.config.ChefLicense = "accept-silent" + if p.config.SkipInstall == false && p.config.InstallCommand == p.guestOSTypeConfig.installCommand { + if p.config.ChefLicense == "" { + p.config.ChefLicense = "accept-silent" + } } if p.config.EncryptedDataBagSecretPath != "" { diff --git a/provisioner/chef-client/provisioner_test.go b/provisioner/chef-client/provisioner_test.go index 8bdfdd059..271726f3e 100644 --- a/provisioner/chef-client/provisioner_test.go +++ b/provisioner/chef-client/provisioner_test.go @@ -164,6 +164,32 @@ func TestProvisionerPrepare_chefLicense(t *testing.T) { if p.config.ChefLicense != "accept" { t.Fatalf("unexpected: %#v", p.config.ChefLicense) } + + // Test set skipInstall true + config = testConfig() + config["skip_install"] = true + p = Provisioner{} + err = p.Prepare(config) + if err != nil { + t.Fatalf("err: %s", err) + } + + if p.config.ChefLicense != "" { + t.Fatalf("unexpected: %#v", "empty string") + } + + // Test set installCommand true + config = testConfig() + config["install_command"] = "install chef" + p = Provisioner{} + err = p.Prepare(config) + if err != nil { + t.Fatalf("err: %s", err) + } + + if p.config.ChefLicense != "" { + t.Fatalf("unexpected: %#v", "empty string") + } } func TestProvisionerPrepare_encryptedDataBagSecretPath(t *testing.T) { diff --git a/provisioner/chef-solo/provisioner.go b/provisioner/chef-solo/provisioner.go index df8cb7d33..a28632466 100644 --- a/provisioner/chef-solo/provisioner.go +++ b/provisioner/chef-solo/provisioner.go @@ -146,8 +146,10 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { p.config.StagingDir = p.guestOSTypeConfig.stagingDir } - if p.config.ChefLicense == "" { - p.config.ChefLicense = "accept-silent" + if p.config.SkipInstall == false && p.config.InstallCommand == p.guestOSTypeConfig.installCommand { + if p.config.ChefLicense == "" { + p.config.ChefLicense = "accept-silent" + } } var errs *packer.MultiError diff --git a/provisioner/chef-solo/provisioner_test.go b/provisioner/chef-solo/provisioner_test.go index 6fd9e7eee..6df4c378c 100644 --- a/provisioner/chef-solo/provisioner_test.go +++ b/provisioner/chef-solo/provisioner_test.go @@ -61,6 +61,32 @@ func TestProvisionerPrepare_chefLicense(t *testing.T) { if p.config.ChefLicense != "accept" { t.Fatalf("unexpected: %#v", p.config.ChefLicense) } + + // Test set skipInstall true + config = testConfig() + config["skip_install"] = true + p = Provisioner{} + err = p.Prepare(config) + if err != nil { + t.Fatalf("err: %s", err) + } + + if p.config.ChefLicense != "" { + t.Fatalf("unexpected: %#v", "empty string") + } + + // Test set installCommand true + config = testConfig() + config["install_command"] = "install chef" + p = Provisioner{} + err = p.Prepare(config) + if err != nil { + t.Fatalf("err: %s", err) + } + + if p.config.ChefLicense != "" { + t.Fatalf("unexpected: %#v", "empty string") + } } func TestProvisionerPrepare_configTemplate(t *testing.T) {