packer-cn/packer/rpc/build_test.go

159 lines
2.9 KiB
Go
Raw Normal View History

package rpc
import (
"errors"
"github.com/mitchellh/packer/packer"
"reflect"
"testing"
)
2013-12-09 17:46:33 -05:00
var testBuildArtifact = &packer.MockArtifact{}
2013-05-22 01:38:41 -04:00
type testBuild struct {
nameCalled bool
prepareCalled bool
prepareVars map[string]string
prepareWarnings []string
runCalled bool
runCache packer.Cache
runUi packer.Ui
setDebugCalled bool
setForceCalled bool
cancelCalled bool
errRunResult bool
}
2013-05-09 14:32:03 -04:00
func (b *testBuild) Name() string {
b.nameCalled = true
return "name"
}
func (b *testBuild) Prepare(v map[string]string) ([]string, error) {
b.prepareCalled = true
b.prepareVars = v
return b.prepareWarnings, nil
}
func (b *testBuild) Run(ui packer.Ui, cache packer.Cache) ([]packer.Artifact, error) {
b.runCalled = true
2013-06-10 01:00:47 -04:00
b.runCache = cache
b.runUi = ui
if b.errRunResult {
return nil, errors.New("foo")
} else {
return []packer.Artifact{testBuildArtifact}, nil
}
}
2013-06-14 15:22:19 -04:00
func (b *testBuild) SetDebug(bool) {
b.setDebugCalled = true
}
func (b *testBuild) SetForce(bool) {
b.setForceCalled = true
}
2013-06-03 19:03:08 -04:00
func (b *testBuild) Cancel() {
b.cancelCalled = true
}
func TestBuild(t *testing.T) {
2013-12-10 16:18:48 -05:00
b := new(testBuild)
client, server := testClientServer(t)
defer client.Close()
defer server.Close()
server.RegisterBuild(b)
bClient := client.Build()
2013-05-09 14:32:03 -04:00
// Test Name
bClient.Name()
2013-10-16 23:04:57 -04:00
if !b.nameCalled {
t.Fatal("name should be called")
}
2013-05-09 14:32:03 -04:00
// Test Prepare
bClient.Prepare(map[string]string{"foo": "bar"})
2013-10-16 23:04:57 -04:00
if !b.prepareCalled {
t.Fatal("prepare should be called")
}
if len(b.prepareVars) != 1 {
t.Fatalf("bad vars: %#v", b.prepareVars)
}
if b.prepareVars["foo"] != "bar" {
t.Fatalf("bad vars: %#v", b.prepareVars)
}
// Test Run
2013-06-10 01:00:47 -04:00
cache := new(testCache)
ui := new(testUi)
artifacts, err := bClient.Run(ui, cache)
2013-10-16 23:04:57 -04:00
if !b.runCalled {
t.Fatal("run should be called")
}
if err != nil {
t.Fatalf("err: %s", err)
}
if len(artifacts) != 1 {
t.Fatalf("bad: %#v", artifacts)
}
if artifacts[0].BuilderId() != "bid" {
t.Fatalf("bad: %#v", artifacts)
}
// Test run with an error
b.errRunResult = true
_, err = bClient.Run(ui, cache)
2013-10-16 23:04:57 -04:00
if err == nil {
t.Fatal("should error")
}
2013-06-14 15:22:19 -04:00
// Test SetDebug
bClient.SetDebug(true)
2013-10-16 23:04:57 -04:00
if !b.setDebugCalled {
t.Fatal("should be called")
}
2013-06-14 15:22:19 -04:00
// Test SetForce
bClient.SetForce(true)
2013-10-16 23:04:57 -04:00
if !b.setForceCalled {
t.Fatal("should be called")
}
2013-06-03 19:03:08 -04:00
// Test Cancel
bClient.Cancel()
2013-10-16 23:04:57 -04:00
if !b.cancelCalled {
t.Fatal("should be called")
}
}
func TestBuildPrepare_Warnings(t *testing.T) {
2013-12-10 16:18:48 -05:00
b := new(testBuild)
client, server := testClientServer(t)
defer client.Close()
defer server.Close()
server.RegisterBuild(b)
bClient := client.Build()
expected := []string{"foo"}
b.prepareWarnings = expected
warnings, err := bClient.Prepare(nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if !reflect.DeepEqual(warnings, expected) {
t.Fatalf("bad: %#v", warnings)
}
}
func TestBuild_ImplementsBuild(t *testing.T) {
2013-12-10 16:26:07 -05:00
var _ packer.Build = new(build)
}