英文:
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 plan
和tf 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
你的用于测试导入的设置是正确的。使用ImportState
和ImportStateVerify
添加步骤应该就足够了。
测试导入是在terraform-provider-sdk
的testStepNewImportState
函数中实现的。它的工作原理如下:
- 前一个步骤使用测试用例的工作目录和状态应用 Terraform 配置。
- 如果下一个步骤将
ImportState
设置为true
,则使用ResourceName
从测试用例的状态中获取资源的id
(或者如果为该步骤设置了ImportStateIdFunc
或ImportStateId
,则使用它们)。 - 创建空的工作目录,初始化新的空状态,并从前一个步骤中导入给定资源名称和id的资源。由于这是一个单独的空状态,所以不会产生冲突。
- 如果
ImportStateVerify
为true
,则比较前一个步骤和导入步骤的资源状态,它们应该是相同的。 - 如果设置了
ImportStateCheck
函数,则使用该函数进行自定义状态验证。这可以在直接状态比较无效的情况下使用。 - 丢弃临时工作目录。
英文:
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:
- Previous step applies terraform config using testcase workdir and state.
- If next step sets
ImportState
to true, useResourceName
to grab resourceid
from testcase state (or useImportStateIdFunc
orImportStateId
if they are set for this step). - 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.
- If
ImportStateVerify
is true, compare resource states from previous step and import step, they should be identical. - 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. - Discard temporary workdir.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论