Skip to content

Commit

Permalink
Enhance variable validation for dependency between `hotpatching_enabl…
Browse files Browse the repository at this point in the history
…ed` and `bypass_platform_safety_checks_on_user_schedule_enabled` (#43)

* Add unit tests for all scenarios

Signed-off-by: Roman Schwarz <[email protected]>

* Fix variable validation

Signed-off-by: Roman Schwarz <[email protected]>

---------

Signed-off-by: Roman Schwarz <[email protected]>
  • Loading branch information
rswrz authored Nov 19, 2024
1 parent b00ebf7 commit 5d31502
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
80 changes: 73 additions & 7 deletions tests/local/input_enable_hotpatching.tftest.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,91 @@ mock_provider "tls" { source = "tests/local/mocks" }
variables {
# Unset default, set in variables.auto.tfvars
image = true
}

run "hotpatching_and_bypass_platform_security_checks_disabled_should_succeed_on_windows" {
command = plan

# Set default for this test file
hotpatching_enabled = true
variables {
image = "Win2022Datacenter"
hotpatching_enabled = false
bypass_platform_safety_checks_on_user_schedule_enabled = false
}
}

run "hotpatching_enabled_and_bypass_platform_security_checks_disabled_should_succeed_on_windows" {
command = plan

variables {
image = "Win2022Datacenter"
hotpatching_enabled = true
bypass_platform_safety_checks_on_user_schedule_enabled = false
}
}

run "should_use_hotpatching_enabled_on_windows" {
run "hotpatching_disabled_and_bypass_platform_security_checks_enabled_should_succeed_on_windows" {
command = plan

variables {
image = "MicrosoftWindowsServer:WindowsServer:2022-datacenter-azure-edition-hotpatch:latest"
image = "Win2022Datacenter"
hotpatching_enabled = false
bypass_platform_safety_checks_on_user_schedule_enabled = true
}
}

run "hotpatching_enabled_should_fail_on_linux" {
run "hotpatching_and_bypass_platform_security_checks_enabled_should_fail_on_windows" {
command = plan

variables {
image = "Ubuntu2204"
image = "Win2022Datacenter"
hotpatching_enabled = true
bypass_platform_safety_checks_on_user_schedule_enabled = true
}

expect_failures = [var.hotpatching_enabled]
}
}


run "hotpatching_and_bypass_platform_security_checks_disabled_should_succeed_on_linux" {
command = plan

variables {
image = "Ubuntu2204"
hotpatching_enabled = false
bypass_platform_safety_checks_on_user_schedule_enabled = false
}
}

run "hotpatching_enabled_and_bypass_platform_security_checks_disabled_should_fail_on_linux" {
command = plan

variables {
image = "Ubuntu2204"
hotpatching_enabled = true
bypass_platform_safety_checks_on_user_schedule_enabled = false
}

expect_failures = [var.hotpatching_enabled]
}

run "hotpatching_disabled_and_bypass_platform_security_checks_enabled_should_succeed_on_linux" {
command = plan

variables {
image = "Ubuntu2204"
hotpatching_enabled = false
bypass_platform_safety_checks_on_user_schedule_enabled = true
}
}

run "hotpatching_and_bypass_platform_security_checks_enabled_should_fail_on_linux" {
command = plan

variables {
image = "Ubuntu2204"
hotpatching_enabled = true
bypass_platform_safety_checks_on_user_schedule_enabled = true
}

expect_failures = [var.hotpatching_enabled]
}
2 changes: 1 addition & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ variable "hotpatching_enabled" {
}

validation {
condition = var.hotpatching_enabled == true ? true : var.bypass_platform_safety_checks_on_user_schedule_enabled
condition = (var.hotpatching_enabled && !var.bypass_platform_safety_checks_on_user_schedule_enabled) || (!var.hotpatching_enabled && var.bypass_platform_safety_checks_on_user_schedule_enabled) || (!var.hotpatching_enabled && !var.bypass_platform_safety_checks_on_user_schedule_enabled)
error_message = "Only one of the following options can be set to true: either bypass_platform_safety_checks_on_user_schedule_enabled or hotpatching_enabled."
}
}
Expand Down

0 comments on commit 5d31502

Please sign in to comment.