@@ -3,10 +3,12 @@ package coderd_test
3
3
import (
4
4
"context"
5
5
"os"
6
+ "sync"
6
7
"testing"
7
8
8
9
"github.com/google/uuid"
9
10
"github.com/stretchr/testify/require"
11
+ "go.uber.org/atomic"
10
12
"golang.org/x/xerrors"
11
13
12
14
"github.com/coder/coder/v2/coderd"
@@ -199,8 +201,15 @@ func TestDynamicParametersWithTerraformValues(t *testing.T) {
199
201
modulesArchive , err := terraform .GetModulesArchive (os .DirFS ("testdata/parameters/modules" ))
200
202
require .NoError (t , err )
201
203
204
+ c := atomic .NewInt32 (0 )
205
+ reject := & dbRejectGitSSHKey {Store : db , hook : func (d * dbRejectGitSSHKey ) {
206
+ if c .Add (1 ) > 1 {
207
+ // Second call forward, reject
208
+ d .SetReject (true )
209
+ }
210
+ }}
202
211
setup := setupDynamicParamsTest (t , setupDynamicParamsTestParams {
203
- db : & dbRejectGitSSHKey { Store : db } ,
212
+ db : reject ,
204
213
ps : ps ,
205
214
provisionerDaemonVersion : provProto .CurrentVersion .String (),
206
215
mainTF : dynamicParametersTerraformSource ,
@@ -444,8 +453,30 @@ func setupDynamicParamsTest(t *testing.T, args setupDynamicParamsTestParams) dyn
444
453
// that is generally impossible to force an error.
445
454
type dbRejectGitSSHKey struct {
446
455
database.Store
456
+ rejectMu sync.RWMutex
457
+ reject bool
458
+ hook func (d * dbRejectGitSSHKey )
459
+ }
460
+
461
+ // SetReject toggles whether GetGitSSHKey should return an error or passthrough to the underlying store.
462
+ func (d * dbRejectGitSSHKey ) SetReject (reject bool ) {
463
+ d .rejectMu .Lock ()
464
+ defer d .rejectMu .Unlock ()
465
+ d .reject = reject
447
466
}
448
467
449
- func (* dbRejectGitSSHKey ) GetGitSSHKey (_ context.Context , _ uuid.UUID ) (database.GitSSHKey , error ) {
450
- return database.GitSSHKey {}, xerrors .New ("forcing a fake error" )
468
+ func (d * dbRejectGitSSHKey ) GetGitSSHKey (ctx context.Context , userID uuid.UUID ) (database.GitSSHKey , error ) {
469
+ if d .hook != nil {
470
+ d .hook (d )
471
+ }
472
+
473
+ d .rejectMu .RLock ()
474
+ reject := d .reject
475
+ d .rejectMu .RUnlock ()
476
+
477
+ if reject {
478
+ return database.GitSSHKey {}, xerrors .New ("forcing a fake error" )
479
+ }
480
+
481
+ return d .Store .GetGitSSHKey (ctx , userID )
451
482
}
0 commit comments