github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/builder/tarsum_test.go (about) 1 package builder 2 3 import ( 4 "bufio" 5 "bytes" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "testing" 10 11 "github.com/docker/docker/pkg/archive" 12 "github.com/docker/docker/pkg/reexec" 13 ) 14 15 const ( 16 filename = "test" 17 contents = "contents test" 18 ) 19 20 func init() { 21 reexec.Init() 22 } 23 24 func TestCloseRootDirectory(t *testing.T) { 25 contextDir, err := ioutil.TempDir("", "builder-tarsum-test") 26 27 if err != nil { 28 t.Fatalf("Error with creating temporary directory: %s", err) 29 } 30 31 tarsum := &tarSumContext{root: contextDir} 32 33 err = tarsum.Close() 34 35 if err != nil { 36 t.Fatalf("Error while executing Close: %s", err) 37 } 38 39 _, err = os.Stat(contextDir) 40 41 if !os.IsNotExist(err) { 42 t.Fatalf("Directory should not exist at this point") 43 defer os.RemoveAll(contextDir) 44 } 45 } 46 47 func TestOpenFile(t *testing.T) { 48 contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") 49 defer cleanup() 50 51 createTestTempFile(t, contextDir, filename, contents, 0777) 52 53 tarSum := &tarSumContext{root: contextDir} 54 55 file, err := tarSum.Open(filename) 56 57 if err != nil { 58 t.Fatalf("Error when executing Open: %s", err) 59 } 60 61 defer file.Close() 62 63 scanner := bufio.NewScanner(file) 64 buff := bytes.NewBufferString("") 65 66 for scanner.Scan() { 67 buff.WriteString(scanner.Text()) 68 } 69 70 if contents != buff.String() { 71 t.Fatalf("Contents are not equal. Expected: %s, got: %s", contents, buff.String()) 72 } 73 74 } 75 76 func TestOpenNotExisting(t *testing.T) { 77 contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") 78 defer cleanup() 79 80 tarSum := &tarSumContext{root: contextDir} 81 82 file, err := tarSum.Open("not-existing") 83 84 if file != nil { 85 t.Fatal("Opened file should be nil") 86 } 87 88 if !os.IsNotExist(err) { 89 t.Fatalf("Error when executing Open: %s", err) 90 } 91 } 92 93 func TestStatFile(t *testing.T) { 94 contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") 95 defer cleanup() 96 97 testFilename := createTestTempFile(t, contextDir, filename, contents, 0777) 98 99 tarSum := &tarSumContext{root: contextDir} 100 101 relPath, fileInfo, err := tarSum.Stat(filename) 102 103 if err != nil { 104 t.Fatalf("Error when executing Stat: %s", err) 105 } 106 107 if relPath != filename { 108 t.Fatalf("Relative path should be equal to %s, got %s", filename, relPath) 109 } 110 111 if fileInfo.Path() != testFilename { 112 t.Fatalf("Full path should be equal to %s, got %s", testFilename, fileInfo.Path()) 113 } 114 } 115 116 func TestStatSubdir(t *testing.T) { 117 contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") 118 defer cleanup() 119 120 contextSubdir := createTestTempSubdir(t, contextDir, "builder-tarsum-test-subdir") 121 122 testFilename := createTestTempFile(t, contextSubdir, filename, contents, 0777) 123 124 tarSum := &tarSumContext{root: contextDir} 125 126 relativePath, err := filepath.Rel(contextDir, testFilename) 127 128 if err != nil { 129 t.Fatalf("Error when getting relative path: %s", err) 130 } 131 132 relPath, fileInfo, err := tarSum.Stat(relativePath) 133 134 if err != nil { 135 t.Fatalf("Error when executing Stat: %s", err) 136 } 137 138 if relPath != relativePath { 139 t.Fatalf("Relative path should be equal to %s, got %s", relativePath, relPath) 140 } 141 142 if fileInfo.Path() != testFilename { 143 t.Fatalf("Full path should be equal to %s, got %s", testFilename, fileInfo.Path()) 144 } 145 } 146 147 func TestStatNotExisting(t *testing.T) { 148 contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") 149 defer cleanup() 150 151 tarSum := &tarSumContext{root: contextDir} 152 153 relPath, fileInfo, err := tarSum.Stat("not-existing") 154 155 if relPath != "" { 156 t.Fatal("Relative path should be nil") 157 } 158 159 if fileInfo != nil { 160 t.Fatalf("File info should be nil") 161 } 162 163 if !os.IsNotExist(err) { 164 t.Fatalf("This file should not exist: %s", err) 165 } 166 } 167 168 func TestRemoveDirectory(t *testing.T) { 169 contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") 170 defer cleanup() 171 172 contextSubdir := createTestTempSubdir(t, contextDir, "builder-tarsum-test-subdir") 173 174 relativePath, err := filepath.Rel(contextDir, contextSubdir) 175 176 if err != nil { 177 t.Fatalf("Error when getting relative path: %s", err) 178 } 179 180 tarSum := &tarSumContext{root: contextDir} 181 182 err = tarSum.Remove(relativePath) 183 184 if err != nil { 185 t.Fatalf("Error when executing Remove: %s", err) 186 } 187 188 _, err = os.Stat(contextSubdir) 189 190 if !os.IsNotExist(err) { 191 t.Fatalf("Directory should not exist at this point") 192 } 193 } 194 195 func TestMakeTarSumContext(t *testing.T) { 196 contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") 197 defer cleanup() 198 199 createTestTempFile(t, contextDir, filename, contents, 0777) 200 201 tarStream, err := archive.Tar(contextDir, archive.Uncompressed) 202 203 if err != nil { 204 t.Fatalf("error: %s", err) 205 } 206 207 defer tarStream.Close() 208 209 tarSum, err := MakeTarSumContext(tarStream) 210 211 if err != nil { 212 t.Fatalf("Error when executing MakeTarSumContext: %s", err) 213 } 214 215 if tarSum == nil { 216 t.Fatalf("Tar sum context should not be nil") 217 } 218 } 219 220 func TestWalkWithoutError(t *testing.T) { 221 contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") 222 defer cleanup() 223 224 contextSubdir := createTestTempSubdir(t, contextDir, "builder-tarsum-test-subdir") 225 226 createTestTempFile(t, contextSubdir, filename, contents, 0777) 227 228 tarSum := &tarSumContext{root: contextDir} 229 230 walkFun := func(path string, fi FileInfo, err error) error { 231 return nil 232 } 233 234 err := tarSum.Walk(contextSubdir, walkFun) 235 236 if err != nil { 237 t.Fatalf("Error when executing Walk: %s", err) 238 } 239 } 240 241 type WalkError struct { 242 } 243 244 func (we WalkError) Error() string { 245 return "Error when executing Walk" 246 } 247 248 func TestWalkWithError(t *testing.T) { 249 contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") 250 defer cleanup() 251 252 contextSubdir := createTestTempSubdir(t, contextDir, "builder-tarsum-test-subdir") 253 254 tarSum := &tarSumContext{root: contextDir} 255 256 walkFun := func(path string, fi FileInfo, err error) error { 257 return WalkError{} 258 } 259 260 err := tarSum.Walk(contextSubdir, walkFun) 261 262 if err == nil { 263 t.Fatalf("Error should not be nil") 264 } 265 }