|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "time" |
| 7 | + "ydbcp/cmd/integration/common" |
| 8 | + pb "ydbcp/pkg/proto/ydbcp/v1alpha1" |
| 9 | + |
| 10 | + "google.golang.org/grpc" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + containerID = "abcde" |
| 15 | + databaseName = "/local" |
| 16 | + ydbcpEndpoint = "0.0.0.0:50051" |
| 17 | + databaseEndpoint = "grpcs://local-ydb:2135" |
| 18 | +) |
| 19 | + |
| 20 | +type backupScenario struct { |
| 21 | + name string |
| 22 | + request *pb.MakeBackupRequest |
| 23 | + expectedRootPath string |
| 24 | + expectedSourcePaths []string |
| 25 | +} |
| 26 | + |
| 27 | +func newMakeBackupRequest(rootPath string, sourcePaths []string) *pb.MakeBackupRequest { |
| 28 | + return &pb.MakeBackupRequest{ |
| 29 | + ContainerId: containerID, |
| 30 | + DatabaseName: databaseName, |
| 31 | + DatabaseEndpoint: databaseEndpoint, |
| 32 | + RootPath: rootPath, |
| 33 | + SourcePaths: sourcePaths, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func runBackupScenario(ctx context.Context, backupClient pb.BackupServiceClient, opClient pb.OperationServiceClient, scenario backupScenario) { |
| 38 | + tbwr, err := backupClient.MakeBackup(ctx, scenario.request) |
| 39 | + if err != nil { |
| 40 | + log.Panicf("scenario %s: failed to make backup: %v", scenario.name, err) |
| 41 | + } |
| 42 | + op, err := opClient.GetOperation( |
| 43 | + ctx, &pb.GetOperationRequest{ |
| 44 | + Id: tbwr.Id, |
| 45 | + }, |
| 46 | + ) |
| 47 | + if err != nil { |
| 48 | + log.Panicf("scenario %s: failed to get operation: %v", scenario.name, err) |
| 49 | + } |
| 50 | + |
| 51 | + if op.GetRootPath() != scenario.expectedRootPath { |
| 52 | + log.Panicf("scenario %s: expected root path %q, got %q", scenario.name, scenario.expectedRootPath, op.GetRootPath()) |
| 53 | + } |
| 54 | + |
| 55 | + if !equalStringSlices(op.GetSourcePaths(), scenario.expectedSourcePaths) { |
| 56 | + log.Panicf("scenario %s: expected source paths %v, got %v", scenario.name, scenario.expectedSourcePaths, op.GetSourcePaths()) |
| 57 | + } |
| 58 | + |
| 59 | + log.Printf("scenario %s: passed", scenario.name) |
| 60 | +} |
| 61 | + |
| 62 | +func equalStringSlices(a, b []string) bool { |
| 63 | + if len(a) != len(b) { |
| 64 | + return false |
| 65 | + } |
| 66 | + for i := range a { |
| 67 | + if a[i] != b[i] { |
| 68 | + return false |
| 69 | + } |
| 70 | + } |
| 71 | + return true |
| 72 | +} |
| 73 | + |
| 74 | +func main() { |
| 75 | + conn := common.CreateGRPCClient(ydbcpEndpoint) |
| 76 | + defer func(conn *grpc.ClientConn) { |
| 77 | + err := conn.Close() |
| 78 | + if err != nil { |
| 79 | + log.Panicln("failed to close connection") |
| 80 | + } |
| 81 | + }(conn) |
| 82 | + backupClient := pb.NewBackupServiceClient(conn) |
| 83 | + opClient := pb.NewOperationServiceClient(conn) |
| 84 | + |
| 85 | + ctx := context.Background() |
| 86 | + |
| 87 | + scenarios := []backupScenario{ |
| 88 | + { |
| 89 | + name: "full backup", |
| 90 | + request: newMakeBackupRequest("", nil), |
| 91 | + expectedRootPath: "", |
| 92 | + expectedSourcePaths: []string{}, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "full backup with specified root path", |
| 96 | + request: newMakeBackupRequest("stocks", nil), |
| 97 | + expectedRootPath: "stocks", |
| 98 | + expectedSourcePaths: []string{}, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "partial backup", |
| 102 | + request: newMakeBackupRequest("", []string{"kv_test"}), |
| 103 | + expectedRootPath: "", |
| 104 | + expectedSourcePaths: []string{"kv_test"}, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "partial backup with specified root path", |
| 108 | + request: newMakeBackupRequest("stocks", []string{"orders", "orderLines"}), |
| 109 | + expectedRootPath: "stocks", |
| 110 | + expectedSourcePaths: []string{"orders", "orderLines"}, |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + for _, scenario := range scenarios { |
| 115 | + runBackupScenario(ctx, backupClient, opClient, scenario) |
| 116 | + time.Sleep(2 * time.Second) |
| 117 | + } |
| 118 | +} |
0 commit comments