File: fsync_test.go

package info (click to toggle)
golang-github-spf13-fsync 0.10.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 140 kB
  • sloc: makefile: 2
file content (274 lines) | stat: -rw-r--r-- 6,613 bytes parent folder | download
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package fsync

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"
	"time"
)

func TestSync(t *testing.T) {
	// create test directory and chdir to it
	dir, err := ioutil.TempDir(os.TempDir(), "fsync_test")
	check(err)
	check(os.Chdir(dir))

	// create test files and directories
	check(os.MkdirAll("src/a", 0755))
	check(ioutil.WriteFile("src/a/b", []byte("file b"), 0644))
	check(ioutil.WriteFile("src/c", []byte("file c"), 0644))
	// set times in the past to make sure times are synced, not accidentally
	// the same
	tt := time.Now().Add(-1 * time.Hour)
	check(os.Chtimes("src/a/b", tt, tt))
	check(os.Chtimes("src/a", tt, tt))
	check(os.Chtimes("src/c", tt, tt))
	check(os.Chtimes("src", tt, tt))

	// create Syncer
	s := NewSyncer()

	// sync
	check(s.SyncTo("dst", "src/a", "src/c"))

	// check results
	testDirContents("dst", 2, t)
	testDirContents("dst/a", 1, t)
	testFile("dst/a/b", []byte("file b"), t)
	testFile("dst/c", []byte("file c"), t)
	testPerms("dst/a", getPerms("src/a"), t)
	testPerms("dst/a/b", getPerms("src/a/b"), t)
	testPerms("dst/c", getPerms("src/c"), t)
	testModTime("dst/a", getModTime("src/a"), t)
	testModTime("dst/a/b", getModTime("src/a/b"), t)
	testModTime("dst/c", getModTime("src/c"), t)

	// sync the parent directory too
	check(s.Sync("dst", "src"))

	// check the results
	testPerms("dst", getPerms("src"), t)
	testModTime("dst", getModTime("src"), t)

	// modify src
	check(ioutil.WriteFile("src/a/b", []byte("file b changed"), 0644))
	check(os.Chmod("src/a", 0775))

	// sync
	check(s.Sync("dst", "src"))

	// check results
	testFile("dst/a/b", []byte("file b changed"), t)
	testPerms("dst/a", getPerms("src/a"), t)
	testModTime("dst", getModTime("src"), t)
	testModTime("dst/a", getModTime("src/a"), t)
	testModTime("dst/a/b", getModTime("src/a/b"), t)
	testModTime("dst/c", getModTime("src/c"), t)

	// remove c
	check(os.Remove("src/c"))

	// sync
	check(s.Sync("dst", "src"))

	// check results; c should still exist
	testDirContents("dst", 2, t)
	testExistence("dst/c", true, t)

	// sync
	s.Delete = true
	check(s.Sync("dst", "src"))

	// check results; c should no longer exist
	testDirContents("dst", 1, t)
	testExistence("dst/c", false, t)

	s.Delete = false
	if err = s.Sync("dst", "src/a/b"); err == nil {
		t.Errorf("expecting ErrFileOverDir, got nothing.\n")
	} else if err != nil && err != ErrFileOverDir {
		panic(err)
	}
}

func TestDeleteFileFilter(t *testing.T) {
	// create test directory and chdir to it
	dir, err := ioutil.TempDir(os.TempDir(), "fsync_test_delete_filter")
	check(err)
	check(os.Chdir(dir))

	// create test files and directories
	check(os.MkdirAll("src/a", 0755))
	check(ioutil.WriteFile("src/a/b", []byte("file b"), 0644))

	check(os.MkdirAll("dst", 0755))
	check(ioutil.WriteFile("dst/c", []byte("file c"), 0644))
	check(ioutil.WriteFile("dst/d", []byte("file c"), 0644))

	// create Syncer
	s := NewSyncer()
	s.Delete = true

	s.DeleteFilter = func(f FileInfo) bool {
		return f.Name() == "d"
	}

	//precondition; dst contains 2 files `c` and `d`
	testDirContents("dst", 2, t)
	testExistence("dst/c", true, t)
	testExistence("dst/d", true, t)

	check(s.Sync("dst", "src"))

	// check results; c should no longer exist
	testDirContents("dst", 2, t)
	testExistence("dst/a/", true, t)
	testExistence("dst/a/b", true, t)
	testExistence("dst/c", false, t)
	testExistence("dst/d", true, t)
}

func TestDeleteFileFilterNotSet(t *testing.T) {
	// create test directory and chdir to it
	dir, err := ioutil.TempDir(os.TempDir(), "fsync_test_delete_filter")
	check(err)
	check(os.Chdir(dir))

	// create test files and directories
	check(os.MkdirAll("src/a", 0755))
	check(ioutil.WriteFile("src/a/b", []byte("file b"), 0644))

	check(os.MkdirAll("dst", 0755))
	check(ioutil.WriteFile("dst/c", []byte("file c"), 0644))
	check(ioutil.WriteFile("dst/d", []byte("file c"), 0644))

	// create Syncer
	s := NewSyncer()
	s.Delete = true

	//precondition; dst contains 2 files `c` and `d`
	testDirContents("dst", 2, t)
	testExistence("dst/c", true, t)
	testExistence("dst/d", true, t)

	check(s.Sync("dst", "src"))

	// check results; c should no longer exist
	testDirContents("dst", 1, t)
	testExistence("dst/a/", true, t)
	testExistence("dst/a/b", true, t)
	testExistence("dst/c", false, t)
	testExistence("dst/d", false, t)
}

func testFile(name string, b []byte, t *testing.T) {
	testExistence(name, true, t)
	c, err := ioutil.ReadFile(name)
	check(err)
	if !bytes.Equal(b, c) {
		t.Errorf("content of file \"%s\" is:\n%s\nexpected:\n%s\n",
			name, c, b)
	}
}

func testExistence(name string, e bool, t *testing.T) {
	_, err := os.Stat(name)
	if os.IsNotExist(err) {
		if e {
			t.Errorf("file \"%s\" does not exist.\n", name)
		}
	} else if err != nil {
		panic(err)
	} else {
		if !e {
			t.Errorf("file \"%s\" exists.\n", name)
		}
	}
}

func testDirContents(name string, count int, t *testing.T) {
	files, err := ioutil.ReadDir(name)
	check(err)
	if len(files) != count {
		t.Errorf("directory \"%s\" has %d children, shoud have %d.\n",
			name, len(files), count)
	}
}

func testPerms(name string, p os.FileMode, t *testing.T) {
	p2 := getPerms(name)
	if p2 != p {
		t.Errorf("permissions for \"%s\" is %v, should be %v.\n",
			name, p2, p)
	}
}

func testModTime(name string, m time.Time, t *testing.T) {
	m2 := getModTime(name)
	if !m2.Equal(m) {
		t.Errorf("modification time for \"%s\" is %v, should be %v.\n",
			name, m2, m)
	}
}

func getPerms(name string) os.FileMode {
	info, err := os.Stat(name)
	check(err)
	return info.Mode().Perm()
}

func getModTime(name string) time.Time {
	info, err := os.Stat(name)
	check(err)
	return info.ModTime()
}

func BenchmarkSync(b *testing.B) {
	createTempDir := func() string {
		tempDir, err := ioutil.TempDir(os.TempDir(), "fsync_test")
		check(err)
		return tempDir
	}

	tempDir := createTempDir()

	createSomeFiles := func(dirname string) {
		for i := 0; i < 5; i++ {
			f, err := os.Create(filepath.Join(tempDir, dirname, fmt.Sprintf("test%d.txt", i)))
			if err != nil {
				b.Fatal(err)
			}
			f.Close()
		}
	}

	depth := 5
	for level := depth; level > 0; level-- {
		dirname := ""
		for i := 0; i < level; i++ {
			dirname = filepath.Join(dirname, fmt.Sprintf("dir%d", i))
			err := os.MkdirAll(filepath.Join(tempDir, dirname), 0755)
			if err != nil && !os.IsExist(err) {
				b.Fatal(err)
			}
		}
		createSomeFiles(dirname)
	}

	src := tempDir
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		dst := createTempDir()
		s := NewSyncer()
		check(s.SyncTo(dst, filepath.Join(src, "dir0")))
		b.StopTimer()
		check(os.RemoveAll(src))
		src = dst
		b.StartTimer()

	}

}