From 26d1482459beb63ef7225d130c666f3adaeb9b50 Mon Sep 17 00:00:00 2001 From: akorotkov Date: Wed, 25 Dec 2024 11:58:21 +0200 Subject: [PATCH] make fields private --- pkg/dto/current_backup.go | 4 +-- pkg/model/last_backup_run.go | 34 +++++++++++++++------- pkg/service/backup_backend.go | 2 +- pkg/service/backup_routine_handler_test.go | 18 ++++-------- pkg/service/backup_scheduler_test.go | 4 +-- 5 files changed, 32 insertions(+), 30 deletions(-) diff --git a/pkg/dto/current_backup.go b/pkg/dto/current_backup.go index 0ca0c02d..368e36ff 100644 --- a/pkg/dto/current_backup.go +++ b/pkg/dto/current_backup.go @@ -33,8 +33,8 @@ func NewCurrentBackupsFromModel(m *model.CurrentBackups) *CurrentBackups { func (c *CurrentBackups) fromModel(m *model.CurrentBackups) { c.Full = NewRunningJobFromModel(m.Full) c.Incremental = NewRunningJobFromModel(m.Incremental) - c.LastFull = m.LastRunTime.Full - c.LastIncremental = m.LastRunTime.Incremental + c.LastFull = m.LastRunTime.FullBackupTime() + c.LastIncremental = m.LastRunTime.IncrementalBackupTime() } // RunningJob tracks progress of currently running job. diff --git a/pkg/model/last_backup_run.go b/pkg/model/last_backup_run.go index d313bd9f..b3a22cf1 100644 --- a/pkg/model/last_backup_run.go +++ b/pkg/model/last_backup_run.go @@ -9,42 +9,54 @@ import ( type LastBackupRun struct { mu sync.RWMutex // Last time the Full backup was performed. - Full *time.Time + full *time.Time // Last time the Incremental backup was performed. - Incremental *time.Time + incremental *time.Time } -func NewLastRun(lastFullBackup *time.Time, lastIncrBackup *time.Time) *LastBackupRun { +func NewLastBackupRun(lastFullBackup *time.Time, lastIncrBackup *time.Time) *LastBackupRun { return &LastBackupRun{ - Full: lastFullBackup, - Incremental: lastIncrBackup, + full: lastFullBackup, + incremental: lastIncrBackup, } } func (r *LastBackupRun) NoFullBackup() bool { r.mu.RLock() defer r.mu.RUnlock() - return r.Full == nil + return r.full == nil } func (r *LastBackupRun) LatestRun() *time.Time { r.mu.RLock() defer r.mu.RUnlock() - if r.Incremental != nil && r.Full != nil && r.Incremental.After(*r.Full) { - return r.Incremental + if r.incremental != nil && r.full != nil && r.incremental.After(*r.full) { + return r.incremental } - return r.Full + return r.full } func (r *LastBackupRun) SetFullBackupTime(t *time.Time) { r.mu.Lock() defer r.mu.Unlock() - r.Full = t + r.full = t } func (r *LastBackupRun) SetIncrementalBackupTime(t *time.Time) { r.mu.Lock() defer r.mu.Unlock() - r.Incremental = t + r.incremental = t +} + +func (r *LastBackupRun) FullBackupTime() *time.Time { + r.mu.RLock() + defer r.mu.RUnlock() + return r.full +} + +func (r *LastBackupRun) IncrementalBackupTime() *time.Time { + r.mu.RLock() + defer r.mu.RUnlock() + return r.incremental } diff --git a/pkg/service/backup_backend.go b/pkg/service/backup_backend.go index 4c194dbf..c5018b1b 100644 --- a/pkg/service/backup_backend.go +++ b/pkg/service/backup_backend.go @@ -40,7 +40,7 @@ func (b *BackupBackend) findLastRun(ctx context.Context) *model.LastBackupRun { incrementalBackupList, _ := b.IncrementalBackupList(ctx, model.TimeBounds{FromTime: lastFullBackup}) lastIncrBackup := lastBackupTime(incrementalBackupList) - return model.NewLastRun(lastFullBackup, lastIncrBackup) + return model.NewLastBackupRun(lastFullBackup, lastIncrBackup) } func lastBackupTime(b []model.BackupDetails) *time.Time { diff --git a/pkg/service/backup_routine_handler_test.go b/pkg/service/backup_routine_handler_test.go index 2558bdf9..8004a334 100644 --- a/pkg/service/backup_routine_handler_test.go +++ b/pkg/service/backup_routine_handler_test.go @@ -239,9 +239,7 @@ func TestRunIncrementalBackup_SkipIfFullBackupInProgress(t *testing.T) { retentionManager := new(mockRetentionManager) handler := setupTestHandler(backupService, clientManager, metadataWriter, configWriter, retentionManager) - handler.lastRun = &model.LastBackupRun{ - Full: util.Ptr(time.Now()), // Set last full run - } + handler.lastRun = model.NewLastBackupRun(util.Ptr(time.Now()), nil) handler.fullBackupHandlers["ns1"] = &mockBackupHandler{} @@ -259,9 +257,7 @@ func TestRunIncrementalBackup_SkipIfIncrementalBackupInProgress(t *testing.T) { retentionManager := new(mockRetentionManager) handler := setupTestHandler(backupService, clientManager, metadataWriter, configWriter, retentionManager) - handler.lastRun = &model.LastBackupRun{ - Full: util.Ptr(time.Now()), // Set last full run - } + handler.lastRun = model.NewLastBackupRun(util.Ptr(time.Now()), nil) handler.incrBackupHandlers["test"] = &mockBackupHandler{} @@ -279,9 +275,7 @@ func TestRunIncrementalBackup_ClientError(t *testing.T) { retentionManager := new(mockRetentionManager) handler := setupTestHandler(backupService, clientManager, metadataWriter, configWriter, retentionManager) - handler.lastRun = &model.LastBackupRun{ - Full: util.Ptr(time.Now()), - } + handler.lastRun = model.NewLastBackupRun(util.Ptr(time.Now()), nil) expectedErr := errors.New("client error") clientManager.On("GetClient", mock.Anything).Return(nil, expectedErr) @@ -302,9 +296,7 @@ func TestRunIncrementalBackup_Success(t *testing.T) { handler := setupTestHandler(backupService, clientManager, metadataWriter, configWriter, retentionManager) now := time.Now() lastRun := now.Add(-1 * time.Hour) - handler.lastRun = &model.LastBackupRun{ - Full: &lastRun, - } + handler.lastRun = model.NewLastBackupRun(&lastRun, nil) backupHandler := new(mockBackupHandler) stats := &models.BackupStats{} @@ -347,7 +339,7 @@ func TestRunIncrementalBackup_Success(t *testing.T) { clientManager.AssertExpectations(t) backupService.AssertExpectations(t) backupHandler.AssertExpectations(t) - assert.Equal(t, now, *handler.CurrentStat().LastRunTime.Incremental) + assert.Equal(t, now, *handler.CurrentStat().LastRunTime.IncrementalBackupTime()) } func TestRunFullBackup_PartialFailure(t *testing.T) { diff --git a/pkg/service/backup_scheduler_test.go b/pkg/service/backup_scheduler_test.go index 5fd5165d..090dba95 100644 --- a/pkg/service/backup_scheduler_test.go +++ b/pkg/service/backup_scheduler_test.go @@ -36,9 +36,7 @@ func TestDisabledRoutine(t *testing.T) { handlers := BackupHandlerHolder{ "routine1": &BackupRoutineHandler{}, - "routine2": &BackupRoutineHandler{lastRun: &model.LastBackupRun{ - Full: util.Ptr(time.Now()), - }}, + "routine2": &BackupRoutineHandler{lastRun: model.NewLastBackupRun(util.Ptr(time.Now()), nil)}, } err := scheduleRoutines(mockScheduler, config.BackupRoutines, handlers)