2013-05-04 02:55:08 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2013-06-12 19:01:42 -04:00
|
|
|
"errors"
|
2013-11-02 23:40:06 -04:00
|
|
|
"reflect"
|
2013-05-04 02:55:08 -04:00
|
|
|
"testing"
|
2016-09-13 20:04:18 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-05-04 02:55:08 -04:00
|
|
|
)
|
|
|
|
|
2013-12-09 17:46:33 -05:00
|
|
|
var testBuildArtifact = &packer.MockArtifact{}
|
2013-05-22 01:38:41 -04:00
|
|
|
|
2013-05-04 02:55:08 -04:00
|
|
|
type testBuild struct {
|
2016-09-13 20:04:18 -04:00
|
|
|
nameCalled bool
|
|
|
|
prepareCalled bool
|
|
|
|
prepareWarnings []string
|
2019-03-27 07:29:09 -04:00
|
|
|
runFn func(context.Context)
|
2016-09-13 20:04:18 -04:00
|
|
|
runCalled bool
|
|
|
|
runUi packer.Ui
|
|
|
|
setDebugCalled bool
|
|
|
|
setForceCalled bool
|
|
|
|
setOnErrorCalled bool
|
2013-06-12 19:01:42 -04:00
|
|
|
|
|
|
|
errRunResult bool
|
2013-05-04 02:55:08 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 14:32:03 -04:00
|
|
|
func (b *testBuild) Name() string {
|
|
|
|
b.nameCalled = true
|
|
|
|
return "name"
|
|
|
|
}
|
|
|
|
|
2013-12-27 11:19:11 -05:00
|
|
|
func (b *testBuild) Prepare() ([]string, error) {
|
2013-05-04 02:55:08 -04:00
|
|
|
b.prepareCalled = true
|
2013-11-02 23:40:06 -04:00
|
|
|
return b.prepareWarnings, nil
|
2013-05-04 02:55:08 -04:00
|
|
|
}
|
|
|
|
|
2019-03-22 09:53:28 -04:00
|
|
|
func (b *testBuild) Run(ctx context.Context, ui packer.Ui) ([]packer.Artifact, error) {
|
2013-05-04 02:55:08 -04:00
|
|
|
b.runCalled = true
|
|
|
|
b.runUi = ui
|
2013-06-12 19:01:42 -04:00
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
if b.runFn != nil {
|
|
|
|
b.runFn(ctx)
|
|
|
|
}
|
|
|
|
|
2013-06-12 19:01:42 -04:00
|
|
|
if b.errRunResult {
|
|
|
|
return nil, errors.New("foo")
|
|
|
|
} else {
|
2013-06-18 13:24:23 -04:00
|
|
|
return []packer.Artifact{testBuildArtifact}, nil
|
2013-06-12 19:01:42 -04:00
|
|
|
}
|
2013-05-04 02:55:08 -04:00
|
|
|
}
|
|
|
|
|
2013-06-14 15:22:19 -04:00
|
|
|
func (b *testBuild) SetDebug(bool) {
|
|
|
|
b.setDebugCalled = true
|
|
|
|
}
|
|
|
|
|
2013-07-12 00:43:23 -04:00
|
|
|
func (b *testBuild) SetForce(bool) {
|
|
|
|
b.setForceCalled = true
|
|
|
|
}
|
|
|
|
|
2016-09-13 20:04:18 -04:00
|
|
|
func (b *testBuild) SetOnError(string) {
|
|
|
|
b.setOnErrorCalled = true
|
|
|
|
}
|
|
|
|
|
2013-11-02 23:40:06 -04:00
|
|
|
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
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
ctx := context.Background()
|
2019-03-22 09:53:28 -04:00
|
|
|
|
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
|
2013-12-27 11:19:11 -05:00
|
|
|
bClient.Prepare()
|
2013-10-16 23:04:57 -04:00
|
|
|
if !b.prepareCalled {
|
|
|
|
t.Fatal("prepare should be called")
|
|
|
|
}
|
|
|
|
|
2013-05-04 02:55:08 -04:00
|
|
|
// Test Run
|
2013-06-13 13:08:31 -04:00
|
|
|
ui := new(testUi)
|
2019-03-22 09:53:28 -04:00
|
|
|
artifacts, err := bClient.Run(ctx, ui)
|
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)
|
|
|
|
}
|
2013-05-04 02:55:08 -04:00
|
|
|
|
2013-06-12 19:01:42 -04:00
|
|
|
// Test run with an error
|
|
|
|
b.errRunResult = true
|
2019-03-22 09:53:28 -04:00
|
|
|
_, err = bClient.Run(ctx, ui)
|
2013-10-16 23:04:57 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
2013-06-12 19:01:42 -04:00
|
|
|
|
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
|
|
|
|
2013-07-12 00:43:23 -04:00
|
|
|
// Test SetForce
|
|
|
|
bClient.SetForce(true)
|
2013-10-16 23:04:57 -04:00
|
|
|
if !b.setForceCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
2013-07-12 00:43:23 -04:00
|
|
|
|
2016-09-13 20:04:18 -04:00
|
|
|
// Test SetOnError
|
|
|
|
bClient.SetOnError("ask")
|
|
|
|
if !b.setOnErrorCalled {
|
|
|
|
t.Fatal("should be called")
|
|
|
|
}
|
2019-03-27 07:29:09 -04:00
|
|
|
}
|
2016-09-13 20:04:18 -04:00
|
|
|
|
2019-03-27 07:29:09 -04:00
|
|
|
func TestBuild_cancel(t *testing.T) {
|
|
|
|
topCtx, cancelTopCtx := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
b := new(testBuild)
|
|
|
|
|
|
|
|
done := make(chan interface{})
|
|
|
|
b.runFn = func(ctx context.Context) {
|
|
|
|
cancelTopCtx()
|
|
|
|
<-ctx.Done()
|
|
|
|
close(done)
|
2013-10-16 23:04:57 -04:00
|
|
|
}
|
2019-03-27 07:29:09 -04:00
|
|
|
|
|
|
|
client, server := testClientServer(t)
|
|
|
|
defer client.Close()
|
|
|
|
defer server.Close()
|
|
|
|
server.RegisterBuild(b)
|
|
|
|
bClient := client.Build()
|
|
|
|
|
|
|
|
bClient.Prepare()
|
|
|
|
|
|
|
|
ui := new(testUi)
|
|
|
|
bClient.Run(topCtx, ui)
|
|
|
|
|
|
|
|
// if context cancellation is not propagated, this will timeout
|
|
|
|
<-done
|
2013-05-04 02:55:08 -04:00
|
|
|
}
|
|
|
|
|
2013-11-02 23:40:06 -04:00
|
|
|
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()
|
2013-11-02 23:40:06 -04:00
|
|
|
|
|
|
|
expected := []string{"foo"}
|
|
|
|
b.prepareWarnings = expected
|
|
|
|
|
2013-12-27 11:19:11 -05:00
|
|
|
warnings, err := bClient.Prepare()
|
2013-11-02 23:40:06 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(warnings, expected) {
|
|
|
|
t.Fatalf("bad: %#v", warnings)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-04 02:55:08 -04:00
|
|
|
func TestBuild_ImplementsBuild(t *testing.T) {
|
2013-12-10 16:26:07 -05:00
|
|
|
var _ packer.Build = new(build)
|
2013-05-04 02:55:08 -04:00
|
|
|
}
|