mirror of https://github.com/hashicorp/terraform
terraform test: connect mock behaviour to test framework (#34205)
* connect mock behaviour to test framework * fix calling unconfigured providerspull/34192/head
parent
aec48aa8d2
commit
dedb296773
@ -0,0 +1,30 @@
|
||||
terraform {
|
||||
required_providers {
|
||||
test = {
|
||||
source = "hashicorp/test"
|
||||
configuration_aliases = [test.primary, test.secondary]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
variable "instances" {
|
||||
type = number
|
||||
}
|
||||
|
||||
resource "test_resource" "primary" {
|
||||
provider = test.primary
|
||||
count = var.instances
|
||||
}
|
||||
|
||||
resource "test_resource" "secondary" {
|
||||
provider = test.secondary
|
||||
count = var.instances
|
||||
}
|
||||
|
||||
output "primary" {
|
||||
value = test_resource.primary
|
||||
}
|
||||
|
||||
output "secondary" {
|
||||
value = test_resource.secondary
|
||||
}
|
||||
@ -0,0 +1,46 @@
|
||||
terraform {
|
||||
required_providers {
|
||||
test = {
|
||||
source = "hashicorp/test"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
provider "test" {
|
||||
alias = "primary"
|
||||
}
|
||||
|
||||
provider "test" {
|
||||
alias = "secondary"
|
||||
}
|
||||
|
||||
variable "instances" {
|
||||
type = number
|
||||
}
|
||||
|
||||
variable "child_instances" {
|
||||
type = number
|
||||
}
|
||||
|
||||
resource "test_resource" "primary" {
|
||||
provider = test.primary
|
||||
count = var.instances
|
||||
}
|
||||
|
||||
resource "test_resource" "secondary" {
|
||||
provider = test.secondary
|
||||
count = var.instances
|
||||
}
|
||||
|
||||
module "child" {
|
||||
count = var.instances
|
||||
|
||||
source = "./child"
|
||||
|
||||
providers = {
|
||||
test.primary = test.primary
|
||||
test.secondary = test.secondary
|
||||
}
|
||||
|
||||
instances = var.child_instances
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
override_module {
|
||||
target = module.child[1]
|
||||
outputs = {
|
||||
primary = [
|
||||
{
|
||||
id = "bbbb"
|
||||
}
|
||||
]
|
||||
secondary = [
|
||||
{
|
||||
id = "cccc"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
variables {
|
||||
instances = 3
|
||||
child_instances = 1
|
||||
}
|
||||
|
||||
run "test" {
|
||||
|
||||
assert {
|
||||
condition = module.child[0].primary[0].id != "bbbb"
|
||||
error_message = "wrongly applied mocks"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.child[1].primary[0].id == "bbbb"
|
||||
error_message = "did not apply mocks"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.child[1].secondary[0].id == "cccc"
|
||||
error_message = "did not apply mocks"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.child[2].secondary[0].id != "cccc"
|
||||
error_message = "wrongly applied mocks"
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,70 @@
|
||||
override_module {
|
||||
target = module.child
|
||||
outputs = {
|
||||
primary = [
|
||||
{
|
||||
id = "bbbb"
|
||||
}
|
||||
]
|
||||
secondary = [
|
||||
{
|
||||
id = "cccc"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
variables {
|
||||
instances = 3
|
||||
child_instances = 1
|
||||
}
|
||||
|
||||
run "test" {
|
||||
|
||||
override_module {
|
||||
target = module.child[1]
|
||||
outputs = {
|
||||
primary = [
|
||||
{
|
||||
id = "aaaa"
|
||||
}
|
||||
]
|
||||
secondary = [
|
||||
{
|
||||
id = "dddd"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.child[0].primary[0].id == "bbbb"
|
||||
error_message = "wrongly applied mocks"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.child[0].secondary[0].id == "cccc"
|
||||
error_message = "did not apply mocks"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.child[2].primary[0].id == "bbbb"
|
||||
error_message = "wrongly applied mocks"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.child[2].secondary[0].id == "cccc"
|
||||
error_message = "did not apply mocks"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.child[1].primary[0].id == "aaaa"
|
||||
error_message = "did not apply mocks"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.child[1].secondary[0].id == "dddd"
|
||||
error_message = "did not apply mocks"
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
|
||||
variables {
|
||||
instances = 1
|
||||
child_instances = 0
|
||||
}
|
||||
|
||||
run "test" {}
|
||||
@ -0,0 +1,39 @@
|
||||
mock_provider "test" {
|
||||
alias = "primary"
|
||||
|
||||
mock_resource "test_resource" {
|
||||
defaults = {
|
||||
id = "aaaa"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
variables {
|
||||
instances = 1
|
||||
child_instances = 1
|
||||
}
|
||||
|
||||
|
||||
run "test" {
|
||||
|
||||
assert {
|
||||
condition = test_resource.primary[0].id == "aaaa"
|
||||
error_message = "did not apply mocks"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.child[0].primary[0].id == "aaaa"
|
||||
error_message = "did not apply mocks"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = test_resource.secondary[0].id != "aaaa"
|
||||
error_message = "wrongly applied mocks"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.child[0].secondary[0].id != "aaaa"
|
||||
error_message = "wrongly applied mocks"
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,52 @@
|
||||
mock_provider "test" {
|
||||
alias = "primary"
|
||||
|
||||
mock_resource "test_resource" {
|
||||
defaults = {
|
||||
id = "aaaa"
|
||||
}
|
||||
}
|
||||
|
||||
override_resource {
|
||||
target = test_resource.primary
|
||||
values = {
|
||||
id = "bbbb"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
variables {
|
||||
instances = 3
|
||||
child_instances = 1
|
||||
}
|
||||
|
||||
run "test" {
|
||||
|
||||
override_resource {
|
||||
target = test_resource.primary[1]
|
||||
values = {
|
||||
id = "cccc"
|
||||
}
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = test_resource.primary[0].id == "bbbb"
|
||||
error_message = "did not apply mocks"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = test_resource.primary[1].id == "cccc"
|
||||
error_message = "did not apply mocks"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = test_resource.primary[2].id == "bbbb"
|
||||
error_message = "did not apply mocks"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.child[0].primary[0].id == "aaaa"
|
||||
error_message = "did not apply mocks"
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue