LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2013 Canonical Ltd. | 1 // Copyright 2013 Canonical Ltd. |
2 // Licensed under the AGPLv3, see LICENCE file for details. | 2 // Licensed under the AGPLv3, see LICENCE file for details. |
3 | 3 |
4 package utils_test | 4 package utils_test |
5 | 5 |
6 import ( | 6 import ( |
| 7 "fmt" |
7 "io/ioutil" | 8 "io/ioutil" |
| 9 "os" |
8 "os/user" | 10 "os/user" |
9 "path/filepath" | 11 "path/filepath" |
10 | 12 |
11 gc "launchpad.net/gocheck" | 13 gc "launchpad.net/gocheck" |
12 | 14 |
13 "launchpad.net/juju-core/juju/osenv" | 15 "launchpad.net/juju-core/juju/osenv" |
| 16 jc "launchpad.net/juju-core/testing/checkers" |
14 "launchpad.net/juju-core/utils" | 17 "launchpad.net/juju-core/utils" |
15 ) | 18 ) |
16 | 19 |
17 type fileSuite struct { | 20 type fileSuite struct { |
18 oldHome string | 21 oldHome string |
19 } | 22 } |
20 | 23 |
21 var _ = gc.Suite(&fileSuite{}) | 24 var _ = gc.Suite(&fileSuite{}) |
22 | 25 |
23 func (s *fileSuite) SetUpTest(c *gc.C) { | 26 func (s *fileSuite) SetUpTest(c *gc.C) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 _, err = f.Write([]byte("hello world")) | 84 _, err = f.Write([]byte("hello world")) |
82 c.Assert(err, gc.IsNil) | 85 c.Assert(err, gc.IsNil) |
83 dest := filepath.Join(dir, "dest") | 86 dest := filepath.Join(dir, "dest") |
84 | 87 |
85 err = utils.CopyFile(dest, f.Name()) | 88 err = utils.CopyFile(dest, f.Name()) |
86 c.Assert(err, gc.IsNil) | 89 c.Assert(err, gc.IsNil) |
87 data, err := ioutil.ReadFile(dest) | 90 data, err := ioutil.ReadFile(dest) |
88 c.Assert(err, gc.IsNil) | 91 c.Assert(err, gc.IsNil) |
89 c.Assert(string(data), gc.Equals, "hello world") | 92 c.Assert(string(data), gc.Equals, "hello world") |
90 } | 93 } |
| 94 |
| 95 var atomicWriteFileTests = []struct { |
| 96 summary string |
| 97 change func(filename string, contents []byte) error |
| 98 check func(c *gc.C, fileInfo os.FileInfo) |
| 99 expectErr string |
| 100 }{{ |
| 101 summary: "atomic file write and chmod 0644", |
| 102 change: func(filename string, contents []byte) error { |
| 103 return utils.AtomicWriteFile(filename, contents, 0765) |
| 104 }, |
| 105 check: func(c *gc.C, fi os.FileInfo) { |
| 106 c.Assert(fi.Mode(), gc.Equals, 0765) |
| 107 }, |
| 108 }, { |
| 109 summary: "atomic file write and change", |
| 110 change: func(filename string, contents []byte) error { |
| 111 chmodChange := func(f *os.File) error { |
| 112 return f.Chmod(0700) |
| 113 } |
| 114 return utils.AtomicWriteFileAndChange(filename, contents, chmodC
hange) |
| 115 }, |
| 116 check: func(c *gc.C, fi os.FileInfo) { |
| 117 c.Assert(fi.Mode(), gc.Equals, 0700) |
| 118 }, |
| 119 }, { |
| 120 summary: "atomic file write empty contents", |
| 121 change: func(filename string, contents []byte) error { |
| 122 nopChange := func(*os.File) error { |
| 123 return nil |
| 124 } |
| 125 return utils.AtomicWriteFileAndChange(filename, contents, nopCha
nge) |
| 126 }, |
| 127 }, { |
| 128 summary: "atomic file write and failing change func", |
| 129 change: func(filename string, contents []byte) error { |
| 130 errChange := func(*os.File) error { |
| 131 return fmt.Errorf("pow!") |
| 132 } |
| 133 return utils.AtomicWriteFileAndChange(filename, contents, errCha
nge) |
| 134 }, |
| 135 expectErr: "pow!", |
| 136 }} |
| 137 |
| 138 func (*fileSuite) TestAtomicWriteFile(c *gc.C) { |
| 139 dir := c.MkDir() |
| 140 name := "test.file" |
| 141 path := filepath.Join(dir, name) |
| 142 assertDirContents := func(names ...string) { |
| 143 fis, err := ioutil.ReadDir(dir) |
| 144 c.Assert(err, gc.IsNil) |
| 145 c.Assert(fis, gc.HasLen, len(names)) |
| 146 for i, name := range names { |
| 147 c.Assert(fis[i].Name(), gc.Equals, name) |
| 148 } |
| 149 } |
| 150 assertNotExist := func(path string) { |
| 151 _, err := os.Lstat(path) |
| 152 c.Assert(err, jc.Satisfies, os.IsNotExist) |
| 153 } |
| 154 |
| 155 for i, test := range atomicWriteFileTests { |
| 156 c.Logf("test %d: %s", i, test.summary) |
| 157 // First - test with file not already there. |
| 158 assertDirContents() |
| 159 assertNotExist(path) |
| 160 contents := []byte("some\ncontents") |
| 161 |
| 162 err := test.change(path, contents) |
| 163 if test.expectErr == "" { |
| 164 c.Assert(err, gc.IsNil) |
| 165 data, err := ioutil.ReadFile(path) |
| 166 c.Assert(err, gc.IsNil) |
| 167 c.Assert(data, jc.DeepEquals, contents) |
| 168 assertDirContents(name) |
| 169 } else { |
| 170 c.Assert(err, gc.ErrorMatches, test.expectErr) |
| 171 assertDirContents() |
| 172 continue |
| 173 } |
| 174 |
| 175 // Second - test with a file already there. |
| 176 contents = []byte("new\ncontents") |
| 177 err = test.change(path, contents) |
| 178 c.Assert(err, gc.IsNil) |
| 179 data, err = ioutil.ReadFile(path) |
| 180 c.Assert(err, gc.IsNil) |
| 181 c.Assert(data, jc.DeepEquals, contents) |
| 182 assertDirContents(name) |
| 183 |
| 184 // Remove the file to reset scenario. |
| 185 c.Assert(os.Remove(path), gc.IsNil) |
| 186 } |
| 187 } |
LEFT | RIGHT |