Skip to content

Commit

Permalink
add missing rollback comments and ensure tests are checking results p…
Browse files Browse the repository at this point in the history
…roperly

Signed-off-by: Mike Mason <[email protected]>
  • Loading branch information
mikemrm committed Dec 14, 2023
1 parent 291ad8e commit 199915c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
28 changes: 18 additions & 10 deletions internal/query/relations.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,27 +330,27 @@ func actionsDiff(oldActions, newActions []string) ([]string, []string) {
return nil, nil
}

old := make(map[string]bool, len(oldActions))
new := make(map[string]bool, len(newActions))
old := make(map[string]struct{}, len(oldActions))
new := make(map[string]struct{}, len(newActions))

var add, rem []string

for _, action := range oldActions {
old[action] = true
old[action] = struct{}{}
}

for _, action := range newActions {
new[action] = true
new[action] = struct{}{}

// If the new action is not in the old actions, then we need to add the action.
if !old[action] {
if _, ok := old[action]; !ok {
add = append(add, action)
}
}

for _, action := range oldActions {
// If the old action is not in the new actions, then we need to remove it.
if !new[action] {
if _, ok := new[action]; !ok {
rem = append(rem, action)
}
}
Expand Down Expand Up @@ -425,7 +425,10 @@ func (e *engine) UpdateRole(ctx context.Context, actor, roleResource types.Resou

logRollbackErr(e.logger, e.store.RollbackContext(dbCtx))

// TODO: add spicedb rollback logic.
// At this point, spicedb changes have already been applied.
// Attempting to rollback could result in failures that could result in the same situation.
//
// TODO: add spicedb rollback logic along with rollback failure scenarios.

return types.Role{}, err
}
Expand Down Expand Up @@ -846,13 +849,13 @@ func (e *engine) ListRoles(ctx context.Context, resource types.Resource) ([]type

out := relationshipsToRoles(relationships)

rolesByID := make(map[string]storage.Role, len(dbRoles))
rolesByID := make(map[gidx.PrefixedID]storage.Role, len(dbRoles))
for _, role := range dbRoles {
rolesByID[role.ID.String()] = role
rolesByID[role.ID] = role
}

for i, role := range out {
if dbRole, ok := rolesByID[role.ID.String()]; ok {
if dbRole, ok := rolesByID[role.ID]; ok {
role.Name = dbRole.Name
role.CreatedBy = dbRole.CreatedBy
role.UpdatedBy = dbRole.UpdatedBy
Expand Down Expand Up @@ -1075,6 +1078,11 @@ func (e *engine) DeleteRole(ctx context.Context, roleResource types.Resource) er

logRollbackErr(e.logger, e.store.RollbackContext(dbCtx))

// At this point, spicedb changes have already been applied.
// Attempting to rollback could result in failures that could result in the same situation.
//
// TODO: add spicedb rollback logic along with rollback failure scenarios.

return err
}

Expand Down
16 changes: 12 additions & 4 deletions internal/query/relations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ func TestRoleUpdate(t *testing.T) {
require.NoError(t, err)
actorRes, err := e.NewResourceFromID(gidx.MustNewID("idntusr"))
require.NoError(t, err)
actorUpdateRes, err := e.NewResourceFromID(gidx.MustNewID("idntusr"))
require.NoError(t, err)

role, err := e.CreateRole(ctx, actorRes, tenRes, "test", []string{"loadbalancer_get"})
require.NoError(t, err)
Expand All @@ -226,15 +228,21 @@ func TestRoleUpdate(t *testing.T) {
Name: "UpdateMissingRole",
Input: gidx.MustNewID(RolePrefix),
CheckFn: func(ctx context.Context, t *testing.T, res testingx.TestResult[types.Role]) {
assert.Error(t, res.Err)
require.Error(t, res.Err)
assert.ErrorIs(t, res.Err, ErrRoleNotFound)
},
},
{
Name: "UpdateSuccess",
Input: role.ID,
CheckFn: func(ctx context.Context, t *testing.T, res testingx.TestResult[types.Role]) {
assert.NoError(t, res.Err)
require.Equal(t, "test2", res.Success.Name)
require.NoError(t, res.Err)
assert.Equal(t, "test2", res.Success.Name)
assert.Equal(t, role.Actions, res.Success.Actions)
assert.Equal(t, role.CreatedBy, res.Success.CreatedBy)
assert.Equal(t, actorUpdateRes.ID, res.Success.UpdatedBy)
assert.Equal(t, role.CreatedAt, res.Success.CreatedAt)
assert.NotEqual(t, role.UpdatedAt, res.Success.UpdatedAt)
},
},
}
Expand All @@ -247,7 +255,7 @@ func TestRoleUpdate(t *testing.T) {
}
}

_, err = e.UpdateRole(ctx, actorRes, roleResource, "test2", nil)
_, err = e.UpdateRole(ctx, actorUpdateRes, roleResource, "test2", nil)
if err != nil {
return testingx.TestResult[types.Role]{
Err: err,
Expand Down

0 comments on commit 199915c

Please sign in to comment.