## Description The Agent Helper module is a building block for modules that need to run multiple scripts in a specific order. It uses `coder exp sync` for dependency management and is designed for orchestrating pre-install, install, post-install, and start scripts. ## Type of Change - [x] New module - [ ] New template - [ ] Bug fix - [ ] Feature/enhancement - [ ] Documentation - [ ] Other ## Module Information <!-- Delete this section if not applicable --> **Path:** `registry/coder/modules/agent-helper` **New version:** `v1.0.0` **Breaking change:** [x] Yes [ ] No ## Testing & Validation - [x] Tests pass (`bun test`) - [x] Code formatted (`bun fmt`) - [x] Changes tested locally ## Related Issues Closes: https://github.com/coder/registry/issues/696 Closes: https://github.com/coder/registry/issues/698 --------- Co-authored-by: DevCats <christofer@coder.com>
272 lines
8.4 KiB
HCL
272 lines
8.4 KiB
HCL
# Test for agent-helper module
|
|
|
|
# Test with all scripts provided
|
|
run "test_with_all_scripts" {
|
|
command = plan
|
|
|
|
variables {
|
|
agent_id = "test-agent-id"
|
|
agent_name = "test-agent"
|
|
module_dir_name = ".test-module"
|
|
pre_install_script = "echo 'pre-install'"
|
|
install_script = "echo 'install'"
|
|
post_install_script = "echo 'post-install'"
|
|
start_script = "echo 'start'"
|
|
}
|
|
|
|
# Verify pre_install_script is created when provided
|
|
assert {
|
|
condition = length(coder_script.pre_install_script) == 1
|
|
error_message = "Pre-install script should be created when pre_install_script is provided"
|
|
}
|
|
|
|
assert {
|
|
condition = coder_script.pre_install_script[0].agent_id == "test-agent-id"
|
|
error_message = "Pre-install script agent ID should match input"
|
|
}
|
|
|
|
assert {
|
|
condition = coder_script.pre_install_script[0].display_name == "Pre-Install Script"
|
|
error_message = "Pre-install script should have correct display name"
|
|
}
|
|
|
|
assert {
|
|
condition = coder_script.pre_install_script[0].run_on_start == true
|
|
error_message = "Pre-install script should run on start"
|
|
}
|
|
|
|
# Verify install_script is created
|
|
assert {
|
|
condition = coder_script.install_script.agent_id == "test-agent-id"
|
|
error_message = "Install script agent ID should match input"
|
|
}
|
|
|
|
assert {
|
|
condition = coder_script.install_script.display_name == "Install Script"
|
|
error_message = "Install script should have correct display name"
|
|
}
|
|
|
|
assert {
|
|
condition = coder_script.install_script.run_on_start == true
|
|
error_message = "Install script should run on start"
|
|
}
|
|
|
|
# Verify post_install_script is created when provided
|
|
assert {
|
|
condition = length(coder_script.post_install_script) == 1
|
|
error_message = "Post-install script should be created when post_install_script is provided"
|
|
}
|
|
|
|
assert {
|
|
condition = coder_script.post_install_script[0].agent_id == "test-agent-id"
|
|
error_message = "Post-install script agent ID should match input"
|
|
}
|
|
|
|
assert {
|
|
condition = coder_script.post_install_script[0].display_name == "Post-Install Script"
|
|
error_message = "Post-install script should have correct display name"
|
|
}
|
|
|
|
assert {
|
|
condition = coder_script.post_install_script[0].run_on_start == true
|
|
error_message = "Post-install script should run on start"
|
|
}
|
|
|
|
# Verify start_script is created
|
|
assert {
|
|
condition = coder_script.start_script.agent_id == "test-agent-id"
|
|
error_message = "Start script agent ID should match input"
|
|
}
|
|
|
|
assert {
|
|
condition = coder_script.start_script.display_name == "Start Script"
|
|
error_message = "Start script should have correct display name"
|
|
}
|
|
|
|
assert {
|
|
condition = coder_script.start_script.run_on_start == true
|
|
error_message = "Start script should run on start"
|
|
}
|
|
|
|
# Verify outputs for script names
|
|
assert {
|
|
condition = output.pre_install_script_name == "test-agent-pre_install_script"
|
|
error_message = "Pre-install script name output should be correctly formatted"
|
|
}
|
|
|
|
assert {
|
|
condition = output.install_script_name == "test-agent-install_script"
|
|
error_message = "Install script name output should be correctly formatted"
|
|
}
|
|
|
|
assert {
|
|
condition = output.post_install_script_name == "test-agent-post_install_script"
|
|
error_message = "Post-install script name output should be correctly formatted"
|
|
}
|
|
|
|
assert {
|
|
condition = output.start_script_name == "test-agent-start_script"
|
|
error_message = "Start script name output should be correctly formatted"
|
|
}
|
|
}
|
|
|
|
# Test with only required scripts (no pre/post install)
|
|
run "test_without_optional_scripts" {
|
|
command = plan
|
|
|
|
variables {
|
|
agent_id = "test-agent-id"
|
|
agent_name = "test-agent"
|
|
module_dir_name = ".test-module"
|
|
install_script = "echo 'install'"
|
|
start_script = "echo 'start'"
|
|
}
|
|
|
|
# Verify pre_install_script is NOT created when not provided
|
|
assert {
|
|
condition = length(coder_script.pre_install_script) == 0
|
|
error_message = "Pre-install script should not be created when pre_install_script is null"
|
|
}
|
|
|
|
# Verify post_install_script is NOT created when not provided
|
|
assert {
|
|
condition = length(coder_script.post_install_script) == 0
|
|
error_message = "Post-install script should not be created when post_install_script is null"
|
|
}
|
|
|
|
# Verify required scripts are still created
|
|
assert {
|
|
condition = coder_script.install_script.agent_id == "test-agent-id"
|
|
error_message = "Install script should be created"
|
|
}
|
|
|
|
assert {
|
|
condition = coder_script.start_script.agent_id == "test-agent-id"
|
|
error_message = "Start script should be created"
|
|
}
|
|
|
|
# Verify outputs
|
|
assert {
|
|
condition = output.pre_install_script_name == "test-agent-pre_install_script"
|
|
error_message = "Pre-install script name output should be generated even when script is not created"
|
|
}
|
|
|
|
assert {
|
|
condition = output.install_script_name == "test-agent-install_script"
|
|
error_message = "Install script name output should be correctly formatted"
|
|
}
|
|
|
|
assert {
|
|
condition = output.post_install_script_name == "test-agent-post_install_script"
|
|
error_message = "Post-install script name output should be generated even when script is not created"
|
|
}
|
|
|
|
assert {
|
|
condition = output.start_script_name == "test-agent-start_script"
|
|
error_message = "Start script name output should be correctly formatted"
|
|
}
|
|
}
|
|
|
|
# Test with mock data sources
|
|
run "test_with_mock_data" {
|
|
command = plan
|
|
|
|
variables {
|
|
agent_id = "mock-agent"
|
|
agent_name = "mock-agent"
|
|
module_dir_name = ".mock-module"
|
|
install_script = "echo 'install'"
|
|
start_script = "echo 'start'"
|
|
}
|
|
|
|
# Mock the data sources for testing
|
|
override_data {
|
|
target = data.coder_workspace.me
|
|
values = {
|
|
id = "test-workspace-id"
|
|
name = "test-workspace"
|
|
owner = "test-owner"
|
|
owner_id = "test-owner-id"
|
|
template_id = "test-template-id"
|
|
template_name = "test-template"
|
|
access_url = "https://coder.example.com"
|
|
start_count = 1
|
|
transition = "start"
|
|
}
|
|
}
|
|
|
|
override_data {
|
|
target = data.coder_workspace_owner.me
|
|
values = {
|
|
id = "test-owner-id"
|
|
email = "test@example.com"
|
|
name = "Test User"
|
|
session_token = "mock-token"
|
|
}
|
|
}
|
|
|
|
override_data {
|
|
target = data.coder_task.me
|
|
values = {
|
|
id = "test-task-id"
|
|
}
|
|
}
|
|
|
|
# Verify scripts are created with mocked data
|
|
assert {
|
|
condition = coder_script.install_script.agent_id == "mock-agent"
|
|
error_message = "Install script should use the mocked agent ID"
|
|
}
|
|
|
|
assert {
|
|
condition = coder_script.start_script.agent_id == "mock-agent"
|
|
error_message = "Start script should use the mocked agent ID"
|
|
}
|
|
}
|
|
|
|
# Test script naming with custom agent_name
|
|
run "test_script_naming" {
|
|
command = plan
|
|
|
|
variables {
|
|
agent_id = "test-agent"
|
|
agent_name = "custom-name"
|
|
module_dir_name = ".test-module"
|
|
install_script = "echo 'install'"
|
|
start_script = "echo 'start'"
|
|
}
|
|
|
|
# Verify script names are constructed correctly
|
|
# The script should contain references to custom-name-* in the sync commands
|
|
assert {
|
|
condition = can(regex("custom-name-install_script", coder_script.install_script.script))
|
|
error_message = "Install script should use custom agent_name in sync commands"
|
|
}
|
|
|
|
assert {
|
|
condition = can(regex("custom-name-start_script", coder_script.start_script.script))
|
|
error_message = "Start script should use custom agent_name in sync commands"
|
|
}
|
|
|
|
# Verify outputs use custom agent_name
|
|
assert {
|
|
condition = output.pre_install_script_name == "custom-name-pre_install_script"
|
|
error_message = "Pre-install script name output should use custom agent_name"
|
|
}
|
|
|
|
assert {
|
|
condition = output.install_script_name == "custom-name-install_script"
|
|
error_message = "Install script name output should use custom agent_name"
|
|
}
|
|
|
|
assert {
|
|
condition = output.post_install_script_name == "custom-name-post_install_script"
|
|
error_message = "Post-install script name output should use custom agent_name"
|
|
}
|
|
|
|
assert {
|
|
condition = output.start_script_name == "custom-name-start_script"
|
|
error_message = "Start script name output should use custom agent_name"
|
|
}
|
|
}
|