command: remove remote package

This commit is contained in:
Mitchell Hashimoto 2015-02-23 10:36:31 -08:00
parent 2c2b560d7f
commit 189e7e700a
1 changed files with 95 additions and 74 deletions

View File

@ -7,7 +7,6 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/hashicorp/terraform/remote"
"github.com/hashicorp/terraform/state" "github.com/hashicorp/terraform/state"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
@ -28,11 +27,15 @@ func TestRemote_disable(t *testing.T) {
s = terraform.NewState() s = terraform.NewState()
s.Serial = 5 s.Serial = 5
s.Remote = conf s.Remote = conf
if err := remote.EnsureDirectory(); err != nil {
t.Fatalf("err: %v", err) // Write the state
statePath := filepath.Join(tmp, DefaultDataDir, DefaultStateFilename)
state := &state.LocalState{Path: statePath}
if err := state.WriteState(s); err != nil {
t.Fatalf("err: %s", err)
} }
if err := remote.PersistState(s); err != nil { if err := state.PersistState(); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %s", err)
} }
ui := new(cli.MockUi) ui := new(cli.MockUi)
@ -47,23 +50,9 @@ func TestRemote_disable(t *testing.T) {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
} }
// Local state file should be removed // Local state file should be removed and the local cache should exist
haveLocal, err := remote.HaveLocalState() testRemoteLocal(t, true)
if err != nil { testRemoteLocalCache(t, false)
t.Fatalf("err: %v", err)
}
if haveLocal {
t.Fatalf("should be disabled")
}
// New state file should be installed
exists, err := remote.ExistsFile(DefaultStateFilename)
if err != nil {
t.Fatalf("err: %v", err)
}
if !exists {
t.Fatalf("failed to make state file")
}
// Check that the state file was updated // Check that the state file was updated
raw, _ := ioutil.ReadFile(DefaultStateFilename) raw, _ := ioutil.ReadFile(DefaultStateFilename)
@ -116,23 +105,9 @@ func TestRemote_disable_noPull(t *testing.T) {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
} }
// Local state file should be removed // Local state file should be removed and the local cache should exist
haveLocal, err := remote.HaveLocalState() testRemoteLocal(t, true)
if err != nil { testRemoteLocalCache(t, false)
t.Fatalf("err: %v", err)
}
if haveLocal {
t.Fatalf("should be disabled")
}
// New state file should be installed
exists, err := remote.ExistsFile(DefaultStateFilename)
if err != nil {
t.Fatalf("err: %v", err)
}
if !exists {
t.Fatalf("failed to make state file")
}
// Check that the state file was updated // Check that the state file was updated
raw, _ := ioutil.ReadFile(DefaultStateFilename) raw, _ := ioutil.ReadFile(DefaultStateFilename)
@ -173,11 +148,15 @@ func TestRemote_disable_otherState(t *testing.T) {
// Persist local remote state // Persist local remote state
s := terraform.NewState() s := terraform.NewState()
s.Serial = 5 s.Serial = 5
if err := remote.EnsureDirectory(); err != nil {
t.Fatalf("err: %v", err) // Write the state
statePath := filepath.Join(tmp, DefaultDataDir, DefaultStateFilename)
state := &state.LocalState{Path: statePath}
if err := state.WriteState(s); err != nil {
t.Fatalf("err: %s", err)
} }
if err := remote.PersistState(s); err != nil { if err := state.PersistState(); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %s", err)
} }
// Also put a file at the default path // Also put a file at the default path
@ -213,11 +192,15 @@ func TestRemote_managedAndNonManaged(t *testing.T) {
// Persist local remote state // Persist local remote state
s := terraform.NewState() s := terraform.NewState()
s.Serial = 5 s.Serial = 5
if err := remote.EnsureDirectory(); err != nil {
t.Fatalf("err: %v", err) // Write the state
statePath := filepath.Join(tmp, DefaultDataDir, DefaultStateFilename)
state := &state.LocalState{Path: statePath}
if err := state.WriteState(s); err != nil {
t.Fatalf("err: %s", err)
} }
if err := remote.PersistState(s); err != nil { if err := state.PersistState(); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %s", err)
} }
// Also put a file at the default path // Also put a file at the default path
@ -267,11 +250,13 @@ func TestRemote_initBlank(t *testing.T) {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
} }
local, _, err := remote.ReadLocalState() remotePath := filepath.Join(DefaultDataDir, DefaultStateFilename)
if err != nil { ls := &state.LocalState{Path: remotePath}
t.Fatalf("err: %v", err) if err := ls.RefreshState(); err != nil {
t.Fatalf("err: %s", err)
} }
local := ls.State()
if local.Remote.Type != "http" { if local.Remote.Type != "http" {
t.Fatalf("Bad: %#v", local.Remote) t.Fatalf("Bad: %#v", local.Remote)
} }
@ -313,11 +298,15 @@ func TestRemote_updateRemote(t *testing.T) {
s.Remote = &terraform.RemoteState{ s.Remote = &terraform.RemoteState{
Type: "invalid", Type: "invalid",
} }
if err := remote.EnsureDirectory(); err != nil {
t.Fatalf("err: %v", err) // Write the state
statePath := filepath.Join(tmp, DefaultDataDir, DefaultStateFilename)
ls := &state.LocalState{Path: statePath}
if err := ls.WriteState(s); err != nil {
t.Fatalf("err: %s", err)
} }
if err := remote.PersistState(s); err != nil { if err := ls.PersistState(); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %s", err)
} }
ui := new(cli.MockUi) ui := new(cli.MockUi)
@ -338,10 +327,12 @@ func TestRemote_updateRemote(t *testing.T) {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
} }
local, _, err := remote.ReadLocalState() remotePath := filepath.Join(DefaultDataDir, DefaultStateFilename)
if err != nil { ls = &state.LocalState{Path: remotePath}
t.Fatalf("err: %v", err) if err := ls.RefreshState(); err != nil {
t.Fatalf("err: %s", err)
} }
local := ls.State()
if local.Remote.Type != "http" { if local.Remote.Type != "http" {
t.Fatalf("Bad: %#v", local.Remote) t.Fatalf("Bad: %#v", local.Remote)
@ -392,10 +383,12 @@ func TestRemote_enableRemote(t *testing.T) {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
} }
local, _, err := remote.ReadLocalState() remotePath := filepath.Join(DefaultDataDir, DefaultStateFilename)
if err != nil { ls := &state.LocalState{Path: remotePath}
t.Fatalf("err: %v", err) if err := ls.RefreshState(); err != nil {
t.Fatalf("err: %s", err)
} }
local := ls.State()
if local.Remote.Type != "http" { if local.Remote.Type != "http" {
t.Fatalf("Bad: %#v", local.Remote) t.Fatalf("Bad: %#v", local.Remote)
@ -407,21 +400,49 @@ func TestRemote_enableRemote(t *testing.T) {
t.Fatalf("Bad: %#v", local.Remote) t.Fatalf("Bad: %#v", local.Remote)
} }
// Backup file should exist // Backup file should exist, state file should not
exist, err := remote.ExistsFile(DefaultStateFilename + DefaultBackupExtention) testRemoteLocal(t, false)
if err != nil { testRemoteLocalBackup(t, true)
t.Fatalf("err: %v", err) }
func testRemoteLocal(t *testing.T, exists bool) {
_, err := os.Stat(DefaultStateFilename)
if os.IsNotExist(err) && !exists {
return
} }
if !exist { if err == nil && exists {
t.Fatalf("backup should exist") return
} }
// State file should not t.Fatalf("bad: %#v", err)
exist, err = remote.ExistsFile(DefaultStateFilename) }
if err != nil {
t.Fatalf("err: %v", err) func testRemoteLocalBackup(t *testing.T, exists bool) {
} _, err := os.Stat(DefaultStateFilename + DefaultBackupExtention)
if exist { if os.IsNotExist(err) && !exists {
t.Fatalf("state file should not exist") return
} }
if err == nil && exists {
return
}
if err == nil && !exists {
t.Fatal("expected local backup to exist")
}
t.Fatalf("bad: %#v", err)
}
func testRemoteLocalCache(t *testing.T, exists bool) {
_, err := os.Stat(filepath.Join(DefaultDataDir, DefaultStateFilename))
if os.IsNotExist(err) && !exists {
return
}
if err == nil && exists {
return
}
if err == nil && !exists {
t.Fatal("expected local cache to exist")
}
t.Fatalf("bad: %#v", err)
} }