rpc: UIOutput

This commit is contained in:
Mitchell Hashimoto 2014-10-04 09:11:51 -07:00
parent 6445e1f16a
commit 509f293bea
3 changed files with 65 additions and 1 deletions

30
rpc/ui_output.go Normal file
View File

@ -0,0 +1,30 @@
package rpc
import (
"net/rpc"
"github.com/hashicorp/terraform/terraform"
)
// UIOutput is an implementatin of terraform.UIOutput that communicates
// over RPC.
type UIOutput struct {
Client *rpc.Client
Name string
}
func (o *UIOutput) Output(v string) {
o.Client.Call(o.Name+".Output", v, new(interface{}))
}
// UIOutputServer is the RPC server for serving UIOutput.
type UIOutputServer struct {
UIOutput terraform.UIOutput
}
func (s *UIOutputServer) Output(
v string,
reply *interface{}) error {
s.UIOutput.Output(v)
return nil
}

34
rpc/ui_output_test.go Normal file
View File

@ -0,0 +1,34 @@
package rpc
import (
"testing"
"github.com/hashicorp/terraform/terraform"
)
func TestUIOutput_impl(t *testing.T) {
var _ terraform.UIOutput = new(UIOutput)
}
func TestUIOutput_input(t *testing.T) {
client, server := testClientServer(t)
defer client.Close()
o := new(terraform.MockUIOutput)
err := server.RegisterName("UIOutput", &UIOutputServer{
UIOutput: o,
})
if err != nil {
t.Fatalf("err: %s", err)
}
output := &UIOutput{Client: client, Name: "UIOutput"}
output.Output("foo")
if !o.OutputCalled {
t.Fatal("output should be called")
}
if o.OutputMessage != "foo" {
t.Fatalf("bad: %#v", o.OutputMessage)
}
}

View File

@ -10,7 +10,7 @@ type MockUIOutput struct {
func (o *MockUIOutput) Output(v string) { func (o *MockUIOutput) Output(v string) {
o.OutputCalled = true o.OutputCalled = true
o.OutputMessage= v o.OutputMessage= v
if o.OutputFn == nil { if o.OutputFn != nil {
o.OutputFn(v) o.OutputFn(v)
} }
} }