2015-02-22 00:13:28 +01:00
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-07-06 16:48:52 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-02-22 00:13:28 +01:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
}
|
2016-07-02 00:37:38 +02:00
|
|
|
|
2017-01-12 22:55:42 +01:00
|
|
|
func testClientLocks(t *testing.T, c Client) {
|
|
|
|
s3Client := c.(*S3Client)
|
|
|
|
|
|
|
|
// initial lock
|
|
|
|
if err := s3Client.Lock("test"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// second lock should fail
|
|
|
|
if err := s3Client.Lock("test"); err == nil {
|
|
|
|
t.Fatal("expected error, got nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
// unlock should work
|
|
|
|
if err := s3Client.Unlock(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we should be able to lock again
|
|
|
|
if err := s3Client.Lock("test"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// unlock should be idempotent
|
|
|
|
if err := s3Client.Unlock(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := s3Client.Unlock(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-02 00:37:38 +02:00
|
|
|
func TestRemoteClient_noPayload(t *testing.T) {
|
|
|
|
s := &State{
|
|
|
|
Client: nilClient{},
|
|
|
|
}
|
2016-07-06 16:48:52 +02:00
|
|
|
if err := s.RefreshState(); err != nil {
|
|
|
|
t.Fatal("error refreshing empty remote state")
|
2016-07-02 00:37:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 }
|
2016-07-06 16:48:52 +02:00
|
|
|
|
|
|
|
// ensure that remote state can be properly initialized
|
|
|
|
func TestRemoteClient_stateInit(t *testing.T) {
|
|
|
|
localStateFile, err := ioutil.TempFile("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// we need to remove the temp files so we recognize there's no local or
|
|
|
|
// remote state.
|
|
|
|
localStateFile.Close()
|
|
|
|
os.Remove(localStateFile.Name())
|
2016-07-07 22:18:00 +02:00
|
|
|
defer os.Remove(localStateFile.Name())
|
2016-07-06 16:48:52 +02:00
|
|
|
|
|
|
|
remoteStateFile, err := ioutil.TempFile("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
remoteStateFile.Close()
|
|
|
|
os.Remove(remoteStateFile.Name())
|
2016-07-07 22:18:00 +02:00
|
|
|
defer os.Remove(remoteStateFile.Name())
|
2016-07-06 16:48:52 +02:00
|
|
|
|
2016-07-07 22:18:00 +02:00
|
|
|
// Now we need an empty state to initialize the state files.
|
|
|
|
newState := terraform.NewState()
|
|
|
|
newState.Remote = &terraform.RemoteState{
|
|
|
|
Type: "_local",
|
|
|
|
Config: map[string]string{"path": remoteStateFile.Name()},
|
2016-07-06 16:48:52 +02:00
|
|
|
}
|
|
|
|
|
2016-07-07 22:18:00 +02:00
|
|
|
remoteClient := &FileClient{
|
|
|
|
Path: remoteStateFile.Name(),
|
2016-07-06 16:48:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cache := &state.CacheState{
|
2016-07-07 22:18:00 +02:00
|
|
|
Cache: &state.LocalState{
|
|
|
|
Path: localStateFile.Name(),
|
|
|
|
},
|
|
|
|
Durable: &State{
|
|
|
|
Client: remoteClient,
|
|
|
|
},
|
2016-07-06 16:48:52 +02:00
|
|
|
}
|
|
|
|
|
2016-07-07 22:18:00 +02:00
|
|
|
// This will write the local state file, and set the state field in the CacheState
|
|
|
|
err = cache.WriteState(newState)
|
|
|
|
if err != nil {
|
2016-07-06 16:48:52 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-07-07 22:18:00 +02:00
|
|
|
// This will persist the local state we just wrote to the remote state file
|
|
|
|
err = cache.PersistState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-07-06 16:48:52 +02:00
|
|
|
|
2016-07-07 22:18:00 +02:00
|
|
|
// now compare the two state files just to be sure
|
|
|
|
localData, err := ioutil.ReadFile(localStateFile.Name())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-07-06 16:48:52 +02:00
|
|
|
|
2016-07-07 22:18:00 +02:00
|
|
|
remoteData, err := ioutil.ReadFile(remoteStateFile.Name())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2016-07-06 16:48:52 +02:00
|
|
|
}
|
|
|
|
|
2016-07-07 22:18:00 +02:00
|
|
|
if !bytes.Equal(localData, remoteData) {
|
|
|
|
t.Log("state files don't match")
|
|
|
|
t.Log("Local:\n", string(localData))
|
|
|
|
t.Log("Remote:\n", string(remoteData))
|
|
|
|
t.Fatal("failed to initialize remote state")
|
|
|
|
}
|
2016-07-06 16:48:52 +02:00
|
|
|
}
|