尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
使用 Terratest 为 Azure Cosmos DB Terraform 模块编写自动化测试
测试开发工具DevOps质量保障【免费下载链接】terratestTerratest is a Go library that makes it easier to write automated tests for your infrastructure code.项目地址https://gitcode.com/gh_mirrors/te/terratest点击查看免费下载导读本文以 Terratest 仓库中的 terraform-azure-cosmosdb-example 示例模块为线索完整讲解如何用 Terraform 在 Azure 上部署一个 Cosmos DB 账户含 SQL 数据库与三个 SQL 容器并配套编写 Go 自动化测试用 Azure SDK 独立回读真实资源属性并与 Terraform 输出断言比对。读完本文你将掌握该示例的资源拓扑、全部变量与输出参数、手动部署流程、测试命令以及 Terratest 中 Cosmos DB 测试辅助函数的底层调用链。⚠️成本警告本文模块及其自动化测试会在你的 Azure 账户中部署真实资源会产生实际费用。模块全景部署了哪些 Azure 资源该文件夹包含一个完整的 Terraform Cosmos DB 模块演示如何用 Terratest 为 Azure Terraform 代码编写自动化测试。模块共部署四类资源资源说明azurerm_resource_group.rg资源组命名terratest-cosmos-rg-${postfix}azurerm_cosmosdb_account.testCosmos DB 账户SQL APIkind GlobalDocumentDBazurerm_cosmosdb_sql_database.testdb一个 SQL 数据库名称固定为testdbazurerm_cosmosdb_sql_container.*三个 SQL 容器test-container-1/2/3对应的 Go 测试位于 test/azure/terraform_azure_cosmosdb_example_test.go用于验证该模块各配置参数与选项的正确性。Terraform 主配置逐块解读main.tf模块入口为 main.tf首先声明 Terraform 版本约束与 azurerm Providerterraform { required_version 1.0 required_providers { azurerm { source hashicorp/azurerm version ~ 3.0 } } } provider azurerm { features {} }资源组resource azurerm_resource_group rg { name terratest-cosmos-rg-${var.postfix} location var.location }Cosmos DB 账户resource azurerm_cosmosdb_account test { name terratest-${var.postfix} location azurerm_resource_group.rg.location resource_group_name azurerm_resource_group.rg.name offer_type Standard kind GlobalDocumentDB consistency_policy { consistency_level Session max_interval_in_seconds 5 max_staleness_prefix 100 } geo_location { location azurerm_resource_group.rg.location failover_priority 0 } }关键点kind GlobalDocumentDB表示使用 SQL文档API一致性策略为Session会话一致性并带有界内过期bounded staleness参数max_interval_in_seconds 5、max_staleness_prefix 100geo_location声明一个主区域failover_priority 0表示最高故障转移优先级。SQL 数据库与容器resource azurerm_cosmosdb_sql_database testdb { name testdb throughput var.throughput resource_group_name azurerm_resource_group.rg.name account_name azurerm_cosmosdb_account.test.name }三个容器结构与container1一致resource azurerm_cosmosdb_sql_container container1 { name test-container-1 throughput var.throughput partition_key_path /key1 resource_group_name azurerm_cosmosdb_account.test.resource_group_name account_name azurerm_cosmosdb_account.test.name database_name azurerm_cosmosdb_sql_database.testdb.name }container2、container3仅名称与分区键不同/key2、/key3。分区键路径是容器必配属性测试将逐一验证。变量与输出参数变量variables.tf模块没有必填变量全部为带默认值的可选参数定义于 variables.tf变量类型默认值说明postfixstringresource追加到资源名后缀集中避免命名冲突locationstringEast USCosmos DB 实例所在区域throughputnumber400数据库账户的 RU/s 吞吐量postfix在自动化测试中被替换为随机数见下文以支持并行测试互不干扰。输出outputs.tf定义于 outputs.tf其中primary_key被标记为sensitive trueoutput resource_group_name { value azurerm_resource_group.rg.name } output account_name { value azurerm_cosmosdb_account.test.name } output endpoint { value azurerm_cosmosdb_account.test.endpoint } output primary_key { value azurerm_cosmosdb_account.test.primary_key sensitive true }手动运行模块注册 Azure 账号使用 Azure CLI 支持的方式之一配置 Azure 凭据安装 Terraform 并确保其位于PATH确保相关环境变量已就绪见下节执行terraform init执行terraform apply完成后执行terraform destroy清理资源。Azure 环境变量速查参考 examples/azure/README.mdTerraform 与 Terratest 均通过以下环境变量获取 Azure 身份信息export ARM_CLIENT_IDyour_app_id export ARM_CLIENT_SECRETyour_password export ARM_SUBSCRIPTION_IDyour_subscription_id export ARM_TENANT_IDyour_tenant_id # 非公有云场景按需选择 export AZURE_ENVIRONMENTAzureUSGovernmentCloud # AzureChinaCloud / AzureGermanCloud / AzurePublicCloud / AzureStackCloudAZURE_ENVIRONMENT会被 modules/azure/client_factory.go 读取用于设定正确的 Azure 端点。Windows 下需以管理员 PowerShell 将这些变量设置为系统环境变量。对模块运行自动化测试前置步骤与手动运行相同Azure 账号、凭据、Terraform、配置 Go 测试环境然后cd test/azure go build terraform_azure_cosmosdb_example_test.go go test -v -timeout 60m -tags azure -run TestTerraformAzureCosmosDBExample说明-tags azure启用源文件顶部//go:build azure构建约束-timeout 60m为真实云资源的创建/销毁留出足够时间-run指定仅运行该用例。测试代码核心流程terraform_azure_cosmosdb_example_test.go 的TestTerraformAzureCosmosDBExample分四步1. 配置 Terraform 选项指向示例目录并用随机 postfix 与吞吐量 400 覆盖默认变量uniquePostfix : random.Random(10000, 99999) throughput : 400 terraformOptions : terraform.Options{ TerraformDir: ../../examples/azure/terraform-azure-cosmosdb-example, Vars: map[string]interface{}{ postfix: uniquePostfix, throughput: throughput, }, }2. 清理与部署defer terraform.DestroyContext(...)保证测试结束自动销毁随后terraform.InitAndApplyContext(...)执行initapply。3. 读取输出terraform.OutputContext(...)获取resource_group_name与account_name。4. 独立回读并断言通过azure模块的辅助函数结合assert.Equal验证账户azure.GetCosmosDBAccountContext(...)断言名称、Kind GlobalDocumentDB、一致性级别DefaultConsistencyLevelSessionSQL 数据库azure.GetCosmosDBSQLDatabaseContext(...)断言名称为testdb并通过GetCosmosDBSQLDatabaseThroughputContext断言吞吐量为400RU/s三个容器逐一断言名称及分区键路径/key1、/key2、/key3并验证容器吞吐量为 400。注意subscriptionID可传空字符串SDK 会回退到环境变量ARM_SUBSCRIPTION_ID的值。源码级原理Cosmos DB 测试辅助函数client_factory.go 中的客户端工厂测试的 SDK 客户端由 client_factory.go 统一创建func CreateCosmosDBAccountClientContextE(_ context.Context, subscriptionID string) (*armcosmos.DatabaseAccountsClient, error) { clientFactory, err : getArmCosmosClientFactory(subscriptionID) ... } func CreateCosmosDBSQLClientContextE(_ context.Context, subscriptionID string) (*armcosmos.SQLResourcesClient, error) { clientFactory, err : getArmCosmosClientFactory(subscriptionID) ... }两处均复用getArmCosmosClientFactory保证凭据与云环境解析逻辑单一来源。cosmosdb.go 中的读取辅助函数modules/azure/cosmosdb.go 提供全部回读能力每个 Get 函数都有三件套...Context失败即require.NoError终止测试、...ContextE返回 error、...WithClient接受外部注入的 client便于测试函数底层 SDK 调用GetCosmosDBAccountWithClientclient.Get(ctx, resourceGroupName, accountName, nil)GetCosmosDBSQLDatabaseWithClientclient.GetSQLDatabase(...)GetCosmosDBSQLContainerWithClientclient.GetSQLContainer(...)GetCosmosDBSQLDatabaseThroughputWithClientclient.GetSQLDatabaseThroughput(...)GetCosmosDBSQLContainerThroughputWithClientclient.GetSQLContainerThroughput(...)例如账户回读GetCosmosDBAccountContext先建 client再经GetCosmosDBAccountWithClient发起 SDK 请求最后把resp.DatabaseAccountGetResults返回给断言层。容器吞吐量的ThroughputSettingsGetResults通过Properties.Resource.Throughput暴露 RU/s 数值供测试与 Terraform 输出比对。测试设计要点总结回读而非仅信 Terraform 输出测试使用 Azure SDK 独立查询真实资源状态避免 Terraform state 与实际云资源不一致的假阳性。随机 postfix 避免命名冲突random.Random(10000, 99999)保证并行运行的测试实例资源名不冲突。defer destroy 保证清理无论测试成功失败terraform destroy都会执行防止残留付费资源。上下文贯穿调用链所有辅助函数接受context.Context支持超时与取消控制。若需进一步探索可阅读 test/azure 下其他测试文件与 modules/azure 中cosmosdb.go、client_factory.go的完整实现。赞分享测试开发工具DevOps质量保障【免费下载链接】terratestTerratest is a Go library that makes it easier to write automated tests for your infrastructure code.项目地址https://gitcode.com/gh_mirrors/te/terratest点击查看免费下载相关推荐使用 Terratest 为 Azure Key Vault Terraform 模块编写自动化测试使用 Terratest 为 Azure Key Vault Terraform 模块编写自动化测试 本文基于 Terratest 仓库中的 terraform测试开发工具DevOps质量保障使用 Terratest 为 Azure Synapse Analytics Terraform 模块编写自动化测试使用 Terratest 为 Azure Synapse Analytics Terraform 模块编写自动化测试 本指南以仓库中的 terraform az测试开发工具DevOps质量保障Travel Planning Agent Platform 实战基于真实 PRD 从零构建智能旅游规划 Agent 平台Travel Planning Agent Platform 实战基于真实 PRD 从零构建智能旅游规划 Agent 平台 导读 本文是 easy vibe测试开发工具DevOps质量保障上一篇终极指南如何用Skia引擎打造惊艳的彩色字体与SVG表情符号下一篇ThinkBayes2项目贡献指南如何参与开源贝叶斯教育项目创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
RELATED

相关推荐

网页的维护与更新完整流程

网页的维护与更新完整流程

网页维护更新哪家强?3个步骤告别拖延症 改个文案建站公司拖一周?这种憋屈事谁没干过?很多老板觉得找对 哪家好 的供应商就能一劳永逸,其实大错特错。网站交付只是开始,后续的 网页的维护与更新 才是决定生死的关键。…

📅 2026/9/27 8:04:26
零基础搞网站?wordpress爱搭配哪家好,避坑指南看这篇

零基础搞网站?wordpress爱搭配哪家好,避坑指南看这篇

零基础搞网站?wordpress爱搭配哪家好,避坑指南看这篇 自己不会代码想做网站,是不是觉得脑子一团浆糊?别慌,这行干了十年,见过太多人卡在第一步。…

📅 2026/9/27 7:59:25
软技能详解:谈判与冲突处理

软技能详解:谈判与冲突处理

软技能详解:谈判与冲突处理 在软件架构与工程管理场景中,谈判和冲突处理不是"锦上添花"的软技能,而是决定技术决策能否落地、团队能否高效协作的核心能力。架构师尤其处于冲突的天然交汇点:他们要在业务方、开发团队、运…

📅 2026/9/27 7:59:25
MORE NEWS

更多资讯

📰

Java对象比较与克隆:Comparable、Comparator接口与深拷贝实现

接口使用实例以及Object类中的equals,hashcode方法1.Comparable 接口实现2.Comparator 接口实现3.Clonable 接口和深拷贝4.对象比较equals方法5.hashcode方法1.Comparable 接口实现 实现该接口时需要加上一个泛型(类比 C的模板) Class Student impleme…

📰

内部开发者工具箱发布(一):统一脚手架的设计理念与架构演进

内部开发者工具箱发布(一):统一脚手架的设计理念与架构演进在百人以上规模的技术团队中,研发痛点往往不是缺少工具,而是“工具林立且割裂”:后端有一套基于 Shell 的部署脚本,前端维护着一套 No…

📰

定制 LangChain 嵌入模型包装器:生产级落地全景总结

定制 LangChain 嵌入模型包装器:生产级落地全景总结在大语言模型(LLM)与 RAG(检索增强生成)系统的数据入库与实时问答链路中,Embeddings(向量嵌入模型组件) 是承载全系统数据流转最频…

📰

cryptography 常见问题完全指南:从安装排错到 PEM 解析的实战 FAQ

密码学 【免费下载链接】cryptography cryptography is a package designed to expose cryptographic primitives and recipes to Python developers. 项目地址: https://gitcode.com/gh_mirrors/cr/cryptography 点击查看 免费下载 cryptography 是 Python 生态中…

📰

哈尔滨品牌建站软件多少钱?3类方案拆解

哈尔滨品牌建站软件多少钱?3类方案拆解 域名选哪个后缀?服务器配多大内存?ICP备案要多久?很多老板在找哈尔滨本地做品牌站时,脑子里全是问号。别急,今天把这笔账算清楚。 核心结论先行:哈尔滨地区做品牌官网,基础型预算在…

📰

基于 LLM 裁判的自动化越狱对抗评测与红蓝演练大盘实战

基于 LLM 裁判的自动化越狱对抗评测与红蓝演练大盘实战在多智能体系统(MAS)面向全网开放或深度集成企业核心数据资产时,系统面临着全球黑客与恶意用户持续发起的**“提示词越狱注入、人设劫持与有害内容诱导(Jailbreak Attacks &a…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬