Skip to content

Commit c20f6e3

Browse files
committed
review notes and fix tests
1 parent 1280b6a commit c20f6e3

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

enterprise/coderd/prebuilds/membership_test.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,20 @@ func TestReconcileAll(t *testing.T) {
3939
}{
4040
// The StoreMembershipReconciler acts based on the provided agplprebuilds.GlobalSnapshot.
4141
// These test cases must therefore trust any valid snapshot, so the only relevant functional test cases are:
42+
43+
// No presets to act on and the prebuilds user does not belong to any organizations.
44+
// Reconciliation should be a no-op
4245
{name: "no presets, no memberships", includePreset: false, preExistingMembership: false},
46+
// If we have a preset that requires prebuilds, but the prebuilds user is not a member of
47+
// that organization, then we should add the membership.
4348
{name: "preset, but no membership", includePreset: true, preExistingMembership: false},
49+
// If the prebuilds system user is already a member of the organization to which a preset belongs,
50+
// then reconciliation should be a no-op:
4451
{name: "preset, but already a member", includePreset: true, preExistingMembership: true},
52+
// If the prebuilds system user is a member of an organization that doesn't have need any prebuilds,
53+
// then it must have required prebuilds in the past. The membership is not currently necessary, but
54+
// the reconciler won't remove it, because there's little cost to keeping it and prebuilds might be
55+
// enabled again.
4556
{name: "member, but no presets", includePreset: false, preExistingMembership: true},
4657
}
4758

@@ -54,7 +65,9 @@ func TestReconcileAll(t *testing.T) {
5465

5566
defaultOrg, err := db.GetDefaultOrganization(ctx)
5667
require.NoError(t, err)
57-
backgroundOrg := dbgen.Organization(t, db, database.Organization{})
68+
69+
// introduce an unrelated organization to ensure that the membership reconciler don't interfere with it.
70+
unrelatedOrg := dbgen.Organization(t, db, database.Organization{})
5871
targetOrg := dbgen.Organization(t, db, database.Organization{})
5972

6073
if !dbtestutil.WillUsePostgres() {
@@ -65,13 +78,13 @@ func TestReconcileAll(t *testing.T) {
6578
})
6679
}
6780

68-
dbgen.OrganizationMember(t, db, database.OrganizationMember{OrganizationID: backgroundOrg.ID, UserID: agplprebuilds.SystemUserID})
81+
dbgen.OrganizationMember(t, db, database.OrganizationMember{OrganizationID: unrelatedOrg.ID, UserID: agplprebuilds.SystemUserID})
6982
if tc.preExistingMembership {
7083
// System user already a member of both orgs.
7184
dbgen.OrganizationMember(t, db, database.OrganizationMember{OrganizationID: targetOrg.ID, UserID: agplprebuilds.SystemUserID})
7285
}
7386

74-
presets := []database.GetTemplatePresetsWithPrebuildsRow{newPresetRow(backgroundOrg.ID)}
87+
presets := []database.GetTemplatePresetsWithPrebuildsRow{newPresetRow(unrelatedOrg.ID)}
7588
if tc.includePreset {
7689
presets = append(presets, newPresetRow(targetOrg.ID))
7790
}
@@ -81,11 +94,11 @@ func TestReconcileAll(t *testing.T) {
8194
UserID: agplprebuilds.SystemUserID,
8295
})
8396
require.NoError(t, err)
84-
expected := []uuid.UUID{defaultOrg.ID, backgroundOrg.ID}
97+
expectedMembershipsBefore := []uuid.UUID{defaultOrg.ID, unrelatedOrg.ID}
8598
if tc.preExistingMembership {
86-
expected = append(expected, targetOrg.ID)
99+
expectedMembershipsBefore = append(expectedMembershipsBefore, targetOrg.ID)
87100
}
88-
require.ElementsMatch(t, expected, extractOrgIDs(preReconcileMemberships))
101+
require.ElementsMatch(t, expectedMembershipsBefore, extractOrgIDs(preReconcileMemberships))
89102

90103
// Reconcile
91104
reconciler := prebuilds.NewStoreMembershipReconciler(db, clock)
@@ -96,10 +109,11 @@ func TestReconcileAll(t *testing.T) {
96109
UserID: agplprebuilds.SystemUserID,
97110
})
98111
require.NoError(t, err)
112+
expectedMembershipsAfter := expectedMembershipsBefore
99113
if !tc.preExistingMembership && tc.includePreset {
100-
expected = append(expected, targetOrg.ID)
114+
expectedMembershipsAfter = append(expectedMembershipsAfter, targetOrg.ID)
101115
}
102-
require.ElementsMatch(t, expected, extractOrgIDs(postReconcileMemberships))
116+
require.ElementsMatch(t, expectedMembershipsAfter, extractOrgIDs(postReconcileMemberships))
103117
})
104118
}
105119
}

enterprise/coderd/prebuilds/reconcile.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,8 @@ func (c *StoreReconciler) ReconcileAll(ctx context.Context) error {
264264
return nil
265265
}
266266

267-
// nolint:gocritic // The prebuilds orchestrator subject is responsible for ensuring that the prebuilds user is a member of the necessary organizations
268-
prebuildsMembershipCtx := dbauthz.AsPrebuildsOrchestrator(ctx)
269267
membershipReconciler := NewStoreMembershipReconciler(c.store, c.clock)
270-
err = membershipReconciler.ReconcileAll(prebuildsMembershipCtx, prebuilds.SystemUserID, snapshot.Presets)
268+
err = membershipReconciler.ReconcileAll(ctx, prebuilds.SystemUserID, snapshot.Presets)
271269
if err != nil {
272270
return xerrors.Errorf("reconcile prebuild membership: %w", err)
273271
}

enterprise/coderd/prebuilds/reconcile_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ func TestNoReconciliationActionsIfNoPresets(t *testing.T) {
4242
// Scenario: No reconciliation actions are taken if there are no presets
4343
t.Parallel()
4444

45+
if !dbtestutil.WillUsePostgres() {
46+
t.Skip("dbmem times out on nesting transactions, postgres ignores the inner ones")
47+
}
48+
4549
clock := quartz.NewMock(t)
4650
ctx := testutil.Context(t, testutil.WaitLong)
4751
db, ps := dbtestutil.NewDB(t)
@@ -83,6 +87,10 @@ func TestNoReconciliationActionsIfNoPrebuilds(t *testing.T) {
8387
// Scenario: No reconciliation actions are taken if there are no prebuilds
8488
t.Parallel()
8589

90+
if !dbtestutil.WillUsePostgres() {
91+
t.Skip("dbmem times out on nesting transactions, postgres ignores the inner ones")
92+
}
93+
8694
clock := quartz.NewMock(t)
8795
ctx := testutil.Context(t, testutil.WaitLong)
8896
db, ps := dbtestutil.NewDB(t)

0 commit comments

Comments
 (0)