Skip to content

Commit

Permalink
packer: warn on bundled plugins usage
Browse files Browse the repository at this point in the history
Since bundled plugins will be removed in an upcoming version of Packer,
this commit adds a new warning message whenever a template uses one such
plugin.

This warning has been implemented on build, validate, console and the
inspect subcommands.

In addition to warning about the upcoming change and potential issue
this will cause, this warning message proposes solutions to the user so
they know what they'll have to do in order not to rely on those bundled
plugins later.
  • Loading branch information
lbajolet-hashicorp committed Jul 18, 2023
1 parent 2824320 commit a2930bd
Show file tree
Hide file tree
Showing 13 changed files with 603 additions and 16 deletions.
181 changes: 181 additions & 0 deletions acctest/plugin/bundled_plugin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package plugin

import (
_ "embed"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"testing"

"github.com/hashicorp/go-multierror"
amazonacc "github.com/hashicorp/packer-plugin-amazon/builder/ebs/acceptance"
"github.com/hashicorp/packer-plugin-sdk/acctest"
"github.com/hashicorp/packer/hcl2template/addrs"
)

//go:embed test-fixtures/basic_amazon_bundled.pkr.hcl
var basicAmazonBundledEbsTemplate string

func TestAccBuildBundledPlugins(t *testing.T) {
plugin := addrs.Plugin{
Hostname: "github.com",
Namespace: "hashicorp",
Type: "amazon",
}
testCase := &acctest.PluginTestCase{
Name: "amazon-ebs_bundled_test",
Setup: func() error {
return cleanupPluginInstallation(plugin)
},
Teardown: func() error {
helper := amazonacc.AMIHelper{
Region: "us-east-1",
Name: "packer-plugin-bundled-amazon-ebs-test",
}
return helper.CleanUpAmi()
},
Template: basicAmazonBundledEbsTemplate,
Type: "amazon-ebs",
Init: false,
Check: func(buildCommand *exec.Cmd, logfile string) error {
if buildCommand.ProcessState != nil {
if buildCommand.ProcessState.ExitCode() != 0 {
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
}
}

rawLogs, err := os.ReadFile(logfile)
if err != nil {
return fmt.Errorf("failed to read logs: %s", err)
}

var errs error

logs := string(rawLogs)

if !strings.Contains(logs, "Warning: Bundled plugins used") {
errs = multierror.Append(errs, errors.New("expected warning about bundled plugins used, did not find it"))
}

if !strings.Contains(logs, "Then run 'packer init' to manage installation of the plugins") {
errs = multierror.Append(errs, errors.New("expected suggestion about packer init in logs, did not find it."))
}

return errs
},
}

acctest.TestPlugin(t, testCase)
}

//go:embed test-fixtures/basic_amazon_with_required_plugins.pkr.hcl
var basicAmazonRequiredPluginEbsTemplate string

func TestAccBuildBundledPluginsWithRequiredPlugins(t *testing.T) {
plugin := addrs.Plugin{
Hostname: "github.com",
Namespace: "hashicorp",
Type: "amazon",
}
testCase := &acctest.PluginTestCase{
Name: "amazon-ebs_with_required_plugins_test",
Setup: func() error {
return cleanupPluginInstallation(plugin)
},
Teardown: func() error {
helper := amazonacc.AMIHelper{
Region: "us-east-1",
Name: "packer-plugin-required-plugin-amazon-ebs-test",
}
return helper.CleanUpAmi()
},
Template: basicAmazonRequiredPluginEbsTemplate,
Type: "amazon-ebs",
Init: false,
Check: func(buildCommand *exec.Cmd, logfile string) error {
if buildCommand.ProcessState != nil {
if buildCommand.ProcessState.ExitCode() != 1 {
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
}
}

rawLogs, err := os.ReadFile(logfile)
if err != nil {
return fmt.Errorf("failed to read logs: %s", err)
}

var errs error

logs := string(rawLogs)

if strings.Contains(logs, "Warning: Bundled plugins used") {
errs = multierror.Append(errs, errors.New("did not expect warning about bundled plugins used"))
}

if !strings.Contains(logs, "Missing plugins") {
errs = multierror.Append(errs, errors.New("expected error about plugins required and not installed, did not find it"))
}

return errs
},
}

acctest.TestPlugin(t, testCase)
}

//go:embed test-fixtures/basic_amazon_bundled.json
var basicAmazonBundledEbsTemplateJSON string

func TestAccBuildBundledPluginsJSON(t *testing.T) {
plugin := addrs.Plugin{
Hostname: "github.com",
Namespace: "hashicorp",
Type: "amazon",
}
testCase := &acctest.PluginTestCase{
Name: "amazon-ebs_bundled_test_json",
Setup: func() error {
return cleanupPluginInstallation(plugin)
},
Teardown: func() error {
helper := amazonacc.AMIHelper{
Region: "us-east-1",
Name: "packer-plugin-bundled-amazon-ebs-test-json",
}
return helper.CleanUpAmi()
},
Template: basicAmazonBundledEbsTemplateJSON,
Type: "amazon-ebs",
Init: false,
Check: func(buildCommand *exec.Cmd, logfile string) error {
if buildCommand.ProcessState != nil {
if buildCommand.ProcessState.ExitCode() != 0 {
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
}
}

rawLogs, err := os.ReadFile(logfile)
if err != nil {
return fmt.Errorf("failed to read logs: %s", err)
}

var errs error

logs := string(rawLogs)

if !strings.Contains(logs, "Warning: Bundled plugins used") {
errs = multierror.Append(errs, errors.New("expected warning about bundled plugins, did not find it."))
}

if !strings.Contains(logs, "plugins with the 'packer plugins install' command") {
errs = multierror.Append(errs, errors.New("expected suggestion about packer plugins install in logs, did not find it."))
}

return errs
},
}

acctest.TestPlugin(t, testCase)
}
10 changes: 10 additions & 0 deletions acctest/plugin/test-fixtures/basic_amazon_bundled.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"builders": [{
"type": "amazon-ebs",
"region": "us-east-1",
"instance_type": "m3.medium",
"source_ami": "ami-76b2a71e",
"ssh_username": "ubuntu",
"ami_name": "packer-plugin-bundled-amazon-ebs-test-json"
}]
}
11 changes: 11 additions & 0 deletions acctest/plugin/test-fixtures/basic_amazon_bundled.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
source "amazon-ebs" "basic-test" {
region = "us-east-1"
instance_type = "m3.medium"
source_ami = "ami-76b2a71e"
ssh_username = "ubuntu"
ami_name = "packer-plugin-bundled-amazon-ebs-test"
}

build {
sources = ["source.amazon-ebs.basic-test"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
packer {
required_plugins {
amazon = {
source = "github.com/hashicorp/amazon",
version = "~> 1"
}
}
}

source "amazon-ebs" "basic-test" {
region = "us-east-1"
instance_type = "m3.medium"
source_ami = "ami-76b2a71e"
ssh_username = "ubuntu"
ami_name = "packer-plugin-bundled-amazon-ebs-test"
}

build {
sources = ["source.amazon-ebs.basic-test"]
}
10 changes: 9 additions & 1 deletion command/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,15 @@ func (c *BuildCommand) RunContext(buildCtx context.Context, cla *BuildArgs) int
return ret
}

diags := packerStarter.Initialize(packer.InitializeOptions{})
diags := packerStarter.DetectPluginBinaries()
ret = writeDiags(c.Ui, nil, diags)
if ret != 0 {
return ret
}

diags = packerStarter.Initialize(packer.InitializeOptions{})
bundledDiags := c.DetectBundledPlugins(packerStarter)
diags = append(bundledDiags, diags...)
ret = writeDiags(c.Ui, nil, diags)
if ret != 0 {
return ret
Expand Down
Loading

0 comments on commit a2930bd

Please sign in to comment.