|
| 1 | +/* |
| 2 | +Copyright 2024 Huawei Cloud Computing Technologies Co., Ltd. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package recover |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/openGemini/openGemini/lib/config" |
| 25 | + "github.com/openGemini/openGemini/lib/fileops" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | +) |
| 28 | + |
| 29 | +func TestRunBackupRecoverConfig(t *testing.T) { |
| 30 | + t.Run("1", func(t *testing.T) { |
| 31 | + rcConifg := &RecoverConfig{} |
| 32 | + err := BackupRecover(rcConifg, nil) |
| 33 | + if err == nil { |
| 34 | + t.Fail() |
| 35 | + } |
| 36 | + }) |
| 37 | + t.Run("2", func(t *testing.T) { |
| 38 | + rcConifg := &RecoverConfig{ |
| 39 | + FullBackupDataPath: "/", |
| 40 | + RecoverMode: FullAndIncRecoverMode, |
| 41 | + } |
| 42 | + err := BackupRecover(rcConifg, nil) |
| 43 | + if err == nil { |
| 44 | + t.Fail() |
| 45 | + } |
| 46 | + }) |
| 47 | + t.Run("3", func(t *testing.T) { |
| 48 | + rcConifg := &RecoverConfig{ |
| 49 | + FullBackupDataPath: "/", |
| 50 | + RecoverMode: "3", |
| 51 | + } |
| 52 | + err := BackupRecover(rcConifg, nil) |
| 53 | + if err == nil { |
| 54 | + t.Fail() |
| 55 | + } |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +func TestRecoverMeta(t *testing.T) { |
| 60 | + tmpDir := t.TempDir() |
| 61 | + |
| 62 | + fullBackupPath := filepath.Join(tmpDir, "openGemini/backup_dir/backup") |
| 63 | + incBackupPath := filepath.Join(tmpDir, "openGemini/backup_dir/backup_inc") |
| 64 | + recoverConfig := &RecoverConfig{ |
| 65 | + RecoverMode: "2", |
| 66 | + FullBackupDataPath: fullBackupPath, |
| 67 | + IncBackupDataPath: incBackupPath, |
| 68 | + } |
| 69 | + tsRecover := &config.TsRecover{ |
| 70 | + Data: config.Store{ |
| 71 | + DataDir: filepath.Join(tmpDir, "openGemini/backup_dir/data"), |
| 72 | + MetaDir: filepath.Join(tmpDir, "openGemini/backup_dir/meta"), |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + t.Run("isInc=true", func(t *testing.T) { |
| 77 | + content := `{"metaIds":["1"],"isNode":true}` |
| 78 | + CreateFile(filepath.Join(tmpDir, "openGemini/backup_dir/backup_inc/meta_backup/backup_log/meta_backup_log.json"), content) |
| 79 | + recoverMeta(tsRecover, recoverConfig, true) |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("isInc=false", func(t *testing.T) { |
| 83 | + content := `{"metaIds":["1","2"],"isNode":false}` |
| 84 | + CreateFile(filepath.Join(tmpDir, "openGemini/backup_dir/backup/meta_backup/backup_log/meta_backup_log.json"), content) |
| 85 | + recoverMeta(tsRecover, recoverConfig, false) |
| 86 | + }) |
| 87 | + |
| 88 | + os.RemoveAll(filepath.Join(tmpDir, "openGemini/backup_dir")) |
| 89 | +} |
| 90 | + |
| 91 | +func TestRecoverError(t *testing.T) { |
| 92 | + fullBackupPath := "/tmp/openGemini/backup_dir/backup" |
| 93 | + incBackupPath := "/tmp/openGemini/backup_dir/backup_inc" |
| 94 | + |
| 95 | + recoverConfig := &RecoverConfig{ |
| 96 | + RecoverMode: "2", |
| 97 | + FullBackupDataPath: fullBackupPath, |
| 98 | + IncBackupDataPath: incBackupPath, |
| 99 | + } |
| 100 | + tsRecover := &config.TsRecover{ |
| 101 | + Data: config.Store{ |
| 102 | + DataDir: "/tmp/openGemini/backup_dir/data", |
| 103 | + MetaDir: "/tmp/openGemini/backup_dir/meta", |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + t.Run("1", func(t *testing.T) { |
| 108 | + err := recoverWithFull(tsRecover, recoverConfig) |
| 109 | + if err == nil { |
| 110 | + t.Fail() |
| 111 | + } |
| 112 | + }) |
| 113 | + |
| 114 | + t.Run("2", func(t *testing.T) { |
| 115 | + err := recoverWithFullAndInc(tsRecover, recoverConfig) |
| 116 | + if err == nil { |
| 117 | + t.Fail() |
| 118 | + } |
| 119 | + }) |
| 120 | +} |
| 121 | + |
| 122 | +func Test_TraversalBackupLogFile_Error(t *testing.T) { |
| 123 | + fullBackupPath := "/tmp/openGemini/backup_dir/backup" |
| 124 | + incBackupPath := "/tmp/openGemini/backup_dir/backup_inc" |
| 125 | + |
| 126 | + recoverConfig := &RecoverConfig{ |
| 127 | + RecoverMode: "2", |
| 128 | + FullBackupDataPath: fullBackupPath, |
| 129 | + IncBackupDataPath: incBackupPath, |
| 130 | + } |
| 131 | + |
| 132 | + err := traversalBackupLogFile(recoverConfig, fullBackupPath, copyWithFull, false) |
| 133 | + |
| 134 | + if err == nil { |
| 135 | + t.Fatal() |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func Test_traversalIncBackupLogFile_Error(t *testing.T) { |
| 140 | + fullBackupPath := "/tmp/openGemini/backup_dir/backup" |
| 141 | + incBackupPath := "/tmp/openGemini/backup_dir/backup_inc" |
| 142 | + |
| 143 | + recoverConfig := &RecoverConfig{ |
| 144 | + RecoverMode: "1", |
| 145 | + FullBackupDataPath: fullBackupPath, |
| 146 | + IncBackupDataPath: incBackupPath, |
| 147 | + } |
| 148 | + |
| 149 | + err := traversalIncBackupLogFile(recoverConfig, incBackupPath) |
| 150 | + |
| 151 | + if err == nil { |
| 152 | + t.Fatal() |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func Test_MergeFileList(t *testing.T) { |
| 157 | + fullBackupPath := "/tmp/openGemini/backup_dir/backup" |
| 158 | + incBackupPath := "/tmp/openGemini/backup_dir/backup_inc" |
| 159 | + |
| 160 | + recoverConfig := &RecoverConfig{ |
| 161 | + RecoverMode: "2", |
| 162 | + FullBackupDataPath: fullBackupPath, |
| 163 | + IncBackupDataPath: incBackupPath, |
| 164 | + } |
| 165 | + |
| 166 | + t.Run("1", func(t *testing.T) { |
| 167 | + listMap := map[string][][]string{"name1": [][]string{[]string{"/tmp/openGemini/backup_dir/path1.file"}, []string{"/tmp/openGemini/backup_dir/path2.file"}}} |
| 168 | + deleteListMap := map[string][][]string{"name1": [][]string{[]string{"/tmp/openGemini/backup_dir/path2.file"}}} |
| 169 | + |
| 170 | + CreateFile("/tmp/openGemini/backup_dir/backup/data_backup/tmp/openGemini/backup_dir/path1.file", "1") |
| 171 | + CreateFile("/tmp/openGemini/backup_dir/backup/data_backup/tmp/openGemini/backup_dir/path2.file", "2") |
| 172 | + CreateFile("/tmp/openGemini/backup_dir/backup_inc/data_backup/tmp/openGemini/backup_dir/path3.file", "3") |
| 173 | + |
| 174 | + err := mergeFileList(recoverConfig, listMap, deleteListMap) |
| 175 | + assert.NoError(t, err) |
| 176 | + |
| 177 | + os.RemoveAll("/tmp/openGemini/backup_dir") |
| 178 | + }) |
| 179 | + |
| 180 | + t.Run("2", func(t *testing.T) { |
| 181 | + listMap := map[string][][]string{"name1": [][]string{[]string{"/tmp/openGemini/backup_dir/path1.file"}, []string{"/tmp/openGemini/backup_dir/path2.file"}}} |
| 182 | + deleteListMap := map[string][][]string{"name1": [][]string{[]string{"/tmp/openGemini/backup_dir/path2.file"}}} |
| 183 | + |
| 184 | + err := mergeFileList(recoverConfig, listMap, deleteListMap) |
| 185 | + if err == nil { |
| 186 | + t.Fatal(err) |
| 187 | + } |
| 188 | + }) |
| 189 | + |
| 190 | +} |
| 191 | + |
| 192 | +func CreateFile(path, content string) { |
| 193 | + p, _ := filepath.Split(path) |
| 194 | + _ = os.MkdirAll(p, 0750) |
| 195 | + fd, _ := fileops.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0640) |
| 196 | + fd.Write([]byte(content)) |
| 197 | + fd.Close() |
| 198 | +} |
0 commit comments