Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle updated packer version output from 1.10 #1380

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Expand Up @@ -4,7 +4,7 @@ env: &env
MODULE_CI_VERSION: v0.46.0
MODULE_GCP_CI_VERSION: v0.1.1
TERRAFORM_VERSION: 1.5.7
PACKER_VERSION: 1.7.4
PACKER_VERSION: 1.10.0
TERRAGRUNT_VERSION: v0.52.0
OPA_VERSION: v0.33.1
GO_VERSION: 1.21.1
Expand Down
4 changes: 4 additions & 0 deletions examples/packer-docker-example/build.pkr.hcl
Expand Up @@ -4,6 +4,10 @@ packer {
version = ">=v1.0.0"
source = "github.com/hashicorp/amazon"
}
docker = {
version = ">=v1.0.1"
source = "github.com/hashicorp/docker"
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion modules/packer/packer.go
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -186,10 +187,11 @@ func hasPackerInit(t testing.TestingT, options *Options) (bool, error) {
Env: options.Env,
WorkingDir: options.WorkingDir,
}
localVersion, err := shell.RunCommandAndGetOutputE(t, cmd)
versionCmdOutput, err := shell.RunCommandAndGetOutputE(t, cmd)
if err != nil {
return false, err
}
localVersion := trimPackerVersion(versionCmdOutput)
thisVersion, err := version.NewVersion(localVersion)
if err != nil {
return false, err
Expand Down Expand Up @@ -262,3 +264,8 @@ func formatPackerArgs(options *Options) []string {

return append(args, options.Template)
}

// From packer 1.10 the -version command output is prefixed with Packer v
func trimPackerVersion(versionCmdOutput string) string {
return strings.Replace(versionCmdOutput, "Packer v", "", -1)
}
25 changes: 25 additions & 0 deletions modules/packer/packer_test.go
Expand Up @@ -174,3 +174,28 @@ func TestFormatPackerArgs(t *testing.T) {
assert.Equal(t, strings.Join(args, " "), test.expected)
}
}

func TestTrimPackerVersion(t *testing.T) {
t.Parallel()

tests := []struct {
versionOutput string
expected string
}{
{
// Pre 1.10 output
versionOutput: "1.7.0",
expected: "1.7.0",
},
{
// From 1.10 matches the output of packer version
versionOutput: "Packer v1.10.0",
expected: "1.10.0",
},
}

for _, test := range tests {
out := trimPackerVersion(test.versionOutput)
assert.Equal(t, test.expected, out)
}
}