github.com/tsuna/docker@v1.7.0-rc3/pkg/fileutils/fileutils_test.go (about) 1 package fileutils 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path" 7 "testing" 8 ) 9 10 // CopyFile with invalid src 11 func TestCopyFileWithInvalidSrc(t *testing.T) { 12 tempFolder, err := ioutil.TempDir("", "docker-fileutils-test") 13 defer os.RemoveAll(tempFolder) 14 if err != nil { 15 t.Fatal(err) 16 } 17 bytes, err := CopyFile("/invalid/file/path", path.Join(tempFolder, "dest")) 18 if err == nil { 19 t.Fatal("Should have fail to copy an invalid src file") 20 } 21 if bytes != 0 { 22 t.Fatal("Should have written 0 bytes") 23 } 24 25 } 26 27 // CopyFile with invalid dest 28 func TestCopyFileWithInvalidDest(t *testing.T) { 29 tempFolder, err := ioutil.TempDir("", "docker-fileutils-test") 30 defer os.RemoveAll(tempFolder) 31 if err != nil { 32 t.Fatal(err) 33 } 34 src := path.Join(tempFolder, "file") 35 err = ioutil.WriteFile(src, []byte("content"), 0740) 36 if err != nil { 37 t.Fatal(err) 38 } 39 bytes, err := CopyFile(src, path.Join(tempFolder, "/invalid/dest/path")) 40 if err == nil { 41 t.Fatal("Should have fail to copy an invalid src file") 42 } 43 if bytes != 0 { 44 t.Fatal("Should have written 0 bytes") 45 } 46 47 } 48 49 // CopyFile with same src and dest 50 func TestCopyFileWithSameSrcAndDest(t *testing.T) { 51 tempFolder, err := ioutil.TempDir("", "docker-fileutils-test") 52 defer os.RemoveAll(tempFolder) 53 if err != nil { 54 t.Fatal(err) 55 } 56 file := path.Join(tempFolder, "file") 57 err = ioutil.WriteFile(file, []byte("content"), 0740) 58 if err != nil { 59 t.Fatal(err) 60 } 61 bytes, err := CopyFile(file, file) 62 if err != nil { 63 t.Fatal(err) 64 } 65 if bytes != 0 { 66 t.Fatal("Should have written 0 bytes as it is the same file.") 67 } 68 } 69 70 // CopyFile with same src and dest but path is different and not clean 71 func TestCopyFileWithSameSrcAndDestWithPathNameDifferent(t *testing.T) { 72 tempFolder, err := ioutil.TempDir("", "docker-fileutils-test") 73 defer os.RemoveAll(tempFolder) 74 if err != nil { 75 t.Fatal(err) 76 } 77 testFolder := path.Join(tempFolder, "test") 78 err = os.MkdirAll(testFolder, 0740) 79 if err != nil { 80 t.Fatal(err) 81 } 82 file := path.Join(testFolder, "file") 83 sameFile := testFolder + "/../test/file" 84 err = ioutil.WriteFile(file, []byte("content"), 0740) 85 if err != nil { 86 t.Fatal(err) 87 } 88 bytes, err := CopyFile(file, sameFile) 89 if err != nil { 90 t.Fatal(err) 91 } 92 if bytes != 0 { 93 t.Fatal("Should have written 0 bytes as it is the same file.") 94 } 95 } 96 97 func TestCopyFile(t *testing.T) { 98 tempFolder, err := ioutil.TempDir("", "docker-fileutils-test") 99 defer os.RemoveAll(tempFolder) 100 if err != nil { 101 t.Fatal(err) 102 } 103 src := path.Join(tempFolder, "src") 104 dest := path.Join(tempFolder, "dest") 105 ioutil.WriteFile(src, []byte("content"), 0777) 106 ioutil.WriteFile(dest, []byte("destContent"), 0777) 107 bytes, err := CopyFile(src, dest) 108 if err != nil { 109 t.Fatal(err) 110 } 111 if bytes != 7 { 112 t.Fatalf("Should have written %d bytes but wrote %d", 7, bytes) 113 } 114 actual, err := ioutil.ReadFile(dest) 115 if err != nil { 116 t.Fatal(err) 117 } 118 if string(actual) != "content" { 119 t.Fatalf("Dest content was '%s', expected '%s'", string(actual), "content") 120 } 121 } 122 123 // Reading a symlink to a directory must return the directory 124 func TestReadSymlinkedDirectoryExistingDirectory(t *testing.T) { 125 var err error 126 if err = os.Mkdir("/tmp/testReadSymlinkToExistingDirectory", 0777); err != nil { 127 t.Errorf("failed to create directory: %s", err) 128 } 129 130 if err = os.Symlink("/tmp/testReadSymlinkToExistingDirectory", "/tmp/dirLinkTest"); err != nil { 131 t.Errorf("failed to create symlink: %s", err) 132 } 133 134 var path string 135 if path, err = ReadSymlinkedDirectory("/tmp/dirLinkTest"); err != nil { 136 t.Fatalf("failed to read symlink to directory: %s", err) 137 } 138 139 if path != "/tmp/testReadSymlinkToExistingDirectory" { 140 t.Fatalf("symlink returned unexpected directory: %s", path) 141 } 142 143 if err = os.Remove("/tmp/testReadSymlinkToExistingDirectory"); err != nil { 144 t.Errorf("failed to remove temporary directory: %s", err) 145 } 146 147 if err = os.Remove("/tmp/dirLinkTest"); err != nil { 148 t.Errorf("failed to remove symlink: %s", err) 149 } 150 } 151 152 // Reading a non-existing symlink must fail 153 func TestReadSymlinkedDirectoryNonExistingSymlink(t *testing.T) { 154 var path string 155 var err error 156 if path, err = ReadSymlinkedDirectory("/tmp/test/foo/Non/ExistingPath"); err == nil { 157 t.Fatalf("error expected for non-existing symlink") 158 } 159 160 if path != "" { 161 t.Fatalf("expected empty path, but '%s' was returned", path) 162 } 163 } 164 165 // Reading a symlink to a file must fail 166 func TestReadSymlinkedDirectoryToFile(t *testing.T) { 167 var err error 168 var file *os.File 169 170 if file, err = os.Create("/tmp/testReadSymlinkToFile"); err != nil { 171 t.Fatalf("failed to create file: %s", err) 172 } 173 174 file.Close() 175 176 if err = os.Symlink("/tmp/testReadSymlinkToFile", "/tmp/fileLinkTest"); err != nil { 177 t.Errorf("failed to create symlink: %s", err) 178 } 179 180 var path string 181 if path, err = ReadSymlinkedDirectory("/tmp/fileLinkTest"); err == nil { 182 t.Fatalf("ReadSymlinkedDirectory on a symlink to a file should've failed") 183 } 184 185 if path != "" { 186 t.Fatalf("path should've been empty: %s", path) 187 } 188 189 if err = os.Remove("/tmp/testReadSymlinkToFile"); err != nil { 190 t.Errorf("failed to remove file: %s", err) 191 } 192 193 if err = os.Remove("/tmp/fileLinkTest"); err != nil { 194 t.Errorf("failed to remove symlink: %s", err) 195 } 196 } 197 198 func TestWildcardMatches(t *testing.T) { 199 match, _ := Matches("fileutils.go", []string{"*"}) 200 if match != true { 201 t.Errorf("failed to get a wildcard match, got %v", match) 202 } 203 } 204 205 // A simple pattern match should return true. 206 func TestPatternMatches(t *testing.T) { 207 match, _ := Matches("fileutils.go", []string{"*.go"}) 208 if match != true { 209 t.Errorf("failed to get a match, got %v", match) 210 } 211 } 212 213 // An exclusion followed by an inclusion should return true. 214 func TestExclusionPatternMatchesPatternBefore(t *testing.T) { 215 match, _ := Matches("fileutils.go", []string{"!fileutils.go", "*.go"}) 216 if match != true { 217 t.Errorf("failed to get true match on exclusion pattern, got %v", match) 218 } 219 } 220 221 // A folder pattern followed by an exception should return false. 222 func TestPatternMatchesFolderExclusions(t *testing.T) { 223 match, _ := Matches("docs/README.md", []string{"docs", "!docs/README.md"}) 224 if match != false { 225 t.Errorf("failed to get a false match on exclusion pattern, got %v", match) 226 } 227 } 228 229 // A folder pattern followed by an exception should return false. 230 func TestPatternMatchesFolderWithSlashExclusions(t *testing.T) { 231 match, _ := Matches("docs/README.md", []string{"docs/", "!docs/README.md"}) 232 if match != false { 233 t.Errorf("failed to get a false match on exclusion pattern, got %v", match) 234 } 235 } 236 237 // A folder pattern followed by an exception should return false. 238 func TestPatternMatchesFolderWildcardExclusions(t *testing.T) { 239 match, _ := Matches("docs/README.md", []string{"docs/*", "!docs/README.md"}) 240 if match != false { 241 t.Errorf("failed to get a false match on exclusion pattern, got %v", match) 242 } 243 } 244 245 // A pattern followed by an exclusion should return false. 246 func TestExclusionPatternMatchesPatternAfter(t *testing.T) { 247 match, _ := Matches("fileutils.go", []string{"*.go", "!fileutils.go"}) 248 if match != false { 249 t.Errorf("failed to get false match on exclusion pattern, got %v", match) 250 } 251 } 252 253 // A filename evaluating to . should return false. 254 func TestExclusionPatternMatchesWholeDirectory(t *testing.T) { 255 match, _ := Matches(".", []string{"*.go"}) 256 if match != false { 257 t.Errorf("failed to get false match on ., got %v", match) 258 } 259 } 260 261 // A single ! pattern should return an error. 262 func TestSingleExclamationError(t *testing.T) { 263 _, err := Matches("fileutils.go", []string{"!"}) 264 if err == nil { 265 t.Errorf("failed to get an error for a single exclamation point, got %v", err) 266 } 267 } 268 269 // A string preceded with a ! should return true from Exclusion. 270 func TestExclusion(t *testing.T) { 271 exclusion := Exclusion("!") 272 if !exclusion { 273 t.Errorf("failed to get true for a single !, got %v", exclusion) 274 } 275 } 276 277 // Matches with no patterns 278 func TestMatchesWithNoPatterns(t *testing.T) { 279 matches, err := Matches("/any/path/there", []string{}) 280 if err != nil { 281 t.Fatal(err) 282 } 283 if matches { 284 t.Fatalf("Should not have match anything") 285 } 286 } 287 288 // Matches with malformed patterns 289 func TestMatchesWithMalformedPatterns(t *testing.T) { 290 matches, err := Matches("/any/path/there", []string{"["}) 291 if err == nil { 292 t.Fatal("Should have failed because of a malformed syntax in the pattern") 293 } 294 if matches { 295 t.Fatalf("Should not have match anything") 296 } 297 } 298 299 // An empty string should return true from Empty. 300 func TestEmpty(t *testing.T) { 301 empty := Empty("") 302 if !empty { 303 t.Errorf("failed to get true for an empty string, got %v", empty) 304 } 305 } 306 307 func TestCleanPatterns(t *testing.T) { 308 cleaned, _, _, _ := CleanPatterns([]string{"docs", "config"}) 309 if len(cleaned) != 2 { 310 t.Errorf("expected 2 element slice, got %v", len(cleaned)) 311 } 312 } 313 314 func TestCleanPatternsStripEmptyPatterns(t *testing.T) { 315 cleaned, _, _, _ := CleanPatterns([]string{"docs", "config", ""}) 316 if len(cleaned) != 2 { 317 t.Errorf("expected 2 element slice, got %v", len(cleaned)) 318 } 319 } 320 321 func TestCleanPatternsExceptionFlag(t *testing.T) { 322 _, _, exceptions, _ := CleanPatterns([]string{"docs", "!docs/README.md"}) 323 if !exceptions { 324 t.Errorf("expected exceptions to be true, got %v", exceptions) 325 } 326 } 327 328 func TestCleanPatternsLeadingSpaceTrimmed(t *testing.T) { 329 _, _, exceptions, _ := CleanPatterns([]string{"docs", " !docs/README.md"}) 330 if !exceptions { 331 t.Errorf("expected exceptions to be true, got %v", exceptions) 332 } 333 } 334 335 func TestCleanPatternsTrailingSpaceTrimmed(t *testing.T) { 336 _, _, exceptions, _ := CleanPatterns([]string{"docs", "!docs/README.md "}) 337 if !exceptions { 338 t.Errorf("expected exceptions to be true, got %v", exceptions) 339 } 340 } 341 342 func TestCleanPatternsErrorSingleException(t *testing.T) { 343 _, _, _, err := CleanPatterns([]string{"!"}) 344 if err == nil { 345 t.Errorf("expected error on single exclamation point, got %v", err) 346 } 347 } 348 349 func TestCleanPatternsFolderSplit(t *testing.T) { 350 _, dirs, _, _ := CleanPatterns([]string{"docs/config/CONFIG.md"}) 351 if dirs[0][0] != "docs" { 352 t.Errorf("expected first element in dirs slice to be docs, got %v", dirs[0][1]) 353 } 354 if dirs[0][1] != "config" { 355 t.Errorf("expected first element in dirs slice to be config, got %v", dirs[0][1]) 356 } 357 }