terraform/state/remote/remote_test.go

62 lines
1.2 KiB
Go
Raw Normal View History

2015-02-22 00:13:28 +01:00
package remote
import (
"bytes"
"testing"
2015-02-24 02:56:29 +01:00
"github.com/hashicorp/terraform/state"
"github.com/hashicorp/terraform/terraform"
2015-02-22 00:13:28 +01:00
)
// testClient is a generic function to test any client.
func testClient(t *testing.T, c Client) {
2015-02-24 02:56:29 +01:00
var buf bytes.Buffer
s := state.TestStateInitial()
if err := terraform.WriteState(s, &buf); err != nil {
t.Fatalf("err: %s", err)
}
data := buf.Bytes()
2015-02-22 00:13:28 +01:00
if err := c.Put(data); err != nil {
t.Fatalf("put: %s", err)
}
p, err := c.Get()
if err != nil {
t.Fatalf("get: %s", err)
}
if !bytes.Equal(p.Data, data) {
t.Fatalf("bad: %#v", p)
}
if err := c.Delete(); err != nil {
t.Fatalf("delete: %s", err)
}
p, err = c.Get()
if err != nil {
t.Fatalf("get: %s", err)
}
if p != nil {
t.Fatalf("bad: %#v", p)
}
}
func TestRemoteClient_noPayload(t *testing.T) {
s := &State{
Client: nilClient{},
}
if err := s.RefreshState(); err != ErrRemoteStateNotFound {
t.Fatal("expected ErrRemoteStateNotFound, got", err)
}
}
// nilClient returns nil for everything
type nilClient struct{}
func (nilClient) Get() (*Payload, error) { return nil, nil }
func (c nilClient) Put([]byte) error { return nil }
func (c nilClient) Delete() error { return nil }