2013-05-04 16:47:11 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2013-10-16 23:04:57 -04:00
|
|
|
"reflect"
|
2013-05-04 16:47:11 -04:00
|
|
|
"testing"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-05-04 16:47:11 -04:00
|
|
|
)
|
|
|
|
|
2013-12-09 17:46:33 -05:00
|
|
|
var testBuilderArtifact = &packer.MockArtifact{}
|
2013-05-22 01:13:29 -04:00
|
|
|
|
2013-11-02 23:47:23 -04:00
|
|
|
func TestBuilderPrepare(t *testing.T) {
|
2013-12-10 15:14:08 -05:00
|
|
|
b := new(packer.MockBuilder)
|
|
|
|
client, server := testClientServer(t)
|
|
|
|
defer client.Close()
|
|
|
|
defer server.Close()
|
|
|
|
server.RegisterBuilder(b)
|
|
|
|
bClient := client.Builder()
|
2013-05-04 16:47:11 -04:00
|
|
|
|
|
|
|
// Test Prepare
|
|
|
|
config := 42
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := bClient.Prepare(config)
|
2013-11-02 23:49:10 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
|
2013-11-02 23:47:23 -04:00
|
|
|
if !b.PrepareCalled {
|
2013-10-16 23:04:57 -04:00
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
|
|
|
|
2014-04-26 16:31:22 -04:00
|
|
|
expected := []interface{}{int64(42)}
|
|
|
|
if !reflect.DeepEqual(b.PrepareConfig, expected) {
|
|
|
|
t.Fatalf("bad: %#v != %#v", b.PrepareConfig, expected)
|
2013-10-16 23:04:57 -04:00
|
|
|
}
|
2013-11-02 23:47:23 -04:00
|
|
|
}
|
|
|
|
|
2013-11-02 23:49:10 -04:00
|
|
|
func TestBuilderPrepare_Warnings(t *testing.T) {
|
2013-12-10 15:14:08 -05:00
|
|
|
b := new(packer.MockBuilder)
|
|
|
|
client, server := testClientServer(t)
|
|
|
|
defer client.Close()
|
|
|
|
defer server.Close()
|
|
|
|
server.RegisterBuilder(b)
|
|
|
|
bClient := client.Builder()
|
2013-11-02 23:49:10 -04:00
|
|
|
|
|
|
|
expected := []string{"foo"}
|
|
|
|
b.PrepareWarnings = expected
|
|
|
|
|
|
|
|
// Test Prepare
|
2019-12-17 00:23:05 -05:00
|
|
|
_, warnings, err := bClient.Prepare(nil)
|
2013-11-02 23:49:10 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(warnings, expected) {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-02 23:47:23 -04:00
|
|
|
func TestBuilderRun(t *testing.T) {
|
2013-12-10 15:14:08 -05:00
|
|
|
b := new(packer.MockBuilder)
|
|
|
|
client, server := testClientServer(t)
|
|
|
|
defer client.Close()
|
|
|
|
defer server.Close()
|
|
|
|
server.RegisterBuilder(b)
|
|
|
|
bClient := client.Builder()
|
2013-05-04 16:50:02 -04:00
|
|
|
|
|
|
|
// Test Run
|
2013-08-30 20:03:55 -04:00
|
|
|
hook := &packer.MockHook{}
|
2013-05-04 16:50:02 -04:00
|
|
|
ui := &testUi{}
|
2019-03-22 09:53:28 -04:00
|
|
|
artifact, err := bClient.Run(context.Background(), ui, hook)
|
2013-10-16 23:04:57 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-11-02 23:47:23 -04:00
|
|
|
if !b.RunCalled {
|
2013-10-16 23:04:57 -04:00
|
|
|
t.Fatal("run should be called")
|
|
|
|
}
|
2013-05-04 16:50:02 -04:00
|
|
|
|
2013-11-02 23:47:23 -04:00
|
|
|
if artifact.Id() != testBuilderArtifact.Id() {
|
|
|
|
t.Fatalf("bad: %s", artifact.Id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderRun_nilResult(t *testing.T) {
|
2013-12-10 15:14:08 -05:00
|
|
|
b := new(packer.MockBuilder)
|
2013-11-02 23:47:23 -04:00
|
|
|
b.RunNilResult = true
|
|
|
|
|
2013-12-10 15:14:08 -05:00
|
|
|
client, server := testClientServer(t)
|
|
|
|
defer client.Close()
|
|
|
|
defer server.Close()
|
|
|
|
server.RegisterBuilder(b)
|
|
|
|
bClient := client.Builder()
|
|
|
|
|
2013-11-02 23:47:23 -04:00
|
|
|
hook := &packer.MockHook{}
|
|
|
|
ui := &testUi{}
|
2019-03-22 09:53:28 -04:00
|
|
|
artifact, err := bClient.Run(context.Background(), ui, hook)
|
2013-10-16 23:04:57 -04:00
|
|
|
if artifact != nil {
|
|
|
|
t.Fatalf("bad: %#v", artifact)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
2013-11-02 23:47:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderRun_ErrResult(t *testing.T) {
|
2013-12-10 15:14:08 -05:00
|
|
|
b := new(packer.MockBuilder)
|
|
|
|
client, server := testClientServer(t)
|
|
|
|
defer client.Close()
|
|
|
|
defer server.Close()
|
|
|
|
server.RegisterBuilder(b)
|
|
|
|
bClient := client.Builder()
|
|
|
|
|
2013-11-02 23:47:23 -04:00
|
|
|
b.RunErrResult = true
|
2013-06-12 18:58:02 -04:00
|
|
|
|
2013-11-02 23:47:23 -04:00
|
|
|
hook := &packer.MockHook{}
|
|
|
|
ui := &testUi{}
|
2019-03-22 09:53:28 -04:00
|
|
|
artifact, err := bClient.Run(context.Background(), ui, hook)
|
2013-10-16 23:04:57 -04:00
|
|
|
if artifact != nil {
|
|
|
|
t.Fatalf("bad: %#v", artifact)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
2013-11-02 23:47:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderCancel(t *testing.T) {
|
2019-03-27 07:29:09 -04:00
|
|
|
topCtx, topCtxCancel := context.WithCancel(context.Background())
|
|
|
|
// var runCtx context.Context
|
|
|
|
|
2013-12-10 15:14:08 -05:00
|
|
|
b := new(packer.MockBuilder)
|
2019-03-27 07:29:09 -04:00
|
|
|
cancelled := false
|
|
|
|
b.RunFn = func(ctx context.Context) {
|
|
|
|
topCtxCancel()
|
|
|
|
<-ctx.Done()
|
|
|
|
cancelled = true
|
|
|
|
}
|
2013-12-10 15:14:08 -05:00
|
|
|
client, server := testClientServer(t)
|
|
|
|
defer client.Close()
|
|
|
|
defer server.Close()
|
|
|
|
server.RegisterBuilder(b)
|
|
|
|
bClient := client.Builder()
|
2013-06-05 18:36:26 -04:00
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
bClient.Run(topCtx, new(testUi), new(packer.MockHook))
|
2019-03-22 09:53:28 -04:00
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
if !cancelled {
|
|
|
|
t.Fatal("context should have been cancelled")
|
2013-10-16 23:04:57 -04:00
|
|
|
}
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 14:32:03 -04:00
|
|
|
func TestBuilder_ImplementsBuilder(t *testing.T) {
|
2013-12-10 16:26:07 -05:00
|
|
|
var _ packer.Builder = new(builder)
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|