From c3d11b762b19df3a3bee0def34a8166ef60a8c02 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Wed, 7 Nov 2018 17:20:46 -0800 Subject: [PATCH] command: Fix testBackendState The hashing function for cached backend configuration is different now, so our hard-coded hash of the HTTP backend address wasn't working anymore. Here we update the hash so that tests using this test backend will work again. Rather than leaving it hard-coded, we'll instead compute it the same way as "terraform init" would. In practice only one test is actually using this function right now, so we also update the test fixture for that test (TestPlan_outBackend) to match the new expectations, though as of this commit it's still failing with an unrelated error. --- command/command_test.go | 23 ++++++++++++++++++- .../test-fixtures/plan-out-backend/main.tf | 7 +++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/command/command_test.go b/command/command_test.go index f5a17cd63..b7921caa7 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -664,6 +664,19 @@ func testInputMap(t *testing.T, answers map[string]string) func() { // testBackendState is used to make a test HTTP server to test a configured // backend. This returns the complete state that can be saved. Use // `testStateFileRemote` to write the returned state. +// +// When using this function, the configuration fixture for the test must +// include an empty configuration block for the HTTP backend, like this: +// +// terraform { +// backend "http" { +// } +// } +// +// If such a block isn't present, or if it isn't empty, then an error will +// be returned about the backend configuration having changed and that +// "terraform init" must be run, since the test backend config cache created +// by this function contains the hash for an empty configuration. func testBackendState(t *testing.T, s *terraform.State, c int) (*terraform.State, *httptest.Server) { t.Helper() @@ -696,11 +709,19 @@ func testBackendState(t *testing.T, s *terraform.State, c int) (*terraform.State srv := httptest.NewServer(http.HandlerFunc(cb)) + backendConfig := &configs.Backend{ + Type: "http", + Config: configs.SynthBody("", map[string]cty.Value{}), + } + b := backendinit.Backend("http")() + configSchema := b.ConfigSchema() + hash := backendConfig.Hash(configSchema) + state := terraform.NewState() state.Backend = &terraform.BackendState{ Type: "http", ConfigRaw: json.RawMessage(fmt.Sprintf(`{"address":%q}`, srv.URL)), - Hash: 2529831861221416334, + Hash: hash, } return state, srv diff --git a/command/test-fixtures/plan-out-backend/main.tf b/command/test-fixtures/plan-out-backend/main.tf index e1be95fa8..38ba171da 100644 --- a/command/test-fixtures/plan-out-backend/main.tf +++ b/command/test-fixtures/plan-out-backend/main.tf @@ -1,9 +1,8 @@ terraform { - backend "http" { - test = true - } + backend "http" { + } } resource "test_instance" "foo" { - ami = "bar" + ami = "bar" }