LEFT | RIGHT |
1 // Copyright 2012, 2013 Canonical Ltd. | 1 // Copyright 2012, 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 charm_test | 4 package charm_test |
5 | 5 |
6 import ( | 6 import ( |
7 "fmt" | |
8 "io/ioutil" | 7 "io/ioutil" |
9 » . "launchpad.net/gocheck" | 8 » "os" |
| 9 » "os/exec" |
| 10 » "path/filepath" |
| 11 |
| 12 » gc "launchpad.net/gocheck" |
| 13 |
10 corecharm "launchpad.net/juju-core/charm" | 14 corecharm "launchpad.net/juju-core/charm" |
11 "launchpad.net/juju-core/testing" | 15 "launchpad.net/juju-core/testing" |
12 "launchpad.net/juju-core/testing/checkers" | 16 "launchpad.net/juju-core/testing/checkers" |
13 "launchpad.net/juju-core/worker/uniter/charm" | 17 "launchpad.net/juju-core/worker/uniter/charm" |
14 "os" | |
15 "os/exec" | |
16 "path/filepath" | |
17 "regexp" | |
18 ) | 18 ) |
19 | 19 |
20 var curl = corecharm.MustParseURL("cs:series/blah-blah-123") | 20 var curl = corecharm.MustParseURL("cs:series/blah-blah-123") |
21 | 21 |
22 type GitDirSuite struct { | 22 type GitDirSuite struct { |
23 testing.GitSuite | 23 testing.GitSuite |
24 LoggingSuite testing.LoggingSuite | 24 LoggingSuite testing.LoggingSuite |
25 » oldLcAll string | 25 » resetLcAll func() |
26 } | 26 } |
27 | 27 |
28 var _ = Suite(&GitDirSuite{}) | 28 var _ = gc.Suite(&GitDirSuite{}) |
29 | 29 |
30 func (s *GitDirSuite) SetUpTest(c *C) { | 30 func (s *GitDirSuite) SetUpTest(c *gc.C) { |
31 s.GitSuite.SetUpTest(c) | 31 s.GitSuite.SetUpTest(c) |
32 s.LoggingSuite.SetUpTest(c) | 32 s.LoggingSuite.SetUpTest(c) |
33 » s.oldLcAll = os.Getenv("LC_ALL") | 33 » s.resetLcAll = testing.PatchEnvironment("LC_ALL", "en_US") |
34 » os.Setenv("LC_ALL", "en_US") | 34 } |
35 } | 35 |
36 | 36 func (s *GitDirSuite) TearDownTest(c *gc.C) { |
37 func (s *GitDirSuite) TearDownTest(c *C) { | 37 » s.resetLcAll() |
38 » os.Setenv("LC_ALL", s.oldLcAll) | |
39 s.LoggingSuite.TearDownTest(c) | 38 s.LoggingSuite.TearDownTest(c) |
40 s.GitSuite.TearDownTest(c) | 39 s.GitSuite.TearDownTest(c) |
41 } | 40 } |
42 | 41 |
43 func (s *GitDirSuite) TestInitConfig(c *C) { | 42 func (s *GitDirSuite) TestInitConfig(c *gc.C) { |
44 base := c.MkDir() | 43 base := c.MkDir() |
45 repo := charm.NewGitDir(filepath.Join(base, "repo")) | 44 repo := charm.NewGitDir(filepath.Join(base, "repo")) |
46 err := repo.Init() | 45 err := repo.Init() |
47 » c.Assert(err, IsNil) | 46 » c.Assert(err, gc.IsNil) |
48 | 47 |
49 cmd := exec.Command("git", "config", "--list", "--local") | 48 cmd := exec.Command("git", "config", "--list", "--local") |
50 cmd.Dir = repo.Path() | 49 cmd.Dir = repo.Path() |
51 out, err := cmd.Output() | 50 out, err := cmd.Output() |
52 » c.Assert(err, IsNil) | 51 » c.Assert(err, gc.IsNil) |
53 » c.Assert(string(out), Matches, "(.|\n)*user.email=juju@localhost.\nuser.
name=juju(.|\n)*") | 52 » outstr := string(out) |
54 } | 53 » c.Assert(outstr, checkers.Contains, "user.email=juju@localhost") |
55 | 54 » c.Assert(outstr, checkers.Contains, "user.name=juju") |
56 func (s *GitDirSuite) TestCreate(c *C) { | 55 } |
| 56 |
| 57 func (s *GitDirSuite) TestCreate(c *gc.C) { |
57 base := c.MkDir() | 58 base := c.MkDir() |
58 repo := charm.NewGitDir(filepath.Join(base, "repo")) | 59 repo := charm.NewGitDir(filepath.Join(base, "repo")) |
59 exists, err := repo.Exists() | 60 exists, err := repo.Exists() |
60 » c.Assert(err, IsNil) | 61 » c.Assert(err, gc.IsNil) |
61 » c.Assert(exists, Equals, false) | 62 » c.Assert(exists, checkers.IsFalse) |
62 | 63 |
63 err = ioutil.WriteFile(repo.Path(), nil, 0644) | 64 err = ioutil.WriteFile(repo.Path(), nil, 0644) |
64 » c.Assert(err, IsNil) | 65 » c.Assert(err, gc.IsNil) |
65 _, err = repo.Exists() | 66 _, err = repo.Exists() |
66 » c.Assert(err, ErrorMatches, `".*/repo" is not a directory`) | 67 » c.Assert(err, gc.ErrorMatches, `".*/repo" is not a directory`) |
67 err = os.Remove(repo.Path()) | 68 err = os.Remove(repo.Path()) |
68 » c.Assert(err, IsNil) | 69 » c.Assert(err, gc.IsNil) |
69 | 70 |
70 err = os.Chmod(base, 0555) | 71 err = os.Chmod(base, 0555) |
71 » c.Assert(err, IsNil) | 72 » c.Assert(err, gc.IsNil) |
72 defer os.Chmod(base, 0755) | 73 defer os.Chmod(base, 0755) |
73 err = repo.Init() | 74 err = repo.Init() |
74 » c.Assert(err, ErrorMatches, ".* permission denied") | 75 » c.Assert(err, gc.ErrorMatches, ".* permission denied") |
75 exists, err = repo.Exists() | 76 exists, err = repo.Exists() |
76 » c.Assert(err, IsNil) | 77 » c.Assert(err, gc.IsNil) |
77 » c.Assert(exists, Equals, false) | 78 » c.Assert(exists, checkers.IsFalse) |
78 | 79 |
79 err = os.Chmod(base, 0755) | 80 err = os.Chmod(base, 0755) |
80 » c.Assert(err, IsNil) | 81 » c.Assert(err, gc.IsNil) |
81 err = repo.Init() | 82 err = repo.Init() |
82 » c.Assert(err, IsNil) | 83 » c.Assert(err, gc.IsNil) |
83 exists, err = repo.Exists() | 84 exists, err = repo.Exists() |
84 » c.Assert(err, IsNil) | 85 » c.Assert(err, gc.IsNil) |
85 » c.Assert(exists, Equals, true) | 86 » c.Assert(exists, checkers.IsTrue) |
86 | 87 |
87 _, err = charm.ReadCharmURL(repo) | 88 _, err = charm.ReadCharmURL(repo) |
88 c.Assert(err, checkers.Satisfies, os.IsNotExist) | 89 c.Assert(err, checkers.Satisfies, os.IsNotExist) |
89 | 90 |
90 err = repo.Init() | 91 err = repo.Init() |
91 » c.Assert(err, IsNil) | 92 » c.Assert(err, gc.IsNil) |
92 } | 93 } |
93 | 94 |
94 func (s *GitDirSuite) TestAddCommitPullRevert(c *C) { | 95 func (s *GitDirSuite) TestAddCommitPullRevert(c *gc.C) { |
95 target := charm.NewGitDir(c.MkDir()) | 96 target := charm.NewGitDir(c.MkDir()) |
96 err := target.Init() | 97 err := target.Init() |
97 » c.Assert(err, IsNil) | 98 » c.Assert(err, gc.IsNil) |
98 err = ioutil.WriteFile(filepath.Join(target.Path(), "initial"), []byte("
initial"), 0644) | 99 err = ioutil.WriteFile(filepath.Join(target.Path(), "initial"), []byte("
initial"), 0644) |
99 » c.Assert(err, IsNil) | 100 » c.Assert(err, gc.IsNil) |
100 err = charm.WriteCharmURL(target, curl) | 101 err = charm.WriteCharmURL(target, curl) |
101 » c.Assert(err, IsNil) | 102 » c.Assert(err, gc.IsNil) |
102 err = target.AddAll() | 103 err = target.AddAll() |
103 » c.Assert(err, IsNil) | 104 » c.Assert(err, gc.IsNil) |
104 dirty, err := target.Dirty() | 105 dirty, err := target.Dirty() |
105 » c.Assert(err, IsNil) | 106 » c.Assert(err, gc.IsNil) |
106 » c.Assert(dirty, Equals, true) | 107 » c.Assert(dirty, checkers.IsTrue) |
107 err = target.Commitf("initial") | 108 err = target.Commitf("initial") |
108 » c.Assert(err, IsNil) | 109 » c.Assert(err, gc.IsNil) |
109 » dirty, err = target.Dirty() | 110 » dirty, err = target.Dirty() |
110 » c.Assert(err, IsNil) | 111 » c.Assert(err, gc.IsNil) |
111 » c.Assert(dirty, Equals, false) | 112 » c.Assert(dirty, checkers.IsFalse) |
112 | 113 |
113 source := newRepo(c) | 114 source := newRepo(c) |
114 err = target.Pull(source) | 115 err = target.Pull(source) |
115 » c.Assert(err, IsNil) | 116 » c.Assert(err, gc.IsNil) |
116 url, err := charm.ReadCharmURL(target) | 117 url, err := charm.ReadCharmURL(target) |
117 » c.Assert(err, IsNil) | 118 » c.Assert(err, gc.IsNil) |
118 » c.Assert(url, DeepEquals, curl) | 119 » c.Assert(url, gc.DeepEquals, curl) |
119 fi, err := os.Stat(filepath.Join(target.Path(), "some-dir")) | 120 fi, err := os.Stat(filepath.Join(target.Path(), "some-dir")) |
120 » c.Assert(err, IsNil) | 121 » c.Assert(err, gc.IsNil) |
121 c.Assert(fi, checkers.Satisfies, os.FileInfo.IsDir) | 122 c.Assert(fi, checkers.Satisfies, os.FileInfo.IsDir) |
122 data, err := ioutil.ReadFile(filepath.Join(target.Path(), "some-file")) | 123 data, err := ioutil.ReadFile(filepath.Join(target.Path(), "some-file")) |
123 » c.Assert(err, IsNil) | 124 » c.Assert(err, gc.IsNil) |
124 » c.Assert(string(data), Equals, "hello") | 125 » c.Assert(string(data), gc.Equals, "hello") |
125 » dirty, err = target.Dirty() | 126 » dirty, err = target.Dirty() |
126 » c.Assert(err, IsNil) | 127 » c.Assert(err, gc.IsNil) |
127 » c.Assert(dirty, Equals, false) | 128 » c.Assert(dirty, checkers.IsFalse) |
128 | 129 |
129 err = ioutil.WriteFile(filepath.Join(target.Path(), "another-file"), []b
yte("blah"), 0644) | 130 err = ioutil.WriteFile(filepath.Join(target.Path(), "another-file"), []b
yte("blah"), 0644) |
130 » c.Assert(err, IsNil) | 131 » c.Assert(err, gc.IsNil) |
131 » dirty, err = target.Dirty() | 132 » dirty, err = target.Dirty() |
132 » c.Assert(err, IsNil) | 133 » c.Assert(err, gc.IsNil) |
133 » c.Assert(dirty, Equals, true) | 134 » c.Assert(dirty, checkers.IsTrue) |
134 err = source.AddAll() | 135 err = source.AddAll() |
135 » c.Assert(err, IsNil) | 136 » c.Assert(err, gc.IsNil) |
136 » dirty, err = target.Dirty() | 137 » dirty, err = target.Dirty() |
137 » c.Assert(err, IsNil) | 138 » c.Assert(err, gc.IsNil) |
138 » c.Assert(dirty, Equals, true) | 139 » c.Assert(dirty, checkers.IsTrue) |
139 | 140 |
140 err = target.Revert() | 141 err = target.Revert() |
141 » c.Assert(err, IsNil) | 142 » c.Assert(err, gc.IsNil) |
142 _, err = os.Stat(filepath.Join(target.Path(), "some-file")) | 143 _, err = os.Stat(filepath.Join(target.Path(), "some-file")) |
143 c.Assert(err, checkers.Satisfies, os.IsNotExist) | 144 c.Assert(err, checkers.Satisfies, os.IsNotExist) |
144 _, err = os.Stat(filepath.Join(target.Path(), "some-dir")) | 145 _, err = os.Stat(filepath.Join(target.Path(), "some-dir")) |
145 c.Assert(err, checkers.Satisfies, os.IsNotExist) | 146 c.Assert(err, checkers.Satisfies, os.IsNotExist) |
146 data, err = ioutil.ReadFile(filepath.Join(target.Path(), "initial")) | 147 data, err = ioutil.ReadFile(filepath.Join(target.Path(), "initial")) |
147 » c.Assert(err, IsNil) | 148 » c.Assert(err, gc.IsNil) |
148 » c.Assert(string(data), Equals, "initial") | 149 » c.Assert(string(data), gc.Equals, "initial") |
149 » dirty, err = target.Dirty() | 150 » dirty, err = target.Dirty() |
150 » c.Assert(err, IsNil) | 151 » c.Assert(err, gc.IsNil) |
151 » c.Assert(dirty, Equals, false) | 152 » c.Assert(dirty, checkers.IsFalse) |
152 } | 153 } |
153 | 154 |
154 func (s *GitDirSuite) TestClone(c *C) { | 155 func (s *GitDirSuite) TestClone(c *gc.C) { |
155 repo, err := newRepo(c).Clone(c.MkDir()) | 156 repo, err := newRepo(c).Clone(c.MkDir()) |
156 » c.Assert(err, IsNil) | 157 » c.Assert(err, gc.IsNil) |
157 _, err = os.Stat(filepath.Join(repo.Path(), "some-file")) | 158 _, err = os.Stat(filepath.Join(repo.Path(), "some-file")) |
158 c.Assert(err, checkers.Satisfies, os.IsNotExist) | 159 c.Assert(err, checkers.Satisfies, os.IsNotExist) |
159 _, err = os.Stat(filepath.Join(repo.Path(), "some-dir")) | 160 _, err = os.Stat(filepath.Join(repo.Path(), "some-dir")) |
160 c.Assert(err, checkers.Satisfies, os.IsNotExist) | 161 c.Assert(err, checkers.Satisfies, os.IsNotExist) |
161 dirty, err := repo.Dirty() | 162 dirty, err := repo.Dirty() |
162 » c.Assert(err, IsNil) | 163 » c.Assert(err, gc.IsNil) |
163 » c.Assert(dirty, Equals, true) | 164 » c.Assert(dirty, checkers.IsTrue) |
164 | 165 |
165 err = repo.AddAll() | 166 err = repo.AddAll() |
166 » c.Assert(err, IsNil) | 167 » c.Assert(err, gc.IsNil) |
167 dirty, err = repo.Dirty() | 168 dirty, err = repo.Dirty() |
168 » c.Assert(err, IsNil) | 169 » c.Assert(err, gc.IsNil) |
169 » c.Assert(dirty, Equals, true) | 170 » c.Assert(dirty, checkers.IsTrue) |
170 err = repo.Commitf("blank overwrite") | 171 err = repo.Commitf("blank overwrite") |
171 » c.Assert(err, IsNil) | 172 » c.Assert(err, gc.IsNil) |
172 dirty, err = repo.Dirty() | 173 dirty, err = repo.Dirty() |
173 » c.Assert(err, IsNil) | 174 » c.Assert(err, gc.IsNil) |
174 » c.Assert(dirty, Equals, false) | 175 » c.Assert(dirty, checkers.IsFalse) |
175 | 176 |
176 lines, err := repo.Log() | 177 lines, err := repo.Log() |
177 » c.Assert(err, IsNil) | 178 » c.Assert(err, gc.IsNil) |
178 » c.Assert(lines, HasLen, 2) | 179 » c.Assert(lines, gc.HasLen, 2) |
179 » c.Assert(lines[0], Matches, "[a-f0-9]{7} blank overwrite") | 180 » c.Assert(lines[0], gc.Matches, "[a-f0-9]{7} blank overwrite") |
180 » c.Assert(lines[1], Matches, "[a-f0-9]{7} im in ur repo committin ur file
s") | 181 » c.Assert(lines[1], gc.Matches, "[a-f0-9]{7} im in ur repo committin ur f
iles") |
181 } | 182 } |
182 | 183 |
183 func (s *GitDirSuite) TestConflictRevert(c *C) { | 184 func (s *GitDirSuite) TestConflictRevert(c *gc.C) { |
184 source := newRepo(c) | 185 source := newRepo(c) |
185 updated, err := source.Clone(c.MkDir()) | 186 updated, err := source.Clone(c.MkDir()) |
186 » c.Assert(err, IsNil) | 187 » c.Assert(err, gc.IsNil) |
187 err = ioutil.WriteFile(filepath.Join(updated.Path(), "some-dir"), []byte
("hello"), 0644) | 188 err = ioutil.WriteFile(filepath.Join(updated.Path(), "some-dir"), []byte
("hello"), 0644) |
188 » c.Assert(err, IsNil) | 189 » c.Assert(err, gc.IsNil) |
189 err = updated.Snapshotf("potential conflict src") | 190 err = updated.Snapshotf("potential conflict src") |
190 » c.Assert(err, IsNil) | 191 » c.Assert(err, gc.IsNil) |
191 conflicted, err := updated.Conflicted() | 192 conflicted, err := updated.Conflicted() |
192 » c.Assert(err, IsNil) | 193 » c.Assert(err, gc.IsNil) |
193 » c.Assert(conflicted, Equals, false) | 194 » c.Assert(conflicted, checkers.IsFalse) |
194 | 195 |
195 target := charm.NewGitDir(c.MkDir()) | 196 target := charm.NewGitDir(c.MkDir()) |
196 err = target.Init() | 197 err = target.Init() |
197 » c.Assert(err, IsNil) | 198 » c.Assert(err, gc.IsNil) |
198 err = target.Pull(source) | 199 err = target.Pull(source) |
199 » c.Assert(err, IsNil) | 200 » c.Assert(err, gc.IsNil) |
200 err = ioutil.WriteFile(filepath.Join(target.Path(), "some-dir", "conflic
ting-file"), []byte("hello"), 0644) | 201 err = ioutil.WriteFile(filepath.Join(target.Path(), "some-dir", "conflic
ting-file"), []byte("hello"), 0644) |
201 » c.Assert(err, IsNil) | 202 » c.Assert(err, gc.IsNil) |
202 err = target.Snapshotf("potential conflict dst") | 203 err = target.Snapshotf("potential conflict dst") |
203 » c.Assert(err, IsNil) | 204 » c.Assert(err, gc.IsNil) |
204 conflicted, err = target.Conflicted() | 205 conflicted, err = target.Conflicted() |
205 » c.Assert(err, IsNil) | 206 » c.Assert(err, gc.IsNil) |
206 » c.Assert(conflicted, Equals, false) | 207 » c.Assert(conflicted, checkers.IsFalse) |
207 | 208 |
208 err = target.Pull(updated) | 209 err = target.Pull(updated) |
209 » c.Assert(err, Equals, charm.ErrConflict) | 210 » c.Assert(err, gc.Equals, charm.ErrConflict) |
210 conflicted, err = target.Conflicted() | 211 conflicted, err = target.Conflicted() |
211 » c.Assert(err, IsNil) | 212 » c.Assert(err, gc.IsNil) |
212 » c.Assert(conflicted, Equals, true) | 213 » c.Assert(conflicted, checkers.IsTrue) |
213 dirty, err := target.Dirty() | 214 dirty, err := target.Dirty() |
214 » c.Assert(err, IsNil) | 215 » c.Assert(err, gc.IsNil) |
215 » c.Assert(dirty, Equals, true) | 216 » c.Assert(dirty, checkers.IsTrue) |
216 | 217 |
217 err = target.Revert() | 218 err = target.Revert() |
218 » c.Assert(err, IsNil) | 219 » c.Assert(err, gc.IsNil) |
219 conflicted, err = target.Conflicted() | 220 conflicted, err = target.Conflicted() |
220 » c.Assert(err, IsNil) | 221 » c.Assert(err, gc.IsNil) |
221 » c.Assert(conflicted, Equals, false) | 222 » c.Assert(conflicted, checkers.IsFalse) |
222 » dirty, err = target.Dirty() | 223 » dirty, err = target.Dirty() |
223 » c.Assert(err, IsNil) | 224 » c.Assert(err, gc.IsNil) |
224 » c.Assert(dirty, Equals, false) | 225 » c.Assert(dirty, checkers.IsFalse) |
225 } | 226 } |
226 | 227 |
227 func newRepo(c *C) *charm.GitDir { | 228 func newRepo(c *gc.C) *charm.GitDir { |
228 repo := charm.NewGitDir(c.MkDir()) | 229 repo := charm.NewGitDir(c.MkDir()) |
229 err := repo.Init() | 230 err := repo.Init() |
230 » c.Assert(err, IsNil) | 231 » c.Assert(err, gc.IsNil) |
231 err = os.Mkdir(filepath.Join(repo.Path(), "some-dir"), 0755) | 232 err = os.Mkdir(filepath.Join(repo.Path(), "some-dir"), 0755) |
232 » c.Assert(err, IsNil) | 233 » c.Assert(err, gc.IsNil) |
233 err = ioutil.WriteFile(filepath.Join(repo.Path(), "some-file"), []byte("
hello"), 0644) | 234 err = ioutil.WriteFile(filepath.Join(repo.Path(), "some-file"), []byte("
hello"), 0644) |
234 » c.Assert(err, IsNil) | 235 » c.Assert(err, gc.IsNil) |
235 err = repo.AddAll() | 236 err = repo.AddAll() |
236 » c.Assert(err, IsNil) | 237 » c.Assert(err, gc.IsNil) |
237 err = repo.Commitf("im in ur repo committin ur %s", "files") | 238 err = repo.Commitf("im in ur repo committin ur %s", "files") |
238 » c.Assert(err, IsNil) | 239 » c.Assert(err, gc.IsNil) |
239 return repo | 240 return repo |
240 } | 241 } |
LEFT | RIGHT |