Skip to content

Commit

Permalink
fix!: add extra validation for start_blocks_login (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
mafredri authored Feb 9, 2024
1 parent 908ab27 commit 1797a03
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
4 changes: 4 additions & 0 deletions provider/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ func scriptResource() *schema.Resource {
CreateContext: func(_ context.Context, rd *schema.ResourceData, _ interface{}) diag.Diagnostics {
rd.SetId(uuid.NewString())
runOnStart, _ := rd.Get("run_on_start").(bool)
startBlocksLogin, _ := rd.Get("start_blocks_login").(bool)
runOnStop, _ := rd.Get("run_on_stop").(bool)
cron, _ := rd.Get("cron").(string)

if !runOnStart && !runOnStop && cron == "" {
return diag.Errorf("at least one of run_on_start, run_on_stop, or cron must be set")
}
if !runOnStart && startBlocksLogin {
return diag.Errorf("start_blocks_login can only be set if run_on_start is true")
}
return nil
},
ReadContext: schema.NoopContext,
Expand Down
61 changes: 61 additions & 0 deletions provider/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,64 @@ func TestScriptNeverRuns(t *testing.T) {
}},
})
}

func TestScriptStartBlocksLoginRequiresRunOnStart(t *testing.T) {
t.Parallel()

resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: `
provider "coder" {
}
resource "coder_script" "example" {
agent_id = ""
display_name = "Hey"
script = "Wow"
run_on_stop = true
start_blocks_login = true
}
`,
ExpectError: regexp.MustCompile(`start_blocks_login can only be set if run_on_start is true`),
}},
})
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: `
provider "coder" {
}
resource "coder_script" "example" {
agent_id = ""
display_name = "Hey"
script = "Wow"
start_blocks_login = true
run_on_start = true
}
`,
Check: func(state *terraform.State) error {
require.Len(t, state.Modules, 1)
require.Len(t, state.Modules[0].Resources, 1)
script := state.Modules[0].Resources["coder_script.example"]
require.NotNil(t, script)
t.Logf("script attributes: %#v", script.Primary.Attributes)
for key, expected := range map[string]string{
"agent_id": "",
"display_name": "Hey",
"script": "Wow",
"start_blocks_login": "true",
"run_on_start": "true",
} {
require.Equal(t, expected, script.Primary.Attributes[key])
}
return nil
},
}},
})
}

0 comments on commit 1797a03

Please sign in to comment.