Files
Packer-Cn/packer/plugin-getter/plugins_test.go
9408d6d0ad Release Packer version 1.16.1 (#13708)
* hcl2template: pass user variable values to plugins as packer_user_variables (#13686)

HCL2 builds do not send the packer_user_variables config key to the
builder, provisioner and post-processor plugins. Legacy JSON builds
send this key (through CoreBuild.packerConfig()). Without the key, the
plugin SDK keeps interpolate.Context.UserVariables nil. Then the
template function {{ user "name" }} fails with 'error calling user:
test'. This failure occurs in each string that a plugin interpolates at
run time, for example the contents of the vagrant post-processor's
vagrantfile_template.

Add the function PackerConfig.userVariableValues(). This function
converts the input variable values to strings, equivalent to the legacy
user variables. The core sends the map at each plugin handoff point:
builder, provisioner, post-processor, and enforced provisioner.

The map does not contain the sensitive variables. A sensitive value
goes to a plugin only if the template refers to it explicitly. The map
does not contain values that are not primitive (lists, maps, objects).
Legacy user variables were always strings.

Co-authored-by: Claude Fable 5 <[email protected]>

* Dependency upgrade (#13689)

* go.mod version upgrade

* actions upgrade

* plugin-getter: install plugins from non-GitHub HTTP sources (#13691)

Packer can only install plugins from github.com, with
releases.hashicorp.com consulted first for HashiCorp-published plugins.
This has been a long-standing gap for air-gapped and policy-restricted
environments (#11164): the source address parser already accepts any
hostname, but both existing getters reject non-github.com sources at
install time.

Add a remote plugin getter that installs plugins from the host named in
a required_plugins source address. The host serves the directory
structure of releases.hashicorp.com under the source's path: an
index.json listing versions, and per version a SHA256SUMS file, the
zips it lists, and - when the zip names carry no plugin protocol
version - the version's manifest.json. A plugin published on
releases.hashicorp.com is therefore mirrored as a verbatim copy of its
tree, with every checksum file and signature upstream-authored. A
plugin published as GitHub release assets is mirrored by copying each
release's assets into a version directory, renaming their SHA256SUMS
file to the unprefixed convention with content unchanged, and writing
an index.json listing the versions. Both kinds of content can be
served side by side by one host.

Getter selection happens per source address: github.com sources keep
the release and github getters unchanged, while any other host is
served by the remote getter over HTTPS. Sources with three or more
components are supported, up to the existing 16-component limit, so
nested artifact-repository paths and hosts that embed the upstream
origin in their path all resolve.

Version discovery, constraint solving, checksum verification, and the
binary naming rules match the existing getters. The installed filename
is rebuilt from validated checksum-file fields and never taken from the
server's response, checksum entries matching neither known naming shape
are rejected rather than guessed at, and nothing the remote metadata
supplies is used to fetch from another origin or path. index.json
parsing is covered by fixtures captured from the live releases API, so
a format change there fails tests rather than user installs.

Closes #11164

* fix: update link to CONTRIBUTING.md for consistency with main branch

* Merge pull request #13701 from hashicorp/sanya-hashicorp/fix-vuln

Module Dependency update

* build(deps): bump google.golang.org/grpc (#13704)

Bumps [google.golang.org/grpc](https://github.com/grpc/grpc-go) from 1.83.1 to 1.83.2.
- [Release notes](https://github.com/grpc/grpc-go/releases)
- [Commits](https://github.com/grpc/grpc-go/compare/v1.83.1...v1.83.2)

---
updated-dependencies:
- dependency-name: google.golang.org/grpc
  dependency-version: 1.83.2
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* [COMPLIANCE] Add/Update Copyright Headers (#13699)

* [COMPLIANCE] Add/Update Copyright Headers

* make generate update

* make generate

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: sanya <[email protected]>
Co-authored-by: Sanya <[email protected]>

* Packer release 1.16.1 (#13705)

* Module upgrade (#13707)

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: Erik Berg <[email protected]>
Co-authored-by: Claude Fable 5 <[email protected]>
Co-authored-by: Benjamin Holmes <[email protected]>
Co-authored-by: elomito <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2026-09-15 22:08:23 +05:30

1215 lines
34 KiB
Go

// Copyright IBM Corp. 2024, 2026
// SPDX-License-Identifier: BUSL-1.1
package plugingetter
import (
"archive/zip"
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/go-version"
"github.com/hashicorp/packer/hcl2template/addrs"
)
var (
pluginFolderOne = filepath.Join("testdata", "plugins")
pluginFolderTwo = filepath.Join("testdata", "plugins_2")
)
func TestRequirement_InstallLatestFromGithub(t *testing.T) {
type fields struct {
Identifier string
VersionConstraints string
}
type args struct {
opts InstallOptions
}
tests := []struct {
name string
fields fields
args args
want *Installation
wantErr bool
}{
{"already-installed-same-api-version",
fields{"amazon", "v1.2.3"},
args{InstallOptions{
[]Getter{
&mockPluginGetter{
Name: "github.com",
Releases: []Release{
{Version: "v1.2.3"},
},
ChecksumFileEntries: map[string][]ChecksumFileEntry{
"1.2.3": {{
// here the checksum file tells us what zipfiles
// to expect. maybe we could cache the zip file
// ? but then the plugin is present on the drive
// twice.
Filename: "packer-plugin-amazon_v1.2.3_x5.0_darwin_amd64.zip",
Checksum: "1337c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
}},
},
},
},
pluginFolderOne,
false,
BinaryInstallationOptions{
APIVersionMajor: "5", APIVersionMinor: "0",
OS: "darwin", ARCH: "amd64",
Checksummers: []Checksummer{
{
Type: "sha256",
Hash: sha256.New(),
},
},
},
}},
nil, false},
{"already-installed-compatible-api-minor-version",
// here 'packer' uses the procol version 5.1 which is compatible
// with the 5.0 one of an already installed plugin.
fields{"amazon", "v1.2.3"},
args{InstallOptions{
[]Getter{
&mockPluginGetter{
Name: "github.com",
Releases: []Release{
{Version: "v1.2.3"},
},
ChecksumFileEntries: map[string][]ChecksumFileEntry{
"1.2.3": {{
Filename: "packer-plugin-amazon_v1.2.3_x5.0_darwin_amd64.zip",
Checksum: "1337c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
}},
},
},
},
pluginFolderOne,
false,
BinaryInstallationOptions{
APIVersionMajor: "5", APIVersionMinor: "1",
OS: "darwin", ARCH: "amd64",
Checksummers: []Checksummer{
{
Type: "sha256",
Hash: sha256.New(),
},
},
},
}},
nil, false},
{"ignore-incompatible-higher-protocol-version",
// here 'packer' needs a binary with protocol version 5.0, and a
// working plugin is already installed; but a plugin with version
// 6.0 is available locally and remotely. It simply needs to be
// ignored.
fields{"amazon", ">= v1"},
args{InstallOptions{
[]Getter{
&mockPluginGetter{
Name: "github.com",
Releases: []Release{
{Version: "v1.2.3"},
{Version: "v1.2.4"},
{Version: "v1.2.5"},
{Version: "v2.0.0"},
},
ChecksumFileEntries: map[string][]ChecksumFileEntry{
"2.0.0": {{
Filename: "packer-plugin-amazon_v2.0.0_x6.0_darwin_amd64.zip",
Checksum: "1337c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
}},
"1.2.5": {{
Filename: "packer-plugin-amazon_v1.2.5_x5.0_darwin_amd64.zip",
Checksum: "1337c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
}},
},
},
},
pluginFolderOne,
false,
BinaryInstallationOptions{
APIVersionMajor: "5", APIVersionMinor: "0",
OS: "darwin", ARCH: "amd64",
Checksummers: []Checksummer{
{
Type: "sha256",
Hash: sha256.New(),
},
},
},
}},
nil, false},
{"upgrade-with-diff-protocol-version",
// here we have something locally and test that a newer version will
// be installed, the newer version has a lower minor protocol
// version than the one we support.
fields{"amazon", ">= v2"},
args{InstallOptions{
[]Getter{
&mockPluginGetter{
Name: "github.com",
Releases: []Release{
{Version: "v1.2.3"},
{Version: "v1.2.4"},
{Version: "v1.2.5"},
{Version: "v2.0.0"},
{Version: "v2.1.0"},
{Version: "v2.10.0"},
},
ChecksumFileEntries: map[string][]ChecksumFileEntry{
"2.10.0": {{
Filename: "packer-plugin-amazon_v2.10.0_x6.0_darwin_amd64.zip",
Checksum: "5763f8b5b5ed248894e8511a089cf399b96c7ef92d784fb30ee6242a7cb35bce",
}},
},
Zips: map[string]io.ReadCloser{
"github.com/hashicorp/packer-plugin-amazon/packer-plugin-amazon_v2.10.0_x6.0_darwin_amd64.zip": zipFile(map[string]string{
// Make the false plugin echo an output that matches a subset of `describe` for install to work
//
// Note: this won't work on Windows as they don't have bin/sh, but this will
// eventually be replaced by acceptance tests.
"packer-plugin-amazon_v2.10.0_x6.0_darwin_amd64": `#!/bin/sh
echo '{"version":"v2.10.0","api_version":"x6.0"}'`,
}),
},
},
},
pluginFolderTwo,
false,
BinaryInstallationOptions{
APIVersionMajor: "6", APIVersionMinor: "1",
OS: "darwin", ARCH: "amd64",
Checksummers: []Checksummer{
{
Type: "sha256",
Hash: sha256.New(),
},
},
},
}},
&Installation{
BinaryPath: "testdata/plugins_2/github.com/hashicorp/amazon/packer-plugin-amazon_v2.10.0_x6.0_darwin_amd64",
Version: "v2.10.0",
}, false},
{"upgrade-with-same-protocol-version",
// here we have something locally and test that a newer version will
// be installed.
fields{"amazon", ">= v2"},
args{InstallOptions{
[]Getter{
&mockPluginGetter{
Name: "github.com",
Releases: []Release{
{Version: "v1.2.3"},
{Version: "v1.2.4"},
{Version: "v1.2.5"},
{Version: "v2.0.0"},
{Version: "v2.1.0"},
{Version: "v2.10.0"},
{Version: "v2.10.1"},
},
ChecksumFileEntries: map[string][]ChecksumFileEntry{
"2.10.1": {{
Filename: "packer-plugin-amazon_v2.10.1_x6.1_darwin_amd64.zip",
Checksum: "51451da5cd7f1ecd8699668d806bafe58a9222430842afbefdc62a6698dab260",
}},
},
Zips: map[string]io.ReadCloser{
"github.com/hashicorp/packer-plugin-amazon/packer-plugin-amazon_v2.10.1_x6.1_darwin_amd64.zip": zipFile(map[string]string{
// Make the false plugin echo an output that matches a subset of `describe` for install to work
//
// Note: this won't work on Windows as they don't have bin/sh, but this will
// eventually be replaced by acceptance tests.
"packer-plugin-amazon_v2.10.1_x6.1_darwin_amd64": `#!/bin/sh
echo '{"version":"v2.10.1","api_version":"x6.1"}'`,
}),
},
},
},
pluginFolderTwo,
false,
BinaryInstallationOptions{
APIVersionMajor: "6", APIVersionMinor: "1",
OS: "darwin", ARCH: "amd64",
Checksummers: []Checksummer{
{
Type: "sha256",
Hash: sha256.New(),
},
},
},
}},
&Installation{
BinaryPath: "testdata/plugins_2/github.com/hashicorp/amazon/packer-plugin-amazon_v2.10.1_x6.1_darwin_amd64",
Version: "v2.10.1",
}, false},
{"upgrade-with-one-missing-checksum-file",
// here we have something locally and test that a newer version will
// be installed.
fields{"amazon", ">= v2"},
args{InstallOptions{
[]Getter{
&mockPluginGetter{
Name: "github.com",
Releases: []Release{
{Version: "v1.2.3"},
{Version: "v1.2.4"},
{Version: "v1.2.5"},
{Version: "v2.0.0"},
{Version: "v2.1.0"},
{Version: "v2.10.0"},
{Version: "v2.10.1"},
},
ChecksumFileEntries: map[string][]ChecksumFileEntry{
"2.10.0": {{
Filename: "packer-plugin-amazon_v2.10.0_x6.1_linux_amd64.zip",
Checksum: "5196f57f37e18bfeac10168db6915caae0341bfc4168ebc3d2b959d746cebd0a",
}},
},
Zips: map[string]io.ReadCloser{
"github.com/hashicorp/packer-plugin-amazon/packer-plugin-amazon_v2.10.0_x6.1_linux_amd64.zip": zipFile(map[string]string{
// Make the false plugin echo an output that matches a subset of `describe` for install to work
//
// Note: this won't work on Windows as they don't have bin/sh, but this will
// eventually be replaced by acceptance tests.
"packer-plugin-amazon_v2.10.0_x6.1_linux_amd64": `#!/bin/sh
echo '{"version":"v2.10.0","api_version":"x6.1"}'`,
}),
},
},
},
pluginFolderTwo,
false,
BinaryInstallationOptions{
APIVersionMajor: "6", APIVersionMinor: "1",
OS: "linux", ARCH: "amd64",
Checksummers: []Checksummer{
{
Type: "sha256",
Hash: sha256.New(),
},
},
},
}},
&Installation{
BinaryPath: "testdata/plugins_2/github.com/hashicorp/amazon/packer-plugin-amazon_v2.10.0_x6.1_linux_amd64",
Version: "v2.10.0",
}, false},
{"wrong-zip-checksum",
// here we have something locally and test that a newer version with
// a wrong checksum will not be installed and error.
fields{"amazon", ">= v2"},
args{InstallOptions{
[]Getter{
&mockPluginGetter{
Name: "github.com",
Releases: []Release{
{Version: "v2.10.0"},
},
ChecksumFileEntries: map[string][]ChecksumFileEntry{
"2.10.0": {{
Filename: "packer-plugin-amazon_v2.10.0_x6.0_darwin_amd64.zip",
Checksum: "133713371337133713371337c4a152edd277366a7f71ff3812583e4a35dd0d4a",
}},
},
Zips: map[string]io.ReadCloser{
"github.com/hashicorp/packer-plugin-amazon/packer-plugin-amazon_v2.10.0_x6.0_darwin_amd64.zip": zipFile(map[string]string{
"packer-plugin-amazon_v2.10.0_x6.0_darwin_amd64": "h4xx",
}),
},
},
},
pluginFolderTwo,
false,
BinaryInstallationOptions{
APIVersionMajor: "6", APIVersionMinor: "1",
OS: "darwin", ARCH: "amd64",
Checksummers: []Checksummer{
{
Type: "sha256",
Hash: sha256.New(),
},
},
},
}},
nil, true},
{"wrong-local-checksum",
// here we have something wrong locally and test that a newer
// version with a wrong checksum will not be installed
// this should totally error.
fields{"amazon", ">= v1"},
args{InstallOptions{
[]Getter{
&mockPluginGetter{
Name: "github.com",
Releases: []Release{
{Version: "v2.10.0"},
},
ChecksumFileEntries: map[string][]ChecksumFileEntry{
"2.10.0": {{
Filename: "packer-plugin-amazon_v2.10.0_x6.0_darwin_amd64.zip",
Checksum: "133713371337133713371337c4a152edd277366a7f71ff3812583e4a35dd0d4a",
}},
},
Zips: map[string]io.ReadCloser{
"github.com/hashicorp/packer-plugin-amazon/packer-plugin-amazon_v2.10.0_x6.0_darwin_amd64.zip": zipFile(map[string]string{
"packer-plugin-amazon_v2.10.0_x6.0_darwin_amd64": "h4xx",
}),
},
},
},
pluginFolderTwo,
false,
BinaryInstallationOptions{
APIVersionMajor: "6", APIVersionMinor: "1",
OS: "darwin", ARCH: "amd64",
Checksummers: []Checksummer{
{
Type: "sha256",
Hash: sha256.New(),
},
},
},
}},
nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
switch tt.name {
case "upgrade-with-diff-protocol-version",
"upgrade-with-same-protocol-version",
"upgrade-with-one-missing-checksum-file":
if runtime.GOOS != "windows" {
break
}
t.Skipf("Test %q cannot run on Windows because of a shell script being invoked, skipping.", tt.name)
}
log.Printf("starting %s test", tt.name)
identifier, err := addrs.ParsePluginSourceString("github.com/hashicorp/" + tt.fields.Identifier)
if err != nil {
t.Fatalf("ParsePluginSourceString(%q): %v", tt.fields.Identifier, err)
}
cts, err := version.NewConstraint(tt.fields.VersionConstraints)
if err != nil {
t.Fatalf("version.NewConstraint(%q): %v", tt.fields.Identifier, err)
}
pr := &Requirement{
Identifier: identifier,
VersionConstraints: cts,
}
got, err := pr.InstallLatest(tt.args.opts)
if (err != nil) != tt.wantErr {
t.Errorf("Requirement.InstallLatest() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(got, tt.want); diff != "" {
t.Errorf("Requirement.InstallLatest() %s", diff)
}
if tt.want != nil && tt.want.BinaryPath != "" {
// Cleanup.
// These two files should be here by now and os.Remove will fail if
// they aren't.
if err := os.Remove(filepath.Clean(tt.want.BinaryPath)); err != nil {
t.Fatal(err)
}
if err := os.Remove(filepath.Clean(tt.want.BinaryPath + "_SHA256SUM")); err != nil {
t.Fatal(err)
}
}
})
}
}
func TestRequirement_InstallLatestFromOfficialRelease(t *testing.T) {
type fields struct {
Identifier string
VersionConstraints string
}
type args struct {
opts InstallOptions
}
tests := []struct {
name string
fields fields
args args
want *Installation
wantErr bool
}{
{"already-installed-same-api-version",
fields{"amazon", "v1.2.3"},
args{InstallOptions{
[]Getter{
&mockPluginGetter{
Name: "releases.hashicorp.com",
Releases: []Release{
{Version: "v1.2.3"},
},
ChecksumFileEntries: map[string][]ChecksumFileEntry{
"1.2.3": {{
// here the checksum file tells us what zipfiles
// to expect. maybe we could cache the zip file
// ? but then the plugin is present on the drive
// twice.
Filename: "packer-plugin-amazon_1.2.3_darwin_amd64.zip",
Checksum: "1337c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
}},
},
Manifest: map[string]io.ReadCloser{
"github.com/hashicorp/packer-plugin-amazon/packer-plugin-amazon_1.2.3_darwin_amd64_manifest.json": manifestFile(map[string]map[string]string{
"metadata": {"protocol_version": "5.0"},
}),
},
},
},
pluginFolderOne,
false,
BinaryInstallationOptions{
APIVersionMajor: "5", APIVersionMinor: "0",
OS: "darwin", ARCH: "amd64",
Checksummers: []Checksummer{
{
Type: "sha256",
Hash: sha256.New(),
},
},
},
}},
nil, false},
{"already-installed-compatible-api-minor-version",
// here 'packer' uses the procol version 5.1 which is compatible
// with the 5.0 one of an already installed plugin.
fields{"amazon", "v1.2.3"},
args{InstallOptions{
[]Getter{
&mockPluginGetter{
Name: "releases.hashicorp.com",
Releases: []Release{
{Version: "v1.2.3"},
},
ChecksumFileEntries: map[string][]ChecksumFileEntry{
"1.2.3": {{
Filename: "packer-plugin-amazon_1.2.3_darwin_amd64.zip",
Checksum: "1337c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
}},
},
Manifest: map[string]io.ReadCloser{
"github.com/hashicorp/packer-plugin-amazon/packer-plugin-amazon_1.2.3_darwin_amd64_manifest.json": manifestFile(map[string]map[string]string{
"metadata": {"protocol_version": "5.0"},
}),
},
},
},
pluginFolderOne,
false,
BinaryInstallationOptions{
APIVersionMajor: "5", APIVersionMinor: "1",
OS: "darwin", ARCH: "amd64",
Checksummers: []Checksummer{
{
Type: "sha256",
Hash: sha256.New(),
},
},
},
}},
nil, false},
{"ignore-incompatible-higher-protocol-version",
// here 'packer' needs a binary with protocol version 5.0, and a
// working plugin is already installed; but a plugin with version
// 6.0 is available locally and remotely. It simply needs to be
// ignored.
fields{"amazon", ">= v1"},
args{InstallOptions{
[]Getter{
&mockPluginGetter{
Name: "releases.hashicorp.com",
Releases: []Release{
{Version: "v1.2.3"},
{Version: "v1.2.4"},
{Version: "v1.2.5"},
{Version: "v2.0.0"},
},
ChecksumFileEntries: map[string][]ChecksumFileEntry{
"1.2.5": {{
Filename: "packer-plugin-amazon_1.2.5_darwin_amd64.zip",
Checksum: "1337c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
}},
},
Manifest: map[string]io.ReadCloser{
"github.com/hashicorp/packer-plugin-amazon/packer-plugin-amazon_1.2.5_darwin_amd64_manifest.json": manifestFile(map[string]map[string]string{
"metadata": {"protocol_version": "5.0"},
}),
},
},
},
pluginFolderOne,
false,
BinaryInstallationOptions{
APIVersionMajor: "5", APIVersionMinor: "0",
OS: "darwin", ARCH: "amd64",
Checksummers: []Checksummer{
{
Type: "sha256",
Hash: sha256.New(),
},
},
},
}},
nil, false},
{"upgrade-with-diff-protocol-version",
// here we have something locally and test that a newer version will
// be installed, the newer version has a lower minor protocol
// version than the one we support.
fields{"amazon", ">= v2"},
args{InstallOptions{
[]Getter{
&mockPluginGetter{
Name: "releases.hashicorp.com",
Releases: []Release{
{Version: "v1.2.3"},
{Version: "v1.2.4"},
{Version: "v1.2.5"},
{Version: "v2.0.0"},
{Version: "v2.1.0"},
{Version: "v2.10.0"},
},
ChecksumFileEntries: map[string][]ChecksumFileEntry{
"2.10.0": {{
Filename: "packer-plugin-amazon_2.10.0_darwin_amd64.zip",
Checksum: "5763f8b5b5ed248894e8511a089cf399b96c7ef92d784fb30ee6242a7cb35bce",
}},
},
Zips: map[string]io.ReadCloser{
"github.com/hashicorp/packer-plugin-amazon/packer-plugin-amazon_2.10.0_darwin_amd64.zip": zipFile(map[string]string{
// Make the false plugin echo an output that matches a subset of `describe` for install to work
//
// Note: this won't work on Windows as they don't have bin/sh, but this will
// eventually be replaced by acceptance tests.
"packer-plugin-amazon_v2.10.0_x6.0_darwin_amd64": `#!/bin/sh
echo '{"version":"v2.10.0","api_version":"x6.0"}'`,
}),
},
Manifest: map[string]io.ReadCloser{
"github.com/hashicorp/packer-plugin-amazon/packer-plugin-amazon_2.10.0_darwin_amd64_manifest.json": manifestFile(map[string]map[string]string{
"metadata": {"protocol_version": "6.0"},
}),
},
},
},
pluginFolderTwo,
false,
BinaryInstallationOptions{
APIVersionMajor: "6", APIVersionMinor: "1",
OS: "darwin", ARCH: "amd64",
Checksummers: []Checksummer{
{
Type: "sha256",
Hash: sha256.New(),
},
},
},
}},
&Installation{
BinaryPath: "testdata/plugins_2/github.com/hashicorp/amazon/packer-plugin-amazon_v2.10.0_x6.0_darwin_amd64",
Version: "v2.10.0",
}, false},
{"wrong-zip-checksum",
// here we have something locally and test that a newer version with
// a wrong checksum will not be installed and error.
fields{"amazon", ">= v2"},
args{InstallOptions{
[]Getter{
&mockPluginGetter{
Name: "releases.hashicorp.com",
Releases: []Release{
{Version: "v2.10.0"},
},
ChecksumFileEntries: map[string][]ChecksumFileEntry{
"2.10.0": {{
Filename: "packer-plugin-amazon_2.10.0_darwin_amd64.zip",
Checksum: "133713371337133713371337c4a152edd277366a7f71ff3812583e4a35dd0d4a",
}},
},
Zips: map[string]io.ReadCloser{
"github.com/hashicorp/packer-plugin-amazon/packer-plugin-amazon_2.10.0_darwin_amd64.zip": zipFile(map[string]string{
"packer-plugin-amazon_v2.10.0_x6.0_darwin_amd64": "h4xx",
}),
},
Manifest: map[string]io.ReadCloser{
"github.com/hashicorp/packer-plugin-amazon/packer-plugin-amazon_2.10.0_darwin_amd64_manifest.json": manifestFile(map[string]map[string]string{
"metadata": {"protocol_version": "6.0"},
}),
},
},
},
pluginFolderTwo,
false,
BinaryInstallationOptions{
APIVersionMajor: "6", APIVersionMinor: "1",
OS: "darwin", ARCH: "amd64",
Checksummers: []Checksummer{
{
Type: "sha256",
Hash: sha256.New(),
},
},
},
}},
nil, true},
{"wrong-local-checksum",
// here we have something wrong locally and test that a newer
// version with a wrong checksum will not be installed
// this should totally error.
fields{"amazon", ">= v1"},
args{InstallOptions{
[]Getter{
&mockPluginGetter{
Name: "releases.hashicorp.com",
Releases: []Release{
{Version: "v2.10.0"},
},
ChecksumFileEntries: map[string][]ChecksumFileEntry{
"2.10.0": {{
Filename: "packer-plugin-amazon_2.10.0_darwin_amd64.zip",
Checksum: "133713371337133713371337c4a152edd277366a7f71ff3812583e4a35dd0d4a",
}},
},
Zips: map[string]io.ReadCloser{
"github.com/hashicorp/packer-plugin-amazon/packer-plugin-amazon_2.10.0_darwin_amd64.zip": zipFile(map[string]string{
"packer-plugin-amazon_v2.10.0_x6.0_darwin_amd64": "h4xx",
}),
},
Manifest: map[string]io.ReadCloser{
"github.com/hashicorp/packer-plugin-amazon/packer-plugin-amazon_2.10.0_darwin_amd64_manifest.json": manifestFile(map[string]map[string]string{
"metadata": {"protocol_version": "6.0"},
}),
},
},
},
pluginFolderTwo,
false,
BinaryInstallationOptions{
APIVersionMajor: "6", APIVersionMinor: "1",
OS: "darwin", ARCH: "amd64",
Checksummers: []Checksummer{
{
Type: "sha256",
Hash: sha256.New(),
},
},
},
}},
nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
switch tt.name {
case "upgrade-with-diff-protocol-version",
"upgrade-with-same-protocol-version",
"upgrade-with-one-missing-checksum-file":
if runtime.GOOS != "windows" {
break
}
t.Skipf("Test %q cannot run on Windows because of a shell script being invoked, skipping.", tt.name)
}
log.Printf("starting %s test", tt.name)
identifier, err := addrs.ParsePluginSourceString("github.com/hashicorp/" + tt.fields.Identifier)
if err != nil {
t.Fatalf("ParsePluginSourceString(%q): %v", tt.fields.Identifier, err)
}
cts, err := version.NewConstraint(tt.fields.VersionConstraints)
if err != nil {
t.Fatalf("version.NewConstraint(%q): %v", tt.fields.Identifier, err)
}
pr := &Requirement{
Identifier: identifier,
VersionConstraints: cts,
}
got, err := pr.InstallLatest(tt.args.opts)
if (err != nil) != tt.wantErr {
t.Errorf("Requirement.InstallLatest() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(got, tt.want); diff != "" {
t.Errorf("Requirement.InstallLatest() %s", diff)
}
if tt.want != nil && tt.want.BinaryPath != "" {
// Cleanup.
// These two files should be here by now and os.Remove will fail if
// they aren't.
if err := os.Remove(filepath.Clean(tt.want.BinaryPath)); err != nil {
t.Fatal(err)
}
if err := os.Remove(filepath.Clean(tt.want.BinaryPath + "_SHA256SUM")); err != nil {
t.Fatal(err)
}
}
})
}
}
type mockPluginGetter struct {
Releases []Release
ChecksumFileEntries map[string][]ChecksumFileEntry
Zips map[string]io.ReadCloser
Name string
APIMajor string
APIMinor string
Manifest map[string]io.ReadCloser
}
func (g *mockPluginGetter) Init(req *Requirement, entry *ChecksumFileEntry) error {
filename := entry.Filename
res := strings.TrimPrefix(filename, req.FilenamePrefix())
// res now looks like v0.2.12_x5.0_freebsd_amd64.zip
entry.Ext = filepath.Ext(res)
res = strings.TrimSuffix(res, entry.Ext)
// res now looks like v0.2.12_x5.0_freebsd_amd64
parts := strings.Split(res, "_")
// ["v0.2.12", "x5.0", "freebsd", "amd64"]
if g.Name == "github.com" {
if len(parts) < 4 {
return fmt.Errorf("malformed filename expected %s{version}_x{protocol-version}_{os}_{arch}", req.FilenamePrefix())
}
entry.BinVersion, entry.ProtVersion, entry.Os, entry.Arch = parts[0], parts[1], parts[2], parts[3]
} else {
if len(parts) < 3 {
return fmt.Errorf("malformed filename expected %s{version}_{os}_{arch}", req.FilenamePrefix())
}
entry.BinVersion, entry.Os, entry.Arch = parts[0], parts[1], parts[2]
entry.BinVersion = strings.TrimPrefix(entry.BinVersion, "v")
}
return nil
}
func (g *mockPluginGetter) Validate(opt GetOptions, expectedVersion string, installOpts BinaryInstallationOptions, entry *ChecksumFileEntry) error {
if g.Name == "github.com" {
expectedBinVersion := "v" + expectedVersion
if entry.BinVersion != expectedBinVersion {
return fmt.Errorf("wrong version: %s does not match expected %s", entry.BinVersion, expectedBinVersion)
}
if entry.Os != installOpts.OS || entry.Arch != installOpts.ARCH {
return fmt.Errorf("wrong system, expected %s_%s", installOpts.OS, installOpts.ARCH)
}
return installOpts.CheckProtocolVersion(entry.ProtVersion)
} else {
if entry.BinVersion != expectedVersion {
return fmt.Errorf("wrong version: %s does not match expected %s", entry.BinVersion, expectedVersion)
}
if entry.Os != installOpts.OS || entry.Arch != installOpts.ARCH {
return fmt.Errorf("wrong system, expected %s_%s got %s_%s", installOpts.OS, installOpts.ARCH, entry.Os, entry.Arch)
}
manifest, err := g.Get("meta", opt)
if err != nil {
return err
}
var data ManifestMeta
body, err := io.ReadAll(manifest)
if err != nil {
log.Printf("Failed to unmarshal manifest json: %s", err)
return err
}
err = json.Unmarshal(body, &data)
if err != nil {
log.Printf("Failed to unmarshal manifest json: %s", err)
return err
}
err = installOpts.CheckProtocolVersion("x" + data.Metadata.ProtocolVersion)
if err != nil {
return err
}
g.APIMajor = strings.Split(data.Metadata.ProtocolVersion, ".")[0]
g.APIMinor = strings.Split(data.Metadata.ProtocolVersion, ".")[1]
log.Printf("#### versions API %s.%s, entry %s.%s", g.APIMajor, g.APIMinor, entry.ProtVersion, entry.BinVersion)
return nil
}
}
func (g *mockPluginGetter) ExpectedFileName(pr *Requirement, version string, entry *ChecksumFileEntry, _ string) string {
pluginSourceParts := strings.Split(pr.Identifier.Source, "/")
if g.Name == "github.com" {
// Mirror the real github getter: reconstruct from validated entry fields,
// never return the raw remote filename.
return strings.Join([]string{
"packer-plugin-" + pluginSourceParts[2],
entry.BinVersion,
entry.ProtVersion,
entry.Os,
entry.Arch + entry.Ext,
}, "_")
}
return strings.Join([]string{fmt.Sprintf("packer-plugin-%s", pluginSourceParts[2]),
"v" + version,
"x" + g.APIMajor + "." + g.APIMinor,
entry.Os,
entry.Arch + ".zip",
}, "_")
}
func (g *mockPluginGetter) Get(what string, options GetOptions) (io.ReadCloser, error) {
var toEncode interface{}
switch what {
case "releases":
toEncode = g.Releases
case "sha256":
enc, ok := g.ChecksumFileEntries[options.version.String()]
if !ok {
return nil, fmt.Errorf("No checksum available for version %q", options.version.String())
}
toEncode = enc
case "zip":
// Note: we'll act as if the plugin sources would always be github sources for now.
// This test will need to be updated if/when we move on to support other sources.
parts := options.PluginRequirement.Identifier.Parts()
acc := fmt.Sprintf("%s/%s/packer-plugin-%s/%s", parts[0], parts[1], parts[2], options.ExpectedZipFilename())
zip, found := g.Zips[acc]
if found == false {
panic(fmt.Sprintf("could not find zipfile %s. %v", acc, g.Zips))
}
return zip, nil
case "meta":
// Note: we'll act as if the plugin sources would always be github sources for now.
// This test will need to be updated if/when we move on to support other sources.
parts := options.PluginRequirement.Identifier.Parts()
acc := fmt.Sprintf("%s/%s/packer-plugin-%s/packer-plugin-%s_%s_%s_%s_manifest.json", parts[0], parts[1], parts[2], parts[2], options.version, options.BinaryInstallationOptions.OS, options.BinaryInstallationOptions.ARCH)
manifest, found := g.Manifest[acc]
if found == false {
panic(fmt.Sprintf("could not find manifest file %s. %v", acc, g.Zips))
}
return manifest, nil
default:
panic("Don't know how to get " + what)
}
read, write := io.Pipe()
go func() {
if err := json.NewEncoder(write).Encode(toEncode); err != nil {
panic(err)
}
}()
return io.NopCloser(read), nil
}
func zipFile(content map[string]string) io.ReadCloser {
buff := bytes.NewBuffer(nil)
zipWriter := zip.NewWriter(buff)
for fileName, content := range content {
header := &zip.FileHeader{
Name: fileName,
UncompressedSize: uint32(len([]byte(content))),
}
fWriter, err := zipWriter.CreateHeader(header)
if err != nil {
panic(err)
}
_, err = io.Copy(fWriter, strings.NewReader(content))
if err != nil {
panic(err)
}
}
err := zipWriter.Close()
if err != nil {
panic(err)
}
return io.NopCloser(buff)
}
func manifestFile(content map[string]map[string]string) io.ReadCloser {
jsonBytes, err := json.Marshal(content)
if err != nil {
fmt.Println("Error marshaling JSON:", err)
}
buffer := bytes.NewBuffer(jsonBytes)
return io.NopCloser(buffer)
}
var _ Getter = &mockPluginGetter{}
func Test_LessInstallList(t *testing.T) {
tests := []struct {
name string
installs InstallList
expectLess bool
}{
{
"v1.2.1 < v1.2.2 => true",
InstallList{
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.1",
APIVersion: "x5.0",
},
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.2",
APIVersion: "x5.0",
},
},
true,
},
{
// Impractical with the changes to the loading model
"v1.2.1 = v1.2.1 => false",
InstallList{
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.1",
APIVersion: "x5.0",
},
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.1",
APIVersion: "x5.0",
},
},
false,
},
{
"v1.2.2 < v1.2.1 => false",
InstallList{
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.2",
APIVersion: "x5.0",
},
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.1",
APIVersion: "x5.0",
},
},
false,
},
{
"v1.2.2-dev < v1.2.2 => true",
InstallList{
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.2-dev",
APIVersion: "x5.0",
},
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.2",
APIVersion: "x5.0",
},
},
true,
},
{
"v1.2.2 < v1.2.2-dev => false",
InstallList{
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.2",
APIVersion: "x5.0",
},
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.2-dev",
APIVersion: "x5.0",
},
},
false,
},
{
"v1.2.1 < v1.2.2-dev => true",
InstallList{
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.1",
APIVersion: "x5.0",
},
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.2-dev",
APIVersion: "x5.0",
},
},
true,
},
{
"v1.2.3 < v1.2.2-dev => false",
InstallList{
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.3",
APIVersion: "x5.0",
},
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.2-dev",
APIVersion: "x5.0",
},
},
false,
},
{
"v1.2.3_x5.0 < v1.2.3_x5.1 => true",
InstallList{
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.3",
APIVersion: "x5.0",
},
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.3",
APIVersion: "x5.1",
},
},
true,
},
{
"v1.2.3_x5.0 < v1.2.3_x5.0 => false",
InstallList{
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.3",
APIVersion: "x5.0",
},
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.3",
APIVersion: "x5.0",
},
},
false,
},
{
"v1.2.3_x4.15 < v1.2.3_x5.0 => true",
InstallList{
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.3",
APIVersion: "x4.15",
},
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.3",
APIVersion: "x5.0",
},
},
true,
},
{
"v1.2.3_x9.0 < v1.2.3_x10.0 => true",
InstallList{
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.3",
APIVersion: "x9.0",
},
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.3",
APIVersion: "x10.0",
},
},
true,
},
{
"v1.2.3_x5.9 < v1.2.3_x5.10 => true",
InstallList{
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.3",
APIVersion: "x5.9",
},
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.3",
APIVersion: "x5.10",
},
},
true,
},
{
"v1.2.3_x5.0 < v1.2.3_x4.15 => false",
InstallList{
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.3",
APIVersion: "x5.0",
},
&Installation{
BinaryPath: "host/org/plugin",
Version: "v1.2.3",
APIVersion: "x4.15",
},
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
isLess := tt.installs.Less(0, 1)
if isLess != tt.expectLess {
t.Errorf("Less mismatch for %s_%s < %s_%s, expected %t, got %t",
tt.installs[0].Version,
tt.installs[0].APIVersion,
tt.installs[1].Version,
tt.installs[1].APIVersion,
tt.expectLess, isLess)
}
})
}
}