@@ -10,6 +10,7 @@ import (
10
10
11
11
"github.com/coder/terraform-provider-coderd/integration"
12
12
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
13
+ "github.com/hashicorp/terraform-plugin-testing/terraform"
13
14
"github.com/stretchr/testify/require"
14
15
)
15
16
@@ -35,6 +36,10 @@ func TestAccUserResource(t *testing.T) {
35
36
cfg2 .Username = PtrTo ("exampleNew" )
36
37
cfg2 .Name = PtrTo ("Example User New" )
37
38
39
+ cfg3 := cfg1
40
+ cfg3 .Email = PtrTo ("example2@coder.com" )
41
+
42
+ var userId string
38
43
resource .Test (t , resource.TestCase {
39
44
PreCheck :func () {testAccPreCheck (t ) },
40
45
ProtoV6ProviderFactories :testAccProtoV6ProviderFactories ,
@@ -68,6 +73,15 @@ func TestAccUserResource(t *testing.T) {
68
73
Check :resource .ComposeAggregateTestCheckFunc (
69
74
resource .TestCheckResourceAttr ("coderd_user.test" ,"username" ,"exampleNew" ),
70
75
resource .TestCheckResourceAttr ("coderd_user.test" ,"name" ,"Example User New" ),
76
+ testAccIdChanged ("coderd_user.test" ,& userId ),
77
+ ),
78
+ },
79
+ // Replace testing
80
+ {
81
+ Config :cfg3 .String (t ),
82
+ Check :resource .ComposeAggregateTestCheckFunc (
83
+ resource .TestCheckResourceAttr ("coderd_user.test" ,"email" ,"example2@coder.com" ),
84
+ testAccIdChanged ("coderd_user.test" ,& userId ),
71
85
),
72
86
},
73
87
// Delete testing automatically occurs in TestCase
@@ -151,3 +165,24 @@ resource "coderd_user" "test" {
151
165
152
166
return buf .String ()
153
167
}
168
+
169
+ // Check if the id has changed since the last time this check function was run.
170
+ func testAccIdChanged (resourceName string ,id * string ) resource.TestCheckFunc {
171
+ return func (s * terraform.State )error {
172
+ rs ,ok := s .RootModule ().Resources [resourceName ]
173
+ if ! ok {
174
+ return fmt .Errorf ("Resource %s not found" ,resourceName )
175
+ }
176
+ if rs .Primary .ID == "" {
177
+ return fmt .Errorf ("No ID is set" )
178
+ }
179
+ if * id == "" {
180
+ * id = rs .Primary .ID
181
+ return nil
182
+ }
183
+ if rs .Primary .ID == * id {
184
+ return fmt .Errorf ("ID did not change from %s" ,rs .Primary .ID )
185
+ }
186
+ return nil
187
+ }
188
+ }