github.com/rothwerx/packer@v0.9.0/packer/rpc/build_test.go (about)

     1  package rpc
     2  
     3  import (
     4  	"errors"
     5  	"github.com/mitchellh/packer/packer"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  var testBuildArtifact = &packer.MockArtifact{}
    11  
    12  type testBuild struct {
    13  	nameCalled      bool
    14  	prepareCalled   bool
    15  	prepareWarnings []string
    16  	runCalled       bool
    17  	runCache        packer.Cache
    18  	runUi           packer.Ui
    19  	setDebugCalled  bool
    20  	setForceCalled  bool
    21  	cancelCalled    bool
    22  
    23  	errRunResult bool
    24  }
    25  
    26  func (b *testBuild) Name() string {
    27  	b.nameCalled = true
    28  	return "name"
    29  }
    30  
    31  func (b *testBuild) Prepare() ([]string, error) {
    32  	b.prepareCalled = true
    33  	return b.prepareWarnings, nil
    34  }
    35  
    36  func (b *testBuild) Run(ui packer.Ui, cache packer.Cache) ([]packer.Artifact, error) {
    37  	b.runCalled = true
    38  	b.runCache = cache
    39  	b.runUi = ui
    40  
    41  	if b.errRunResult {
    42  		return nil, errors.New("foo")
    43  	} else {
    44  		return []packer.Artifact{testBuildArtifact}, nil
    45  	}
    46  }
    47  
    48  func (b *testBuild) SetDebug(bool) {
    49  	b.setDebugCalled = true
    50  }
    51  
    52  func (b *testBuild) SetForce(bool) {
    53  	b.setForceCalled = true
    54  }
    55  
    56  func (b *testBuild) Cancel() {
    57  	b.cancelCalled = true
    58  }
    59  
    60  func TestBuild(t *testing.T) {
    61  	b := new(testBuild)
    62  	client, server := testClientServer(t)
    63  	defer client.Close()
    64  	defer server.Close()
    65  	server.RegisterBuild(b)
    66  	bClient := client.Build()
    67  
    68  	// Test Name
    69  	bClient.Name()
    70  	if !b.nameCalled {
    71  		t.Fatal("name should be called")
    72  	}
    73  
    74  	// Test Prepare
    75  	bClient.Prepare()
    76  	if !b.prepareCalled {
    77  		t.Fatal("prepare should be called")
    78  	}
    79  
    80  	// Test Run
    81  	cache := new(testCache)
    82  	ui := new(testUi)
    83  	artifacts, err := bClient.Run(ui, cache)
    84  	if !b.runCalled {
    85  		t.Fatal("run should be called")
    86  	}
    87  
    88  	if err != nil {
    89  		t.Fatalf("err: %s", err)
    90  	}
    91  
    92  	if len(artifacts) != 1 {
    93  		t.Fatalf("bad: %#v", artifacts)
    94  	}
    95  
    96  	if artifacts[0].BuilderId() != "bid" {
    97  		t.Fatalf("bad: %#v", artifacts)
    98  	}
    99  
   100  	// Test run with an error
   101  	b.errRunResult = true
   102  	_, err = bClient.Run(ui, cache)
   103  	if err == nil {
   104  		t.Fatal("should error")
   105  	}
   106  
   107  	// Test SetDebug
   108  	bClient.SetDebug(true)
   109  	if !b.setDebugCalled {
   110  		t.Fatal("should be called")
   111  	}
   112  
   113  	// Test SetForce
   114  	bClient.SetForce(true)
   115  	if !b.setForceCalled {
   116  		t.Fatal("should be called")
   117  	}
   118  
   119  	// Test Cancel
   120  	bClient.Cancel()
   121  	if !b.cancelCalled {
   122  		t.Fatal("should be called")
   123  	}
   124  }
   125  
   126  func TestBuildPrepare_Warnings(t *testing.T) {
   127  	b := new(testBuild)
   128  	client, server := testClientServer(t)
   129  	defer client.Close()
   130  	defer server.Close()
   131  	server.RegisterBuild(b)
   132  	bClient := client.Build()
   133  
   134  	expected := []string{"foo"}
   135  	b.prepareWarnings = expected
   136  
   137  	warnings, err := bClient.Prepare()
   138  	if err != nil {
   139  		t.Fatalf("err: %s", err)
   140  	}
   141  	if !reflect.DeepEqual(warnings, expected) {
   142  		t.Fatalf("bad: %#v", warnings)
   143  	}
   144  }
   145  
   146  func TestBuild_ImplementsBuild(t *testing.T) {
   147  	var _ packer.Build = new(build)
   148  }