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.
This commit is contained in:
Martin Atkins 2018-11-07 17:20:46 -08:00
parent e20346bf4f
commit c3d11b762b
2 changed files with 25 additions and 5 deletions

View File

@ -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("<testBackendState>", 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

View File

@ -1,9 +1,8 @@
terraform {
backend "http" {
test = true
}
backend "http" {
}
}
resource "test_instance" "foo" {
ami = "bar"
ami = "bar"
}