【问题标题】:Using terraform output in kitchen terraform tests在厨房 terraform 测试中使用 terraform 输出
【发布时间】:2021-02-08 16:04:17
【问题描述】:

我正在使用 Kitchen terraform 在 GCP 上部署/测试环境。

我正在努力让厨房/检查部分使用 terraform 输出值,所以我可以在我的测试中使用它们。

这就是我所拥有的

我的inspec.yml

name: default
depends:
  - name: inspec-gcp
    url: https://github.com/inspec/inspec-gcp/archive/master.tar.gz
supports:
  - platform: gcp
attributes:
- name: gcloud_project
  required: true
  description: gcp project
  type: string

我的厨房 Yaml

driver:
  name: terraform
  root_module_directory: test/fixtures/tf_module

provisioner:
  name: terraform

verifier:
  name: terraform
  format: documentation
  systems:
    - name: default
      backend: gcp
      controls:
        - instance

platforms:
  - name: terraform

suites:
  - name: kt_suite

我的单元测试

gcloud_project = attribute('gcloud_project', 
  { description: "The name of the project where resources are deployed." })

  control "instance" do
    describe google_compute_instance(project: "#{gcloud_project}",  zone: 'us-central1-c', name: 'test') do
        its('status') { should eq 'RUNNING' }
      its('machine_type') { should match 'n1-standard-1' }
    end
  end

我的输出.tf

output "gcloud_project" {
  description = "The name of the GCP project to deploy against. We need this output to pass the value to tests."
  value       = "${var.project}"
}

我得到的错误是

  ×  instance: /mnt/c/Users/Github/terra-test-project/test/integration/kt_suite/controls/default.rb:4
     ×  Control Source Code Error /mnt/c/Users/Github/terra-test-project/test/integration/kt_suite/controls/default.rb:4
     bad URI(is not URI?): "https://compute.googleapis.com/compute/v1/projects/Input 'gcloud_project' does not have a value. Skipping test./zones/us-central1-c/instances/test"

如果我直接在控制循环中声明项目名称,一切正常,但显然不想这样做。

如何让厨房/inspec 使用 terraform 输出?

【问题讨论】:

    标签: unit-testing google-cloud-platform terraform test-kitchen


    【解决方案1】:

    看起来这可能只是由于拼写错误。您在 inspec.yml 中的 attributes 下列出了 gcp_project,但在其他任何地方都列出了 gcloud_project

    【讨论】:

    • 嘿,感谢您注意到这一点。我现在改了,还是一样的问题。 terraform 似乎没有将变量传递给厨房,我想知道我是否缺少步骤或其他东西
    • 不确定,但我注意到您似乎不需要从 Terraform 输出项目名称,因为似乎在 2018 年添加了支持以访问 inspec 中的 Terraform 输入变量。 yml。见github.com/newcontext-oss/kitchen-terraform/issues/205rubydoc.info/gems/kitchen-terraform/Kitchen/Verifier/Terraform
    • 设法得到一个新的错误尝试这个 - >>>>>> 验证“默认”系统失败:由于缺少“gcloud_project”键,从输出中解析系统属性失败厨房实例状态下的 Terraform 输出。此错误表明可用的 Terraform 输出需要使用 kitchen converge 更新,或者提供了错误的密钥。有谁知道这实际工作的任何例子。我已将输出放在根 terraform 的 output.tf 和 test/fixtures/tf_module 的 main.tf 中。不知道该放在哪里
    【解决方案2】:

    不确定这是否已修复,但我正在使用类似下面的东西并且效果很好。我认为这可能是您使用 google_project 属性的方式。

    单元测试

    dataset_name   = input('dataset_name')
    account_name = input('account_name')
    project_id = input('project_id')
    
    control "gcp" do
      title "Google Cloud configuration"
    
      describe google_service_account(
        name: account_name,
        project: project_id
      ) do
        it { should exist }
      end
      describe google_bigquery_dataset(
        name: dataset_name,
        project: project_id
      ) do
        it { should exist }
      end
    end
    

    inspec.yml

    name: big_query
    depends:
      - name: inspec-gcp
        git: https://github.com/inspec/inspec-gcp.git
        tag: v1.8.0
    supports:
      - platform: gcp
    inputs:
      - name: dataset_name
        required: true
        type: string
      - name: account_name
        required: true
        type: string
      - name : project_id
        required: true
        type: string
    

    【讨论】:

      猜你喜欢
      • 2020-05-19
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 2018-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      相关资源
      最近更新 更多