Skip to content

Commit

Permalink
Fix regression in docker-tag post-processor (hashicorp#8593)
Browse files Browse the repository at this point in the history
hashicorpGH-8392 introduced the feature that multiple tags can be specified. This
(presumably inadvertently) broke the option to tag an image without the
actual tag specified -- Docker handles this by assuming `latest` as the
tag.

In addition, use-cases exist for the `repository` field containing the
tag already, which was also broken.
  • Loading branch information
pitkley authored and sylviamoss committed Jan 13, 2020
1 parent d6a351b commit d7d00d8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* builder/virtualbox-vm: use config as a non pointer to avoid a panic [GH-8576]
* core: Fix crash when build.sources is set to an invalid name [GH-8569]
* core: Fix loading of external plugins. GH-8543]
* post-processor/docker-tag: Fix regression if no tags were specified. [GH-8593]
* post-processor/vagrant: correctly handle the diskSize property as a qemu size
string [GH-8567]
* provisioner/ansible: Fix password sanitization to account for empty string
Expand Down
23 changes: 16 additions & 7 deletions post-processor/docker-tag/post-processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,26 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact

importRepo := p.config.Repository
var lastTaggedRepo = importRepo
for _, tag := range p.config.Tag {
local := importRepo + ":" + tag
if len(p.config.Tag) > 0 {
for _, tag := range p.config.Tag {
local := importRepo + ":" + tag
ui.Message("Tagging image: " + artifact.Id())
ui.Message("Repository: " + local)

err := driver.TagImage(artifact.Id(), local, p.config.Force)
if err != nil {
return nil, false, true, err
}

lastTaggedRepo = local
}
} else {
ui.Message("Tagging image: " + artifact.Id())
ui.Message("Repository: " + local)

err := driver.TagImage(artifact.Id(), local, p.config.Force)
ui.Message("Repository: " + importRepo)
err := driver.TagImage(artifact.Id(), importRepo, p.config.Force)
if err != nil {
return nil, false, true, err
}

lastTaggedRepo = local
}

// Build the artifact
Expand Down
39 changes: 39 additions & 0 deletions post-processor/docker-tag/post-processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,42 @@ func TestPostProcessor_PostProcess_Force(t *testing.T) {
t.Fatal("bad force")
}
}

func TestPostProcessor_PostProcess_NoTag(t *testing.T) {
driver := &docker.MockDriver{}
p := &PostProcessor{Driver: driver}
c := testConfig()
delete(c, "tag")
if err := p.Configure(c); err != nil {
t.Fatalf("err %s", err)
}

artifact := &packer.MockArtifact{BuilderIdValue: dockerimport.BuilderId, IdValue: "1234567890abcdef"}

result, keep, forceOverride, err := p.PostProcess(context.Background(), testUi(), artifact)
if _, ok := result.(packer.Artifact); !ok {
t.Fatal("should be instance of Artifact")
}
if !keep {
t.Fatal("should keep")
}
if !forceOverride {
t.Fatal("Should force keep no matter what user sets.")
}
if err != nil {
t.Fatalf("err: %s", err)
}

if driver.TagImageCalled != 1 {
t.Fatal("should call TagImage")
}
if driver.TagImageImageId != "1234567890abcdef" {
t.Fatal("bad image id")
}
if driver.TagImageRepo[0] != "foo" {
t.Fatal("bad repo")
}
if driver.TagImageForce {
t.Fatal("bad force")
}
}

0 comments on commit d7d00d8

Please sign in to comment.