-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
239 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"time" | ||
"ydbcp/internal/connectors/db/yql/queries" | ||
"ydbcp/internal/server/services/backup" | ||
"ydbcp/internal/types" | ||
pb "ydbcp/pkg/proto/ydbcp/v1alpha1" | ||
) | ||
|
||
func NewBackupScheduleHandler( | ||
service *backup.BackupService, | ||
queryBuilderFactory queries.WriteQueryBulderFactory, | ||
) types.BackupScheduleHandler { | ||
return func(ctx context.Context, schedule types.BackupSchedule) error { | ||
return BackupScheduleHandler(ctx, schedule, service, queryBuilderFactory) | ||
} | ||
} | ||
|
||
func BackupScheduleHandler( | ||
ctx context.Context, schedule types.BackupSchedule, s *backup.BackupService, | ||
queryBuilderFactory queries.WriteQueryBulderFactory, | ||
) error { | ||
if !schedule.Active { //maybe just select active schedules; will be done in processor | ||
return nil | ||
} | ||
now := time.Now() | ||
if schedule.NextLaunch != nil && schedule.NextLaunch.Before(now) { | ||
b, op, err := s.MakeBackupImpl( | ||
ctx, &pb.MakeBackupRequest{ | ||
ContainerId: schedule.ContainerID, | ||
DatabaseName: schedule.DatabaseName, | ||
DatabaseEndpoint: schedule.DatabaseEndpoint, | ||
SourcePaths: schedule.SourcePaths, | ||
SourcePathsToExclude: schedule.SourcePathsToExclude, | ||
}, &schedule.ID, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
schedule.LastBackupID = &op.BackupID | ||
err = schedule.UpdateNextLaunch(now) | ||
if err != nil { | ||
return err | ||
} | ||
return s.Driver.ExecuteUpsert( | ||
ctx, | ||
queryBuilderFactory().WithCreateBackup(*b).WithCreateOperation(op).WithUpdateBackupSchedule(schedule), | ||
) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Operations" | ||
"testing" | ||
"time" | ||
"ydbcp/internal/auth" | ||
"ydbcp/internal/config" | ||
"ydbcp/internal/connectors/client" | ||
"ydbcp/internal/connectors/db" | ||
"ydbcp/internal/connectors/db/yql/queries" | ||
"ydbcp/internal/server/services/backup" | ||
"ydbcp/internal/types" | ||
auth2 "ydbcp/pkg/plugins/auth" | ||
pb "ydbcp/pkg/proto/ydbcp/v1alpha1" | ||
) | ||
|
||
func TestBackupScheduleHandler(t *testing.T) { | ||
ctx := context.Background() | ||
now := time.Now() | ||
schedule := types.BackupSchedule{ | ||
ID: "12345", | ||
ContainerID: "abcde", | ||
Active: true, | ||
DatabaseName: "mydb", | ||
DatabaseEndpoint: "mydb.valid.com", | ||
ScheduleSettings: &pb.BackupScheduleSettings{ | ||
SchedulePattern: &pb.BackupSchedulePattern{Crontab: "* * * * * *"}, | ||
}, | ||
NextLaunch: &now, | ||
LastBackupID: nil, | ||
LastSuccessfulBackupID: nil, | ||
} | ||
opMap := make(map[string]types.Operation) | ||
backupMap := make(map[string]types.Backup) | ||
ydbOpMap := make(map[string]*Ydb_Operations.Operation) | ||
scheduleMap := make(map[string]types.BackupSchedule) | ||
scheduleMap[schedule.ID] = schedule | ||
dbConnector := db.NewMockDBConnector( | ||
db.WithBackups(backupMap), | ||
db.WithOperations(opMap), | ||
db.WithBackupSchedules(scheduleMap), | ||
) | ||
clientConnector := client.NewMockClientConnector( | ||
client.WithOperations(ydbOpMap), | ||
) | ||
|
||
mockAuth := auth.NewMockAuthProvider( | ||
auth.WithToken("", "abcde", auth2.AuthCodeSuccess), | ||
auth.WithContainer( | ||
"abcde", | ||
&auth.MockContainer{Permissions: map[auth.MockSubjectID]auth.MockPermissionsList{"abcde": map[string]bool{"ydb.databases.backup": true}}}, | ||
), | ||
) | ||
backupService := backup.NewBackupService( | ||
dbConnector, | ||
clientConnector, | ||
config.S3Config{ | ||
S3ForcePathStyle: false, | ||
IsMock: true, | ||
}, | ||
mockAuth, | ||
[]string{".valid.com"}, | ||
true, | ||
) | ||
|
||
handler := NewBackupScheduleHandler(backupService, queries.NewWriteTableQueryMock) | ||
err := handler(ctx, schedule) | ||
assert.Empty(t, err) | ||
|
||
// check operation status (should be pending) | ||
ops, err := dbConnector.SelectOperations(ctx, &queries.ReadTableQueryImpl{}) | ||
assert.Empty(t, err) | ||
assert.NotEmpty(t, ops) | ||
assert.Equal(t, len(ops), 1) | ||
assert.Equal(t, types.OperationStateRunning, ops[0].GetState()) | ||
|
||
// check backup status (should be running) | ||
backups, err := dbConnector.SelectBackups(ctx, &queries.ReadTableQueryImpl{}) | ||
assert.Empty(t, err) | ||
assert.NotEmpty(t, backups) | ||
assert.Equal(t, len(backups), 1) | ||
assert.Equal(t, types.BackupStateRunning, backups[0].Status) | ||
|
||
// check schedule | ||
schedules, err := dbConnector.SelectBackupSchedules(ctx, &queries.ReadTableQueryImpl{}) | ||
assert.Empty(t, err) | ||
assert.NotEmpty(t, schedules) | ||
assert.Equal(t, len(schedules), 1) | ||
assert.Equal(t, *schedules[0].LastBackupID, backups[0].ID) | ||
assert.Greater(t, *schedules[0].NextLaunch, now) | ||
} |
Oops, something went wrong.