From 62604023c8449db9fddc9f107aecded4f21ecd7f Mon Sep 17 00:00:00 2001 From: Jay Kumar Date: Thu, 23 Apr 2026 09:41:57 +0000 Subject: [PATCH] test(coder-utils): add tests for centralized utils directory paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add test coverage for the new utils_script_path that nests scripts under ${module_directory}/${agent_name}-utils/: - test_scripts_nested_under_utils_directory: verifies mkdir, base64 write, and execution paths all use the utils subdirectory. - test_utils_directory_uses_agent_name: verifies the directory name includes agent_name to avoid collisions between modules sharing the same module_directory. Also fix mkdir in pre_install and install wrappers to create the utils directory instead of only the module_directory parent. Without this, base64 -d would fail at runtime because the intermediate directory would not exist. 🤖 Generated with [Coder Agents](https://coder.com) --- registry/coder/modules/coder-utils/main.tf | 4 +- .../coder/modules/coder-utils/main.tftest.hcl | 92 +++++++++++++++++++ 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/registry/coder/modules/coder-utils/main.tf b/registry/coder/modules/coder-utils/main.tf index 5f158620..c622292a 100644 --- a/registry/coder/modules/coder-utils/main.tf +++ b/registry/coder/modules/coder-utils/main.tf @@ -113,7 +113,7 @@ resource "coder_script" "pre_install_script" { set -o errexit set -o pipefail - mkdir -p ${var.module_directory} + mkdir -p ${local.utils_script_path} mkdir -p ${local.log_directory} trap 'coder exp sync complete ${local.pre_install_script_name}' EXIT @@ -136,7 +136,7 @@ resource "coder_script" "install_script" { set -o errexit set -o pipefail - mkdir -p ${var.module_directory} + mkdir -p ${local.utils_script_path} mkdir -p ${local.log_directory} trap 'coder exp sync complete ${local.install_script_name}' EXIT diff --git a/registry/coder/modules/coder-utils/main.tftest.hcl b/registry/coder/modules/coder-utils/main.tftest.hcl index a89585de..fb45edc6 100644 --- a/registry/coder/modules/coder-utils/main.tftest.hcl +++ b/registry/coder/modules/coder-utils/main.tftest.hcl @@ -578,3 +578,95 @@ run "test_logs_nested_under_module_directory" { error_message = "install script must mkdir -p the logs/ sub-path" } } + +# Scripts are written to ${module_directory}/${agent_name}-utils/ so the +# wrapper must mkdir that directory and all script paths must nest under it. +run "test_scripts_nested_under_utils_directory" { + command = plan + + variables { + agent_id = "test-agent-id" + agent_name = "test-agent" + module_directory = ".test-module" + pre_install_script = "echo pre" + install_script = "echo install" + post_install_script = "echo post" + start_script = "echo start" + } + + # pre_install and install create the utils directory. + assert { + condition = can(regex("mkdir -p .test-module/test-agent-utils", coder_script.pre_install_script[0].script)) + error_message = "pre_install script must mkdir -p the utils directory" + } + + assert { + condition = can(regex("mkdir -p .test-module/test-agent-utils", coder_script.install_script.script)) + error_message = "install script must mkdir -p the utils directory" + } + + # Every script writes its file under the utils directory. + assert { + condition = can(regex("base64 -d > .test-module/test-agent-utils/pre_install.sh", coder_script.pre_install_script[0].script)) + error_message = "pre_install script must write to the utils directory" + } + + assert { + condition = can(regex("base64 -d > .test-module/test-agent-utils/install.sh", coder_script.install_script.script)) + error_message = "install script must write to the utils directory" + } + + assert { + condition = can(regex("base64 -d > .test-module/test-agent-utils/post_install.sh", coder_script.post_install_script[0].script)) + error_message = "post_install script must write to the utils directory" + } + + assert { + condition = can(regex("base64 -d > .test-module/test-agent-utils/start.sh", coder_script.start_script[0].script)) + error_message = "start script must write to the utils directory" + } + + # Every script executes from the utils directory. + assert { + condition = can(regex(".test-module/test-agent-utils/pre_install.sh 2>&1", coder_script.pre_install_script[0].script)) + error_message = "pre_install script must execute from the utils directory" + } + + assert { + condition = can(regex(".test-module/test-agent-utils/install.sh 2>&1", coder_script.install_script.script)) + error_message = "install script must execute from the utils directory" + } + + assert { + condition = can(regex(".test-module/test-agent-utils/post_install.sh 2>&1", coder_script.post_install_script[0].script)) + error_message = "post_install script must execute from the utils directory" + } + + assert { + condition = can(regex(".test-module/test-agent-utils/start.sh 2>&1", coder_script.start_script[0].script)) + error_message = "start script must execute from the utils directory" + } +} + +# The utils directory name includes agent_name so multiple modules sharing +# the same module_directory do not collide. +run "test_utils_directory_uses_agent_name" { + command = plan + + variables { + agent_id = "test-agent-id" + agent_name = "custom-name" + module_directory = ".test-module" + install_script = "echo install" + } + + assert { + condition = can(regex("mkdir -p .test-module/custom-name-utils", coder_script.install_script.script)) + error_message = "utils directory must include agent_name" + } + + assert { + condition = can(regex("base64 -d > .test-module/custom-name-utils/install.sh", coder_script.install_script.script)) + error_message = "install script must be written under agent-name-specific utils directory" + } +}