From 81a386bf5eb20be7e28ee0fcd24d51d21113de55 Mon Sep 17 00:00:00 2001 From: yangci Date: Thu, 30 Jan 2025 14:20:59 -0500 Subject: [PATCH] fix: cron validator regex escape characters (#45) ![image](https://github.com/user-attachments/assets/4c01a034-55b0-4c32-b63b-d8695ead6f88) I'm doing a cron expression for `0 */6 * * *` This condition is failing if cron expression contains `*/{number}`. It's this escape character. Could fix it via the second statement where it's escaped. But thinking that this condition is too strict, wondering if it can just be to check if it has 5 expressions. Or if we even need this validator? since validating cron expressions looks messy. ## Summary by CodeRabbit - **Bug Fixes** - Updated drift detection schedule validation to support more flexible cron schedule formats. - Improved input validation for scheduling configurations with enhanced regex pattern. - **Style** - Adjusted whitespace and alignment for variable assignments and conditions in test files for improved readability. --- main.tf | 2 +- tests/main.tftest.hcl | 32 ++++++++++++++++---------------- tests/single-instance.tftest.hcl | 30 +++++++++++++++--------------- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/main.tf b/main.tf index 89c047e..3e92f87 100644 --- a/main.tf +++ b/main.tf @@ -376,7 +376,7 @@ resource "spacelift_drift_detection" "default" { lifecycle { precondition { - condition = alltrue([for schedule in try(local.stack_configs[each.key].drift_detection_schedule, var.drift_detection_schedule) : can(regex("^([0-9,\\-\\*]+\\s+){4}[0-9,\\-\\*]+$", schedule))]) + condition = alltrue([for schedule in try(local.stack_configs[each.key].drift_detection_schedule, var.drift_detection_schedule) : can(regex("^([0-9,\\-*/]+\\s+){4}[0-9,\\-*/]+$", schedule))]) error_message = "Invalid cron schedule format for drift detection" } } diff --git a/tests/main.tftest.hcl b/tests/main.tftest.hcl index 0a5af45..0e1b816 100644 --- a/tests/main.tftest.hcl +++ b/tests/main.tftest.hcl @@ -1,12 +1,12 @@ variables { - root_modules_path = "./tests/fixtures/multi-instance" + root_modules_path = "./tests/fixtures/multi-instance" common_config_file = "common.yaml" github_enterprise = { namespace = "masterpointio" } - repository = "terraform-spacelift-automation" + repository = "terraform-spacelift-automation" all_root_modules_enabled = true - aws_integration_enabled = false + aws_integration_enabled = false before_init = [ "echo 'Hello'" ] @@ -20,7 +20,7 @@ run "test_labels_are_created_correctly" { command = plan assert { - condition = contains(local.labels["root-module-a-test"], "nobackend") + condition = contains(local.labels["root-module-a-test"], "nobackend") error_message = "Global labels were not created correctly: ${jsonencode(local.labels)}" } } @@ -30,7 +30,7 @@ run "test_root_module_fileset_collects_all_root_modules" { command = plan assert { - condition = contains(local._all_root_modules, "root-module-a") + condition = contains(local._all_root_modules, "root-module-a") error_message = "Root module fileset was not created correctly: ${jsonencode(local._all_root_modules)}" } } @@ -40,7 +40,7 @@ run "test_common_labels_are_appended_to_stack_labels" { command = plan assert { - condition = contains(local.configs["root-module-a-test"].stack_settings.labels, "common_label") && contains(local.configs["root-module-a-test"].stack_settings.labels, "test_label") + condition = contains(local.configs["root-module-a-test"].stack_settings.labels, "common_label") && contains(local.configs["root-module-a-test"].stack_settings.labels, "test_label") error_message = "Common labels were not merged correctly: ${jsonencode(local.configs)}" } } @@ -50,7 +50,7 @@ run "test_stacks_include_expected" { command = plan assert { - condition = contains(local.stacks, "root-module-a-test") + condition = contains(local.stacks, "root-module-a-test") error_message = "Stack names were not created correctly: ${jsonencode(local.stacks)}" } } @@ -70,7 +70,7 @@ run "test_folder_labels_are_correct_format" { command = plan assert { - condition = contains(local._folder_labels["root-module-a-test"], "folder:root-module-a/test") + condition = contains(local._folder_labels["root-module-a-test"], "folder:root-module-a/test") error_message = "Folder label was not created correctly for root-module-a: ${jsonencode(local._folder_labels)}" } } @@ -80,7 +80,7 @@ run "test_workspace_when_default_tf_workspace_enabled_is_false" { command = plan assert { - condition = local.configs["root-module-a-test"].terraform_workspace == "test" + condition = local.configs["root-module-a-test"].terraform_workspace == "test" error_message = "Terraform workspace was not set correctly when default_tf_workspace_enabled is false: ${jsonencode(local.configs)}" } } @@ -90,7 +90,7 @@ run "test_workspace_when_default_tf_workspace_enabled" { command = plan assert { - condition = local.configs["root-module-a-default-example"].terraform_workspace == "default" + condition = local.configs["root-module-a-default-example"].terraform_workspace == "default" error_message = "Default Terraform workspace was not used correctly: ${jsonencode(local.configs)}" } } @@ -100,7 +100,7 @@ run "test_administrative_label_is_added_to_stack" { command = plan assert { - condition = contains(local.labels["root-module-a-default-example"], "administrative") + condition = contains(local.labels["root-module-a-default-example"], "administrative") error_message = "Administrative label was not added to the stack: ${jsonencode(local.labels)}" } } @@ -110,7 +110,7 @@ run "test_administrative_label_is_not_added_to_stack_when_not_administrative" { command = plan assert { - condition = !contains(local.labels["root-module-a-test"], "administrative") + condition = !contains(local.labels["root-module-a-test"], "administrative") error_message = "Administrative label was added to the stack when it should not have been: ${jsonencode(local.labels)}" } } @@ -120,7 +120,7 @@ run "test_depends_on_label_is_added_to_stack" { command = plan assert { - condition = contains(local.labels["root-module-a-test"], "depends-on:spacelift-automation-default") + condition = contains(local.labels["root-module-a-test"], "depends-on:spacelift-automation-default") error_message = "Depends-on label was not added to the stack: ${jsonencode(local.labels)}" } } @@ -130,7 +130,7 @@ run "test_before_init_excludes_the_expected_tfvars_copy_command_when_tfvars_are_ command = plan assert { - condition = !contains(local.before_init["root-module-a-default-example"], "cp tfvars/default-example.tfvars spacelift.auto.tfvars") + condition = !contains(local.before_init["root-module-a-default-example"], "cp tfvars/default-example.tfvars spacelift.auto.tfvars") error_message = "Before_init was not created correctly: ${jsonencode(local.before_init)}" } } @@ -140,7 +140,7 @@ run "test_before_init_includes_the_expected_tfvars_copy_command" { command = plan assert { - condition = contains(local.before_init["root-module-a-test"], "cp tfvars/test.tfvars spacelift.auto.tfvars") + condition = contains(local.before_init["root-module-a-test"], "cp tfvars/test.tfvars spacelift.auto.tfvars") error_message = "Before_init was not created correctly: ${jsonencode(local.before_init)}" } } @@ -150,7 +150,7 @@ run "test_before_init_includes_the_default_before_init_and_stack_before_init" { command = plan assert { - condition = contains(local.before_init["root-module-a-default-example"], "echo 'Hello'") && contains(local.before_init["root-module-a-default-example"], "echo 'World'") + condition = contains(local.before_init["root-module-a-default-example"], "echo 'Hello'") && contains(local.before_init["root-module-a-default-example"], "echo 'World'") error_message = "Before_init was not created correctly: ${jsonencode(local.before_init)}" } } diff --git a/tests/single-instance.tftest.hcl b/tests/single-instance.tftest.hcl index 75a0d1a..37e8fb1 100644 --- a/tests/single-instance.tftest.hcl +++ b/tests/single-instance.tftest.hcl @@ -3,9 +3,9 @@ variables { github_enterprise = { namespace = "masterpointio" } - repository = "terraform-spacelift-automation" + repository = "terraform-spacelift-automation" all_root_modules_enabled = true - aws_integration_enabled = false + aws_integration_enabled = false before_init = [ "echo 'Hello'" ] @@ -18,7 +18,7 @@ run "test_single_instance_root_module_fileset_collects_all_root_modules" { command = plan assert { - condition = contains(local._all_root_modules, "root-module-a") + condition = contains(local._all_root_modules, "root-module-a") error_message = "Root module fileset was not created correctly: ${jsonencode(local._all_root_modules)}" } } @@ -28,7 +28,7 @@ run "test_single_instance_stacks_include_expected" { command = plan assert { - condition = contains(local.stacks, "root-module-a") + condition = contains(local.stacks, "root-module-a") error_message = "Stack names were not created correctly: ${jsonencode(local.stacks)}" } } @@ -37,7 +37,7 @@ run "test_single_instance_stacks_only_include_default_stack" { command = plan assert { - condition = length(local._root_module_yaml_decoded["root-module-a"]) == 1 && local._root_module_yaml_decoded["root-module-a"]["default"] != null + condition = length(local._root_module_yaml_decoded["root-module-a"]) == 1 && local._root_module_yaml_decoded["root-module-a"]["default"] != null error_message = "_root_module_yaml_decoded is not a single instance: ${jsonencode(local._root_module_yaml_decoded)}" } } @@ -46,7 +46,7 @@ run "test_single_instance_stack_configs_stack_name_is_correct" { command = plan assert { - condition = length(local._root_module_stack_configs) == 2 && local._root_module_stack_configs["root-module-a"] != null && local._root_module_stack_configs["root-module-b"] != null + condition = length(local._root_module_stack_configs) == 2 && local._root_module_stack_configs["root-module-a"] != null && local._root_module_stack_configs["root-module-b"] != null error_message = "_root_module_stack_configs is not expected structure: ${jsonencode(local._root_module_stack_configs)}" } } @@ -55,7 +55,7 @@ run "test_single_instance_stack_configs_use_default_tf_workspace" { command = plan assert { - condition = local._root_module_stack_configs["root-module-a"].terraform_workspace == "default" + condition = local._root_module_stack_configs["root-module-a"].terraform_workspace == "default" error_message = "terraform_workspace is not set to default: ${jsonencode(local._root_module_stack_configs)}" } } @@ -64,7 +64,7 @@ run "test_single_instance_stack_configs_project_root_is_correct" { command = plan assert { - condition = local._root_module_stack_configs["root-module-a"].project_root == "${var.root_modules_path}/root-module-a" + condition = local._root_module_stack_configs["root-module-a"].project_root == "${var.root_modules_path}/root-module-a" error_message = "project_root is not correct for root-module-a: ${jsonencode(local._root_module_stack_configs)}" } } @@ -74,7 +74,7 @@ run "test_single_instance_stack_configs_custom_project_root_is_used_when_specifi assert { # `tests/fixtures/single-instance/root-module-b/stack.yaml` specifies `project_root` as "" to indicate root directory of project as source. - condition = local._root_module_stack_configs["root-module-b"].project_root == "" + condition = local._root_module_stack_configs["root-module-b"].project_root == "" error_message = "Custom project_root not used: ${jsonencode(local._root_module_stack_configs)}" } } @@ -84,7 +84,7 @@ run "test_single_instance_administrative_label_is_not_added_to_stack_when_not_ad command = plan assert { - condition = !contains(local.labels["root-module-a"], "administrative") + condition = !contains(local.labels["root-module-a"], "administrative") error_message = "Administrative label was added to the stack when it should not have been: ${jsonencode(local.labels)}" } } @@ -94,7 +94,7 @@ run "test_single_instance_depends_on_label_is_added_to_stack" { command = plan assert { - condition = contains(local.labels["root-module-a"], "depends-on:spacelift-automation-default") + condition = contains(local.labels["root-module-a"], "depends-on:spacelift-automation-default") error_message = "Depends-on label was not added to the stack: ${jsonencode(local.labels)}" } } @@ -104,7 +104,7 @@ run "test_single_instance_folder_label_is_added_to_stack_and_doesnt_include_work command = plan assert { - condition = contains(local.labels["root-module-a"], "folder:root-module-a") + condition = contains(local.labels["root-module-a"], "folder:root-module-a") error_message = "Folder label was not added to the stack: ${jsonencode(local.labels)}" } } @@ -114,7 +114,7 @@ run "test_single_instance_stack_yaml_labels_are_included_in_stack_labels" { command = plan assert { - condition = contains(local.labels["root-module-a"], "stack_label") + condition = contains(local.labels["root-module-a"], "stack_label") error_message = "Stack.yaml labels were not included in the stack labels: ${jsonencode(local.labels)}" } } @@ -124,7 +124,7 @@ run "test_single_instance_before_init_steps_are_added_to_stack" { command = plan assert { - condition = contains(local.before_init["root-module-a"], "echo 'Hello'") && contains(local.before_init["root-module-a"], "echo 'World'") + condition = contains(local.before_init["root-module-a"], "echo 'Hello'") && contains(local.before_init["root-module-a"], "echo 'World'") error_message = "Before_init steps were not added to the stack: ${jsonencode(local.before_init)}" } } @@ -134,7 +134,7 @@ run "test_single_instance_before_init_tfvar_cp_command_is_not_added_to_stack" { command = plan assert { - condition = !contains(local.before_init["root-module-a"], "cp tfvars/") + condition = !contains(local.before_init["root-module-a"], "cp tfvars/") error_message = "Before_init tfvar cp command was added to the stack: ${jsonencode(local.before_init)}" } }