mcpField
O
opentofu-modules

opentofu-modules

github.com/ionfury云与 DevOps2572 次安装今天更新
代码仓库

关于 opentofu-modules

为 homelab 基础设施编写 OpenTofu 模块与测试的技能,面向维护基础设施配置的开发者。它规定模块结构,并以 plan 验证、输出断言和特性开关测试为重点。

主要功能

  1. 01规定模块文件结构
  2. 02使用 plan 命令测试配置
  3. 03验证输入规则与输出
  4. 04覆盖特性开关状态
  5. 05按关注点组织测试

适用场景

  1. 01为新的 homelab 模块建立文件与测试结构
  2. 02在不创建资源的情况下验证配置
  3. 03测试变量校验和功能开关行为

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/ionfury/homelab --skill opentofu-modules

安装后在 Claude Code 中输入/opentofu-modules确认已加载。

附带文件references/opentofu-testing.mdtests.yaml

详细介绍

OpenTofu 模块与测试

为 homelab 基础设施编写 OpenTofu 模块和测试。模块位于 infrastructure/modules/,测试位于 infrastructure/modules/<name>/tests/。使用 task tg:test-<module> 运行测试(例如 task tg:test-config);使用 task tg:fmt 格式化。OpenTofu 版本固定于 .opentofu-version。

模块结构

每个模块必须包含:

infrastructure/modules/<name>/
├── variables.tf    # Input definitions with descriptions and validations
├── main.tf         # Primary resources and locals
├── outputs.tf      # Output definitions
├── versions.tf     # Provider and OpenTofu version constraints
└── tests/          # Test directory
    └── *.tftest.hcl

测试文件结构

使用 .tftest.hcl 扩展名。定义顶层 variables,作为所有 run 块继承的默认值。

# Top-level variables set defaults for ALL run blocks
variables {
  name     = "test-cluster"
  features = ["gateway-api", "longhorn"]

  networking = {
    id           = 1
    internal_tld = "internal.test.local"
    # ... other required fields
  }

  # Default machine - inherited unless overridden
  machines = {
    node1 = {
      cluster = "test-cluster"
      type    = "controlplane"
      install = { selector = "disk.model = *" }
      interfaces = [{
        id           = "eth0"
        hardwareAddr = "aa:bb:cc:dd:ee:01"
        addresses    = [{ ip = "192.168.10.101" }]
      }]
    }
  }
}

run "descriptive_test_name" {
  command = plan  # Use plan mode - no real resources created

  variables {
    features = ["prometheus"]  # Only override what differs
  }

  assert {
    condition     = output.some_value == "expected"
    error_message = "Descriptive failure message"
  }
}

关键模式

始终使用 command = plan —— 在不创建资源的情况下验证配置。仅当 run 块中的变量与顶层默认值不同时才包含它们;继承的变量无需重复。针对模块输出进行断言,而非内部资源。

测试功能开关

测试启用和禁用两种状态:

run "feature_enabled" {
  command = plan
  variables { features = ["longhorn"] }

  assert {
    condition = alltrue([
      for m in output.talos.talos_machines :
      contains(m.install.extensions, "iscsi-tools")
    ])
    error_message = "Extension should be added when feature enabled"
  }
}

run "feature_disabled" {
  command = plan
  variables { features = [] }

  assert {
    condition = alltrue([
      for m in output.talos.talos_machines :
      !contains(m.install.extensions, "iscsi-tools")
    ])
    error_message = "Extension should not be present without feature"
  }
}
测试校验

使用 expect_failures 验证变量校验规则:

run "invalid_version_rejected" {
  command = plan
  variables {
    versions = {
      talos = "1.9.0"  # Missing v prefix - should fail
      # ...
    }
  }
  expect_failures = [var.versions]
}

测试组织

按关注点组织测试:

  • plan.tftest.hcl - 基本结构与输出校验
  • validation.tftest.hcl - 输入校验规则
  • feature_<name>.tftest.hcl - 功能开关行为
  • edge_cases.tftest.hcl - 边界条件

详细参考

有关 OpenTofu 测试语法、模拟 provider 和高级模式,请参阅: references/opentofu-testing.md

评论

支持 Markdown