Files
coder/codersdk/templates_test.go
T
Steven Masley 940f988cfb feat: add deployment-level option to disable Terraform module caching (#29187)
<!-- Generated by Coder Agents on behalf of @Emyrk. -->

<img width="762" height="239" alt="Screenshot From 2026-09-11 17-09-41"
src="https://github.com/user-attachments/assets/9f31fd1e-4cb2-47ab-8d37-4073df9f0a7c"
/>


Closes #22834.

Adds `CODER_PROVISIONER_DISABLE_MODULE_CACHE` so an admin can disable
Terraform module caching for every template at once instead of toggling
~100 templates by hand.

- The deployment setting wins. Templates cannot opt back in, and the
per-template toggle becomes read-only while the deployment setting is
on.
- Modules stay cached at template import, so dynamic parameter rendering
still resolves module-defined parameters. Only the workspace build is
denied the cached files.
- Templates report `module_cache_disabled_by_deployment`, and template
settings shows why the checkbox is locked.

<details><summary>Agent context</summary>

Decisions taken with @Emyrk before implementing:

1. **Precedence.** `deployment || template`, no per-template override,
so no migration and no nullable column. The rule lives in one place,
`codersdk.ModuleCacheDisabled`.
2. **Import-time caching untouched.** `dynamicparameters/render.go`
overlays `cached_module_files` at `.terraform/modules` to render
module-defined parameters. Skipping the cache there would reproduce the
`Module not loaded. Did you run terraform init?` failure reported by a
customer, so the deployment setting only affects what `AcquireJob` hands
the provisioner.
3. **Write path.** A PATCH carrying `disable_module_cache` is discarded
while the deployment setting is on, rather than persisted. Persisting it
would silently keep templates opted out after the deployment setting is
removed.
4. **API shape.** `disable_module_cache` keeps its meaning (this
template's stored value) and the new read-only
`module_cache_disabled_by_deployment` reports the deployment state.
Reading the effective value from `/deployment/config` was rejected
because template admins may lack deployment-config read permission.
5. **Naming.** `CODER_PROVISIONER_DISABLE_MODULE_CACHE` over the issue's
`CODER_DISABLE_TERRAFORM_MODULE_CACHE`, to match the provisioning option
group.

Note: `InsertTemplateParams` has no `disable_module_cache` binding, so
new templates always store `false`. With deployment precedence they
still behave as opted out, so the create path needed no change.

Out of scope and tracked elsewhere: the `coderd_template` Terraform
provider attribute (separate repo) and backporting the per-template
toggle to `release/2.30`.

</details>

---

<sub>Coder Agents on behalf of @Emyrk.</sub>
2026-09-11 18:13:16 -05:00

63 lines
1.5 KiB
Go

package codersdk_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/serpent"
)
func TestModuleCacheDisabled(t *testing.T) {
t.Parallel()
cases := []struct {
name string
deploymentValues *codersdk.DeploymentValues
templateDisabled bool
expected bool
}{
{
name: "NoDeploymentValues",
deploymentValues: nil,
expected: false,
},
{
name: "NoDeploymentValuesTemplateDisabled",
deploymentValues: nil,
templateDisabled: true,
expected: true,
},
{
name: "BothEnabled",
deploymentValues: deploymentValuesWithModuleCacheDisabled(false),
expected: false,
},
{
name: "TemplateDisabled",
deploymentValues: deploymentValuesWithModuleCacheDisabled(false),
templateDisabled: true,
expected: true,
},
{
// The deployment cannot be overridden by a template.
name: "DeploymentDisabled",
deploymentValues: deploymentValuesWithModuleCacheDisabled(true),
expected: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
require.Equal(t, tc.expected, codersdk.ModuleCacheDisabled(tc.deploymentValues, tc.templateDisabled))
})
}
}
func deploymentValuesWithModuleCacheDisabled(disabled bool) *codersdk.DeploymentValues {
dv := &codersdk.DeploymentValues{}
dv.Provisioner.DisableModuleCache = serpent.Bool(disabled)
return dv
}