|
| 1 | +package backup |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + // "k8s.io/apimachinery/pkg/runtime/schema" |
| 11 | + "k8s.io/client-go/kubernetes/scheme" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" // nolint |
| 14 | + "sigs.k8s.io/yaml" |
| 15 | + |
| 16 | + api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" |
| 17 | +) |
| 18 | + |
| 19 | +func TestApplyCustomPBMConfig(t *testing.T) { |
| 20 | + cr := expectedCR(t) |
| 21 | + |
| 22 | + storage := cr.Spec.Backup.Storages["test-s3-storage"] |
| 23 | + |
| 24 | + cli := buildFakeClient(t) |
| 25 | + |
| 26 | + config, err := GetPBMConfig(context.Background(), cli, cr, storage) |
| 27 | + if err != nil { |
| 28 | + t.Fatal(err) |
| 29 | + } |
| 30 | + |
| 31 | + expectedS3Retryer := storage.S3.Retryer |
| 32 | + if expectedS3Retryer.NumMaxRetries != config.Storage.S3.Retryer.NumMaxRetries { |
| 33 | + t.Errorf("expected %d, got %d", expectedS3Retryer.NumMaxRetries, config.Storage.S3.Retryer.NumMaxRetries) |
| 34 | + } |
| 35 | + if expectedS3Retryer.MaxRetryDelay.Duration != config.Storage.S3.Retryer.MaxRetryDelay { |
| 36 | + t.Errorf("expected %d, got %d", expectedS3Retryer.MaxRetryDelay.Duration, config.Storage.S3.Retryer.MaxRetryDelay) |
| 37 | + } |
| 38 | + if expectedS3Retryer.MinRetryDelay.Duration != config.Storage.S3.Retryer.MinRetryDelay { |
| 39 | + t.Errorf("expected %d, got %d", expectedS3Retryer.MinRetryDelay.Duration, config.Storage.S3.Retryer.MinRetryDelay) |
| 40 | + } |
| 41 | + |
| 42 | + expectedBackupOptions := cr.Spec.Backup.Configuration.BackupOptions |
| 43 | + if expectedBackupOptions.OplogSpanMin != config.Backup.OplogSpanMin { |
| 44 | + t.Errorf("expected %f, got %f", expectedBackupOptions.OplogSpanMin, config.Backup.OplogSpanMin) |
| 45 | + } |
| 46 | + if len(expectedBackupOptions.Priority) != len(config.Backup.Priority) { |
| 47 | + t.Errorf("expected %d, got %d", len(expectedBackupOptions.Priority), len(config.Backup.Priority)) |
| 48 | + } |
| 49 | + if expectedBackupOptions.Priority["localhost:28019"] != config.Backup.Priority["localhost:28019"] { |
| 50 | + t.Errorf("expected %f, got %f", expectedBackupOptions.Priority["localhost:28019"], config.Backup.Priority["localhost:28019"]) |
| 51 | + } |
| 52 | + if expectedBackupOptions.Timeouts.Starting != config.Backup.Timeouts.Starting { |
| 53 | + t.Errorf("expected %d, got %d", expectedBackupOptions.Timeouts.Starting, config.Backup.Timeouts.Starting) |
| 54 | + } |
| 55 | + |
| 56 | + expectedRestoreOptions := cr.Spec.Backup.Configuration.RestoreOptions |
| 57 | + if expectedRestoreOptions.BatchSize != config.Restore.BatchSize { |
| 58 | + t.Errorf("expected %d, got %d", expectedRestoreOptions.BatchSize, config.Restore.BatchSize) |
| 59 | + } |
| 60 | + if expectedRestoreOptions.NumInsertionWorkers != config.Restore.NumInsertionWorkers { |
| 61 | + t.Errorf("expected %d, got %d", expectedRestoreOptions.NumInsertionWorkers, config.Restore.NumInsertionWorkers) |
| 62 | + } |
| 63 | + if expectedRestoreOptions.NumDownloadWorkers != config.Restore.NumDownloadWorkers { |
| 64 | + t.Errorf("expected %d, got %d", expectedRestoreOptions.NumDownloadWorkers, config.Restore.NumDownloadWorkers) |
| 65 | + } |
| 66 | + if expectedRestoreOptions.MaxDownloadBufferMb != config.Restore.MaxDownloadBufferMb { |
| 67 | + t.Errorf("expected %d, got %d", expectedRestoreOptions.MaxDownloadBufferMb, config.Restore.MaxDownloadBufferMb) |
| 68 | + } |
| 69 | + if expectedRestoreOptions.DownloadChunkMb != config.Restore.DownloadChunkMb { |
| 70 | + t.Errorf("expected %d, got %d", expectedRestoreOptions.DownloadChunkMb, config.Restore.DownloadChunkMb) |
| 71 | + } |
| 72 | + if expectedRestoreOptions.MongodLocation != config.Restore.MongodLocation { |
| 73 | + t.Errorf("expected %s, got %s", expectedRestoreOptions.MongodLocation, config.Restore.MongodLocation) |
| 74 | + } |
| 75 | + if len(expectedRestoreOptions.MongodLocationMap) != len(config.Restore.MongodLocationMap) { |
| 76 | + t.Errorf("expected %d, got %d", len(expectedRestoreOptions.MongodLocationMap), len(config.Restore.MongodLocationMap)) |
| 77 | + } |
| 78 | + if expectedRestoreOptions.MongodLocationMap["node01:1017"] != config.Restore.MongodLocationMap["node01:1017"] { |
| 79 | + t.Errorf("expected %s, got %s", expectedRestoreOptions.MongodLocationMap["node01:1017"], config.Restore.MongodLocationMap["node01:1017"]) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func buildFakeClient(t *testing.T) client.WithWatch { |
| 84 | + t.Helper() |
| 85 | + |
| 86 | + s := scheme.Scheme |
| 87 | + if err := scheme.AddToScheme(s); err != nil { |
| 88 | + t.Fatal(err, "failed to add client-go scheme") |
| 89 | + } |
| 90 | + |
| 91 | + secret := &corev1.Secret{ |
| 92 | + TypeMeta: metav1.TypeMeta{ |
| 93 | + APIVersion: "/v1", |
| 94 | + Kind: "Secret", |
| 95 | + }, |
| 96 | + ObjectMeta: metav1.ObjectMeta{ |
| 97 | + Name: "test-secret", |
| 98 | + Namespace: "test-namespace", |
| 99 | + }, |
| 100 | + } |
| 101 | + return fake.NewClientBuilder().WithScheme(s).WithObjects(secret).Build() |
| 102 | +} |
| 103 | + |
| 104 | +func expectedCR(t *testing.T) *api.PerconaServerMongoDB { |
| 105 | + t.Helper() |
| 106 | + |
| 107 | + data, err := os.ReadFile("testdata/cr-with-pbm-config.yaml") |
| 108 | + if err != nil { |
| 109 | + t.Fatal(err) |
| 110 | + } |
| 111 | + cr := new(api.PerconaServerMongoDB) |
| 112 | + |
| 113 | + if err := yaml.Unmarshal(data, cr); err != nil { |
| 114 | + t.Fatal(err) |
| 115 | + } |
| 116 | + |
| 117 | + return cr |
| 118 | +} |
0 commit comments