packer-cn/packer/rpc/ui_test.go

156 lines
3.0 KiB
Go
Raw Normal View History

2013-05-03 18:49:15 -04:00
package rpc
import (
2018-09-06 10:15:09 -04:00
"io"
"reflect"
2013-05-03 18:49:15 -04:00
"testing"
"github.com/hashicorp/packer/packer"
2013-05-03 18:49:15 -04:00
)
type testUi struct {
2018-09-06 10:15:09 -04:00
askCalled bool
askQuery string
errorCalled bool
errorMessage string
machineCalled bool
machineType string
machineArgs []string
messageCalled bool
messageMessage string
sayCalled bool
sayMessage string
progressBarCalled bool
progressBarStartCalled bool
progressBarAddCalled bool
progressBarFinishCalled bool
progressBarNewProxyReaderCalled bool
2013-05-03 18:49:15 -04:00
}
func (u *testUi) Ask(query string) (string, error) {
u.askCalled = true
u.askQuery = query
return "foo", nil
}
func (u *testUi) Error(message string) {
2013-05-08 18:12:48 -04:00
u.errorCalled = true
u.errorMessage = message
2013-05-08 18:12:48 -04:00
}
2013-08-11 21:16:00 -04:00
func (u *testUi) Machine(t string, args ...string) {
u.machineCalled = true
u.machineType = t
u.machineArgs = args
}
2013-06-03 14:30:38 -04:00
func (u *testUi) Message(message string) {
u.messageCalled = true
u.messageMessage = message
}
func (u *testUi) Say(message string) {
2013-05-03 18:49:15 -04:00
u.sayCalled = true
u.sayMessage = message
2013-05-03 18:49:15 -04:00
}
func (u *testUi) ProgressBar() packer.ProgressBar {
u.progressBarCalled = true
2018-09-06 10:15:09 -04:00
return u
}
func (u *testUi) Start(int64) {
2018-09-06 10:15:09 -04:00
u.progressBarStartCalled = true
}
func (u *testUi) Add(int64) {
2018-09-06 10:15:09 -04:00
u.progressBarAddCalled = true
}
func (u *testUi) Finish() {
u.progressBarFinishCalled = true
}
func (u *testUi) NewProxyReader(r io.Reader) io.Reader {
u.progressBarNewProxyReaderCalled = true
return r
}
2013-05-03 18:49:15 -04:00
func TestUiRPC(t *testing.T) {
// Create the UI to test
ui := new(testUi)
// Start the RPC server
2013-12-09 19:22:11 -05:00
client, server := testClientServer(t)
defer client.Close()
defer server.Close()
server.RegisterUi(ui)
2013-05-03 18:49:15 -04:00
2013-12-09 19:22:11 -05:00
uiClient := client.Ui()
2013-05-03 18:49:15 -04:00
// Basic error and say tests
result, err := uiClient.Ask("query")
2013-10-16 23:04:57 -04:00
if err != nil {
t.Fatalf("err: %s", err)
}
if !ui.askCalled {
t.Fatal("should be called")
}
if ui.askQuery != "query" {
t.Fatalf("bad: %s", ui.askQuery)
}
if result != "foo" {
t.Fatalf("bad: %#v", result)
}
uiClient.Error("message")
2013-10-16 23:04:57 -04:00
if ui.errorMessage != "message" {
t.Fatalf("bad: %#v", ui.errorMessage)
}
2013-05-08 18:12:48 -04:00
2013-06-03 14:30:38 -04:00
uiClient.Message("message")
2013-10-16 23:04:57 -04:00
if ui.messageMessage != "message" {
t.Fatalf("bad: %#v", ui.errorMessage)
}
2013-06-03 14:30:38 -04:00
uiClient.Say("message")
2013-10-16 23:04:57 -04:00
if ui.sayMessage != "message" {
t.Fatalf("bad: %#v", ui.errorMessage)
}
2018-09-06 10:15:09 -04:00
bar := uiClient.ProgressBar()
if ui.progressBarCalled != true {
2018-09-06 10:15:09 -04:00
t.Errorf("ProgressBar not called.")
}
bar.Start(100)
if ui.progressBarStartCalled != true {
t.Errorf("progressBar.Start not called.")
}
bar.Add(1)
if ui.progressBarAddCalled != true {
t.Errorf("progressBar.Add not called.")
}
bar.Finish()
if ui.progressBarFinishCalled != true {
t.Errorf("progressBar.Finish not called.")
}
uiClient.Machine("foo", "bar", "baz")
if !ui.machineCalled {
t.Fatal("machine should be called")
}
if ui.machineType != "foo" {
t.Fatalf("bad type: %#v", ui.machineType)
}
expected := []string{"bar", "baz"}
if !reflect.DeepEqual(ui.machineArgs, expected) {
t.Fatalf("bad: %#v", ui.machineArgs)
}
2013-05-03 18:49:15 -04:00
}