forked from juju/utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_test.go
181 lines (166 loc) · 4.56 KB
/
file_test.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
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
// Copyright 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package utils_test
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "launchpad.net/gocheck"
"github.com/juju/utils"
)
type fileSuite struct {
testing.IsolationSuite
}
var _ = gc.Suite(&fileSuite{})
func (*fileSuite) TestNormalizePath(c *gc.C) {
home := c.MkDir()
err := utils.SetHome(home)
c.Assert(err, gc.IsNil)
// TODO (frankban) bug 1324841: improve the isolation of this suite.
currentUser, err := user.Current()
c.Assert(err, gc.IsNil)
for i, test := range []struct {
path string
expected string
err string
}{{
path: "/var/lib/juju",
expected: "/var/lib/juju",
}, {
path: "~/foo",
expected: filepath.Join(home, "foo"),
}, {
path: "~/foo//../bar",
expected: filepath.Join(home, "bar"),
}, {
path: "~",
expected: home,
}, {
path: "~" + currentUser.Username,
expected: currentUser.HomeDir,
}, {
path: "~" + currentUser.Username + "/foo",
expected: filepath.Join(currentUser.HomeDir, "foo"),
}, {
path: "~" + currentUser.Username + "/foo//../bar",
expected: filepath.Join(currentUser.HomeDir, "bar"),
}, {
path: "~foobar/path",
err: "user: unknown user foobar",
}} {
c.Logf("test %d: %s", i, test.path)
actual, err := utils.NormalizePath(test.path)
if test.err != "" {
c.Check(err, gc.ErrorMatches, test.err)
} else {
c.Check(err, gc.IsNil)
c.Check(actual, gc.Equals, test.expected)
}
}
}
func (*fileSuite) TestCopyFile(c *gc.C) {
dir := c.MkDir()
f, err := ioutil.TempFile(dir, "source")
c.Assert(err, gc.IsNil)
defer f.Close()
_, err = f.Write([]byte("hello world"))
c.Assert(err, gc.IsNil)
dest := filepath.Join(dir, "dest")
err = utils.CopyFile(dest, f.Name())
c.Assert(err, gc.IsNil)
data, err := ioutil.ReadFile(dest)
c.Assert(err, gc.IsNil)
c.Assert(string(data), gc.Equals, "hello world")
}
var atomicWriteFileTests = []struct {
summary string
change func(filename string, contents []byte) error
check func(c *gc.C, fileInfo os.FileInfo)
expectErr string
}{{
summary: "atomic file write and chmod 0644",
change: func(filename string, contents []byte) error {
return utils.AtomicWriteFile(filename, contents, 0765)
},
check: func(c *gc.C, fi os.FileInfo) {
c.Assert(fi.Mode(), gc.Equals, 0765)
},
}, {
summary: "atomic file write and change",
change: func(filename string, contents []byte) error {
chmodChange := func(f *os.File) error {
return f.Chmod(0700)
}
return utils.AtomicWriteFileAndChange(filename, contents, chmodChange)
},
check: func(c *gc.C, fi os.FileInfo) {
c.Assert(fi.Mode(), gc.Equals, 0700)
},
}, {
summary: "atomic file write empty contents",
change: func(filename string, contents []byte) error {
nopChange := func(*os.File) error {
return nil
}
return utils.AtomicWriteFileAndChange(filename, contents, nopChange)
},
}, {
summary: "atomic file write and failing change func",
change: func(filename string, contents []byte) error {
errChange := func(*os.File) error {
return fmt.Errorf("pow!")
}
return utils.AtomicWriteFileAndChange(filename, contents, errChange)
},
expectErr: "pow!",
}}
func (*fileSuite) TestAtomicWriteFile(c *gc.C) {
dir := c.MkDir()
name := "test.file"
path := filepath.Join(dir, name)
assertDirContents := func(names ...string) {
fis, err := ioutil.ReadDir(dir)
c.Assert(err, gc.IsNil)
c.Assert(fis, gc.HasLen, len(names))
for i, name := range names {
c.Assert(fis[i].Name(), gc.Equals, name)
}
}
assertNotExist := func(path string) {
_, err := os.Lstat(path)
c.Assert(err, jc.Satisfies, os.IsNotExist)
}
for i, test := range atomicWriteFileTests {
c.Logf("test %d: %s", i, test.summary)
// First - test with file not already there.
assertDirContents()
assertNotExist(path)
contents := []byte("some\ncontents")
err := test.change(path, contents)
if test.expectErr == "" {
c.Assert(err, gc.IsNil)
data, err := ioutil.ReadFile(path)
c.Assert(err, gc.IsNil)
c.Assert(data, jc.DeepEquals, contents)
assertDirContents(name)
} else {
c.Assert(err, gc.ErrorMatches, test.expectErr)
assertDirContents()
continue
}
// Second - test with a file already there.
contents = []byte("new\ncontents")
err = test.change(path, contents)
c.Assert(err, gc.IsNil)
data, err := ioutil.ReadFile(path)
c.Assert(err, gc.IsNil)
c.Assert(data, jc.DeepEquals, contents)
assertDirContents(name)
// Remove the file to reset scenario.
c.Assert(os.Remove(path), gc.IsNil)
}
}