Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita9ebf4b

Browse files
authored
fix: validate thatcoder_script has a way to run (#162)
Previously, this would just silently push the script andit 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 parent73943b2 commita9ebf4b

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

‎provider/script.go

Lines changed: 7 additions & 0 deletions
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,iinterface{}) 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+
returndiag.Errorf("at least one of run_on_start, run_on_stop, or cron must be set")
27+
}
2128
returnnil
2229
},
2330
ReadContext:schema.NoopContext,

‎provider/script_test.go

Lines changed: 75 additions & 0 deletions
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+
funcTestScript(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+
forkey,expected:=rangemap[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+
returnnil
49+
},
50+
}},
51+
})
52+
}
53+
54+
funcTestScriptNeverRuns(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)

[8]ページ先頭

©2009-2025 Movatter.jp