Skip to content

Commit

Permalink
Revert #7464 and allow an empty state
Browse files Browse the repository at this point in the history
Revert back to using a nil state. The external usage of the state shoudl
always check the Empty() method.
  • Loading branch information
jbardin committed Jul 7, 2016
1 parent eb70dec commit 3622bfd
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
76 changes: 74 additions & 2 deletions state/remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package remote

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/hashicorp/terraform/state"
Expand Down Expand Up @@ -46,8 +49,8 @@ func TestRemoteClient_noPayload(t *testing.T) {
s := &State{
Client: nilClient{},
}
if err := s.RefreshState(); err != ErrRemoteStateNotFound {
t.Fatal("expected ErrRemoteStateNotFound, got", err)
if err := s.RefreshState(); err != nil {
t.Fatal("error refreshing empty remote state")
}
}

Expand All @@ -59,3 +62,72 @@ 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())
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())

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())

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

durable := &State{
Client: remoteClient,
}

cache := &state.CacheState{
Cache: local,
Durable: durable,
}

if err := cache.RefreshState(); err != nil {
t.Fatal(err)
}

switch cache.RefreshResult() {

// 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:

t.Fatal("unexpected refresh result:", cache.RefreshResult())
}

}
6 changes: 2 additions & 4 deletions state/remote/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package remote

import (
"bytes"
"errors"

"github.com/hashicorp/terraform/terraform"
)

var ErrRemoteStateNotFound = errors.New("no remote state found")

// State implements the State interfaces in the state package to handle
// reading and writing the remote state. This State on its own does no
// local caching so every persist will go to the remote storage and local
Expand Down Expand Up @@ -37,8 +34,9 @@ func (s *State) RefreshState() error {
return err
}

// no remote state is OK
if payload == nil {
return ErrRemoteStateNotFound
return nil
}

state, err := terraform.ReadState(bytes.NewReader(payload.Data))
Expand Down

0 comments on commit 3622bfd

Please sign in to comment.