Ability to dump test state programmatically

I’m looking to access the Terraform state programmatically with SDK v2 in a test (mainly for debugging purposes). Similar to what used to output in SDK v1 when the state didn’t match the test assertion. Is there some documentation or a snippet I can use to dump this out or is it lost to the past with V1?

What do you mean “programmatically”, in this case? Can you share what you would do in SDKv1 to achieve this?

In v1, it would be achieved by running an acceptance test with TF_LOG=DEBUG and having a field assertion mismatch. The state at each part of the lifecycle would be dumped and you can see the field structure and their values.

By programmatically, I just meant being able to call it explicitly in a test or code without needing something internal to Terraform to fail (such as an acceptance test run)

I jumped back onto SDK v1 to get the example output here. Apologies, I don’t quite know what this is called in Terraform land :joy:

DESTROY: cloudflare_ruleset.tryraohzcz
  description:                                      "tryraohzcz ruleset description" => ""
  id:                                               "57000dfc81a04ae3ade26698e50b6355" => ""
  kind:                                             "zone" => ""
  name:                                             "transform rule for headers" => ""
  phase:                                            "http_request_late_transform" => ""
  rules.#:                                          "1" => ""
  rules.0.action:                                   "rewrite" => ""
  rules.0.action_parameters.#:                      "1" => ""
  rules.0.action_parameters.0.headers.#:            "3" => ""
  rules.0.action_parameters.0.headers.0.expression: "" => ""
  rules.0.action_parameters.0.headers.0.name:       "example1" => ""
  rules.0.action_parameters.0.headers.0.operation:  "set" => ""
  rules.0.action_parameters.0.headers.0.value:      "my-http-header-value1" => ""
  rules.0.action_parameters.0.headers.1.expression: "cf.zone.name" => ""
  rules.0.action_parameters.0.headers.1.name:       "example2" => ""
  rules.0.action_parameters.0.headers.1.operation:  "set" => ""
  rules.0.action_parameters.0.headers.1.value:      "" => ""
  rules.0.action_parameters.0.headers.2.expression: "" => ""
  rules.0.action_parameters.0.headers.2.name:       "example3" => ""
  rules.0.action_parameters.0.headers.2.operation:  "remove" => ""
  rules.0.action_parameters.0.headers.2.value:      "" => ""
  rules.0.action_parameters.0.id:                   "" => ""
  rules.0.action_parameters.0.increment:            "0" => ""
  rules.0.action_parameters.0.overrides.#:          "0" => ""
  rules.0.action_parameters.0.products.#:           "0" => ""
  rules.0.action_parameters.0.ruleset:              "" => ""
  rules.0.action_parameters.0.uri.#:                "0" => ""
  rules.0.action_parameters.0.version:              "" => ""
  rules.0.description:                              "example header transformation rule" => ""
  rules.0.enabled:                                  "false" => ""
  rules.0.expression:                               "true" => ""
  rules.0.id:                                       "8bec8813a9e64917b3ee696c8d5f022c" => ""
  rules.0.ref:                                      "" => ""
  rules.0.version:                                  "" => ""
  zone_id:                                          "0da42c8d2132a9ddaf714f9e7c920711" => ""



STATE:

cloudflare_ruleset.tryraohzcz:
  ID = 57000dfc81a04ae3ade26698e50b6355
  provider = provider.cloudflare
  description = tryraohzcz ruleset description
  kind = zone
  name = transform rule for headers
  phase = http_request_late_transform
  rules.# = 1
  rules.0.action = rewrite
  rules.0.action_parameters.# = 1
  rules.0.action_parameters.0.headers.# = 3
  rules.0.action_parameters.0.headers.0.expression =
  rules.0.action_parameters.0.headers.0.name = example1
  rules.0.action_parameters.0.headers.0.operation = set
  rules.0.action_parameters.0.headers.0.value = my-http-header-value1
  rules.0.action_parameters.0.headers.1.expression = cf.zone.name
  rules.0.action_parameters.0.headers.1.name = example2
  rules.0.action_parameters.0.headers.1.operation = set
  rules.0.action_parameters.0.headers.1.value =
  rules.0.action_parameters.0.headers.2.expression =
  rules.0.action_parameters.0.headers.2.name = example3
  rules.0.action_parameters.0.headers.2.operation = remove
  rules.0.action_parameters.0.headers.2.value =
  rules.0.action_parameters.0.id =
  rules.0.action_parameters.0.increment = 0
  rules.0.action_parameters.0.ruleset =
  rules.0.action_parameters.0.version =
  rules.0.description = example header transformation rule
  rules.0.enabled = false
  rules.0.expression = true
  rules.0.id = 8bec8813a9e64917b3ee696c8d5f022c
  rules.0.ref =
  rules.0.version =
  zone_id = 0da42c8d2132a9ddaf714f9e7c920711

Is this issue what you’re looking for? I think so, I just want to confirm: https://github.com/hashicorp/terraform-plugin-sdk/issues/699

Ah TestStep diff output! That looks like it. Thanks a bunch 🙇‍♂️. I’ll follow that for updates.