Commit 19867b75 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer/rpc: don't depend on cgl

parent 24ad445e
package rpc
import (
"cgl.tideland.biz/asserts"
"github.com/mitchellh/packer/packer"
"net/rpc"
"reflect"
"testing"
)
......@@ -30,8 +30,6 @@ func (testArtifact) Destroy() error {
}
func TestArtifactRPC(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
// Create the interface to test
a := new(testArtifact)
......@@ -42,21 +40,29 @@ func TestArtifactRPC(t *testing.T) {
// Create the client over RPC and run some methods to verify it works
client, err := rpc.Dial("tcp", address)
assert.Nil(err, "should be able to connect")
if err != nil {
t.Fatalf("err: %s", err)
}
aClient := Artifact(client)
// Test
assert.Equal(aClient.BuilderId(), "bid", "should have correct builder ID")
assert.Equal(aClient.Files(), []string{"a", "b"}, "should have correct builder ID")
assert.Equal(aClient.Id(), "id", "should have correct builder ID")
assert.Equal(aClient.String(), "string", "should have correct builder ID")
}
if aClient.BuilderId() != "bid" {
t.Fatalf("bad: %s", aClient.BuilderId())
}
func TestArtifact_Implements(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
if !reflect.DeepEqual(aClient.Files(), []string{"a", "b"}) {
t.Fatalf("bad: %#v", aClient.Files())
}
var r packer.Artifact
a := Artifact(nil)
if aClient.Id() != "id" {
t.Fatalf("bad: %s", aClient.Id())
}
assert.Implementor(a, &r, "should be an Artifact")
if aClient.String() != "string" {
t.Fatalf("bad: %s", aClient.String())
}
}
func TestArtifact_Implements(t *testing.T) {
var _ packer.Artifact = Artifact(nil)
}
package rpc
import (
"cgl.tideland.biz/asserts"
"errors"
"github.com/mitchellh/packer/packer"
"net/rpc"
......@@ -60,8 +59,6 @@ func (b *testBuild) Cancel() {
}
func TestBuildRPC(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
// Create the interface to test
b := new(testBuild)
......@@ -72,16 +69,23 @@ func TestBuildRPC(t *testing.T) {
// Create the client over RPC and run some methods to verify it works
client, err := rpc.Dial("tcp", address)
assert.Nil(err, "should be able to connect")
if err != nil {
t.Fatalf("err: %s", err)
}
bClient := Build(client)
// Test Name
bClient.Name()
assert.True(b.nameCalled, "name should be called")
if !b.nameCalled {
t.Fatal("name should be called")
}
// Test Prepare
bClient.Prepare(map[string]string{"foo": "bar"})
assert.True(b.prepareCalled, "prepare should be called")
if !b.prepareCalled {
t.Fatal("prepare should be called")
}
if len(b.prepareVars) != 1 {
t.Fatalf("bad vars: %#v", b.prepareVars)
}
......@@ -94,44 +98,65 @@ func TestBuildRPC(t *testing.T) {
cache := new(testCache)
ui := new(testUi)
artifacts, err := bClient.Run(ui, cache)
assert.True(b.runCalled, "run should be called")
assert.Nil(err, "should not error")
assert.Equal(len(artifacts), 1, "should have one artifact")
assert.Equal(artifacts[0].BuilderId(), "bid", "should have proper builder id")
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 the UI given to run, which should be fully functional
if b.runCalled {
b.runCache.Lock("foo")
assert.True(cache.lockCalled, "lock should be called")
if !cache.lockCalled {
t.Fatal("lock shuld be called")
}
b.runUi.Say("format")
assert.True(ui.sayCalled, "say should be called")
assert.Equal(ui.sayMessage, "format", "message should be correct")
if !ui.sayCalled {
t.Fatal("say should be called")
}
if ui.sayMessage != "format" {
t.Fatalf("bad: %#v", ui.sayMessage)
}
}
// Test run with an error
b.errRunResult = true
_, err = bClient.Run(ui, cache)
assert.NotNil(err, "should not nil")
if err == nil {
t.Fatal("should error")
}
// Test SetDebug
bClient.SetDebug(true)
assert.True(b.setDebugCalled, "should be called")
if !b.setDebugCalled {
t.Fatal("should be called")
}
// Test SetForce
bClient.SetForce(true)
assert.True(b.setForceCalled, "should be called")
if !b.setForceCalled {
t.Fatal("should be called")
}
// Test Cancel
bClient.Cancel()
assert.True(b.cancelCalled, "cancel should be called")
if !b.cancelCalled {
t.Fatal("should be called")
}
}
func TestBuild_ImplementsBuild(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
var realBuild packer.Build
b := Build(nil)
assert.Implementor(b, &realBuild, "should be a Build")
var _ packer.Build = Build(nil)
}
package rpc
import (
"cgl.tideland.biz/asserts"
"errors"
"github.com/mitchellh/packer/packer"
"net/rpc"
"reflect"
"testing"
)
......@@ -49,8 +49,6 @@ func (b *testBuilder) Cancel() {
}
func TestBuilderRPC(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
// Create the interface to test
b := new(testBuilder)
......@@ -61,60 +59,88 @@ func TestBuilderRPC(t *testing.T) {
// Create the client over RPC and run some methods to verify it works
client, err := rpc.Dial("tcp", address)
assert.Nil(err, "should be able to connect")
if err != nil {
t.Fatalf("err: %s", err)
}
// Test Prepare
config := 42
bClient := Builder(client)
bClient.Prepare(config)
assert.True(b.prepareCalled, "prepare should be called")
assert.Equal(b.prepareConfig, []interface{}{42}, "prepare should be called with right arg")
if !b.prepareCalled {
t.Fatal("should be called")
}
if !reflect.DeepEqual(b.prepareConfig, []interface{}{42}) {
t.Fatalf("bad: %#v", b.prepareConfig)
}
// Test Run
cache := new(testCache)
hook := &packer.MockHook{}
ui := &testUi{}
artifact, err := bClient.Run(ui, hook, cache)
assert.Nil(err, "should have no error")
assert.True(b.runCalled, "runs hould be called")
if err != nil {
t.Fatalf("err: %s", err)
}
if !b.runCalled {
t.Fatal("run should be called")
}
if b.runCalled {
b.runCache.Lock("foo")
assert.True(cache.lockCalled, "lock should be called")
if !cache.lockCalled {
t.Fatal("should be called")
}
b.runHook.Run("foo", nil, nil, nil)
assert.True(hook.RunCalled, "run should be called")
if !hook.RunCalled {
t.Fatal("should be called")
}
b.runUi.Say("format")
assert.True(ui.sayCalled, "say should be called")
assert.Equal(ui.sayMessage, "format", "message should be correct")
if !ui.sayCalled {
t.Fatal("say should be called")
}
assert.Equal(artifact.Id(), testBuilderArtifact.Id(), "should have artifact Id")
if ui.sayMessage != "format" {
t.Fatalf("bad: %s", ui.sayMessage)
}
if artifact.Id() != testBuilderArtifact.Id() {
t.Fatalf("bad: %s", artifact.Id())
}
}
// Test run with nil result
b.nilRunResult = true
artifact, err = bClient.Run(ui, hook, cache)
assert.Nil(artifact, "should be nil")
assert.Nil(err, "should have no error")
if artifact != nil {
t.Fatalf("bad: %#v", artifact)
}
if err != nil {
t.Fatalf("bad: %#v", err)
}
// Test with an error
b.errRunResult = true
b.nilRunResult = false
artifact, err = bClient.Run(ui, hook, cache)
assert.Nil(artifact, "should be nil")
assert.NotNil(err, "should have error")
if artifact != nil {
t.Fatalf("bad: %#v", artifact)
}
if err == nil {
t.Fatal("should have error")
}
// Test Cancel
bClient.Cancel()
assert.True(b.cancelCalled, "cancel should be called")
if !b.cancelCalled {
t.Fatal("cancel should be called")
}
}
func TestBuilder_ImplementsBuilder(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
var realBuilder packer.Builder
b := Builder(nil)
assert.Implementor(b, &realBuilder, "should be a Builder")
var _ packer.Builder = Builder(nil)
}
package rpc
import (
"cgl.tideland.biz/asserts"
"github.com/mitchellh/packer/packer"
"net/rpc"
"testing"
......@@ -49,8 +48,6 @@ func TestCache_Implements(t *testing.T) {
}
func TestCacheRPC(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
// Create the interface to test
c := new(testCache)
......@@ -61,26 +58,44 @@ func TestCacheRPC(t *testing.T) {
// Create the client over RPC and run some methods to verify it works
rpcClient, err := rpc.Dial("tcp", address)
assert.Nil(err, "should be able to connect")
if err != nil {
t.Fatalf("bad: %s", err)
}
client := Cache(rpcClient)
// Test Lock
client.Lock("foo")
assert.True(c.lockCalled, "should be called")
assert.Equal(c.lockKey, "foo", "should have proper key")
if !c.lockCalled {
t.Fatal("should be called")
}
if c.lockKey != "foo" {
t.Fatalf("bad: %s", c.lockKey)
}
// Test Unlock
client.Unlock("foo")
assert.True(c.unlockCalled, "should be called")
assert.Equal(c.unlockKey, "foo", "should have proper key")
if !c.unlockCalled {
t.Fatal("should be called")
}
if c.unlockKey != "foo" {
t.Fatalf("bad: %s", c.unlockKey)
}
// Test RLock
client.RLock("foo")
assert.True(c.rlockCalled, "should be called")
assert.Equal(c.rlockKey, "foo", "should have proper key")
if !c.rlockCalled {
t.Fatal("should be called")
}
if c.rlockKey != "foo" {
t.Fatalf("bad: %s", c.rlockKey)
}
// Test RUnlock
client.RUnlock("foo")
assert.True(c.runlockCalled, "should be called")
assert.Equal(c.runlockKey, "foo", "should have proper key")
if !c.runlockCalled {
t.Fatal("should be called")
}
if c.runlockKey != "foo" {
t.Fatalf("bad: %s", c.runlockKey)
}
}
package rpc
import (
"cgl.tideland.biz/asserts"
"github.com/mitchellh/packer/packer"
"net/rpc"
"reflect"
"testing"
)
......@@ -29,8 +29,6 @@ func (tc *TestCommand) Synopsis() string {
}
func TestRPCCommand(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
// Create the command
command := new(TestCommand)
......@@ -42,37 +40,45 @@ func TestRPCCommand(t *testing.T) {
// Create the command client over RPC and run some methods to verify
// we get the proper behavior.
client, err := rpc.Dial("tcp", address)
assert.Nil(err, "should be no error")
if err != nil {
t.Fatalf("err: %s", err)
}
clientComm := Command(client)
//Test Help
help := clientComm.Help()
assert.Equal(help, "bar", "helps hould be correct")
if help != "bar" {
t.Fatalf("bad: %s", help)
}
// Test run
runArgs := []string{"foo", "bar"}
testEnv := &testEnvironment{}
exitCode := clientComm.Run(testEnv, runArgs)
assert.Equal(command.runArgs, runArgs, "Correct args should be sent")
assert.Equal(exitCode, 0, "Exit code should be correct")
if !reflect.DeepEqual(command.runArgs, runArgs) {
t.Fatalf("bad: %#v", command.runArgs)
}
if exitCode != 0 {
t.Fatalf("bad: %d", exitCode)
}
assert.NotNil(command.runEnv, "should have an env")
if command.runEnv != nil {
command.runEnv.Ui()
assert.True(testEnv.uiCalled, "UI should be called on env")
if command.runEnv == nil {
t.Fatal("runEnv should not be nil")
}
command.runEnv.Ui()
if !testEnv.uiCalled {
t.Fatal("ui should be called")
}
// Test Synopsis
synopsis := clientComm.Synopsis()
assert.Equal(synopsis, "foo", "Synopsis should be correct")
if synopsis != "foo" {
t.Fatalf("bad: %#v", synopsis)
}
}
func TestCommand_Implements(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
var r packer.Command
c := Command(nil)
assert.Implementor(c, &r, "should be a Builder")
var _ packer.Command = Command(nil)
}
package rpc
import (
"cgl.tideland.biz/asserts"
"github.com/mitchellh/packer/packer"
"net/rpc"
"reflect"
"testing"
)
......@@ -65,8 +65,6 @@ func (e *testEnvironment) Ui() packer.Ui {
}
func TestEnvironmentRPC(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
// Create the interface to test
e := &testEnvironment{}
......@@ -77,49 +75,70 @@ func TestEnvironmentRPC(t *testing.T) {
// Create the client over RPC and run some methods to verify it works
client, err := rpc.Dial("tcp", address)
assert.Nil(err, "should be able to connect")
if err != nil {
t.Fatalf("err: %s", err)
}
eClient := &Environment{client}
// Test Builder
builder, _ := eClient.Builder("foo")
assert.True(e.builderCalled, "Builder should be called")
assert.Equal(e.builderName, "foo", "Correct name for Builder")
if !e.builderCalled {
t.Fatal("builder should be called")
}
if e.builderName != "foo" {
t.Fatalf("bad: %#v", e.builderName)
}
builder.Prepare(nil)
assert.True(testEnvBuilder.prepareCalled, "Prepare should be called")
if !testEnvBuilder.prepareCalled {
t.Fatal("should be called")
}
// Test Cache
cache := eClient.Cache()
cache.Lock("foo")
assert.True(testEnvCache.lockCalled, "lock should be called")
if !testEnvCache.lockCalled {
t.Fatal("should be called")
}
// Test Cli
cliArgs := []string{"foo", "bar"}
result, _ := eClient.Cli(cliArgs)
assert.True(e.cliCalled, "CLI should be called")
assert.Equal(e.cliArgs, cliArgs, "args should match")
assert.Equal(result, 42, "result shuld be 42")
if !e.cliCalled {
t.Fatal("should be called")
}
if !reflect.DeepEqual(e.cliArgs, cliArgs) {
t.Fatalf("bad: %#v", e.cliArgs)
}
if result != 42 {
t.Fatalf("bad: %#v", result)
}
// Test Provisioner
_, _ = eClient.Provisioner("foo")
assert.True(e.provCalled, "provisioner should be called")
assert.Equal(e.provName, "foo", "should have proper name")
if !e.provCalled {
t.Fatal("should be called")
}
if e.provName != "foo" {
t.Fatalf("bad: %s", e.provName)
}
// Test Ui
ui := eClient.Ui()
assert.True(e.uiCalled, "Ui should've been called")
if !e.uiCalled {
t.Fatal("should be called")
}
// Test calls on the Ui
ui.Say("format")
assert.True(testEnvUi.sayCalled, "Say should be called")
assert.Equal(testEnvUi.sayMessage, "format", "message should match")
if !testEnvUi.sayCalled {
t.Fatal("should be called")
}
if testEnvUi.sayMessage != "format" {
t.Fatalf("bad: %#v", testEnvUi.sayMessage)
}
}
func TestEnvironment_ImplementsEnvironment(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
var realVar packer.Environment
e := &Environment{nil}
assert.Implementor(e, &realVar, "should be an Environment")
var _ packer.Environment = new(Environment)
}
package rpc
import (
"cgl.tideland.biz/asserts"
"errors"
"testing"
)
func TestBasicError_ImplementsError(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
var r error
e := &BasicError{""}
assert.Implementor(e, &r, "should be an error")
var _ error = new(BasicError)
}
func TestBasicError_MatchesMessage(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
err := errors.New("foo")
wrapped := NewBasicError(err)
assert.Equal(wrapped.Error(), err.Error(), "should have the same error")
if wrapped.Error() != err.Error() {
t.Fatalf("bad: %#v", wrapped.Error())
}
}
package rpc
import (
"cgl.tideland.biz/asserts"
"github.com/mitchellh/packer/packer"
"net/rpc"
"reflect"
......@@ -11,8 +10,6 @@ import (
)
func TestHookRPC(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
// Create the UI to test
h := new(packer.MockHook)
......@@ -23,27 +20,28 @@ func TestHookRPC(t *testing.T) {
// Create the client over RPC and run some methods to verify it works
client, err := rpc.Dial("tcp", address)
assert.Nil(err, "should be able to connect")
if err != nil {
t.Fatalf("err: %s", err)
}
hClient := Hook(client)
// Test Run
ui := &testUi{}
hClient.Run("foo", ui, nil, 42)
assert.True(h.RunCalled, "run should be called")
if !h.RunCalled {
t.Fatal("should be called")
}
// Test Cancel
hClient.Cancel()
assert.True(h.CancelCalled, "cancel should be called")
if !h.CancelCalled {
t.Fatal("should be called")
}
}
func TestHook_Implements(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
var r packer.Hook
h := &hook{nil}
assert.Implementor(h, &r, "should be a Hook")
var _ packer.Hook = new(hook)
}
func TestHook_cancelWhileRun(t *testing.T) {
......
package rpc
import (
"cgl.tideland.biz/asserts"
"net"
"strings"
"testing"
......@@ -13,21 +12,27 @@ func addrPort(address net.Addr) string {
}
func Test_netListenerInRange(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
// Open up port 10000 so that we take up a port
L1000, err := net.Listen("tcp", "127.0.0.1:11000")
defer L1000.Close()
assert.Nil(err, "should be able to bind to port 10000")
if err != nil {
t.Fatalf("bad: %s", err)
}
if err == nil {
// Verify it selects an open port
L := netListenerInRange(11000, 11005)
assert.NotNil(L, "should have a listener")
assert.Equal(addrPort(L.Addr()), "11001", "should bind to open port")
if L == nil {
t.Fatal("L should not be nil")
}
if addrPort(L.Addr()) != "11001" {
t.Fatalf("bad: %s", L.Addr())
}
// Returns nil if there are no open ports
L = netListenerInRange(11000, 11000)
assert.Nil(L, "should not get a listener")
if L != nil {
t.Fatalf("bad: %#v", L)
}
}
}
package rpc
import (
"cgl.tideland.biz/asserts"
"github.com/mitchellh/packer/packer"
"net/rpc"
"reflect"
"testing"
)
func TestProvisionerRPC(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
// Create the interface to test
p := new(packer.MockProvisioner)
......@@ -20,23 +18,33 @@ func TestProvisionerRPC(t *testing.T) {
// Create the client over RPC and run some methods to verify it works
client, err := rpc.Dial("tcp", address)
assert.Nil(err, "should be able to connect")
if err != nil {
t.Fatalf("err: %s", err)
}
// Test Prepare
config := 42
pClient := Provisioner(client)
pClient.Prepare(config)
assert.True(p.PrepCalled, "prepare should be called")
assert.Equal(p.PrepConfigs, []interface{}{42}, "prepare should be called with right arg")
if !p.PrepCalled {
t.Fatal("should be called")
}
if !reflect.DeepEqual(p.PrepConfigs, []interface{}{42}) {
t.Fatalf("bad: %#v", p.PrepConfigs)
}
// Test Provision
ui := &testUi{}
comm := &packer.MockCommunicator{}
pClient.Provision(ui, comm)
assert.True(p.ProvCalled, "provision should be called")
if !p.ProvCalled {
t.Fatal("should be called")
}
p.ProvUi.Say("foo")
assert.True(ui.sayCalled, "say should be called")
if !ui.sayCalled {
t.Fatal("should be called")
}
// Test Cancel
pClient.Cancel()
......@@ -46,10 +54,5 @@ func TestProvisionerRPC(t *testing.T) {
}
func TestProvisioner_Implements(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
var r packer.Provisioner
p := Provisioner(nil)
assert.Implementor(p, &r, "should be a provisioner")
var _ packer.Provisioner = Provisioner(nil)
}
package rpc
import (
"cgl.tideland.biz/asserts"
"net/rpc"
"reflect"
"testing"
......@@ -49,8 +48,6 @@ func (u *testUi) Say(message string) {
}
func TestUiRPC(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
// Create the UI to test
ui := new(testUi)
......@@ -69,19 +66,33 @@ func TestUiRPC(t *testing.T) {
// Basic error and say tests
result, err := uiClient.Ask("query")
assert.Nil(err, "should not error")
assert.True(ui.askCalled, "ask should be called")
assert.Equal(ui.askQuery, "query", "should be correct")
assert.Equal(result, "foo", "should have correct result")
if err != nil {
t.Fatalf("err: %s", err)
}
if !ui.askCalled {
t.Fatal("should be called")
}
if ui.askQuery != "query" {
t.Fatalf("bad: %s", ui.askQuery)
}
if result != "foo" {
t.Fatalf("bad: %#v", result)
}
uiClient.Error("message")
assert.Equal(ui.errorMessage, "message", "message should be correct")
if ui.errorMessage != "message" {
t.Fatalf("bad: %#v", ui.errorMessage)
}
uiClient.Message("message")
assert.Equal(ui.messageMessage, "message", "message should be correct")
if ui.messageMessage != "message" {
t.Fatalf("bad: %#v", ui.errorMessage)
}
uiClient.Say("message")
assert.Equal(ui.sayMessage, "message", "message should be correct")
if ui.sayMessage != "message" {
t.Fatalf("bad: %#v", ui.errorMessage)
}
uiClient.Machine("foo", "bar", "baz")
if !ui.machineCalled {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment