@@ -100,3 +100,147 @@ func TestRepository_UpsertController(t *testing.T) {
100
100
})
101
101
}
102
102
}
103
+
104
+ func TestRepository_UpdateController (t * testing.T ) {
105
+ ctx := context .Background ()
106
+ conn , _ := db .TestSetup (t , "postgres" )
107
+ rw := db .New (conn )
108
+ wrapper := db .TestWrapper (t )
109
+ testKms := kms .TestKms (t , conn , wrapper )
110
+ testRepo , err := NewRepository (ctx , rw , rw , testKms )
111
+ require .NoError (t , err )
112
+
113
+ iamRepo := iam .TestRepo (t , conn , wrapper )
114
+ iam .TestScopes (t , iamRepo )
115
+
116
+ tests := []struct {
117
+ name string
118
+ originalController * store.Controller
119
+ updatedController * store.Controller
120
+ wantCount int
121
+ wantErr bool
122
+ }{
123
+ {
124
+ name : "nil-controller" ,
125
+ wantErr : true ,
126
+ },
127
+ {
128
+ name : "empty-id" ,
129
+ updatedController : & store.Controller {
130
+ PrivateId : "" ,
131
+ Address : "127.0.0.1" ,
132
+ },
133
+ wantErr : true ,
134
+ },
135
+ {
136
+ name : "empty-address" ,
137
+ updatedController : & store.Controller {
138
+ PrivateId : "test-controller" ,
139
+ Address : "" ,
140
+ },
141
+ wantErr : true ,
142
+ },
143
+ {
144
+ name : "controller-not-found" ,
145
+ updatedController : & store.Controller {
146
+ PrivateId : "test-new-ipv4-controller" ,
147
+ Address : "127.0.0.1" ,
148
+ Description : "new ipv4 description" ,
149
+ },
150
+ wantErr : true ,
151
+ },
152
+ {
153
+ name : "valid-ipv4-controller" ,
154
+ originalController : & store.Controller {
155
+ PrivateId : "ipv4-controller" ,
156
+ Address : "127.0.0.1" ,
157
+ Description : "ipv4 description" ,
158
+ },
159
+ updatedController : & store.Controller {
160
+ PrivateId : "ipv4-controller" ,
161
+ Address : "127.0.0.2" ,
162
+ Description : "new ipv4 description" ,
163
+ },
164
+ wantCount : 1 ,
165
+ },
166
+ {
167
+ name : "valid-ipv6-controller" ,
168
+ originalController : & store.Controller {
169
+ PrivateId : "test-ipv6-controller" ,
170
+ Address : "[2001:4860:4860:0:0:0:0:8888]" ,
171
+ Description : "ipv6 description" ,
172
+ },
173
+ updatedController : & store.Controller {
174
+ PrivateId : "test-ipv6-controller" ,
175
+ Address : "[2001:4860:4860:0:0:0:0:9999]" ,
176
+ Description : "new ipv6 description" ,
177
+ },
178
+ wantCount : 1 ,
179
+ },
180
+ {
181
+ name : "valid-abbreviated-ipv6-controller" ,
182
+ originalController : & store.Controller {
183
+ PrivateId : "test-abbreviated-ipv6-controller" ,
184
+ Address : "[2001:4860:4860::8888]" ,
185
+ Description : "abbreviated ipv6 description" ,
186
+ },
187
+ updatedController : & store.Controller {
188
+ PrivateId : "test-abbreviated-ipv6-controller" ,
189
+ Address : "[2001:4860:4860::9999]" ,
190
+ Description : "new abbreviated ipv6 description" ,
191
+ },
192
+ wantCount : 1 ,
193
+ },
194
+ {
195
+ name : "valid-controller-short-name" ,
196
+ originalController : & store.Controller {
197
+ PrivateId : "test" ,
198
+ Address : "127.0.0.1" ,
199
+ Description : "short name description" ,
200
+ },
201
+ updatedController : & store.Controller {
202
+ PrivateId : "test" ,
203
+ Address : "127.0.0.2" ,
204
+ Description : "new short name description" ,
205
+ },
206
+ wantCount : 1 ,
207
+ },
208
+ }
209
+ for _ , tt := range tests {
210
+ t .Run (tt .name , func (t * testing.T ) {
211
+ assert , require := assert .New (t ), require .New (t )
212
+
213
+ var originalControllerEntry , updatedControllerEntry * store.Controller
214
+ // Insert the original controller attributes if they exist
215
+ if tt .originalController != nil {
216
+ _ , err := testRepo .UpsertController (ctx , tt .originalController )
217
+ require .NoError (err )
218
+
219
+ // Retrieve the original controller in the database
220
+ controllerList , err := testRepo .ListControllers (ctx , []Option {}... )
221
+ require .NoError (err )
222
+ originalControllerEntry = controllerList [len (controllerList )- 1 ]
223
+ }
224
+
225
+ // Update the controller with the updated attributes
226
+ got , err := testRepo .UpdateController (ctx , tt .updatedController )
227
+ if tt .wantErr {
228
+ require .Error (err )
229
+ assert .Equal (0 , got )
230
+ return
231
+ }
232
+ require .NoError (err )
233
+ assert .Equal (tt .wantCount , got )
234
+
235
+ // Retrieve the updated controller in the database and assert updated successfully
236
+ controllerList , err := testRepo .ListControllers (ctx , []Option {}... )
237
+ require .NoError (err )
238
+ updatedControllerEntry = controllerList [len (controllerList )- 1 ]
239
+
240
+ assert .Equal (tt .updatedController .PrivateId , updatedControllerEntry .PrivateId )
241
+ assert .Equal (tt .updatedController .Address , updatedControllerEntry .Address )
242
+ assert .Equal (tt .updatedController .Description , updatedControllerEntry .Description )
243
+ assert .True (updatedControllerEntry .UpdateTime .AsTime ().After (originalControllerEntry .UpdateTime .AsTime ()))
244
+ })
245
+ }
246
+ }
0 commit comments