github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/pkg/backup/backup_test.go (about) 1 // Copyright 2015 The rkt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package backup 16 17 import ( 18 "fmt" 19 "io/ioutil" 20 "os" 21 "path/filepath" 22 "strconv" 23 "testing" 24 ) 25 26 const tstprefix = "backup-test" 27 28 // TestBackup tests backup creation with limit 4 29 func TestBackup(t *testing.T) { 30 dir, err := ioutil.TempDir("", tstprefix) 31 if err != nil { 32 t.Fatalf("error creating tempdir: %v", err) 33 } 34 defer os.RemoveAll(dir) 35 srcDir := filepath.Join(dir, "src") 36 backupsDir := filepath.Join(dir, "backups") 37 limit := 4 38 39 for i := 0; i < limit*2; i++ { 40 if err := os.RemoveAll(srcDir); err != nil { 41 t.Fatalf("error removing srcDir: %v", err) 42 } 43 if err := os.MkdirAll(srcDir, 0755); err != nil { 44 t.Fatalf("error creating srcDir: %v", err) 45 } 46 if err := touch(filepath.Join(srcDir, strconv.Itoa(i))); err != nil { 47 t.Fatalf("error creating a file: %v", err) 48 } 49 if err := CreateBackup(srcDir, backupsDir, limit); err != nil { 50 t.Fatalf("error creating a backup: %v", err) 51 } 52 if err := checkBackups(backupsDir, limit, i); err != nil { 53 t.Fatalf("error checking the backup: %v", err) 54 } 55 } 56 } 57 58 func touch(fileName string) error { 59 file, err := os.Create(fileName) 60 if err != nil { 61 return err 62 } 63 if err := file.Close(); err != nil { 64 return err 65 } 66 return nil 67 } 68 69 func checkBackups(backupsDir string, limit, i int) error { 70 expected := getExpectedTree(limit, i) 71 return checkDirectory(backupsDir, expected) 72 } 73 74 // Expectations for limit L 75 // 76 // for iteration I (0, 1, 2, ...) 77 // at most D srcDir (where D = min(L,I + 1) 78 // dir 0 with file I 79 // dir 1 with file I - 1 80 // ... 81 // dir D - 1 with file I - L 82 func getExpectedTree(limit, iteration int) map[string]string { 83 dirCount := iteration 84 if iteration > limit { 85 dirCount = limit 86 } 87 expected := make(map[string]string, dirCount) 88 for j := 0; j <= dirCount; j++ { 89 expected[strconv.Itoa(j)] = strconv.Itoa(iteration - j) 90 } 91 return expected 92 } 93 94 func checkDirectory(dir string, expected map[string]string) error { 95 list, err := ioutil.ReadDir(dir) 96 if err != nil { 97 return err 98 } 99 for _, fi := range list { 100 if !fi.IsDir() { 101 return fmt.Errorf("%s is not a directory", fi.Name()) 102 } 103 dirName := fi.Name() 104 if _, err := strconv.Atoi(dirName); err != nil { 105 return fmt.Errorf("Name is not a number: %v", err) 106 } 107 expectedFileName, ok := expected[dirName] 108 if !ok { 109 return fmt.Errorf("Unexpected name found: %s", dirName) 110 } 111 subList, err := ioutil.ReadDir(filepath.Join(dir, dirName)) 112 if err != nil { 113 return fmt.Errorf("Failed to get a list of files in %s: %v", 114 dirName, err) 115 } 116 filesCount := len(subList) 117 if filesCount != 1 { 118 return fmt.Errorf("Expected only 1 file in %s, got %d", 119 dirName, filesCount) 120 } 121 gottenFileName := subList[0].Name() 122 if gottenFileName != expectedFileName { 123 return fmt.Errorf("Expected %s in %s, got %s", 124 expectedFileName, dirName, gottenFileName) 125 } 126 } 127 return nil 128 }