Fix the version parsing in ChecksumFileEntry.init() so that plugins whose name contain v's can `packer init` (#10760)

* fix
* test
This commit is contained in:
Dom Del Nano 2021-03-15 03:31:21 -07:00 committed by GitHub
parent 4bbeec4733
commit 77ce2b39d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 3 deletions

View File

@ -305,12 +305,12 @@ func (e ChecksumFileEntry) Arch() string { return e.arch }
//
func (e *ChecksumFileEntry) init(req *Requirement) (err error) {
filename := e.Filename
res := strings.TrimLeft(filename, req.FilenamePrefix())
res := strings.TrimPrefix(filename, req.FilenamePrefix())
// res now looks like v0.2.12_x5.0_freebsd_amd64.zip
e.ext = filepath.Ext(res)
res = strings.TrimRight(res, e.ext)
res = strings.TrimSuffix(res, e.ext)
// res now looks like v0.2.12_x5.0_freebsd_amd64
parts := strings.Split(res, "_")
@ -326,7 +326,7 @@ func (e *ChecksumFileEntry) init(req *Requirement) (err error) {
func (e *ChecksumFileEntry) validate(expectedVersion string, installOpts BinaryInstallationOptions) error {
if e.binVersion != expectedVersion {
return fmt.Errorf("wrong version, expected %s ", expectedVersion)
return fmt.Errorf("wrong version: '%s' does not match expected %s ", e.binVersion, expectedVersion)
}
if e.os != installOpts.OS || e.arch != installOpts.ARCH {
return fmt.Errorf("wrong system, expected %s_%s ", installOpts.OS, installOpts.ARCH)

View File

@ -27,6 +27,32 @@ var (
pluginFolderWrongChecksums = filepath.Join("testdata", "wrong_checksums")
)
func TestChecksumFileEntry_init(t *testing.T) {
expectedVersion := "v0.3.0"
req := &Requirement{
Identifier: &addrs.Plugin{
Hostname: "github.com",
Namespace: "ddelnano",
Type: "xenserver",
},
}
checkSum := &ChecksumFileEntry{
Filename: fmt.Sprintf("packer-plugin-xenserver_%s_x5.0_darwin_amd64.zip", expectedVersion),
Checksum: "0f5969b069b9c0a58f2d5786c422341c70dfe17bd68f896fcbd46677e8c913f1",
}
err := checkSum.init(req)
if err != nil {
t.Fatalf("ChecksumFileEntry.init failure: %v", err)
}
if checkSum.binVersion != expectedVersion {
t.Errorf("failed to parse ChecksumFileEntry properly expected version '%s' but found '%s'", expectedVersion, checkSum.binVersion)
}
}
func TestPlugin_ListInstallations(t *testing.T) {
type fields struct {