remote: testing file init
This commit is contained in:
parent
d332b8ad58
commit
2d1f985a93
|
@ -41,6 +41,10 @@ const (
|
||||||
// just up to date. (Push/Pull)
|
// just up to date. (Push/Pull)
|
||||||
StateChangeNoop StateChangeResult = iota
|
StateChangeNoop StateChangeResult = iota
|
||||||
|
|
||||||
|
// StateChangeInit indicates that there is no local or
|
||||||
|
// remote state, and that the state was initialized
|
||||||
|
StateChangeInit
|
||||||
|
|
||||||
// StateChangeUpdateLocal indicates the local state
|
// StateChangeUpdateLocal indicates the local state
|
||||||
// was updated. (Pull)
|
// was updated. (Pull)
|
||||||
StateChangeUpdateLocal
|
StateChangeUpdateLocal
|
||||||
|
@ -74,6 +78,8 @@ func (sc StateChangeResult) String() string {
|
||||||
switch sc {
|
switch sc {
|
||||||
case StateChangeNoop:
|
case StateChangeNoop:
|
||||||
return "Local and remote state in sync"
|
return "Local and remote state in sync"
|
||||||
|
case StateChangeInit:
|
||||||
|
return "Local state initialized"
|
||||||
case StateChangeUpdateLocal:
|
case StateChangeUpdateLocal:
|
||||||
return "Local state updated"
|
return "Local state updated"
|
||||||
case StateChangeUpdateRemote:
|
case StateChangeUpdateRemote:
|
||||||
|
@ -96,6 +102,8 @@ func (sc StateChangeResult) SuccessfulPull() bool {
|
||||||
switch sc {
|
switch sc {
|
||||||
case StateChangeNoop:
|
case StateChangeNoop:
|
||||||
return true
|
return true
|
||||||
|
case StateChangeInit:
|
||||||
|
return true
|
||||||
case StateChangeUpdateLocal:
|
case StateChangeUpdateLocal:
|
||||||
return true
|
return true
|
||||||
case StateChangeLocalNewer:
|
case StateChangeLocalNewer:
|
||||||
|
@ -266,7 +274,7 @@ func RefreshState(conf *terraform.RemoteState) (StateChangeResult, error) {
|
||||||
return StateChangeNoop,
|
return StateChangeNoop,
|
||||||
fmt.Errorf("Failed to persist state: %v", err)
|
fmt.Errorf("Failed to persist state: %v", err)
|
||||||
}
|
}
|
||||||
return StateChangeNoop, nil
|
return StateChangeInit, nil
|
||||||
|
|
||||||
case remoteState == nil && localState != nil:
|
case remoteState == nil && localState != nil:
|
||||||
fallthrough
|
fallthrough
|
||||||
|
|
|
@ -2,7 +2,11 @@ package remote
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/md5"
|
||||||
|
"encoding/base64"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -68,22 +72,45 @@ func TestValidateConfig(t *testing.T) {
|
||||||
// TODO:
|
// TODO:
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRefreshState_Blank(t *testing.T) {
|
func TestRefreshState_Init(t *testing.T) {
|
||||||
// TODO
|
defer fixDir(testDir(t))
|
||||||
}
|
remote, srv := testRemote(t, nil)
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
func TestRefreshState_Update_Newer(t *testing.T) {
|
sc, err := RefreshState(remote)
|
||||||
// TODO
|
if err != nil {
|
||||||
}
|
t.Fatalf("err: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
func TestRefreshState_Update_Older(t *testing.T) {
|
if sc != StateChangeInit {
|
||||||
// TODO
|
t.Fatalf("bad: %s", sc)
|
||||||
|
}
|
||||||
|
|
||||||
|
local := testReadLocal(t)
|
||||||
|
if !local.Remote.Equals(remote) {
|
||||||
|
t.Fatalf("Bad: %#v", local)
|
||||||
|
}
|
||||||
|
if local.Serial != 1 {
|
||||||
|
t.Fatalf("Bad: %#v", local)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRefreshState_Noop(t *testing.T) {
|
func TestRefreshState_Noop(t *testing.T) {
|
||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRefreshState_UpdateLocal(t *testing.T) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRefreshState_LocalNewer(t *testing.T) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRefreshState_Conflict(t *testing.T) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
func TestBlankState(t *testing.T) {
|
func TestBlankState(t *testing.T) {
|
||||||
remote := &terraform.RemoteState{
|
remote := &terraform.RemoteState{
|
||||||
Name: "foo",
|
Name: "foo",
|
||||||
|
@ -104,17 +131,8 @@ func TestBlankState(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPersist(t *testing.T) {
|
func TestPersist(t *testing.T) {
|
||||||
tmp, err := ioutil.TempDir("", "remote")
|
tmp, cwd := testDir(t)
|
||||||
if err != nil {
|
defer fixDir(tmp, cwd)
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tmp)
|
|
||||||
cwd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %v", err)
|
|
||||||
}
|
|
||||||
os.Chdir(tmp)
|
|
||||||
defer os.Chdir(cwd)
|
|
||||||
|
|
||||||
EnsureDirectory()
|
EnsureDirectory()
|
||||||
|
|
||||||
|
@ -157,3 +175,75 @@ func TestPersist(t *testing.T) {
|
||||||
t.Fatalf("remote mismatch")
|
t.Fatalf("remote mismatch")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// testRemote is used to make a test HTTP server to
|
||||||
|
// return a given state file
|
||||||
|
func testRemote(t *testing.T, s *terraform.State) (*terraform.RemoteState, *httptest.Server) {
|
||||||
|
var b64md5 string
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
|
if s != nil {
|
||||||
|
terraform.WriteState(s, buf)
|
||||||
|
md5 := md5.Sum(buf.Bytes())
|
||||||
|
b64md5 = base64.StdEncoding.EncodeToString(md5[:16])
|
||||||
|
}
|
||||||
|
|
||||||
|
cb := func(resp http.ResponseWriter, req *http.Request) {
|
||||||
|
if s == nil {
|
||||||
|
resp.WriteHeader(404)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
resp.Header().Set("Content-MD5", b64md5)
|
||||||
|
resp.Write(buf.Bytes())
|
||||||
|
}
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(cb))
|
||||||
|
remote := &terraform.RemoteState{
|
||||||
|
Name: "foo",
|
||||||
|
Server: srv.URL,
|
||||||
|
}
|
||||||
|
return remote, srv
|
||||||
|
}
|
||||||
|
|
||||||
|
// testDir is used to change the current working directory
|
||||||
|
// into a test directory that should be remoted after
|
||||||
|
func testDir(t *testing.T) (string, string) {
|
||||||
|
tmp, err := ioutil.TempDir("", "remote")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %v", err)
|
||||||
|
}
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %v", err)
|
||||||
|
}
|
||||||
|
os.Chdir(tmp)
|
||||||
|
if err := EnsureDirectory(); err != nil {
|
||||||
|
t.Fatalf("err: %v", err)
|
||||||
|
}
|
||||||
|
return tmp, cwd
|
||||||
|
}
|
||||||
|
|
||||||
|
// fixDir is used to as a defer to testDir
|
||||||
|
func fixDir(tmp, cwd string) {
|
||||||
|
os.Chdir(cwd)
|
||||||
|
os.RemoveAll(tmp)
|
||||||
|
}
|
||||||
|
|
||||||
|
// testReadLocal is used to just get the local state
|
||||||
|
func testReadLocal(t *testing.T) *terraform.State {
|
||||||
|
path, err := HiddenStatePath()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %v", err)
|
||||||
|
}
|
||||||
|
raw, err := ioutil.ReadFile(path)
|
||||||
|
if err != nil && !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("err: %v", err)
|
||||||
|
}
|
||||||
|
if raw == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
s, err := terraform.ReadState(bytes.NewReader(raw))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %v", err)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue