Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Move Reasoner implementation to identity package (#4468)
Browse files Browse the repository at this point in the history
Move the Reasoner implementation from the kopia package to the identity
package. This will help avoid import cycles if we want to start
persisting Reason information in the backup model.

---

#### Does this PR need a docs update or release note?

- [ ] ✅ Yes, it's included
- [ ] 🕐 Yes, but in a later PR
- [x] ⛔ No

#### Type of change

- [ ] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [x] 🤖 Supportability/Tests
- [ ] 💻 CI/Deployment
- [ ] 🧹 Tech Debt/Cleanup

#### Test Plan

- [ ] 💪 Manual
- [x] ⚡ Unit test
- [x] 💚 E2E
  • Loading branch information
ashmrtn authored Oct 11, 2023
1 parent ba64f07 commit 84b9de9
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 147 deletions.
14 changes: 7 additions & 7 deletions src/internal/kopia/backup_bases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func (suite *BackupBasesUnitSuite) TestMergeBackupBases() {
reasons := make([]identity.Reasoner, 0, len(i.cat))

for _, c := range i.cat {
reasons = append(reasons, NewReason("", ro, path.ExchangeService, c))
reasons = append(reasons, identity.NewReason("", ro, path.ExchangeService, c))
}

m := makeManifest(baseID, "", "b"+baseID, reasons...)
Expand All @@ -294,7 +294,7 @@ func (suite *BackupBasesUnitSuite) TestMergeBackupBases() {
reasons := make([]identity.Reasoner, 0, len(i.cat))

for _, c := range i.cat {
reasons = append(reasons, NewReason("", ro, path.ExchangeService, c))
reasons = append(reasons, identity.NewReason("", ro, path.ExchangeService, c))
}

m := makeManifest(baseID, "", "a"+baseID, reasons...)
Expand Down Expand Up @@ -529,7 +529,7 @@ func (suite *BackupBasesUnitSuite) TestFixupAndVerify() {
ro := "resource_owner"

makeMan := func(pct path.CategoryType, id, incmpl, bID string) ManifestEntry {
r := NewReason("", ro, path.ExchangeService, pct)
r := identity.NewReason("", ro, path.ExchangeService, pct)
return makeManifest(id, incmpl, bID, r)
}

Expand Down Expand Up @@ -727,23 +727,23 @@ func (suite *BackupBasesUnitSuite) TestFixupAndVerify() {
res := validMail1()
res.mergeBases[0].Reasons = append(
res.mergeBases[0].Reasons,
NewReason("", ro, path.ExchangeService, path.ContactsCategory))
identity.NewReason("", ro, path.ExchangeService, path.ContactsCategory))

res.assistBases[0].Reasons = append(
res.assistBases[0].Reasons,
NewReason("", ro, path.ExchangeService, path.ContactsCategory))
identity.NewReason("", ro, path.ExchangeService, path.ContactsCategory))

return res
}(),
expect: func() *backupBases {
res := validMail1()
res.mergeBases[0].Reasons = append(
res.mergeBases[0].Reasons,
NewReason("", ro, path.ExchangeService, path.ContactsCategory))
identity.NewReason("", ro, path.ExchangeService, path.ContactsCategory))

res.assistBases[0].Reasons = append(
res.assistBases[0].Reasons,
NewReason("", ro, path.ExchangeService, path.ContactsCategory))
identity.NewReason("", ro, path.ExchangeService, path.ContactsCategory))

return res
}(),
Expand Down
49 changes: 0 additions & 49 deletions src/internal/kopia/base_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,55 +30,6 @@ const (
userTagPrefix = "tag:"
)

func NewReason(
tenant, resource string,
service path.ServiceType,
category path.CategoryType,
) identity.Reasoner {
return reason{
tenant: tenant,
resource: resource,
service: service,
category: category,
}
}

type reason struct {
// tenant appears here so that when this is moved to an inject package nothing
// needs changed. However, kopia itself is blind to the fields in the reason
// struct and relies on helper functions to get the information it needs.
tenant string
resource string
service path.ServiceType
category path.CategoryType
}

func (r reason) Tenant() string {
return r.tenant
}

func (r reason) ProtectedResource() string {
return r.resource
}

func (r reason) Service() path.ServiceType {
return r.service
}

func (r reason) Category() path.CategoryType {
return r.category
}

func (r reason) SubtreePath() (path.Path, error) {
p, err := path.BuildPrefix(
r.Tenant(),
r.ProtectedResource(),
r.Service(),
r.Category())

return p, clues.Wrap(err, "building path").OrNil()
}

func tagKeys(r identity.Reasoner) []string {
return []string{
r.ProtectedResource(),
Expand Down
52 changes: 26 additions & 26 deletions src/internal/kopia/base_finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ var (

testAllUsersAllCats = []identity.Reasoner{
// User1 email and events.
NewReason("", testUser1, path.ExchangeService, path.EmailCategory),
NewReason("", testUser1, path.ExchangeService, path.EventsCategory),
identity.NewReason("", testUser1, path.ExchangeService, path.EmailCategory),
identity.NewReason("", testUser1, path.ExchangeService, path.EventsCategory),
// User2 email and events.
NewReason("", testUser2, path.ExchangeService, path.EmailCategory),
NewReason("", testUser2, path.ExchangeService, path.EventsCategory),
identity.NewReason("", testUser2, path.ExchangeService, path.EmailCategory),
identity.NewReason("", testUser2, path.ExchangeService, path.EventsCategory),
// User3 email and events.
NewReason("", testUser3, path.ExchangeService, path.EmailCategory),
NewReason("", testUser3, path.ExchangeService, path.EventsCategory),
identity.NewReason("", testUser3, path.ExchangeService, path.EmailCategory),
identity.NewReason("", testUser3, path.ExchangeService, path.EventsCategory),
}
testAllUsersMail = []identity.Reasoner{
NewReason("", testUser1, path.ExchangeService, path.EmailCategory),
NewReason("", testUser2, path.ExchangeService, path.EmailCategory),
NewReason("", testUser3, path.ExchangeService, path.EmailCategory),
identity.NewReason("", testUser1, path.ExchangeService, path.EmailCategory),
identity.NewReason("", testUser2, path.ExchangeService, path.EmailCategory),
identity.NewReason("", testUser3, path.ExchangeService, path.EmailCategory),
}
testUser1Mail = []identity.Reasoner{
NewReason("", testUser1, path.ExchangeService, path.EmailCategory),
identity.NewReason("", testUser1, path.ExchangeService, path.EmailCategory),
}
)

Expand Down Expand Up @@ -294,7 +294,7 @@ func (suite *BaseFinderUnitSuite) TestNoResult_NoBackupsOrSnapshots() {
bg: mockEmptyModelGetter{},
}
reasons := []identity.Reasoner{
NewReason("", "a-user", path.ExchangeService, path.EmailCategory),
identity.NewReason("", "a-user", path.ExchangeService, path.EmailCategory),
}

bb := bf.FindBases(ctx, reasons, nil)
Expand All @@ -314,7 +314,7 @@ func (suite *BaseFinderUnitSuite) TestNoResult_ErrorListingSnapshots() {
bg: mockEmptyModelGetter{},
}
reasons := []identity.Reasoner{
NewReason("", "a-user", path.ExchangeService, path.EmailCategory),
identity.NewReason("", "a-user", path.ExchangeService, path.EmailCategory),
}

bb := bf.FindBases(ctx, reasons, nil)
Expand Down Expand Up @@ -561,14 +561,14 @@ func (suite *BaseFinderUnitSuite) TestGetBases() {
},
expectedBaseReasons: map[int][]identity.Reasoner{
0: {
NewReason("", testUser1, path.ExchangeService, path.EmailCategory),
NewReason("", testUser2, path.ExchangeService, path.EmailCategory),
NewReason("", testUser3, path.ExchangeService, path.EmailCategory),
identity.NewReason("", testUser1, path.ExchangeService, path.EmailCategory),
identity.NewReason("", testUser2, path.ExchangeService, path.EmailCategory),
identity.NewReason("", testUser3, path.ExchangeService, path.EmailCategory),
},
1: {
NewReason("", testUser1, path.ExchangeService, path.EventsCategory),
NewReason("", testUser2, path.ExchangeService, path.EventsCategory),
NewReason("", testUser3, path.ExchangeService, path.EventsCategory),
identity.NewReason("", testUser1, path.ExchangeService, path.EventsCategory),
identity.NewReason("", testUser2, path.ExchangeService, path.EventsCategory),
identity.NewReason("", testUser3, path.ExchangeService, path.EventsCategory),
},
},
backupData: []backupInfo{
Expand Down Expand Up @@ -611,20 +611,20 @@ func (suite *BaseFinderUnitSuite) TestGetBases() {
},
expectedBaseReasons: map[int][]identity.Reasoner{
2: {
NewReason("", testUser1, path.ExchangeService, path.EmailCategory),
NewReason("", testUser2, path.ExchangeService, path.EmailCategory),
NewReason("", testUser1, path.ExchangeService, path.EventsCategory),
NewReason("", testUser2, path.ExchangeService, path.EventsCategory),
identity.NewReason("", testUser1, path.ExchangeService, path.EmailCategory),
identity.NewReason("", testUser2, path.ExchangeService, path.EmailCategory),
identity.NewReason("", testUser1, path.ExchangeService, path.EventsCategory),
identity.NewReason("", testUser2, path.ExchangeService, path.EventsCategory),
},
},
expectedAssistReasons: map[int][]identity.Reasoner{
0: {
NewReason("", testUser1, path.ExchangeService, path.EventsCategory),
NewReason("", testUser2, path.ExchangeService, path.EventsCategory),
identity.NewReason("", testUser1, path.ExchangeService, path.EventsCategory),
identity.NewReason("", testUser2, path.ExchangeService, path.EventsCategory),
},
1: {
NewReason("", testUser1, path.ExchangeService, path.EmailCategory),
NewReason("", testUser2, path.ExchangeService, path.EmailCategory),
identity.NewReason("", testUser1, path.ExchangeService, path.EmailCategory),
identity.NewReason("", testUser2, path.ExchangeService, path.EmailCategory),
},
},
backupData: []backupInfo{
Expand Down
46 changes: 23 additions & 23 deletions src/internal/kopia/cleanup_backups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,13 +648,13 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
manifestWithReasons(
manifestWithTime(baseTime, snapCurrent()),
"tenant1",
NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime, deetsCurrent()),

manifestWithReasons(
manifestWithTime(baseTime.Add(time.Second), snapCurrent2()),
"tenant1",
NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime.Add(time.Second), deetsCurrent2()),
},
backups: []backupRes{
Expand All @@ -675,19 +675,19 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
manifestWithReasons(
manifestWithTime(baseTime, snapCurrent()),
"tenant1",
NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime, deetsCurrent()),

manifestWithReasons(
manifestWithTime(baseTime.Add(time.Second), snapCurrent2()),
"tenant1",
NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime.Add(time.Second), deetsCurrent2()),

manifestWithReasons(
manifestWithTime(baseTime.Add(time.Minute), snapCurrent3()),
"tenant1",
NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime.Add(time.Minute), deetsCurrent3()),
},
backups: []backupRes{
Expand Down Expand Up @@ -719,13 +719,13 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
manifestWithReasons(
manifestWithTime(baseTime, snapCurrent()),
"tenant1",
NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime, deetsCurrent()),

manifestWithReasons(
manifestWithTime(baseTime.Add(time.Second), snapCurrent2()),
"tenant1",
NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime.Add(time.Second), deetsCurrent2()),
},
backups: []backupRes{
Expand All @@ -749,19 +749,19 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
manifestWithReasons(
manifestWithTime(baseTime, snapCurrent()),
"tenant1",
NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime, deetsCurrent()),

manifestWithReasons(
manifestWithTime(baseTime.Add(time.Second), snapCurrent2()),
"tenant1",
NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime.Add(time.Second), deetsCurrent2()),

manifestWithReasons(
manifestWithTime(baseTime.Add(time.Minute), snapCurrent3()),
"tenant1",
NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime.Add(time.Minute), deetsCurrent3()),
},
backups: []backupRes{
Expand All @@ -786,19 +786,19 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
manifestWithReasons(
manifestWithTime(baseTime, snapCurrent()),
"tenant1",
NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime, deetsCurrent()),

manifestWithReasons(
manifestWithTime(baseTime.Add(time.Minute), snapCurrent2()),
"tenant1",
NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime.Add(time.Minute), deetsCurrent2()),

manifestWithReasons(
manifestWithTime(baseTime.Add(time.Second), snapCurrent3()),
"tenant1",
NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime.Add(time.Second), deetsCurrent3()),
},
backups: []backupRes{
Expand All @@ -823,14 +823,14 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
manifestWithReasons(
manifestWithTime(baseTime, snapCurrent()),
"tenant1",
NewReason("", "ro", path.ExchangeService, path.EmailCategory),
NewReason("", "ro", path.ExchangeService, path.ContactsCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory),
identity.NewReason("", "ro", path.ExchangeService, path.ContactsCategory)),
manifestWithTime(baseTime, deetsCurrent()),

manifestWithReasons(
manifestWithTime(baseTime.Add(time.Second), snapCurrent2()),
"tenant1",
NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime.Add(time.Second), deetsCurrent2()),
},
backups: []backupRes{
Expand All @@ -851,13 +851,13 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
manifestWithReasons(
manifestWithTime(baseTime, snapCurrent()),
"tenant1",
NewReason("", "ro1", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro1", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime, deetsCurrent()),

manifestWithReasons(
manifestWithTime(baseTime.Add(time.Second), snapCurrent2()),
"tenant1",
NewReason("", "ro2", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro2", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime.Add(time.Second), deetsCurrent2()),
},
backups: []backupRes{
Expand All @@ -878,13 +878,13 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
manifestWithReasons(
manifestWithTime(baseTime, snapCurrent()),
"tenant1",
NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime, deetsCurrent()),

manifestWithReasons(
manifestWithTime(baseTime.Add(time.Second), snapCurrent2()),
"tenant2",
NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime.Add(time.Second), deetsCurrent2()),
},
backups: []backupRes{
Expand All @@ -905,19 +905,19 @@ func (suite *BackupCleanupUnitSuite) TestCleanupOrphanedData() {
manifestWithReasons(
manifestWithTime(baseTime, snapCurrent()),
"",
NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime, deetsCurrent()),

manifestWithReasons(
manifestWithTime(baseTime.Add(time.Second), snapCurrent2()),
"tenant1",
NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime.Add(time.Second), deetsCurrent2()),

manifestWithReasons(
manifestWithTime(baseTime.Add(time.Minute), snapCurrent3()),
"tenant1",
NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
identity.NewReason("", "ro", path.ExchangeService, path.EmailCategory)),
manifestWithTime(baseTime.Add(time.Minute), deetsCurrent3()),
},
backups: []backupRes{
Expand Down
2 changes: 1 addition & 1 deletion src/internal/kopia/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ func makeManifestEntry(
var reasons []identity.Reasoner

for _, c := range categories {
reasons = append(reasons, NewReason(tenant, resourceOwner, service, c))
reasons = append(reasons, identity.NewReason(tenant, resourceOwner, service, c))
}

return ManifestEntry{
Expand Down
Loading

0 comments on commit 84b9de9

Please sign in to comment.