2014-01-13 16:21:34 -05:00
|
|
|
package dockerpush
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-03-22 09:56:02 -04:00
|
|
|
"context"
|
2018-01-22 20:21:10 -05:00
|
|
|
"testing"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/docker"
|
2019-03-22 09:56:02 -04:00
|
|
|
dockerimport "github.com/hashicorp/packer/post-processor/docker-import"
|
2014-01-13 16:21:34 -05:00
|
|
|
)
|
|
|
|
|
2020-11-20 13:21:29 -05:00
|
|
|
func testUi() *packersdk.BasicUi {
|
|
|
|
return &packersdk.BasicUi{
|
2014-01-13 16:21:34 -05:00
|
|
|
Reader: new(bytes.Buffer),
|
|
|
|
Writer: new(bytes.Buffer),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessor_ImplementsPostProcessor(t *testing.T) {
|
2020-12-01 17:48:55 -05:00
|
|
|
var _ packersdk.PostProcessor = new(PostProcessor)
|
2014-01-13 16:21:34 -05:00
|
|
|
}
|
2014-03-13 00:30:13 -04:00
|
|
|
|
|
|
|
func TestPostProcessor_PostProcess(t *testing.T) {
|
|
|
|
driver := &docker.MockDriver{}
|
|
|
|
p := &PostProcessor{Driver: driver}
|
2020-11-20 13:21:29 -05:00
|
|
|
artifact := &packersdk.MockArtifact{
|
2014-03-13 00:30:13 -04:00
|
|
|
BuilderIdValue: dockerimport.BuilderId,
|
2014-04-26 14:12:43 -04:00
|
|
|
IdValue: "foo/bar",
|
2014-03-13 00:30:13 -04:00
|
|
|
}
|
|
|
|
|
2019-04-08 13:59:42 -04:00
|
|
|
result, keep, forceOverride, err := p.PostProcess(context.Background(), testUi(), artifact)
|
2020-11-19 15:17:11 -05:00
|
|
|
if _, ok := result.(packersdk.Artifact); !ok {
|
2018-07-20 16:27:29 -04:00
|
|
|
t.Fatal("should be instance of Artifact")
|
2014-03-13 00:30:13 -04:00
|
|
|
}
|
2018-07-20 16:27:29 -04:00
|
|
|
if !keep {
|
|
|
|
t.Fatal("should keep")
|
2014-03-13 00:30:13 -04:00
|
|
|
}
|
2019-04-03 14:32:49 -04:00
|
|
|
if forceOverride {
|
|
|
|
t.Fatal("Should default to keep, but not override user wishes")
|
|
|
|
}
|
2014-03-13 00:30:13 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !driver.PushCalled {
|
|
|
|
t.Fatal("should call push")
|
|
|
|
}
|
|
|
|
if driver.PushName != "foo/bar" {
|
|
|
|
t.Fatal("bad name")
|
|
|
|
}
|
2018-07-20 16:27:29 -04:00
|
|
|
if result.Id() != "foo/bar" {
|
|
|
|
t.Fatal("bad image id")
|
|
|
|
}
|
2014-03-13 00:30:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessor_PostProcess_portInName(t *testing.T) {
|
|
|
|
driver := &docker.MockDriver{}
|
|
|
|
p := &PostProcessor{Driver: driver}
|
2020-11-20 13:21:29 -05:00
|
|
|
artifact := &packersdk.MockArtifact{
|
2014-03-13 00:30:13 -04:00
|
|
|
BuilderIdValue: dockerimport.BuilderId,
|
2014-04-26 14:12:43 -04:00
|
|
|
IdValue: "localhost:5000/foo/bar",
|
2014-03-13 00:30:13 -04:00
|
|
|
}
|
|
|
|
|
2019-04-08 13:59:42 -04:00
|
|
|
result, keep, forceOverride, err := p.PostProcess(context.Background(), testUi(), artifact)
|
2020-11-19 15:17:11 -05:00
|
|
|
if _, ok := result.(packersdk.Artifact); !ok {
|
2018-07-20 16:27:29 -04:00
|
|
|
t.Fatal("should be instance of Artifact")
|
2014-03-13 00:30:13 -04:00
|
|
|
}
|
2018-07-20 16:27:29 -04:00
|
|
|
if !keep {
|
|
|
|
t.Fatal("should keep")
|
2014-03-13 00:30:13 -04:00
|
|
|
}
|
2019-04-03 14:32:49 -04:00
|
|
|
if forceOverride {
|
|
|
|
t.Fatal("Should default to keep, but not override user wishes")
|
|
|
|
}
|
2014-03-13 00:30:13 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !driver.PushCalled {
|
|
|
|
t.Fatal("should call push")
|
|
|
|
}
|
|
|
|
if driver.PushName != "localhost:5000/foo/bar" {
|
|
|
|
t.Fatal("bad name")
|
|
|
|
}
|
2018-07-20 16:27:29 -04:00
|
|
|
if result.Id() != "localhost:5000/foo/bar" {
|
|
|
|
t.Fatal("bad image id")
|
|
|
|
}
|
2014-03-13 00:30:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostProcessor_PostProcess_tags(t *testing.T) {
|
|
|
|
driver := &docker.MockDriver{}
|
|
|
|
p := &PostProcessor{Driver: driver}
|
2020-11-20 13:21:29 -05:00
|
|
|
artifact := &packersdk.MockArtifact{
|
2014-03-13 00:30:13 -04:00
|
|
|
BuilderIdValue: dockerimport.BuilderId,
|
2014-04-26 14:12:43 -04:00
|
|
|
IdValue: "hashicorp/ubuntu:precise",
|
2014-03-13 00:30:13 -04:00
|
|
|
}
|
|
|
|
|
2019-04-08 13:59:42 -04:00
|
|
|
result, keep, forceOverride, err := p.PostProcess(context.Background(), testUi(), artifact)
|
2020-11-19 15:17:11 -05:00
|
|
|
if _, ok := result.(packersdk.Artifact); !ok {
|
2018-07-20 16:27:29 -04:00
|
|
|
t.Fatal("should be instance of Artifact")
|
2014-03-13 00:30:13 -04:00
|
|
|
}
|
2018-07-20 16:27:29 -04:00
|
|
|
if !keep {
|
|
|
|
t.Fatal("should keep")
|
2014-03-13 00:30:13 -04:00
|
|
|
}
|
2019-04-03 14:32:49 -04:00
|
|
|
if forceOverride {
|
|
|
|
t.Fatal("Should default to keep, but not override user wishes")
|
|
|
|
}
|
2014-03-13 00:30:13 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !driver.PushCalled {
|
|
|
|
t.Fatal("should call push")
|
|
|
|
}
|
2016-06-16 06:00:33 -04:00
|
|
|
if driver.PushName != "hashicorp/ubuntu:precise" {
|
2014-03-13 00:30:13 -04:00
|
|
|
t.Fatalf("bad name: %s", driver.PushName)
|
|
|
|
}
|
2018-07-20 16:27:29 -04:00
|
|
|
if result.Id() != "hashicorp/ubuntu:precise" {
|
|
|
|
t.Fatal("bad image id")
|
|
|
|
}
|
2014-03-13 00:30:13 -04:00
|
|
|
}
|