Make remote state test run
Make them compile against the new interface. The tests will be updated later to check new behavior.
This commit is contained in:
parent
200c8de4e9
commit
0ad6f08204
|
@ -2,8 +2,6 @@ package remote
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/state"
|
||||
|
@ -61,75 +59,3 @@ func (nilClient) Get() (*Payload, error) { return nil, nil }
|
|||
func (c nilClient) Put([]byte) error { return nil }
|
||||
|
||||
func (c nilClient) Delete() error { return nil }
|
||||
|
||||
// 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())
|
||||
defer os.Remove(localStateFile.Name())
|
||||
|
||||
remoteStateFile, err := ioutil.TempFile("", "tf")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
remoteStateFile.Close()
|
||||
os.Remove(remoteStateFile.Name())
|
||||
defer os.Remove(remoteStateFile.Name())
|
||||
|
||||
// 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()},
|
||||
}
|
||||
|
||||
remoteClient := &FileClient{
|
||||
Path: remoteStateFile.Name(),
|
||||
}
|
||||
|
||||
cache := &state.CacheState{
|
||||
Cache: &state.LocalState{
|
||||
Path: localStateFile.Name(),
|
||||
},
|
||||
Durable: &State{
|
||||
Client: remoteClient,
|
||||
},
|
||||
}
|
||||
|
||||
// This will write the local state file, and set the state field in the CacheState
|
||||
err = cache.WriteState(newState)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// This will persist the local state we just wrote to the remote state file
|
||||
err = cache.PersistState()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// now compare the two state files just to be sure
|
||||
localData, err := ioutil.ReadFile(localStateFile.Name())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
remoteData, err := ioutil.ReadFile(remoteStateFile.Name())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -57,31 +57,40 @@ func TestRemoteLocks(t *testing.T, a, b Client) {
|
|||
t.Fatal("client B not a state.Locker")
|
||||
}
|
||||
|
||||
if err := lockerA.Lock("test client A"); err != nil {
|
||||
infoA := &state.LockInfo{
|
||||
Operation: "test",
|
||||
Who: "client A",
|
||||
}
|
||||
infoB := &state.LockInfo{
|
||||
Operation: "test",
|
||||
Who: "client B",
|
||||
}
|
||||
|
||||
if _, err := lockerA.Lock(infoA); err != nil {
|
||||
t.Fatal("unable to get initial lock:", err)
|
||||
}
|
||||
|
||||
if err := lockerB.Lock("test client B"); err == nil {
|
||||
lockerA.Unlock()
|
||||
if _, err := lockerB.Lock(infoB); err == nil {
|
||||
lockerA.Unlock("")
|
||||
t.Fatal("client B obtained lock while held by client A")
|
||||
} else {
|
||||
t.Log("lock info error:", err)
|
||||
}
|
||||
|
||||
if err := lockerA.Unlock(); err != nil {
|
||||
if err := lockerA.Unlock(""); err != nil {
|
||||
t.Fatal("error unlocking client A", err)
|
||||
}
|
||||
|
||||
if err := lockerB.Lock("test client B"); err != nil {
|
||||
if _, err := lockerB.Lock(infoB); err != nil {
|
||||
t.Fatal("unable to obtain lock from client B")
|
||||
}
|
||||
|
||||
if err := lockerB.Unlock(); err != nil {
|
||||
if err := lockerB.Unlock(""); err != nil {
|
||||
t.Fatal("error unlocking client B:", err)
|
||||
}
|
||||
|
||||
// unlock should be repeatable
|
||||
if err := lockerA.Unlock(); err != nil {
|
||||
if err := lockerA.Unlock(""); err != nil {
|
||||
t.Fatal("Unlock error from client A when state was not locked:", err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue