简体   繁体   English

如何模拟 Terraform 函数

[英]How to mock Terraform functions

I'm studying Terraform and as a test lover I'm using Terratest to test it out but, I'm not being able to mock Terraform's functions such as timestamp() .我正在研究 Terraform,作为测试爱好者,我正在使用Terratest 对其进行测试,但是我无法模拟Terraform 的函数,例如timestamp() Tried some stuffs such as use the library monkey for mocking but so far, none of my approches worked.尝试了一些东西,例如将图书馆猴子用于 mocking,但到目前为止,我的方法都没有奏效。

Does anyone have an idea about how to mock terraform's functions for testing proposals?有没有人知道如何模拟 terraform 的功能来测试提案?

Here a small example that can exemplify my question:这里有一个小例子可以说明我的问题:

file: main.tf文件:main.tf

locals {
  creation_time = formatdate("YYYYMMDDhhmmss", timestamp())
}

file: outputs.tf文件:outputs.tf

output "CreationDate" {
  value = local.creation_time,
  description = "Bla bla bla"
}

./tests/main_test.go ./tests/main_test.go

package study

import (
  "testing"
  "github.com/stretchr/testify/assert"
  "github.com/stretchr/terratest/modules/terraform"
  "bou.ke/monkey"
)

func Test(t * testing.T) {
  t.Parallel()

  terraformOptions := &terraform.Options{
    TerraformDir" "../",
  }

  monkey.Patch(time.Now, func() time.Time {
    return time.Date(2022, 12, 8, 23, 59, 1, time.UTC)
  })

  defer terraform.Destroy(t, terraformOptions)
  terraform.InitAndApply(t, terraformOptions)

  output = terraform.Output(t, terraformOptions, "CreationDate")
  assert.Equal(t, "20221208235901", output)
}

Super simplified example about how to run it:关于如何运行它的超级简化示例:

go mod init study
go mod tidy
cd tests
go test

So, I'm expecting to mock a function from Terraform and assert this value to make sure terraform's file/module does what's expected.因此,我期望从 Terraform 模拟 function 并断言此值以确保 terraform 的文件/模块执行预期的操作。

Unlike some other languages, the Terraform language does not support this sort "monkey patch"-style mocking where you unilaterally change the definition of something for the entire program.与其他一些语言不同,Terraform 语言不支持这种“猴子补丁”式的 mocking,您可以在其中单方面更改整个程序的某些内容的定义。

Terraform modules can their behavior only based on input variables and provider behaviors, so you can add optional features to your module to make it explicitly configurable, if you wish: Terraform 模块的行为只能基于输入变量和提供者行为,因此您可以向模块添加可选功能以使其显式可配置,如果您愿意:

variable "override_timestamp" {
  type    = string
  default = null
}

locals {
  creation_time = coalesce(var.override_timestamp, timestamp())
}

Of course, this technique will not test that your module is actually calling timestamp in the normal case, so it's questionable whether this would actually be useful for testing.当然,这种技术不会测试您的模块在正常情况下是否实际调用timestamp ,因此这是否真的对测试有用值得怀疑。

It might be more profitable to instead design your test driver to verify that the timestamp has overall correct syntax and then do some fuzzy matching on it, such as asserting that the reported instant is greater than or equal to the test start time and less than or equal to the test end time.将测试驱动程序设计为验证时间戳是否具有整体正确的语法然后对其进行一些模糊匹配可能更有利可图,例如断言报告的瞬间大于或等于测试开始时间且小于或等于等于测试结束时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM