rpc: Test errors, basic error

This commit is contained in:
Mitchell Hashimoto 2014-05-28 15:31:09 -07:00
parent 5ef20cf3a2
commit 91317a8608
4 changed files with 116 additions and 3 deletions

21
rpc/error.go Normal file
View File

@ -0,0 +1,21 @@
package rpc
// This is a type that wraps error types so that they can be messaged
// across RPC channels. Since "error" is an interface, we can't always
// gob-encode the underlying structure. This is a valid error interface
// implementer that we will push across.
type BasicError struct {
Message string
}
func NewBasicError(err error) *BasicError {
if err == nil {
return nil
}
return &BasicError{err.Error()}
}
func (e *BasicError) Error() string {
return e.Message
}

26
rpc/error_test.go Normal file
View File

@ -0,0 +1,26 @@
package rpc
import (
"errors"
"testing"
)
func TestBasicError_ImplementsError(t *testing.T) {
var _ error = new(BasicError)
}
func TestBasicError_MatchesMessage(t *testing.T) {
err := errors.New("foo")
wrapped := NewBasicError(err)
if wrapped.Error() != err.Error() {
t.Fatalf("bad: %#v", wrapped.Error())
}
}
func TestNewBasicError_nil(t *testing.T) {
r := NewBasicError(nil)
if r != nil {
t.Fatalf("bad: %#v", r)
}
}

View File

@ -17,8 +17,11 @@ func (p *ResourceProvider) Configure(c map[string]interface{}) ([]string, error)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if resp.Error != nil {
err = resp.Error
}
return resp.Warnings, resp.Error return resp.Warnings, err
} }
type ResourceProviderServer struct { type ResourceProviderServer struct {
@ -27,7 +30,7 @@ type ResourceProviderServer struct {
type ResourceProviderConfigureResponse struct { type ResourceProviderConfigureResponse struct {
Warnings []string Warnings []string
Error error Error *BasicError
} }
func (s *ResourceProviderServer) Configure( func (s *ResourceProviderServer) Configure(
@ -36,7 +39,7 @@ func (s *ResourceProviderServer) Configure(
warnings, err := s.Provider.Configure(config) warnings, err := s.Provider.Configure(config)
*reply = ResourceProviderConfigureResponse{ *reply = ResourceProviderConfigureResponse{
Warnings: warnings, Warnings: warnings,
Error: err, Error: NewBasicError(err),
} }
return nil return nil
} }

View File

@ -1,6 +1,7 @@
package rpc package rpc
import ( import (
"errors"
"reflect" "reflect"
"testing" "testing"
@ -32,3 +33,65 @@ func TestResourceProvider_configure(t *testing.T) {
t.Fatalf("bad: %#v", e) t.Fatalf("bad: %#v", e)
} }
} }
func TestResourceProvider_configure_errors(t *testing.T) {
p := new(terraform.MockResourceProvider)
client, server := testClientServer(t)
name, err := Register(server, p)
if err != nil {
t.Fatalf("err: %s", err)
}
provider := &ResourceProvider{Client: client, Name: name}
p.ConfigureReturnError = errors.New("foo")
// Configure
config := map[string]interface{}{"foo": "bar"}
w, e := provider.Configure(config)
if !p.ConfigureCalled {
t.Fatal("configure should be called")
}
if !reflect.DeepEqual(p.ConfigureConfig, config) {
t.Fatalf("bad: %#v", p.ConfigureConfig)
}
if w != nil {
t.Fatalf("bad: %#v", w)
}
if e == nil {
t.Fatal("should have error")
}
if e.Error() != "foo" {
t.Fatalf("bad: %s", e)
}
}
func TestResourceProvider_configure_warnings(t *testing.T) {
p := new(terraform.MockResourceProvider)
client, server := testClientServer(t)
name, err := Register(server, p)
if err != nil {
t.Fatalf("err: %s", err)
}
provider := &ResourceProvider{Client: client, Name: name}
p.ConfigureReturnWarnings = []string{"foo", "bar"}
// Configure
config := map[string]interface{}{"foo": "bar"}
w, e := provider.Configure(config)
if !p.ConfigureCalled {
t.Fatal("configure should be called")
}
if !reflect.DeepEqual(p.ConfigureConfig, config) {
t.Fatalf("bad: %#v", p.ConfigureConfig)
}
if w == nil {
t.Fatal("should have warnings")
}
if !reflect.DeepEqual(w, []string{"foo", "bar"}) {
t.Fatalf("bad: %#v", w)
}
if e != nil {
t.Fatalf("bad: %#v", e)
}
}