test(coder-utils): add tests for centralized utils directory paths

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)
This commit is contained in:
Jay Kumar 2026-04-23 09:41:57 +00:00
parent d475551deb
commit 62604023c8
2 changed files with 94 additions and 2 deletions

View File

@ -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

View File

@ -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"
}
}