github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/pkg/ioutils/fswriters_test.go (about) 1 package ioutils 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "runtime" 9 "testing" 10 ) 11 12 var ( 13 testMode os.FileMode = 0640 14 ) 15 16 func init() { 17 // Windows does not support full Linux file mode 18 if runtime.GOOS == "windows" { 19 testMode = 0666 20 } 21 } 22 23 func TestAtomicWriteToFile(t *testing.T) { 24 tmpDir, err := ioutil.TempDir("", "atomic-writers-test") 25 if err != nil { 26 t.Fatalf("Error when creating temporary directory: %s", err) 27 } 28 defer os.RemoveAll(tmpDir) 29 30 expected := []byte("barbaz") 31 if err := AtomicWriteFile(filepath.Join(tmpDir, "foo"), expected, testMode); err != nil { 32 t.Fatalf("Error writing to file: %v", err) 33 } 34 35 actual, err := ioutil.ReadFile(filepath.Join(tmpDir, "foo")) 36 if err != nil { 37 t.Fatalf("Error reading from file: %v", err) 38 } 39 40 if !bytes.Equal(actual, expected) { 41 t.Fatalf("Data mismatch, expected %q, got %q", expected, actual) 42 } 43 44 st, err := os.Stat(filepath.Join(tmpDir, "foo")) 45 if err != nil { 46 t.Fatalf("Error statting file: %v", err) 47 } 48 if expected := os.FileMode(testMode); st.Mode() != expected { 49 t.Fatalf("Mode mismatched, expected %o, got %o", expected, st.Mode()) 50 } 51 } 52 53 func TestAtomicWriteSetCommit(t *testing.T) { 54 tmpDir, err := ioutil.TempDir("", "atomic-writerset-test") 55 if err != nil { 56 t.Fatalf("Error when creating temporary directory: %s", err) 57 } 58 defer os.RemoveAll(tmpDir) 59 60 if err := os.Mkdir(filepath.Join(tmpDir, "tmp"), 0700); err != nil { 61 t.Fatalf("Error creating tmp directory: %s", err) 62 } 63 64 targetDir := filepath.Join(tmpDir, "target") 65 ws, err := NewAtomicWriteSet(filepath.Join(tmpDir, "tmp")) 66 if err != nil { 67 t.Fatalf("Error creating atomic write set: %s", err) 68 } 69 70 expected := []byte("barbaz") 71 if err := ws.WriteFile("foo", expected, testMode); err != nil { 72 t.Fatalf("Error writing to file: %v", err) 73 } 74 75 if _, err := ioutil.ReadFile(filepath.Join(targetDir, "foo")); err == nil { 76 t.Fatalf("Expected error reading file where should not exist") 77 } 78 79 if err := ws.Commit(targetDir); err != nil { 80 t.Fatalf("Error committing file: %s", err) 81 } 82 83 actual, err := ioutil.ReadFile(filepath.Join(targetDir, "foo")) 84 if err != nil { 85 t.Fatalf("Error reading from file: %v", err) 86 } 87 88 if !bytes.Equal(actual, expected) { 89 t.Fatalf("Data mismatch, expected %q, got %q", expected, actual) 90 } 91 92 st, err := os.Stat(filepath.Join(targetDir, "foo")) 93 if err != nil { 94 t.Fatalf("Error statting file: %v", err) 95 } 96 if expected := os.FileMode(testMode); st.Mode() != expected { 97 t.Fatalf("Mode mismatched, expected %o, got %o", expected, st.Mode()) 98 } 99 100 } 101 102 func TestAtomicWriteSetCancel(t *testing.T) { 103 tmpDir, err := ioutil.TempDir("", "atomic-writerset-test") 104 if err != nil { 105 t.Fatalf("Error when creating temporary directory: %s", err) 106 } 107 defer os.RemoveAll(tmpDir) 108 109 if err := os.Mkdir(filepath.Join(tmpDir, "tmp"), 0700); err != nil { 110 t.Fatalf("Error creating tmp directory: %s", err) 111 } 112 113 ws, err := NewAtomicWriteSet(filepath.Join(tmpDir, "tmp")) 114 if err != nil { 115 t.Fatalf("Error creating atomic write set: %s", err) 116 } 117 118 expected := []byte("barbaz") 119 if err := ws.WriteFile("foo", expected, testMode); err != nil { 120 t.Fatalf("Error writing to file: %v", err) 121 } 122 123 if err := ws.Cancel(); err != nil { 124 t.Fatalf("Error committing file: %s", err) 125 } 126 127 if _, err := ioutil.ReadFile(filepath.Join(tmpDir, "target", "foo")); err == nil { 128 t.Fatalf("Expected error reading file where should not exist") 129 } else if !os.IsNotExist(err) { 130 t.Fatalf("Unexpected error reading file: %s", err) 131 } 132 }