From 189e7e700af348b908fa402ff6eb4e44d0498280 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 23 Feb 2015 10:36:31 -0800 Subject: [PATCH] command: remove remote package --- command/remote_test.go | 169 +++++++++++++++++++++++------------------ 1 file changed, 95 insertions(+), 74 deletions(-) diff --git a/command/remote_test.go b/command/remote_test.go index 54c31f9ed7..f656fc2feb 100644 --- a/command/remote_test.go +++ b/command/remote_test.go @@ -7,7 +7,6 @@ import ( "path/filepath" "testing" - "github.com/hashicorp/terraform/remote" "github.com/hashicorp/terraform/state" "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/cli" @@ -28,11 +27,15 @@ func TestRemote_disable(t *testing.T) { s = terraform.NewState() s.Serial = 5 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 { - t.Fatalf("err: %v", err) + if err := state.PersistState(); err != nil { + t.Fatalf("err: %s", err) } ui := new(cli.MockUi) @@ -47,23 +50,9 @@ func TestRemote_disable(t *testing.T) { t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) } - // Local state file should be removed - haveLocal, err := remote.HaveLocalState() - if err != nil { - 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") - } + // Local state file should be removed and the local cache should exist + testRemoteLocal(t, true) + testRemoteLocalCache(t, false) // Check that the state file was updated raw, _ := ioutil.ReadFile(DefaultStateFilename) @@ -116,23 +105,9 @@ func TestRemote_disable_noPull(t *testing.T) { t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) } - // Local state file should be removed - haveLocal, err := remote.HaveLocalState() - if err != nil { - 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") - } + // Local state file should be removed and the local cache should exist + testRemoteLocal(t, true) + testRemoteLocalCache(t, false) // Check that the state file was updated raw, _ := ioutil.ReadFile(DefaultStateFilename) @@ -173,11 +148,15 @@ func TestRemote_disable_otherState(t *testing.T) { // Persist local remote state s := terraform.NewState() 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 { - t.Fatalf("err: %v", err) + if err := state.PersistState(); err != nil { + t.Fatalf("err: %s", err) } // Also put a file at the default path @@ -213,11 +192,15 @@ func TestRemote_managedAndNonManaged(t *testing.T) { // Persist local remote state s := terraform.NewState() 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 { - t.Fatalf("err: %v", err) + if err := state.PersistState(); err != nil { + t.Fatalf("err: %s", err) } // 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()) } - local, _, err := remote.ReadLocalState() - if err != nil { - t.Fatalf("err: %v", err) + remotePath := filepath.Join(DefaultDataDir, DefaultStateFilename) + ls := &state.LocalState{Path: remotePath} + if err := ls.RefreshState(); err != nil { + t.Fatalf("err: %s", err) } + local := ls.State() if local.Remote.Type != "http" { t.Fatalf("Bad: %#v", local.Remote) } @@ -313,11 +298,15 @@ func TestRemote_updateRemote(t *testing.T) { s.Remote = &terraform.RemoteState{ 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 { - t.Fatalf("err: %v", err) + if err := ls.PersistState(); err != nil { + t.Fatalf("err: %s", err) } ui := new(cli.MockUi) @@ -338,10 +327,12 @@ func TestRemote_updateRemote(t *testing.T) { t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) } - local, _, err := remote.ReadLocalState() - if err != nil { - t.Fatalf("err: %v", err) + remotePath := filepath.Join(DefaultDataDir, DefaultStateFilename) + ls = &state.LocalState{Path: remotePath} + if err := ls.RefreshState(); err != nil { + t.Fatalf("err: %s", err) } + local := ls.State() if local.Remote.Type != "http" { t.Fatalf("Bad: %#v", local.Remote) @@ -392,10 +383,12 @@ func TestRemote_enableRemote(t *testing.T) { t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) } - local, _, err := remote.ReadLocalState() - if err != nil { - t.Fatalf("err: %v", err) + remotePath := filepath.Join(DefaultDataDir, DefaultStateFilename) + ls := &state.LocalState{Path: remotePath} + if err := ls.RefreshState(); err != nil { + t.Fatalf("err: %s", err) } + local := ls.State() if local.Remote.Type != "http" { t.Fatalf("Bad: %#v", local.Remote) @@ -407,21 +400,49 @@ func TestRemote_enableRemote(t *testing.T) { t.Fatalf("Bad: %#v", local.Remote) } - // Backup file should exist - exist, err := remote.ExistsFile(DefaultStateFilename + DefaultBackupExtention) - if err != nil { - t.Fatalf("err: %v", err) + // Backup file should exist, state file should not + testRemoteLocal(t, false) + testRemoteLocalBackup(t, true) +} + +func testRemoteLocal(t *testing.T, exists bool) { + _, err := os.Stat(DefaultStateFilename) + if os.IsNotExist(err) && !exists { + return } - if !exist { - t.Fatalf("backup should exist") + if err == nil && exists { + return } - // State file should not - exist, err = remote.ExistsFile(DefaultStateFilename) - if err != nil { - t.Fatalf("err: %v", err) - } - if exist { - t.Fatalf("state file should not exist") - } + t.Fatalf("bad: %#v", err) +} + +func testRemoteLocalBackup(t *testing.T, exists bool) { + _, err := os.Stat(DefaultStateFilename + DefaultBackupExtention) + if os.IsNotExist(err) && !exists { + 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) }