Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: test parameter immutability validation #18583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Jun 25, 2025

What should happen in this situation. A template where the parameter isimmutable sets the mutability of immutable.

Terraform:

data "coder_parameter" "isimmutable" {
  name   = "isimmutable"
  type    = "bool"
  mutable = true
  default = "true"
}

data "coder_parameter" "immutable" {
  name    = "immutable"
  type    = "string"
  mutable = data.coder_parameter.isimmutable.value == "false"
  default = "Hello World"
}

Trivial Case:

  1. Workspace built with isimmutable = true & immutable = "bar"
  2. Workspace updates immutable = foo
    1. Breaks, because you cannot update an immutable value

Immutability source from the past

  1. Workspace built with isimmutable = true & immutable = "bar"
  2. Workspace updates immutable = foo & isimmutable = false

Either it:

  • Breaks because isimmutable WAS immutable on the previous build
    • (I think this one)
  • Works because isimmutable is now mutable from the new param

Comment on lines +355 to +388
t.Run("ImmutableChangeValue", func(t *testing.T) {
// Ok this is a weird test to document how things are working.
// What if a parameter flips it's immutability based on a value?
// The current behavior is to source immutability from the previous build.
t.Parallel()

ctx := testutil.Context(t, testutil.WaitShort)
// Start with a new template that has 1 parameter that is immutable
immutable, _ := coderdtest.DynamicParameterTemplate(t, templateAdmin, orgID, coderdtest.DynamicParameterTemplateParams{
MainTF: string(must(os.ReadFile("testdata/parameters/dynamicimmutable/main.tf"))),
})

// Create the workspace with the immutable parameter
wrk, err := templateAdmin.CreateUserWorkspace(ctx, codersdk.Me, codersdk.CreateWorkspaceRequest{
TemplateID: immutable.ID,
Name: coderdtest.RandomUsername(t),
RichParameterValues: []codersdk.WorkspaceBuildParameter{
{Name: "isimmutable", Value: "true"},
{Name: "immutable", Value: "coder"},
},
})
require.NoError(t, err)
coderdtest.AwaitWorkspaceBuildJobCompleted(t, templateAdmin, wrk.LatestBuild.ID)

// Try new values
_, err = templateAdmin.CreateWorkspaceBuild(ctx, wrk.ID, codersdk.CreateWorkspaceBuildRequest{
Transition: codersdk.WorkspaceTransitionStart,
RichParameterValues: []codersdk.WorkspaceBuildParameter{
{Name: "isimmutable", Value: "false"},
{Name: "immutable", Value: "not-coder"},
},
})
require.ErrorContains(t, err, `Previously immutable parameter changed`)
})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant