1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
package ioutils
import (
"bytes"
"os"
"path/filepath"
"runtime"
"testing"
)
var testMode os.FileMode = 0o640
func init() {
// Windows does not support full Linux file mode
if runtime.GOOS == "windows" {
testMode = 0o666
}
}
func TestAtomicWriteToFile(t *testing.T) {
tmpDir := t.TempDir()
expected := []byte("barbaz")
if err := AtomicWriteFile(filepath.Join(tmpDir, "foo"), expected, testMode); err != nil {
t.Fatalf("Error writing to file: %v", err)
}
actual, err := os.ReadFile(filepath.Join(tmpDir, "foo"))
if err != nil {
t.Fatalf("Error reading from file: %v", err)
}
if !bytes.Equal(actual, expected) {
t.Fatalf("Data mismatch, expected %q, got %q", expected, actual)
}
st, err := os.Stat(filepath.Join(tmpDir, "foo"))
if err != nil {
t.Fatalf("Error statting file: %v", err)
}
if expected := testMode; st.Mode() != expected {
t.Fatalf("Mode mismatched, expected %o, got %o", expected, st.Mode())
}
}
func TestAtomicCommitAndRollbackFile(t *testing.T) {
tmpDir := t.TempDir()
path := filepath.Join(tmpDir, "foo")
oldData := "olddata"
newData := "newdata"
check := func(n int, initData string, writeData string, expected string, explicit bool, commit bool) {
if err := os.WriteFile(path, []byte(initData), 0o644); err != nil {
t.Fatalf("Failed creating initial file: %v", err)
}
opts := &AtomicFileWriterOptions{ExplicitCommit: explicit}
w, err := NewAtomicFileWriterWithOpts(filepath.Join(tmpDir, "foo"), 0o644, opts)
if err != nil {
t.Fatalf("(%d) Failed creating writer: %v", n, err)
}
if _, err := w.Write([]byte(writeData)); err != nil {
t.Fatalf("(%d) Failed writing content: %v", n, err)
}
if commit {
if err := w.Commit(); err != nil {
t.Fatalf("(%d) Failed committing writer: %v", n, err)
}
}
if err := w.Close(); err != nil {
t.Fatalf("(%d) Failed closing writer: %v", n, err)
}
actual, err := os.ReadFile(filepath.Join(tmpDir, "foo"))
if err != nil {
t.Fatalf("(%d) Error reading from file: %v", n, err)
}
// Verify write never happened since no call to commit
if !bytes.Equal(actual, []byte(expected)) {
t.Fatalf("(%d) Data mismatch, expected %q, got %q", n, expected, actual)
}
}
check(1, oldData, newData, oldData, true, false)
check(2, oldData, newData, newData, true, true)
check(3, oldData, newData, newData, false, false)
check(4, oldData, newData, newData, false, true)
}
func TestAtomicWriteSetCommit(t *testing.T) {
tmpDir := t.TempDir()
if err := os.Mkdir(filepath.Join(tmpDir, "tmp"), 0o700); err != nil {
t.Fatalf("Error creating tmp directory: %s", err)
}
targetDir := filepath.Join(tmpDir, "target")
ws, err := NewAtomicWriteSet(filepath.Join(tmpDir, "tmp"))
if err != nil {
t.Fatalf("Error creating atomic write set: %s", err)
}
expected := []byte("barbaz")
if err := ws.WriteFile("foo", expected, testMode); err != nil {
t.Fatalf("Error writing to file: %v", err)
}
if _, err := os.ReadFile(filepath.Join(targetDir, "foo")); err == nil {
t.Fatalf("Expected error reading file where should not exist")
}
if err := ws.Commit(targetDir); err != nil {
t.Fatalf("Error committing file: %s", err)
}
actual, err := os.ReadFile(filepath.Join(targetDir, "foo"))
if err != nil {
t.Fatalf("Error reading from file: %v", err)
}
if !bytes.Equal(actual, expected) {
t.Fatalf("Data mismatch, expected %q, got %q", expected, actual)
}
st, err := os.Stat(filepath.Join(targetDir, "foo"))
if err != nil {
t.Fatalf("Error statting file: %v", err)
}
if expected := testMode; st.Mode() != expected {
t.Fatalf("Mode mismatched, expected %o, got %o", expected, st.Mode())
}
}
func TestAtomicWriteSetCancel(t *testing.T) {
tmpDir := t.TempDir()
if err := os.Mkdir(filepath.Join(tmpDir, "tmp"), 0o700); err != nil {
t.Fatalf("Error creating tmp directory: %s", err)
}
ws, err := NewAtomicWriteSet(filepath.Join(tmpDir, "tmp"))
if err != nil {
t.Fatalf("Error creating atomic write set: %s", err)
}
expected := []byte("barbaz")
if err := ws.WriteFile("foo", expected, testMode); err != nil {
t.Fatalf("Error writing to file: %v", err)
}
if err := ws.Cancel(); err != nil {
t.Fatalf("Error committing file: %s", err)
}
if _, err := os.ReadFile(filepath.Join(tmpDir, "target", "foo")); err == nil {
t.Fatalf("Expected error reading file where should not exist")
} else if !os.IsNotExist(err) {
t.Fatalf("Unexpected error reading file: %s", err)
}
}
|