github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/packer/rpc/ui_test.go (about)

     1  package rpc
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  type testUi struct {
     9  	askCalled      bool
    10  	askQuery       string
    11  	errorCalled    bool
    12  	errorMessage   string
    13  	machineCalled  bool
    14  	machineType    string
    15  	machineArgs    []string
    16  	messageCalled  bool
    17  	messageMessage string
    18  	sayCalled      bool
    19  	sayMessage     string
    20  }
    21  
    22  func (u *testUi) Ask(query string) (string, error) {
    23  	u.askCalled = true
    24  	u.askQuery = query
    25  	return "foo", nil
    26  }
    27  
    28  func (u *testUi) Error(message string) {
    29  	u.errorCalled = true
    30  	u.errorMessage = message
    31  }
    32  
    33  func (u *testUi) Machine(t string, args ...string) {
    34  	u.machineCalled = true
    35  	u.machineType = t
    36  	u.machineArgs = args
    37  }
    38  
    39  func (u *testUi) Message(message string) {
    40  	u.messageCalled = true
    41  	u.messageMessage = message
    42  }
    43  
    44  func (u *testUi) Say(message string) {
    45  	u.sayCalled = true
    46  	u.sayMessage = message
    47  }
    48  
    49  func TestUiRPC(t *testing.T) {
    50  	// Create the UI to test
    51  	ui := new(testUi)
    52  
    53  	// Start the RPC server
    54  	client, server := testClientServer(t)
    55  	defer client.Close()
    56  	defer server.Close()
    57  	server.RegisterUi(ui)
    58  
    59  	uiClient := client.Ui()
    60  
    61  	// Basic error and say tests
    62  	result, err := uiClient.Ask("query")
    63  	if err != nil {
    64  		t.Fatalf("err: %s", err)
    65  	}
    66  	if !ui.askCalled {
    67  		t.Fatal("should be called")
    68  	}
    69  	if ui.askQuery != "query" {
    70  		t.Fatalf("bad: %s", ui.askQuery)
    71  	}
    72  	if result != "foo" {
    73  		t.Fatalf("bad: %#v", result)
    74  	}
    75  
    76  	uiClient.Error("message")
    77  	if ui.errorMessage != "message" {
    78  		t.Fatalf("bad: %#v", ui.errorMessage)
    79  	}
    80  
    81  	uiClient.Message("message")
    82  	if ui.messageMessage != "message" {
    83  		t.Fatalf("bad: %#v", ui.errorMessage)
    84  	}
    85  
    86  	uiClient.Say("message")
    87  	if ui.sayMessage != "message" {
    88  		t.Fatalf("bad: %#v", ui.errorMessage)
    89  	}
    90  
    91  	uiClient.Machine("foo", "bar", "baz")
    92  	if !ui.machineCalled {
    93  		t.Fatal("machine should be called")
    94  	}
    95  
    96  	if ui.machineType != "foo" {
    97  		t.Fatalf("bad type: %#v", ui.machineType)
    98  	}
    99  
   100  	expected := []string{"bar", "baz"}
   101  	if !reflect.DeepEqual(ui.machineArgs, expected) {
   102  		t.Fatalf("bad: %#v", ui.machineArgs)
   103  	}
   104  }