如何在验收测试中测试 Terraform 的导入功能?

huangapple go评论105阅读模式
英文:

How can I test terraform import in acceptance tests?

问题

我找到了这篇关于在验收测试中测试tf import文章

func TestAccExampleThing_basic(t *testing.T) {
  /* ... 可能存在的验收测试逻辑 ... */

  resource.ParallelTest(t, resource.TestCase{
    /* ... 存在的 TestCase 函数 ... */
    Steps: []resource.TestStep{
      /* ... 存在的 TestStep ... */
      {
        ResourceName:      "example_thing.test",
        ImportState:       true,
        ImportStateVerify: true,
      },
    },
  })
}

但是对我来说仍然很困惑。假设existing TestCase function会初始化一些小的配置并运行tf plantf apply,以便tf状态文件不为空,那么在第2步(测试用例)期间会发生什么呢?按照我的理解,如果我们尝试导入已经在本地tf状态中的资源(在第1步之后),应该会出现错误或其他情况,对吗?

或者更令人困惑的是,这个组合的测试中,步骤2和步骤4都是在terraform-provider-aws中进行导入(据我所见,步骤3更新了在步骤1中创建的状态中的一个属性,但步骤2和步骤4具体做了什么)?

英文:

I found this article about testing tf import in acceptance tests:

func TestAccExampleThing_basic(t *testing.T) {
  /* ... potentially existing acceptance testing logic ... */

  resource.ParallelTest(t, resource.TestCase{
    /* ... existing TestCase functions ... */
    Steps: []resource.TestStep{
      /* ... existing TestStep ... */
      {
        ResourceName:      "example_thing.test",
        ImportState:       true,
        ImportStateVerify: true,
      },
    },
  })
}

But it's still very confusing to me though. Let's say existing TestCase function will initialize some small config & run tf plan & tf apply so tf state file won't be empty and then what exactly will happen during step (test case) #2? The way I think about it there should be an error or something if we try to import the resource that's already in tf state locally (after step#1), right?

Or what's even more confusing, this combined test where steps #2, and #4 are both imports in terraform-provider-aws (as far as I can see step#3 updated one of the attributes in our state that was created in step#1 but what exactly step#2 and step#4 does)?

答案1

得分: 5

你的用于测试导入的设置是正确的。使用ImportStateImportStateVerify添加步骤应该就足够了。

测试导入是在terraform-provider-sdktestStepNewImportState函数中实现的。它的工作原理如下:

  1. 前一个步骤使用测试用例的工作目录和状态应用 Terraform 配置。
  2. 如果下一个步骤将ImportState设置为true,则使用ResourceName从测试用例的状态中获取资源的id(或者如果为该步骤设置了ImportStateIdFuncImportStateId,则使用它们)。
  3. 创建空的工作目录,初始化新的空状态,并从前一个步骤中导入给定资源名称和id的资源。由于这是一个单独的空状态,所以不会产生冲突。
  4. 如果ImportStateVerifytrue,则比较前一个步骤和导入步骤的资源状态,它们应该是相同的。
  5. 如果设置了ImportStateCheck函数,则使用该函数进行自定义状态验证。这可以在直接状态比较无效的情况下使用。
  6. 丢弃临时工作目录。
英文:

Your setup for testing import is correct. Adding steps with ImportState and ImportStateVerify should be enough.

Testing import is implemented in terraform-provider-sdk testStepNewImportState function. How it works:

  1. Previous step applies terraform config using testcase workdir and state.
  2. If next step sets ImportState to true, use ResourceName to grab resource id from testcase state (or use ImportStateIdFunc or ImportStateId if they are set for this step).
  3. Create empty workdir, initialize new empty state, and import resource given resource name and id from previous step. There would be no conflicts since this is a separate empty state.
  4. If ImportStateVerify is true, compare resource states from previous step and import step, they should be identical.
  5. If ImportStateCheck function is set, use this function for custom state validation. This can be used in case if direct state comparison will be not valid.
  6. Discard temporary workdir.

huangapple
  • 本文由 发表于 2021年8月18日 06:29:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/68824711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定