Skip to content

Commit

Permalink
Add remote state init test
Browse files Browse the repository at this point in the history
Verify that a remote state file is correctly initialized in the same
manner as used by the `terraform remote config`
  • Loading branch information
jbardin committed Jul 7, 2016
1 parent 2c27dd4 commit 7481382
Showing 1 changed file with 37 additions and 35 deletions.
72 changes: 37 additions & 35 deletions state/remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package remote

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"
Expand Down Expand Up @@ -74,60 +73,63 @@ func TestRemoteClient_stateInit(t *testing.T) {
// remote state.
localStateFile.Close()
os.Remove(localStateFile.Name())
//defer os.Remove(localStateFile.Name())
fmt.Println("LOCAL:", localStateFile.Name())

local := &state.LocalState{
Path: localStateFile.Name(),
}
if err := local.RefreshState(); err != nil {
t.Fatal(err)
}
localState := local.State()

fmt.Println("localState.Empty():", localState.Empty())
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()
fmt.Println("LOCAL:", localStateFile.Name())
fmt.Println("REMOTE:", remoteStateFile.Name())
defer os.Remove(remoteStateFile.Name())

remoteClient := &FileClient{
Path: 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()},
}

durable := &State{
Client: remoteClient,
remoteClient := &FileClient{
Path: remoteStateFile.Name(),
}

cache := &state.CacheState{
Cache: local,
Durable: durable,
Cache: &state.LocalState{
Path: localStateFile.Name(),
},
Durable: &State{
Client: remoteClient,
},
}

if err := cache.RefreshState(); err != nil {
// 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)
}

switch cache.RefreshResult() {
// This will persist the local state we just wrote to the remote state file
err = cache.PersistState()
if err != nil {
t.Fatal(err)
}

// we should be "refreshing" the remote state to initialize it
case state.CacheRefreshLocalNewer:
// Write our local state out to the durable storage to start.
if err := cache.WriteState(localState); err != nil {
t.Fatal("Error preparing remote state:", err)
}
if err := cache.PersistState(); err != nil {
t.Fatal("Error preparing remote state:", err)
}
default:
// now compare the two state files just to be sure
localData, err := ioutil.ReadFile(localStateFile.Name())
if err != nil {
t.Fatal(err)
}

t.Fatal("unexpected refresh result:", cache.RefreshResult())
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")
}
}

0 comments on commit 7481382

Please sign in to comment.