/
test_helpers.go
78 lines (67 loc) · 1.98 KB
/
test_helpers.go
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
package watcherx
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func kubernetesAtomicWrite(t *testing.T, dir, fileName, content string) {
// atomic write according to https://github.com/kubernetes/kubernetes/blob/master/pkg/volume/util/atomic_writer.go
const (
dataDirName = "..data"
newDataDirName = "..data_tmp"
)
// (2)
dataDirPath := filepath.Join(dir, dataDirName)
oldTsDir, err := os.Readlink(dataDirPath)
if err != nil {
require.True(t, os.IsNotExist(err), "%+v", err)
// although Readlink() returns "" on err, don't be fragile by relying on it (since it's not specified in docs)
// empty oldTsDir indicates that it didn't exist
oldTsDir = ""
}
oldTsPath := filepath.Join(dir, oldTsDir)
// (3) we are not interested in the case where a file gets deleted as we just operate on one file
// (4) we assume the file needs an update
// (5)
tsDir, err := ioutil.TempDir(dir, time.Now().UTC().Format("..2006_01_02_15_04_05."))
require.NoError(t, err)
tsDirName := filepath.Base(tsDir)
// (6)
require.NoError(
t,
ioutil.WriteFile(path.Join(tsDir, fileName), []byte(content), 0600),
)
// (7)
_, err = os.Readlink(filepath.Join(dir, fileName))
if err != nil && os.IsNotExist(err) {
// The link into the data directory for this path doesn't exist; create it
require.NoError(
t,
os.Symlink(filepath.Join(dataDirName, fileName), filepath.Join(dir, fileName)),
)
}
// (8)
newDataDirPath := filepath.Join(dir, newDataDirName)
require.NoError(
t,
os.Symlink(tsDirName, newDataDirPath),
)
// (9)
if runtime.GOOS == "windows" {
require.NoError(t, os.Remove(dataDirPath))
require.NoError(t, os.Symlink(tsDirName, dataDirPath))
require.NoError(t, os.Remove(newDataDirPath))
} else {
require.NoError(t, os.Rename(newDataDirPath, dataDirPath))
}
// (10) in our case there is nothing to remove
// (11)
if len(oldTsDir) > 0 {
require.NoError(t, os.RemoveAll(oldTsPath))
}
}