|
| 1 | +package plugin |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/hashicorp/go-multierror" |
| 13 | + amazonacc "github.com/hashicorp/packer-plugin-amazon/builder/ebs/acceptance" |
| 14 | + "github.com/hashicorp/packer-plugin-sdk/acctest" |
| 15 | + "github.com/hashicorp/packer/hcl2template/addrs" |
| 16 | +) |
| 17 | + |
| 18 | +//go:embed test-fixtures/basic_amazon_bundled.pkr.hcl |
| 19 | +var basicAmazonBundledEbsTemplate string |
| 20 | + |
| 21 | +func TestAccBuildBundledPlugins(t *testing.T) { |
| 22 | + plugin := addrs.Plugin{ |
| 23 | + Hostname: "github.com", |
| 24 | + Namespace: "hashicorp", |
| 25 | + Type: "amazon", |
| 26 | + } |
| 27 | + testCase := &acctest.PluginTestCase{ |
| 28 | + Name: "amazon-ebs_bundled_test", |
| 29 | + Setup: func() error { |
| 30 | + return cleanupPluginInstallation(plugin) |
| 31 | + }, |
| 32 | + Teardown: func() error { |
| 33 | + helper := amazonacc.AMIHelper{ |
| 34 | + Region: "us-east-1", |
| 35 | + Name: "packer-plugin-bundled-amazon-ebs-test", |
| 36 | + } |
| 37 | + return helper.CleanUpAmi() |
| 38 | + }, |
| 39 | + Template: basicAmazonBundledEbsTemplate, |
| 40 | + Type: "amazon-ebs", |
| 41 | + Init: false, |
| 42 | + Check: func(buildCommand *exec.Cmd, logfile string) error { |
| 43 | + if buildCommand.ProcessState != nil { |
| 44 | + if buildCommand.ProcessState.ExitCode() != 0 { |
| 45 | + return fmt.Errorf("Bad exit code. Logfile: %s", logfile) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + rawLogs, err := os.ReadFile(logfile) |
| 50 | + if err != nil { |
| 51 | + return fmt.Errorf("failed to read logs: %s", err) |
| 52 | + } |
| 53 | + |
| 54 | + var errs error |
| 55 | + |
| 56 | + logs := string(rawLogs) |
| 57 | + |
| 58 | + if !strings.Contains(logs, "Warning: Bundled plugins used") { |
| 59 | + errs = multierror.Append(errs, errors.New("expected warning about bundled plugins used, did not find it")) |
| 60 | + } |
| 61 | + |
| 62 | + if !strings.Contains(logs, "Then run 'packer init' to manage installation of the plugins") { |
| 63 | + errs = multierror.Append(errs, errors.New("expected suggestion about packer init in logs, did not find it.")) |
| 64 | + } |
| 65 | + |
| 66 | + return errs |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + acctest.TestPlugin(t, testCase) |
| 71 | +} |
| 72 | + |
| 73 | +//go:embed test-fixtures/basic_amazon_with_required_plugins.pkr.hcl |
| 74 | +var basicAmazonRequiredPluginEbsTemplate string |
| 75 | + |
| 76 | +func TestAccBuildBundledPluginsWithRequiredPlugins(t *testing.T) { |
| 77 | + plugin := addrs.Plugin{ |
| 78 | + Hostname: "github.com", |
| 79 | + Namespace: "hashicorp", |
| 80 | + Type: "amazon", |
| 81 | + } |
| 82 | + testCase := &acctest.PluginTestCase{ |
| 83 | + Name: "amazon-ebs_with_required_plugins_test", |
| 84 | + Setup: func() error { |
| 85 | + return cleanupPluginInstallation(plugin) |
| 86 | + }, |
| 87 | + Teardown: func() error { |
| 88 | + helper := amazonacc.AMIHelper{ |
| 89 | + Region: "us-east-1", |
| 90 | + Name: "packer-plugin-required-plugin-amazon-ebs-test", |
| 91 | + } |
| 92 | + return helper.CleanUpAmi() |
| 93 | + }, |
| 94 | + Template: basicAmazonRequiredPluginEbsTemplate, |
| 95 | + Type: "amazon-ebs", |
| 96 | + Init: false, |
| 97 | + Check: func(buildCommand *exec.Cmd, logfile string) error { |
| 98 | + if buildCommand.ProcessState != nil { |
| 99 | + if buildCommand.ProcessState.ExitCode() != 1 { |
| 100 | + return fmt.Errorf("Bad exit code. Logfile: %s", logfile) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + rawLogs, err := os.ReadFile(logfile) |
| 105 | + if err != nil { |
| 106 | + return fmt.Errorf("failed to read logs: %s", err) |
| 107 | + } |
| 108 | + |
| 109 | + var errs error |
| 110 | + |
| 111 | + logs := string(rawLogs) |
| 112 | + |
| 113 | + if strings.Contains(logs, "Warning: Bundled plugins used") { |
| 114 | + errs = multierror.Append(errs, errors.New("did not expect warning about bundled plugins used")) |
| 115 | + } |
| 116 | + |
| 117 | + if !strings.Contains(logs, "Missing plugins") { |
| 118 | + errs = multierror.Append(errs, errors.New("expected error about plugins required and not installed, did not find it")) |
| 119 | + } |
| 120 | + |
| 121 | + return errs |
| 122 | + }, |
| 123 | + } |
| 124 | + |
| 125 | + acctest.TestPlugin(t, testCase) |
| 126 | +} |
| 127 | + |
| 128 | +//go:embed test-fixtures/basic_amazon_bundled.json |
| 129 | +var basicAmazonBundledEbsTemplateJSON string |
| 130 | + |
| 131 | +func TestAccBuildBundledPluginsJSON(t *testing.T) { |
| 132 | + plugin := addrs.Plugin{ |
| 133 | + Hostname: "github.com", |
| 134 | + Namespace: "hashicorp", |
| 135 | + Type: "amazon", |
| 136 | + } |
| 137 | + testCase := &acctest.PluginTestCase{ |
| 138 | + Name: "amazon-ebs_bundled_test_json", |
| 139 | + Setup: func() error { |
| 140 | + return cleanupPluginInstallation(plugin) |
| 141 | + }, |
| 142 | + Teardown: func() error { |
| 143 | + helper := amazonacc.AMIHelper{ |
| 144 | + Region: "us-east-1", |
| 145 | + Name: "packer-plugin-bundled-amazon-ebs-test-json", |
| 146 | + } |
| 147 | + return helper.CleanUpAmi() |
| 148 | + }, |
| 149 | + Template: basicAmazonBundledEbsTemplateJSON, |
| 150 | + Type: "amazon-ebs", |
| 151 | + Init: false, |
| 152 | + Check: func(buildCommand *exec.Cmd, logfile string) error { |
| 153 | + if buildCommand.ProcessState != nil { |
| 154 | + if buildCommand.ProcessState.ExitCode() != 0 { |
| 155 | + return fmt.Errorf("Bad exit code. Logfile: %s", logfile) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + rawLogs, err := os.ReadFile(logfile) |
| 160 | + if err != nil { |
| 161 | + return fmt.Errorf("failed to read logs: %s", err) |
| 162 | + } |
| 163 | + |
| 164 | + var errs error |
| 165 | + |
| 166 | + logs := string(rawLogs) |
| 167 | + |
| 168 | + if !strings.Contains(logs, "Warning: Bundled plugins used") { |
| 169 | + errs = multierror.Append(errs, errors.New("expected warning about bundled plugins, did not find it.")) |
| 170 | + } |
| 171 | + |
| 172 | + if !strings.Contains(logs, "plugins with the 'packer plugins install' command") { |
| 173 | + errs = multierror.Append(errs, errors.New("expected suggestion about packer plugins install in logs, did not find it.")) |
| 174 | + } |
| 175 | + |
| 176 | + return errs |
| 177 | + }, |
| 178 | + } |
| 179 | + |
| 180 | + acctest.TestPlugin(t, testCase) |
| 181 | +} |
0 commit comments