Skip to content

Commit a9ebf4b

Browse files
authored
fix: validate that coder_script has a way to run (#162)
Previously, this would just silently push the script and it would never run, because it wasn't configured to do so. This changes it so all scripts have to be ran at some point.
1 parent 73943b2 commit a9ebf4b

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

provider/script.go

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ func scriptResource() *schema.Resource {
1818
Description: "Use this resource to run a script from an agent.",
1919
CreateContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
2020
rd.SetId(uuid.NewString())
21+
runOnStart, _ := rd.Get("run_on_start").(bool)
22+
runOnStop, _ := rd.Get("run_on_stop").(bool)
23+
cron, _ := rd.Get("cron").(string)
24+
25+
if !runOnStart && !runOnStop && cron == "" {
26+
return diag.Errorf("at least one of run_on_start, run_on_stop, or cron must be set")
27+
}
2128
return nil
2229
},
2330
ReadContext: schema.NoopContext,

provider/script_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package provider_test
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
7+
"github.com/coder/terraform-provider-coder/provider"
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
13+
)
14+
15+
func TestScript(t *testing.T) {
16+
t.Parallel()
17+
18+
resource.Test(t, resource.TestCase{
19+
Providers: map[string]*schema.Provider{
20+
"coder": provider.New(),
21+
},
22+
IsUnitTest: true,
23+
Steps: []resource.TestStep{{
24+
Config: `
25+
provider "coder" {
26+
}
27+
resource "coder_script" "example" {
28+
agent_id = "some id"
29+
display_name = "Hey"
30+
script = "Wow"
31+
cron = "* * * * *"
32+
}
33+
`,
34+
Check: func(state *terraform.State) error {
35+
require.Len(t, state.Modules, 1)
36+
require.Len(t, state.Modules[0].Resources, 1)
37+
script := state.Modules[0].Resources["coder_script.example"]
38+
require.NotNil(t, script)
39+
t.Logf("script attributes: %#v", script.Primary.Attributes)
40+
for key, expected := range map[string]string{
41+
"agent_id": "some id",
42+
"display_name": "Hey",
43+
"script": "Wow",
44+
"cron": "* * * * *",
45+
} {
46+
require.Equal(t, expected, script.Primary.Attributes[key])
47+
}
48+
return nil
49+
},
50+
}},
51+
})
52+
}
53+
54+
func TestScriptNeverRuns(t *testing.T) {
55+
t.Parallel()
56+
57+
resource.Test(t, resource.TestCase{
58+
Providers: map[string]*schema.Provider{
59+
"coder": provider.New(),
60+
},
61+
IsUnitTest: true,
62+
Steps: []resource.TestStep{{
63+
Config: `
64+
provider "coder" {
65+
}
66+
resource "coder_script" "example" {
67+
agent_id = ""
68+
display_name = "Hey"
69+
script = "Wow"
70+
}
71+
`,
72+
ExpectError: regexp.MustCompile(`at least one of run_on_start, run_on_stop, or cron must be set`),
73+
}},
74+
})
75+
}

0 commit comments

Comments
 (0)