Merge pull request #12773 from hashicorp/b-push

command/push: properly test for local state
This commit is contained in:
Mitchell Hashimoto 2017-03-16 11:02:09 -07:00 committed by GitHub
commit b3521cf27e
5 changed files with 120 additions and 18 deletions

View File

@ -138,6 +138,20 @@ func (m *Meta) Backend(opts *BackendOpts) (backend.Enhanced, error) {
return local, nil return local, nil
} }
// IsLocalBackend returns true if the backend is a local backend. We use this
// for some checks that require a remote backend.
func (m *Meta) IsLocalBackend(b backend.Backend) bool {
// Is it a local backend?
bLocal, ok := b.(*backendlocal.Local)
// If it is, does it not have an alternate state backend?
if ok {
ok = bLocal.Backend == nil
}
return ok
}
// Operation initializes a new backend.Operation struct. // Operation initializes a new backend.Operation struct.
// //
// This prepares the operation. After calling this, the caller is expected // This prepares the operation. After calling this, the caller is expected

View File

@ -71,24 +71,6 @@ func (c *PushCommand) Run(args []string) int {
return 1 return 1
} }
/*
// Verify the state is remote, we can't push without a remote state
s, err := c.State()
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to read state: %s", err))
return 1
}
if !s.State().IsRemote() {
c.Ui.Error(
"Remote state is not enabled. For Atlas to run Terraform\n" +
"for you, remote state must be used and configured. Remote\n" +
"state via any backend is accepted, not just Atlas. To\n" +
"configure remote state, use the `terraform remote config`\n" +
"command.")
return 1
}
*/
// Check if the path is a plan // Check if the path is a plan
plan, err := c.Plan(configPath) plan, err := c.Plan(configPath)
if err != nil { if err != nil {
@ -125,6 +107,17 @@ func (c *PushCommand) Run(args []string) int {
return 1 return 1
} }
// We require a non-local backend
if c.IsLocalBackend(b) {
c.Ui.Error(
"Remote state is not enabled. For Atlas to run Terraform\n" +
"for you, remote state must be used and configured. Remote\n" +
"state via any backend is accepted, not just Atlas. To\n" +
"configure remote state, use the `terraform remote config`\n" +
"command.")
return 1
}
// We require a local backend // We require a local backend
local, ok := b.(backend.Local) local, ok := b.(backend.Local)
if !ok { if !ok {

View File

@ -9,9 +9,11 @@ import (
"path/filepath" "path/filepath"
"reflect" "reflect"
"sort" "sort"
"strings"
"testing" "testing"
atlas "github.com/hashicorp/atlas-go/v1" atlas "github.com/hashicorp/atlas-go/v1"
"github.com/hashicorp/terraform/helper/copy"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
@ -73,6 +75,70 @@ func TestPush_good(t *testing.T) {
} }
} }
func TestPush_goodBackendInit(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
copy.CopyDir(testFixturePath("push-backend-new"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
// init backend
ui := new(cli.MockUi)
ci := &InitCommand{
Meta: Meta{
Ui: ui,
},
}
if code := ci.Run(nil); code != 0 {
t.Fatalf("bad: %d\n%s", code, ui.ErrorWriter)
}
// Path where the archive will be "uploaded" to
archivePath := testTempFile(t)
defer os.Remove(archivePath)
client := &mockPushClient{File: archivePath}
ui = new(cli.MockUi)
c := &PushCommand{
Meta: Meta{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
},
client: client,
}
args := []string{
"-vcs=false",
td,
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
actual := testArchiveStr(t, archivePath)
expected := []string{
// Expected weird behavior, doesn't affect unpackaging
".terraform/",
".terraform/",
".terraform/terraform.tfstate",
".terraform/terraform.tfstate",
"main.tf",
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
variables := make(map[string]interface{})
if !reflect.DeepEqual(client.UpsertOptions.Variables, variables) {
t.Fatalf("bad: %#v", client.UpsertOptions)
}
if client.UpsertOptions.Name != "hello" {
t.Fatalf("bad: %#v", client.UpsertOptions)
}
}
func TestPush_noUploadModules(t *testing.T) { func TestPush_noUploadModules(t *testing.T) {
// Path where the archive will be "uploaded" to // Path where the archive will be "uploaded" to
archivePath := testTempFile(t) archivePath := testTempFile(t)
@ -662,6 +728,12 @@ func TestPush_noState(t *testing.T) {
} }
func TestPush_noRemoteState(t *testing.T) { func TestPush_noRemoteState(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
copy.CopyDir(testFixturePath("push-no-remote"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
state := &terraform.State{ state := &terraform.State{
Modules: []*terraform.ModuleState{ Modules: []*terraform.ModuleState{
&terraform.ModuleState{ &terraform.ModuleState{
@ -679,19 +751,32 @@ func TestPush_noRemoteState(t *testing.T) {
} }
statePath := testStateFile(t, state) statePath := testStateFile(t, state)
// Path where the archive will be "uploaded" to
archivePath := testTempFile(t)
defer os.Remove(archivePath)
client := &mockPushClient{File: archivePath}
ui := new(cli.MockUi) ui := new(cli.MockUi)
c := &PushCommand{ c := &PushCommand{
Meta: Meta{ Meta: Meta{
Ui: ui, Ui: ui,
}, },
client: client,
} }
args := []string{ args := []string{
"-vcs=false",
"-state", statePath, "-state", statePath,
td,
} }
if code := c.Run(args); code != 1 { if code := c.Run(args); code != 1 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
} }
errStr := ui.ErrorWriter.String()
if !strings.Contains(errStr, "Remote state") {
t.Fatalf("bad: %s", errStr)
}
} }
func TestPush_plan(t *testing.T) { func TestPush_plan(t *testing.T) {

View File

@ -0,0 +1,5 @@
terraform {
backend "inmem" {}
}
atlas { name = "hello" }

View File

@ -0,0 +1,5 @@
resource "aws_instance" "foo" {}
atlas {
name = "foo"
}