command: Update tests for changes in "terraform" package

This is not exhaustive, but it gets the tests in this package compiling
again and gets most of them working.
This commit is contained in:
Martin Atkins 2018-05-22 19:33:45 -07:00
parent cdc8abdae0
commit a270a18a4d
7 changed files with 81 additions and 32 deletions

View File

@ -5,8 +5,10 @@ import (
"strings"
"testing"
"github.com/hashicorp/terraform/config/configschema"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli"
"github.com/zclconf/go-cty/cty"
)
func TestApply_destroy(t *testing.T) {
@ -29,6 +31,8 @@ func TestApply_destroy(t *testing.T) {
statePath := testStateFile(t, originalState)
p := testProvider()
p.GetSchemaReturn = applyFixtureSchema()
ui := new(cli.MockUi)
c := &ApplyCommand{
Destroy: true,
@ -147,7 +151,7 @@ func TestApply_destroyLockedState(t *testing.T) {
func TestApply_destroyPlan(t *testing.T) {
planPath := testPlanFile(t, &terraform.Plan{
Module: testModule(t, "apply"),
Config: testModule(t, "apply"),
})
p := testProvider()
@ -195,6 +199,21 @@ func TestApply_destroyTargeted(t *testing.T) {
statePath := testStateFile(t, originalState)
p := testProvider()
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
"test_instance": {
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Computed: true},
},
},
"test_load_balancer": {
Attributes: map[string]*configschema.Attribute{
"instances": {Type: cty.List(cty.String), Optional: true},
},
},
},
}
ui := new(cli.MockUi)
c := &ApplyCommand{
Destroy: true,

View File

@ -16,15 +16,20 @@ import (
"testing"
"time"
"github.com/mitchellh/cli"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/config/configschema"
"github.com/hashicorp/terraform/state"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli"
)
func TestApply(t *testing.T) {
statePath := testTempFile(t)
p := testProvider()
p.GetSchemaReturn = applyFixtureSchema()
ui := new(cli.MockUi)
c := &ApplyCommand{
Meta: Meta{
@ -495,7 +500,7 @@ func TestApply_plan(t *testing.T) {
defaultInputWriter = new(bytes.Buffer)
planPath := testPlanFile(t, &terraform.Plan{
Module: testModule(t, "apply"),
Config: testModule(t, "apply"),
})
statePath := testTempFile(t)
@ -620,7 +625,7 @@ func TestApply_plan_remoteState(t *testing.T) {
state.Remote = conf
planPath := testPlanFile(t, &terraform.Plan{
Module: testModule(t, "apply"),
Config: testModule(t, "apply"),
State: state,
})
@ -664,7 +669,7 @@ func TestApply_planWithVarFile(t *testing.T) {
}
planPath := testPlanFile(t, &terraform.Plan{
Module: testModule(t, "apply"),
Config: testModule(t, "apply"),
})
statePath := testTempFile(t)
@ -706,7 +711,7 @@ func TestApply_planWithVarFile(t *testing.T) {
func TestApply_planVars(t *testing.T) {
planPath := testPlanFile(t, &terraform.Plan{
Module: testModule(t, "apply"),
Config: testModule(t, "apply"),
})
statePath := testTempFile(t)
@ -739,7 +744,7 @@ func TestApply_planNoModuleFiles(t *testing.T) {
p := testProvider()
planFile := testPlanFile(t, &terraform.Plan{
Module: testModule(t, "apply-plan-no-module"),
Config: testModule(t, "apply-plan-no-module"),
})
apply := &ApplyCommand{
@ -1527,6 +1532,21 @@ func testHttpHandlerHeader(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}
// applyFixtureSchema returns a schema suitable for processing the
// configuration in test-fixtures/apply . This schema should be
// assigned to a mock provider named "test".
func applyFixtureSchema() *terraform.ProviderSchema {
return &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
"test_instance": {
Attributes: map[string]*configschema.Attribute{
"ami": {Type: cty.String, Optional: true},
},
},
},
}
}
const applyVarFile = `
foo = "bar"
`

View File

@ -19,8 +19,8 @@ import (
"syscall"
"testing"
backendInit "github.com/hashicorp/terraform/backend/init"
"github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/configs/configload"
"github.com/hashicorp/terraform/helper/logging"
"github.com/hashicorp/terraform/terraform"
)
@ -113,21 +113,30 @@ func metaOverridesForProviderAndProvisioner(p terraform.ResourceProvider, pr ter
}
}
func testModule(t *testing.T, name string) *module.Tree {
func testModule(t *testing.T, name string) *configs.Config {
t.Helper()
mod, err := module.NewTreeModule("", filepath.Join(fixtureDir, name))
if err != nil {
t.Fatalf("err: %s", err)
dir := filepath.Join(fixtureDir, name)
// FIXME: We're not dealing with the cleanup function here because
// this testModule function is used all over and so we don't want to
// change its interface at this late stage.
loader, _ := configload.NewLoaderForTests(t)
// Test modules usually do not refer to remote sources, and for local
// sources only this ultimately just records all of the module paths
// in a JSON file so that we can load them below.
diags := loader.InstallModules(dir, true, configload.InstallHooksImpl{})
if diags.HasErrors() {
t.Fatal(diags.Error())
}
s := module.NewStorage(tempDir(t), nil)
s.Mode = module.GetModeGet
if err := mod.Load(s); err != nil {
t.Fatalf("err: %s", err)
config, diags := loader.LoadConfig(dir)
if diags.HasErrors() {
t.Fatal(diags.Error())
}
return mod
return config
}
// testPlan returns a non-nil noop plan.
@ -141,7 +150,7 @@ func testPlan(t *testing.T) *terraform.Plan {
}
return &terraform.Plan{
Module: testModule(t, "apply"),
Config: testModule(t, "apply"),
State: state,
}
}

View File

@ -121,7 +121,7 @@ func TestGraph_plan(t *testing.T) {
},
},
Module: testModule(t, "graph"),
Config: testModule(t, "graph"),
})
ui := new(cli.MockUi)

View File

@ -2742,7 +2742,7 @@ func TestMetaBackend_planLocal(t *testing.T) {
// Create the plan
plan := &terraform.Plan{
Module: testModule(t, "backend-plan-local"),
Config: testModule(t, "backend-plan-local"),
State: nil,
}
@ -2829,7 +2829,7 @@ func TestMetaBackend_planLocalStatePath(t *testing.T) {
// Create the plan
plan := &terraform.Plan{
Module: testModule(t, "backend-plan-local"),
Config: testModule(t, "backend-plan-local"),
State: original,
}
@ -2925,7 +2925,7 @@ func TestMetaBackend_planLocalMatch(t *testing.T) {
// Create the plan
plan := &terraform.Plan{
Module: testModule(t, "backend-plan-local-match"),
Config: testModule(t, "backend-plan-local-match"),
State: testStateRead(t, DefaultStateFilename),
}
@ -3018,7 +3018,7 @@ func TestMetaBackend_planLocalMismatchLineage(t *testing.T) {
// Create the plan
plan := &terraform.Plan{
Module: testModule(t, "backend-plan-local-mismatch-lineage"),
Config: testModule(t, "backend-plan-local-mismatch-lineage"),
State: planState,
}
@ -3070,7 +3070,7 @@ func TestMetaBackend_planLocalNewer(t *testing.T) {
// Create the plan
plan := &terraform.Plan{
Module: testModule(t, "backend-plan-local-newer"),
Config: testModule(t, "backend-plan-local-newer"),
State: planState,
}
@ -3124,7 +3124,7 @@ func TestMetaBackend_planBackendEmptyDir(t *testing.T) {
// Create the plan
plan := &terraform.Plan{
Module: testModule(t, "backend-plan-backend-empty-config"),
Config: testModule(t, "backend-plan-backend-empty-config"),
State: planState,
Backend: backendState.Backend,
}
@ -3226,7 +3226,7 @@ func TestMetaBackend_planBackendMatch(t *testing.T) {
// Create the plan
plan := &terraform.Plan{
Module: testModule(t, "backend-plan-backend-empty-config"),
Config: testModule(t, "backend-plan-backend-empty-config"),
State: planState,
Backend: backendState.Backend,
}
@ -3331,7 +3331,7 @@ func TestMetaBackend_planBackendMismatchLineage(t *testing.T) {
// Create the plan
plan := &terraform.Plan{
Module: testModule(t, "backend-plan-backend-empty-config"),
Config: testModule(t, "backend-plan-backend-empty-config"),
State: planState,
Backend: backendState.Backend,
}
@ -3390,7 +3390,7 @@ func TestMetaBackend_planLegacy(t *testing.T) {
// Create the plan
plan := &terraform.Plan{
Module: testModule(t, "backend-plan-legacy-data"),
Config: testModule(t, "backend-plan-legacy-data"),
State: planState,
}

View File

@ -84,7 +84,7 @@ func TestPlan_plan(t *testing.T) {
defer testFixCwd(t, tmp, cwd)
planPath := testPlanFile(t, &terraform.Plan{
Module: testModule(t, "apply"),
Config: testModule(t, "apply"),
})
p := testProvider()

View File

@ -5,7 +5,8 @@ import (
"strings"
"testing"
"github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli"
)
@ -68,7 +69,7 @@ func TestShow_noArgsNoState(t *testing.T) {
func TestShow_plan(t *testing.T) {
planPath := testPlanFile(t, &terraform.Plan{
Module: new(module.Tree),
Config: configs.NewEmptyConfig(),
})
ui := new(cli.MockUi)