~gz/juju-core/trunk

« back to all changes in this revision

Viewing changes to charm/dir_test.go

[r=rogpeppe] all: standardise gocheck imports, A-D

This is an automated change to use "gc" for gocheck
imports throughout. To avoid clogging Rietveld, I've
split it up into three parts - this is the first.

https://codereview.appspot.com/12940044/

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
        "strings"
14
14
        "syscall"
15
15
 
16
 
        . "launchpad.net/gocheck"
 
16
        gc "launchpad.net/gocheck"
17
17
 
18
18
        "launchpad.net/juju-core/charm"
19
19
        "launchpad.net/juju-core/testing"
23
23
        testing.LoggingSuite
24
24
}
25
25
 
26
 
var _ = Suite(&DirSuite{})
 
26
var _ = gc.Suite(&DirSuite{})
27
27
 
28
 
func (s *DirSuite) TestReadDir(c *C) {
 
28
func (s *DirSuite) TestReadDir(c *gc.C) {
29
29
        path := testing.Charms.DirPath("dummy")
30
30
        dir, err := charm.ReadDir(path)
31
 
        c.Assert(err, IsNil)
 
31
        c.Assert(err, gc.IsNil)
32
32
        checkDummy(c, dir, path)
33
33
}
34
34
 
35
 
func (s *DirSuite) TestReadDirWithoutConfig(c *C) {
 
35
func (s *DirSuite) TestReadDirWithoutConfig(c *gc.C) {
36
36
        path := testing.Charms.DirPath("varnish")
37
37
        dir, err := charm.ReadDir(path)
38
 
        c.Assert(err, IsNil)
 
38
        c.Assert(err, gc.IsNil)
39
39
 
40
40
        // A lacking config.yaml file still causes a proper
41
41
        // Config value to be returned.
42
 
        c.Assert(dir.Config().Options, HasLen, 0)
 
42
        c.Assert(dir.Config().Options, gc.HasLen, 0)
43
43
}
44
44
 
45
 
func (s *DirSuite) TestBundleTo(c *C) {
 
45
func (s *DirSuite) TestBundleTo(c *gc.C) {
46
46
        baseDir := c.MkDir()
47
47
        charmDir := testing.Charms.ClonedDirPath(baseDir, "dummy")
48
48
        var haveSymlinks = true
50
50
                haveSymlinks = false
51
51
        }
52
52
        dir, err := charm.ReadDir(charmDir)
53
 
        c.Assert(err, IsNil)
 
53
        c.Assert(err, gc.IsNil)
54
54
        path := filepath.Join(baseDir, "bundle.charm")
55
55
        file, err := os.Create(path)
56
 
        c.Assert(err, IsNil)
 
56
        c.Assert(err, gc.IsNil)
57
57
        err = dir.BundleTo(file)
58
58
        file.Close()
59
 
        c.Assert(err, IsNil)
 
59
        c.Assert(err, gc.IsNil)
60
60
 
61
61
        zipr, err := zip.OpenReader(path)
62
 
        c.Assert(err, IsNil)
 
62
        c.Assert(err, gc.IsNil)
63
63
        defer zipr.Close()
64
64
 
65
65
        var metaf, instf, emptyf, revf, symf *zip.File
83
83
                }
84
84
        }
85
85
 
86
 
        c.Assert(revf, NotNil)
 
86
        c.Assert(revf, gc.NotNil)
87
87
        reader, err := revf.Open()
88
 
        c.Assert(err, IsNil)
 
88
        c.Assert(err, gc.IsNil)
89
89
        data, err := ioutil.ReadAll(reader)
90
90
        reader.Close()
91
 
        c.Assert(err, IsNil)
92
 
        c.Assert(string(data), Equals, "1")
 
91
        c.Assert(err, gc.IsNil)
 
92
        c.Assert(string(data), gc.Equals, "1")
93
93
 
94
 
        c.Assert(metaf, NotNil)
 
94
        c.Assert(metaf, gc.NotNil)
95
95
        reader, err = metaf.Open()
96
 
        c.Assert(err, IsNil)
 
96
        c.Assert(err, gc.IsNil)
97
97
        meta, err := charm.ReadMeta(reader)
98
98
        reader.Close()
99
 
        c.Assert(err, IsNil)
100
 
        c.Assert(meta.Name, Equals, "dummy")
 
99
        c.Assert(err, gc.IsNil)
 
100
        c.Assert(meta.Name, gc.Equals, "dummy")
101
101
 
102
 
        c.Assert(instf, NotNil)
 
102
        c.Assert(instf, gc.NotNil)
103
103
        // Despite it being 0751, we pack and unpack it as 0755.
104
 
        c.Assert(instf.Mode()&0777, Equals, os.FileMode(0755))
 
104
        c.Assert(instf.Mode()&0777, gc.Equals, os.FileMode(0755))
105
105
 
106
106
        if haveSymlinks {
107
 
                c.Assert(symf, NotNil)
108
 
                c.Assert(symf.Mode()&0777, Equals, os.FileMode(0777))
 
107
                c.Assert(symf, gc.NotNil)
 
108
                c.Assert(symf.Mode()&0777, gc.Equals, os.FileMode(0777))
109
109
                reader, err = symf.Open()
110
 
                c.Assert(err, IsNil)
 
110
                c.Assert(err, gc.IsNil)
111
111
                data, err = ioutil.ReadAll(reader)
112
112
                reader.Close()
113
 
                c.Assert(err, IsNil)
114
 
                c.Assert(string(data), Equals, "../target")
 
113
                c.Assert(err, gc.IsNil)
 
114
                c.Assert(string(data), gc.Equals, "../target")
115
115
        } else {
116
 
                c.Assert(symf, IsNil)
 
116
                c.Assert(symf, gc.IsNil)
117
117
        }
118
118
 
119
 
        c.Assert(emptyf, NotNil)
120
 
        c.Assert(emptyf.Mode()&os.ModeType, Equals, os.ModeDir)
 
119
        c.Assert(emptyf, gc.NotNil)
 
120
        c.Assert(emptyf.Mode()&os.ModeType, gc.Equals, os.ModeDir)
121
121
        // Despite it being 0750, we pack and unpack it as 0755.
122
 
        c.Assert(emptyf.Mode()&0777, Equals, os.FileMode(0755))
 
122
        c.Assert(emptyf.Mode()&0777, gc.Equals, os.FileMode(0755))
123
123
}
124
124
 
125
125
// Bug #864164: Must complain if charm hooks aren't executable
126
 
func (s *DirSuite) TestBundleToWithNonExecutableHooks(c *C) {
 
126
func (s *DirSuite) TestBundleToWithNonExecutableHooks(c *gc.C) {
127
127
        hooks := []string{"install", "start", "config-changed", "upgrade-charm", "stop"}
128
128
        for _, relName := range []string{"foo", "bar", "self"} {
129
129
                for _, kind := range []string{"joined", "changed", "departed", "broken"} {
134
134
        dir := testing.Charms.Dir("all-hooks")
135
135
        path := filepath.Join(c.MkDir(), "bundle.charm")
136
136
        file, err := os.Create(path)
137
 
        c.Assert(err, IsNil)
 
137
        c.Assert(err, gc.IsNil)
138
138
        err = dir.BundleTo(file)
139
139
        file.Close()
140
 
        c.Assert(err, IsNil)
 
140
        c.Assert(err, gc.IsNil)
141
141
 
142
142
        tlog := c.GetTestLog()
143
143
        for _, hook := range hooks {
144
144
                fullpath := filepath.Join(dir.Path, "hooks", hook)
145
145
                exp := fmt.Sprintf(`^(.|\n)*WARNING juju charm: making "%s" executable in charm(.|\n)*$`, fullpath)
146
 
                c.Assert(tlog, Matches, exp, Commentf("hook %q was not made executable", fullpath))
 
146
                c.Assert(tlog, gc.Matches, exp, gc.Commentf("hook %q was not made executable", fullpath))
147
147
        }
148
148
 
149
149
        // Expand it and check the hooks' permissions
150
150
        // (But do not use ExpandTo(), just use the raw zip)
151
151
        f, err := os.Open(path)
152
 
        c.Assert(err, IsNil)
 
152
        c.Assert(err, gc.IsNil)
153
153
        defer f.Close()
154
154
        fi, err := f.Stat()
155
 
        c.Assert(err, IsNil)
 
155
        c.Assert(err, gc.IsNil)
156
156
        size := fi.Size()
157
157
        zipr, err := zip.NewReader(f, size)
158
 
        c.Assert(err, IsNil)
 
158
        c.Assert(err, gc.IsNil)
159
159
        allhooks := dir.Meta().Hooks()
160
160
        for _, zfile := range zipr.File {
161
161
                cleanName := filepath.Clean(zfile.Name)
163
163
                        hookName := filepath.Base(cleanName)
164
164
                        if _, ok := allhooks[hookName]; ok {
165
165
                                perms := zfile.Mode()
166
 
                                c.Assert(perms&0100 != 0, Equals, true, Commentf("hook %q is not executable", hookName))
 
166
                                c.Assert(perms&0100 != 0, gc.Equals, true, gc.Commentf("hook %q is not executable", hookName))
167
167
                        }
168
168
                }
169
169
        }
170
170
}
171
171
 
172
 
func (s *DirSuite) TestBundleToWithBadType(c *C) {
 
172
func (s *DirSuite) TestBundleToWithBadType(c *gc.C) {
173
173
        charmDir := testing.Charms.ClonedDirPath(c.MkDir(), "dummy")
174
174
        badFile := filepath.Join(charmDir, "hooks", "badfile")
175
175
 
176
176
        // Symlink targeting a path outside of the charm.
177
177
        err := os.Symlink("../../target", badFile)
178
 
        c.Assert(err, IsNil)
 
178
        c.Assert(err, gc.IsNil)
179
179
 
180
180
        dir, err := charm.ReadDir(charmDir)
181
 
        c.Assert(err, IsNil)
 
181
        c.Assert(err, gc.IsNil)
182
182
 
183
183
        err = dir.BundleTo(&bytes.Buffer{})
184
 
        c.Assert(err, ErrorMatches, `symlink "hooks/badfile" links out of charm: "../../target"`)
 
184
        c.Assert(err, gc.ErrorMatches, `symlink "hooks/badfile" links out of charm: "../../target"`)
185
185
 
186
186
        // Symlink targeting an absolute path.
187
187
        os.Remove(badFile)
188
188
        err = os.Symlink("/target", badFile)
189
 
        c.Assert(err, IsNil)
 
189
        c.Assert(err, gc.IsNil)
190
190
 
191
191
        dir, err = charm.ReadDir(charmDir)
192
 
        c.Assert(err, IsNil)
 
192
        c.Assert(err, gc.IsNil)
193
193
 
194
194
        err = dir.BundleTo(&bytes.Buffer{})
195
 
        c.Assert(err, ErrorMatches, `symlink "hooks/badfile" is absolute: "/target"`)
 
195
        c.Assert(err, gc.ErrorMatches, `symlink "hooks/badfile" is absolute: "/target"`)
196
196
 
197
197
        // Can't bundle special files either.
198
198
        os.Remove(badFile)
199
199
        err = syscall.Mkfifo(badFile, 0644)
200
 
        c.Assert(err, IsNil)
 
200
        c.Assert(err, gc.IsNil)
201
201
 
202
202
        dir, err = charm.ReadDir(charmDir)
203
 
        c.Assert(err, IsNil)
 
203
        c.Assert(err, gc.IsNil)
204
204
 
205
205
        err = dir.BundleTo(&bytes.Buffer{})
206
 
        c.Assert(err, ErrorMatches, `file is a named pipe: "hooks/badfile"`)
 
206
        c.Assert(err, gc.ErrorMatches, `file is a named pipe: "hooks/badfile"`)
207
207
}
208
208
 
209
 
func (s *DirSuite) TestDirRevisionFile(c *C) {
 
209
func (s *DirSuite) TestDirRevisionFile(c *gc.C) {
210
210
        charmDir := testing.Charms.ClonedDirPath(c.MkDir(), "dummy")
211
211
        revPath := filepath.Join(charmDir, "revision")
212
212
 
213
213
        // Missing revision file
214
214
        err := os.Remove(revPath)
215
 
        c.Assert(err, IsNil)
 
215
        c.Assert(err, gc.IsNil)
216
216
 
217
217
        dir, err := charm.ReadDir(charmDir)
218
 
        c.Assert(err, IsNil)
219
 
        c.Assert(dir.Revision(), Equals, 0)
 
218
        c.Assert(err, gc.IsNil)
 
219
        c.Assert(dir.Revision(), gc.Equals, 0)
220
220
 
221
221
        // Missing revision file with old revision in metadata
222
222
        file, err := os.OpenFile(filepath.Join(charmDir, "metadata.yaml"), os.O_WRONLY|os.O_APPEND, 0)
223
 
        c.Assert(err, IsNil)
 
223
        c.Assert(err, gc.IsNil)
224
224
        _, err = file.Write([]byte("\nrevision: 1234\n"))
225
 
        c.Assert(err, IsNil)
 
225
        c.Assert(err, gc.IsNil)
226
226
 
227
227
        dir, err = charm.ReadDir(charmDir)
228
 
        c.Assert(err, IsNil)
229
 
        c.Assert(dir.Revision(), Equals, 1234)
 
228
        c.Assert(err, gc.IsNil)
 
229
        c.Assert(dir.Revision(), gc.Equals, 1234)
230
230
 
231
231
        // Revision file with bad content
232
232
        err = ioutil.WriteFile(revPath, []byte("garbage"), 0666)
233
 
        c.Assert(err, IsNil)
 
233
        c.Assert(err, gc.IsNil)
234
234
 
235
235
        dir, err = charm.ReadDir(charmDir)
236
 
        c.Assert(err, ErrorMatches, "invalid revision file")
237
 
        c.Assert(dir, IsNil)
 
236
        c.Assert(err, gc.ErrorMatches, "invalid revision file")
 
237
        c.Assert(dir, gc.IsNil)
238
238
}
239
239
 
240
 
func (s *DirSuite) TestDirSetRevision(c *C) {
 
240
func (s *DirSuite) TestDirSetRevision(c *gc.C) {
241
241
        dir := testing.Charms.ClonedDir(c.MkDir(), "dummy")
242
 
        c.Assert(dir.Revision(), Equals, 1)
 
242
        c.Assert(dir.Revision(), gc.Equals, 1)
243
243
        dir.SetRevision(42)
244
 
        c.Assert(dir.Revision(), Equals, 42)
 
244
        c.Assert(dir.Revision(), gc.Equals, 42)
245
245
 
246
246
        var b bytes.Buffer
247
247
        err := dir.BundleTo(&b)
248
 
        c.Assert(err, IsNil)
 
248
        c.Assert(err, gc.IsNil)
249
249
 
250
250
        bundle, err := charm.ReadBundleBytes(b.Bytes())
251
 
        c.Assert(bundle.Revision(), Equals, 42)
 
251
        c.Assert(bundle.Revision(), gc.Equals, 42)
252
252
}
253
253
 
254
 
func (s *DirSuite) TestDirSetDiskRevision(c *C) {
 
254
func (s *DirSuite) TestDirSetDiskRevision(c *gc.C) {
255
255
        charmDir := testing.Charms.ClonedDirPath(c.MkDir(), "dummy")
256
256
        dir, err := charm.ReadDir(charmDir)
257
 
        c.Assert(err, IsNil)
 
257
        c.Assert(err, gc.IsNil)
258
258
 
259
 
        c.Assert(dir.Revision(), Equals, 1)
 
259
        c.Assert(dir.Revision(), gc.Equals, 1)
260
260
        dir.SetDiskRevision(42)
261
 
        c.Assert(dir.Revision(), Equals, 42)
 
261
        c.Assert(dir.Revision(), gc.Equals, 42)
262
262
 
263
263
        dir, err = charm.ReadDir(charmDir)
264
 
        c.Assert(err, IsNil)
265
 
        c.Assert(dir.Revision(), Equals, 42)
 
264
        c.Assert(err, gc.IsNil)
 
265
        c.Assert(dir.Revision(), gc.Equals, 42)
266
266
}