github.com/HashDataInc/packer@v1.3.2/packer/rpc/build_test.go (about)

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