mirror of
https://github.com/opentffoundation/opentf.git
synced 2025-12-25 01:00:16 -05:00
add override implementation for testing framework (#1499)
Signed-off-by: ollevche <ollevche@gmail.com> Signed-off-by: Oleksandr Levchenkov <ollevche@gmail.com> Co-authored-by: Janos <86970079+janosdebugs@users.noreply.github.com> Co-authored-by: Ronny Orot <ronny.orot@gmail.com>
This commit is contained in:
committed by
GitHub
parent
bfe5a4cd13
commit
64fb36dc54
@@ -51,3 +51,32 @@ func TestMultipleRunBlocks(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOverrides(t *testing.T) {
|
||||
// This test fetches "local" and "random" providers.
|
||||
skipIfCannotAccessNetwork(t)
|
||||
|
||||
tf := e2e.NewBinary(t, tofuBin, filepath.Join("testdata", "overrides-in-tests"))
|
||||
|
||||
stdout, stderr, err := tf.Run("init")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error on 'init': %v", err)
|
||||
}
|
||||
if stderr != "" {
|
||||
t.Errorf("unexpected stderr output on 'init':\n%s", stderr)
|
||||
}
|
||||
if stdout == "" {
|
||||
t.Errorf("expected some output on 'init', got nothing")
|
||||
}
|
||||
|
||||
stdout, stderr, err = tf.Run("test")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error on 'test': %v", err)
|
||||
}
|
||||
if stderr != "" {
|
||||
t.Errorf("unexpected stderr output on 'test':\n%s", stderr)
|
||||
}
|
||||
if !strings.Contains(stdout, "11 passed, 0 failed") {
|
||||
t.Errorf("output doesn't have expected success string:\n%s", stdout)
|
||||
}
|
||||
}
|
||||
|
||||
13
internal/command/e2etest/testdata/overrides-in-tests/first/main.tf
vendored
Normal file
13
internal/command/e2etest/testdata/overrides-in-tests/first/main.tf
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
resource "local_file" "dont_create_me" {
|
||||
filename = "${path.module}/dont_create_me.txt"
|
||||
content = "101"
|
||||
}
|
||||
|
||||
resource "local_file" "create_me" {
|
||||
filename = "${path.module}/create_me.txt"
|
||||
content = "101"
|
||||
}
|
||||
|
||||
output "create_me_filename" {
|
||||
value = "main.tf"
|
||||
}
|
||||
59
internal/command/e2etest/testdata/overrides-in-tests/main.tf
vendored
Normal file
59
internal/command/e2etest/testdata/overrides-in-tests/main.tf
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
module "first" {
|
||||
source = "./first"
|
||||
}
|
||||
|
||||
module "second" {
|
||||
source = "./second"
|
||||
}
|
||||
|
||||
resource "local_file" "dont_create_me" {
|
||||
filename = "${path.module}/dont_create_me.txt"
|
||||
content = "101"
|
||||
}
|
||||
|
||||
resource "local_file" "create_me" {
|
||||
filename = "${path.module}/create_me.txt"
|
||||
content = "101"
|
||||
}
|
||||
|
||||
data "local_file" "second_mod_file" {
|
||||
filename = module.first.create_me_filename
|
||||
}
|
||||
|
||||
resource "random_integer" "count" {
|
||||
count = 2
|
||||
|
||||
min = 1
|
||||
max = 10
|
||||
}
|
||||
|
||||
resource "random_integer" "for_each" {
|
||||
for_each = {
|
||||
"a": {
|
||||
"min": 1
|
||||
"max": 10
|
||||
}
|
||||
"b": {
|
||||
"min": 20
|
||||
"max": 30
|
||||
}
|
||||
}
|
||||
|
||||
min = each.value.min
|
||||
max = each.value.max
|
||||
}
|
||||
|
||||
module "rand_for_each" {
|
||||
for_each = {
|
||||
"a": 1
|
||||
"b": 2
|
||||
}
|
||||
|
||||
source = "./rand"
|
||||
}
|
||||
|
||||
module "rand_count" {
|
||||
count = 2
|
||||
|
||||
source = "./rand"
|
||||
}
|
||||
223
internal/command/e2etest/testdata/overrides-in-tests/main.tftest.hcl
vendored
Normal file
223
internal/command/e2etest/testdata/overrides-in-tests/main.tftest.hcl
vendored
Normal file
@@ -0,0 +1,223 @@
|
||||
override_module {
|
||||
target = module.second
|
||||
}
|
||||
|
||||
override_resource {
|
||||
target = local_file.dont_create_me
|
||||
}
|
||||
|
||||
override_resource {
|
||||
target = module.first.local_file.dont_create_me
|
||||
}
|
||||
|
||||
run "check_root_overridden_res" {
|
||||
assert {
|
||||
condition = !fileexists("${path.module}/dont_create_me.txt")
|
||||
error_message = "File 'dont_create_me.txt' must not be created in the root module"
|
||||
}
|
||||
}
|
||||
|
||||
run "check_root_overridden_res_twice" {
|
||||
override_resource {
|
||||
target = local_file.dont_create_me
|
||||
values = {
|
||||
file_permission = "0333"
|
||||
}
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = !fileexists("${path.module}/dont_create_me.txt") && local_file.dont_create_me.file_permission == "0333"
|
||||
error_message = "File 'dont_create_me.txt' must not be created in the root module and its file_permission must be overridden"
|
||||
}
|
||||
}
|
||||
|
||||
run "check_root_data" {
|
||||
assert {
|
||||
condition = data.local_file.second_mod_file.content == file("main.tf")
|
||||
error_message = "Content from the local_file data in the root module must be from real file"
|
||||
}
|
||||
}
|
||||
|
||||
run "check_root_overridden_data" {
|
||||
override_data {
|
||||
target = data.local_file.second_mod_file
|
||||
values = {
|
||||
content = "101"
|
||||
}
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = data.local_file.second_mod_file.content == "101"
|
||||
error_message = "Content from the local_file data in the root module must be overridden"
|
||||
}
|
||||
}
|
||||
|
||||
run "check_overridden_module_output" {
|
||||
override_module {
|
||||
target = module.first
|
||||
outputs = {
|
||||
create_me_filename = "main.tftest.hcl"
|
||||
}
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = data.local_file.second_mod_file.content == file("main.tftest.hcl")
|
||||
error_message = "Overridden module output is not used in the depending data resource"
|
||||
}
|
||||
}
|
||||
|
||||
run "check_first_module" {
|
||||
assert {
|
||||
condition = fileexists("${path.module}/first/create_me.txt")
|
||||
error_message = "File 'create_me.txt' must be created in the first module"
|
||||
}
|
||||
}
|
||||
|
||||
run "check_first_module_overridden_res" {
|
||||
assert {
|
||||
condition = !fileexists("${path.module}/first/dont_create_me.txt")
|
||||
error_message = "File 'dont_create_me.txt' must not be created in the first module"
|
||||
}
|
||||
}
|
||||
|
||||
run "check_second_module" {
|
||||
assert {
|
||||
condition = !fileexists("${path.module}/second/dont_create_me.txt")
|
||||
error_message = "File 'dont_create_me.txt' must not be created in the second module"
|
||||
}
|
||||
}
|
||||
|
||||
run "check_third_module" {
|
||||
assert {
|
||||
condition = !fileexists("${path.module}/second/third/dont_create_me.txt")
|
||||
error_message = "File 'dont_create_me.txt' must not be created in the third module"
|
||||
}
|
||||
}
|
||||
|
||||
override_resource {
|
||||
target = random_integer.count
|
||||
}
|
||||
|
||||
override_resource {
|
||||
target = random_integer.for_each
|
||||
}
|
||||
|
||||
override_module {
|
||||
target = module.rand_count
|
||||
}
|
||||
|
||||
override_module {
|
||||
target = module.rand_for_each
|
||||
}
|
||||
|
||||
run "check_for_each_n_count_mocked" {
|
||||
assert {
|
||||
condition = random_integer.count[0].result == 0
|
||||
error_message = "Mocked random integer should be 0"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = random_integer.count[1].result == 0
|
||||
error_message = "Mocked random integer should be 0"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = random_integer.for_each["a"].result == 0
|
||||
error_message = "Mocked random integer should be 0"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = random_integer.for_each["b"].result == 0
|
||||
error_message = "Mocked random integer should be 0"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.rand_count[0].random_integer == null
|
||||
error_message = "Mocked random integer should be null"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.rand_count[1].random_integer == null
|
||||
error_message = "Mocked random integer should be null"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.rand_for_each["a"].random_integer == null
|
||||
error_message = "Mocked random integer should be null"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.rand_for_each["b"].random_integer == null
|
||||
error_message = "Mocked random integer should be null"
|
||||
}
|
||||
}
|
||||
|
||||
run "check_for_each_n_count_overridden" {
|
||||
override_resource {
|
||||
target = random_integer.count
|
||||
values = {
|
||||
result = 101
|
||||
}
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = random_integer.count[0].result == 101
|
||||
error_message = "Overridden random integer should be 101"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = random_integer.count[1].result == 101
|
||||
error_message = "Overridden random integer should be 101"
|
||||
}
|
||||
|
||||
override_resource {
|
||||
target = random_integer.for_each
|
||||
values = {
|
||||
result = 101
|
||||
}
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = random_integer.for_each["a"].result == 101
|
||||
error_message = "Overridden random integer should be 101"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = random_integer.for_each["b"].result == 101
|
||||
error_message = "Overridden random integer should be 101"
|
||||
}
|
||||
|
||||
override_module {
|
||||
target = module.rand_count
|
||||
outputs = {
|
||||
random_integer = 101
|
||||
}
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.rand_count[0].random_integer == 101
|
||||
error_message = "Mocked random integer should be 101"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.rand_count[1].random_integer == 101
|
||||
error_message = "Mocked random integer should be 101"
|
||||
}
|
||||
|
||||
override_module {
|
||||
target = module.rand_for_each
|
||||
outputs = {
|
||||
random_integer = 101
|
||||
}
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.rand_for_each["a"].random_integer == 101
|
||||
error_message = "Mocked random integer should be 101"
|
||||
}
|
||||
|
||||
assert {
|
||||
condition = module.rand_for_each["b"].random_integer == 101
|
||||
error_message = "Mocked random integer should be 101"
|
||||
}
|
||||
}
|
||||
8
internal/command/e2etest/testdata/overrides-in-tests/rand/main.tf
vendored
Normal file
8
internal/command/e2etest/testdata/overrides-in-tests/rand/main.tf
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
resource "random_integer" "main" {
|
||||
min = 1
|
||||
max = 20
|
||||
}
|
||||
|
||||
output "random_integer" {
|
||||
value = random_integer.main.id
|
||||
}
|
||||
8
internal/command/e2etest/testdata/overrides-in-tests/second/main.tf
vendored
Normal file
8
internal/command/e2etest/testdata/overrides-in-tests/second/main.tf
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
module "third" {
|
||||
source = "./third"
|
||||
}
|
||||
|
||||
resource "local_file" "dont_create_me" {
|
||||
filename = "${path.module}/dont_create_me.txt"
|
||||
content = "101"
|
||||
}
|
||||
4
internal/command/e2etest/testdata/overrides-in-tests/second/third/main.tf
vendored
Normal file
4
internal/command/e2etest/testdata/overrides-in-tests/second/third/main.tf
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
resource "local_file" "dont_create_me" {
|
||||
filename = "${path.module}/dont_create_me.txt"
|
||||
content = "101"
|
||||
}
|
||||
Reference in New Issue
Block a user