packer-cn/post-processor/docker-push/post-processor_test.go

135 lines
3.1 KiB
Go
Raw Normal View History

package dockerpush
import (
"bytes"
2019-03-22 09:56:02 -04:00
"context"
2018-01-22 20:21:10 -05:00
"testing"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/builder/docker"
"github.com/hashicorp/packer/packer"
2019-03-22 09:56:02 -04:00
dockerimport "github.com/hashicorp/packer/post-processor/docker-import"
)
func testConfig() map[string]interface{} {
return map[string]interface{}{}
}
func testPP(t *testing.T) *PostProcessor {
var p PostProcessor
if err := p.Configure(testConfig()); err != nil {
t.Fatalf("err: %s", err)
}
return &p
}
func testUi() *packer.BasicUi {
return &packer.BasicUi{
Reader: new(bytes.Buffer),
Writer: new(bytes.Buffer),
}
}
func TestPostProcessor_ImplementsPostProcessor(t *testing.T) {
var _ packer.PostProcessor = new(PostProcessor)
}
func TestPostProcessor_PostProcess(t *testing.T) {
driver := &docker.MockDriver{}
p := &PostProcessor{Driver: driver}
artifact := &packer.MockArtifact{
BuilderIdValue: dockerimport.BuilderId,
2014-04-26 14:12:43 -04:00
IdValue: "foo/bar",
}
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")
}
2019-04-03 14:32:49 -04:00
if forceOverride {
t.Fatal("Should default to keep, but not override user wishes")
}
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")
}
if result.Id() != "foo/bar" {
t.Fatal("bad image id")
}
}
func TestPostProcessor_PostProcess_portInName(t *testing.T) {
driver := &docker.MockDriver{}
p := &PostProcessor{Driver: driver}
artifact := &packer.MockArtifact{
BuilderIdValue: dockerimport.BuilderId,
2014-04-26 14:12:43 -04:00
IdValue: "localhost:5000/foo/bar",
}
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")
}
2019-04-03 14:32:49 -04:00
if forceOverride {
t.Fatal("Should default to keep, but not override user wishes")
}
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")
}
if result.Id() != "localhost:5000/foo/bar" {
t.Fatal("bad image id")
}
}
func TestPostProcessor_PostProcess_tags(t *testing.T) {
driver := &docker.MockDriver{}
p := &PostProcessor{Driver: driver}
artifact := &packer.MockArtifact{
BuilderIdValue: dockerimport.BuilderId,
2014-04-26 14:12:43 -04:00
IdValue: "hashicorp/ubuntu:precise",
}
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")
}
2019-04-03 14:32:49 -04:00
if forceOverride {
t.Fatal("Should default to keep, but not override user wishes")
}
if err != nil {
t.Fatalf("err: %s", err)
}
if !driver.PushCalled {
t.Fatal("should call push")
}
if driver.PushName != "hashicorp/ubuntu:precise" {
t.Fatalf("bad name: %s", driver.PushName)
}
if result.Id() != "hashicorp/ubuntu:precise" {
t.Fatal("bad image id")
}
}