~gz/juju-core/trunk

« back to all changes in this revision

Viewing changes to charm/meta_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:
11
11
        "path/filepath"
12
12
        "strings"
13
13
 
14
 
        . "launchpad.net/gocheck"
 
14
        gc "launchpad.net/gocheck"
15
15
 
16
16
        "launchpad.net/juju-core/charm"
17
17
        "launchpad.net/juju-core/testing"
33
33
 
34
34
type MetaSuite struct{}
35
35
 
36
 
var _ = Suite(&MetaSuite{})
 
36
var _ = gc.Suite(&MetaSuite{})
37
37
 
38
 
func (s *MetaSuite) TestReadMetaVersion1(c *C) {
 
38
func (s *MetaSuite) TestReadMetaVersion1(c *gc.C) {
39
39
        meta, err := charm.ReadMeta(repoMeta("dummy"))
40
 
        c.Assert(err, IsNil)
41
 
        c.Assert(meta.Name, Equals, "dummy")
42
 
        c.Assert(meta.Summary, Equals, "That's a dummy charm.")
43
 
        c.Assert(meta.Description, Equals,
 
40
        c.Assert(err, gc.IsNil)
 
41
        c.Assert(meta.Name, gc.Equals, "dummy")
 
42
        c.Assert(meta.Summary, gc.Equals, "That's a dummy charm.")
 
43
        c.Assert(meta.Description, gc.Equals,
44
44
                "This is a longer description which\npotentially contains multiple lines.\n")
45
 
        c.Assert(meta.Format, Equals, 1)
46
 
        c.Assert(meta.OldRevision, Equals, 0)
47
 
        c.Assert(meta.Subordinate, Equals, false)
 
45
        c.Assert(meta.Format, gc.Equals, 1)
 
46
        c.Assert(meta.OldRevision, gc.Equals, 0)
 
47
        c.Assert(meta.Subordinate, gc.Equals, false)
48
48
}
49
49
 
50
 
func (s *MetaSuite) TestReadMetaVersion2(c *C) {
 
50
func (s *MetaSuite) TestReadMetaVersion2(c *gc.C) {
51
51
        meta, err := charm.ReadMeta(repoMeta("format2"))
52
 
        c.Assert(err, IsNil)
53
 
        c.Assert(meta.Name, Equals, "format2")
54
 
        c.Assert(meta.Format, Equals, 2)
55
 
        c.Assert(meta.Categories, HasLen, 0)
 
52
        c.Assert(err, gc.IsNil)
 
53
        c.Assert(meta.Name, gc.Equals, "format2")
 
54
        c.Assert(meta.Format, gc.Equals, 2)
 
55
        c.Assert(meta.Categories, gc.HasLen, 0)
56
56
}
57
57
 
58
 
func (s *MetaSuite) TestReadCategory(c *C) {
 
58
func (s *MetaSuite) TestReadCategory(c *gc.C) {
59
59
        meta, err := charm.ReadMeta(repoMeta("category"))
60
 
        c.Assert(err, IsNil)
61
 
        c.Assert(meta.Categories, DeepEquals, []string{"database"})
 
60
        c.Assert(err, gc.IsNil)
 
61
        c.Assert(meta.Categories, gc.DeepEquals, []string{"database"})
62
62
}
63
63
 
64
 
func (s *MetaSuite) TestSubordinate(c *C) {
 
64
func (s *MetaSuite) TestSubordinate(c *gc.C) {
65
65
        meta, err := charm.ReadMeta(repoMeta("logging"))
66
 
        c.Assert(err, IsNil)
67
 
        c.Assert(meta.Subordinate, Equals, true)
 
66
        c.Assert(err, gc.IsNil)
 
67
        c.Assert(meta.Subordinate, gc.Equals, true)
68
68
}
69
69
 
70
 
func (s *MetaSuite) TestSubordinateWithoutContainerRelation(c *C) {
 
70
func (s *MetaSuite) TestSubordinateWithoutContainerRelation(c *gc.C) {
71
71
        r := repoMeta("dummy")
72
72
        hackYaml := ReadYaml(r)
73
73
        hackYaml["subordinate"] = true
74
74
        _, err := charm.ReadMeta(hackYaml.Reader())
75
 
        c.Assert(err, ErrorMatches, "subordinate charm \"dummy\" lacks \"requires\" relation with container scope")
 
75
        c.Assert(err, gc.ErrorMatches, "subordinate charm \"dummy\" lacks \"requires\" relation with container scope")
76
76
}
77
77
 
78
 
func (s *MetaSuite) TestScopeConstraint(c *C) {
 
78
func (s *MetaSuite) TestScopeConstraint(c *gc.C) {
79
79
        meta, err := charm.ReadMeta(repoMeta("logging"))
80
 
        c.Assert(err, IsNil)
81
 
        c.Assert(meta.Provides["logging-client"].Scope, Equals, charm.ScopeGlobal)
82
 
        c.Assert(meta.Requires["logging-directory"].Scope, Equals, charm.ScopeContainer)
83
 
        c.Assert(meta.Subordinate, Equals, true)
 
80
        c.Assert(err, gc.IsNil)
 
81
        c.Assert(meta.Provides["logging-client"].Scope, gc.Equals, charm.ScopeGlobal)
 
82
        c.Assert(meta.Requires["logging-directory"].Scope, gc.Equals, charm.ScopeContainer)
 
83
        c.Assert(meta.Subordinate, gc.Equals, true)
84
84
}
85
85
 
86
 
func (s *MetaSuite) TestParseMetaRelations(c *C) {
 
86
func (s *MetaSuite) TestParseMetaRelations(c *gc.C) {
87
87
        meta, err := charm.ReadMeta(repoMeta("mysql"))
88
 
        c.Assert(err, IsNil)
89
 
        c.Assert(meta.Provides["server"], Equals, charm.Relation{
 
88
        c.Assert(err, gc.IsNil)
 
89
        c.Assert(meta.Provides["server"], gc.Equals, charm.Relation{
90
90
                Name:      "server",
91
91
                Role:      charm.RoleProvider,
92
92
                Interface: "mysql",
93
93
                Scope:     charm.ScopeGlobal,
94
94
        })
95
 
        c.Assert(meta.Requires, IsNil)
96
 
        c.Assert(meta.Peers, IsNil)
 
95
        c.Assert(meta.Requires, gc.IsNil)
 
96
        c.Assert(meta.Peers, gc.IsNil)
97
97
 
98
98
        meta, err = charm.ReadMeta(repoMeta("riak"))
99
 
        c.Assert(err, IsNil)
100
 
        c.Assert(meta.Provides["endpoint"], Equals, charm.Relation{
 
99
        c.Assert(err, gc.IsNil)
 
100
        c.Assert(meta.Provides["endpoint"], gc.Equals, charm.Relation{
101
101
                Name:      "endpoint",
102
102
                Role:      charm.RoleProvider,
103
103
                Interface: "http",
104
104
                Scope:     charm.ScopeGlobal,
105
105
        })
106
 
        c.Assert(meta.Provides["admin"], Equals, charm.Relation{
 
106
        c.Assert(meta.Provides["admin"], gc.Equals, charm.Relation{
107
107
                Name:      "admin",
108
108
                Role:      charm.RoleProvider,
109
109
                Interface: "http",
110
110
                Scope:     charm.ScopeGlobal,
111
111
        })
112
 
        c.Assert(meta.Peers["ring"], Equals, charm.Relation{
 
112
        c.Assert(meta.Peers["ring"], gc.Equals, charm.Relation{
113
113
                Name:      "ring",
114
114
                Role:      charm.RolePeer,
115
115
                Interface: "riak",
116
116
                Limit:     1,
117
117
                Scope:     charm.ScopeGlobal,
118
118
        })
119
 
        c.Assert(meta.Requires, IsNil)
 
119
        c.Assert(meta.Requires, gc.IsNil)
120
120
 
121
121
        meta, err = charm.ReadMeta(repoMeta("terracotta"))
122
 
        c.Assert(err, IsNil)
123
 
        c.Assert(meta.Provides["dso"], Equals, charm.Relation{
 
122
        c.Assert(err, gc.IsNil)
 
123
        c.Assert(meta.Provides["dso"], gc.Equals, charm.Relation{
124
124
                Name:      "dso",
125
125
                Role:      charm.RoleProvider,
126
126
                Interface: "terracotta",
127
127
                Optional:  true,
128
128
                Scope:     charm.ScopeGlobal,
129
129
        })
130
 
        c.Assert(meta.Peers["server-array"], Equals, charm.Relation{
 
130
        c.Assert(meta.Peers["server-array"], gc.Equals, charm.Relation{
131
131
                Name:      "server-array",
132
132
                Role:      charm.RolePeer,
133
133
                Interface: "terracotta-server",
134
134
                Limit:     1,
135
135
                Scope:     charm.ScopeGlobal,
136
136
        })
137
 
        c.Assert(meta.Requires, IsNil)
 
137
        c.Assert(meta.Requires, gc.IsNil)
138
138
 
139
139
        meta, err = charm.ReadMeta(repoMeta("wordpress"))
140
 
        c.Assert(err, IsNil)
141
 
        c.Assert(meta.Provides["url"], Equals, charm.Relation{
 
140
        c.Assert(err, gc.IsNil)
 
141
        c.Assert(meta.Provides["url"], gc.Equals, charm.Relation{
142
142
                Name:      "url",
143
143
                Role:      charm.RoleProvider,
144
144
                Interface: "http",
145
145
                Scope:     charm.ScopeGlobal,
146
146
        })
147
 
        c.Assert(meta.Requires["db"], Equals, charm.Relation{
 
147
        c.Assert(meta.Requires["db"], gc.Equals, charm.Relation{
148
148
                Name:      "db",
149
149
                Role:      charm.RoleRequirer,
150
150
                Interface: "mysql",
151
151
                Limit:     1,
152
152
                Scope:     charm.ScopeGlobal,
153
153
        })
154
 
        c.Assert(meta.Requires["cache"], Equals, charm.Relation{
 
154
        c.Assert(meta.Requires["cache"], gc.Equals, charm.Relation{
155
155
                Name:      "cache",
156
156
                Role:      charm.RoleRequirer,
157
157
                Interface: "varnish",
159
159
                Optional:  true,
160
160
                Scope:     charm.ScopeGlobal,
161
161
        })
162
 
        c.Assert(meta.Peers, IsNil)
 
162
        c.Assert(meta.Peers, gc.IsNil)
163
163
}
164
164
 
165
165
var relationsConstraintsTests = []struct {
208
208
        },
209
209
}
210
210
 
211
 
func (s *MetaSuite) TestRelationsConstraints(c *C) {
 
211
func (s *MetaSuite) TestRelationsConstraints(c *gc.C) {
212
212
        check := func(s, e string) {
213
213
                meta, err := charm.ReadMeta(strings.NewReader(s))
214
214
                if e != "" {
215
 
                        c.Assert(err, ErrorMatches, e)
216
 
                        c.Assert(meta, IsNil)
 
215
                        c.Assert(err, gc.ErrorMatches, e)
 
216
                        c.Assert(meta, gc.IsNil)
217
217
                } else {
218
 
                        c.Assert(err, IsNil)
219
 
                        c.Assert(meta, NotNil)
 
218
                        c.Assert(err, gc.IsNil)
 
219
                        c.Assert(meta, gc.NotNil)
220
220
                }
221
221
        }
222
222
        prefix := "name: a\nsummary: b\ndescription: c\n"
239
239
  innocuous: juju-info`, "")
240
240
}
241
241
 
242
 
func (s *MetaSuite) TestCheckMismatchedRelationName(c *C) {
 
242
func (s *MetaSuite) TestCheckMismatchedRelationName(c *gc.C) {
243
243
        // This  Check case cannot be covered by the above
244
244
        // TestRelationsConstraints tests.
245
245
        meta := charm.Meta{
255
255
                },
256
256
        }
257
257
        err := meta.Check()
258
 
        c.Assert(err, ErrorMatches, `charm "foo" has mismatched role "peer"; expected "provider"`)
 
258
        c.Assert(err, gc.ErrorMatches, `charm "foo" has mismatched role "peer"; expected "provider"`)
259
259
}
260
260
 
261
 
func (s *MetaSuite) TestCheckMismatchedRole(c *C) {
 
261
func (s *MetaSuite) TestCheckMismatchedRole(c *gc.C) {
262
262
        // This  Check case cannot be covered by the above
263
263
        // TestRelationsConstraints tests.
264
264
        meta := charm.Meta{
273
273
                },
274
274
        }
275
275
        err := meta.Check()
276
 
        c.Assert(err, ErrorMatches, `charm "foo" has mismatched relation name ""; expected "foo"`)
 
276
        c.Assert(err, gc.ErrorMatches, `charm "foo" has mismatched relation name ""; expected "foo"`)
277
277
}
278
278
 
279
279
// Test rewriting of a given interface specification into long form.
286
286
// This test ensures test coverage on each of these branches, along
287
287
// with ensuring the conversion object properly raises SchemaError
288
288
// exceptions on invalid data.
289
 
func (s *MetaSuite) TestIfaceExpander(c *C) {
 
289
func (s *MetaSuite) TestIfaceExpander(c *gc.C) {
290
290
        e := charm.IfaceExpander(nil)
291
291
 
292
292
        path := []string{"<pa", "th>"}
293
293
 
294
294
        // Shorthand is properly rewritten
295
295
        v, err := e.Coerce("http", path)
296
 
        c.Assert(err, IsNil)
297
 
        c.Assert(v, DeepEquals, map[string]interface{}{"interface": "http", "limit": nil, "optional": false, "scope": string(charm.ScopeGlobal)})
 
296
        c.Assert(err, gc.IsNil)
 
297
        c.Assert(v, gc.DeepEquals, map[string]interface{}{"interface": "http", "limit": nil, "optional": false, "scope": string(charm.ScopeGlobal)})
298
298
 
299
299
        // Defaults are properly applied
300
300
        v, err = e.Coerce(map[string]interface{}{"interface": "http"}, path)
301
 
        c.Assert(err, IsNil)
302
 
        c.Assert(v, DeepEquals, map[string]interface{}{"interface": "http", "limit": nil, "optional": false, "scope": string(charm.ScopeGlobal)})
 
301
        c.Assert(err, gc.IsNil)
 
302
        c.Assert(v, gc.DeepEquals, map[string]interface{}{"interface": "http", "limit": nil, "optional": false, "scope": string(charm.ScopeGlobal)})
303
303
 
304
304
        v, err = e.Coerce(map[string]interface{}{"interface": "http", "limit": 2}, path)
305
 
        c.Assert(err, IsNil)
306
 
        c.Assert(v, DeepEquals, map[string]interface{}{"interface": "http", "limit": int64(2), "optional": false, "scope": string(charm.ScopeGlobal)})
 
305
        c.Assert(err, gc.IsNil)
 
306
        c.Assert(v, gc.DeepEquals, map[string]interface{}{"interface": "http", "limit": int64(2), "optional": false, "scope": string(charm.ScopeGlobal)})
307
307
 
308
308
        v, err = e.Coerce(map[string]interface{}{"interface": "http", "optional": true}, path)
309
 
        c.Assert(err, IsNil)
310
 
        c.Assert(v, DeepEquals, map[string]interface{}{"interface": "http", "limit": nil, "optional": true, "scope": string(charm.ScopeGlobal)})
 
309
        c.Assert(err, gc.IsNil)
 
310
        c.Assert(v, gc.DeepEquals, map[string]interface{}{"interface": "http", "limit": nil, "optional": true, "scope": string(charm.ScopeGlobal)})
311
311
 
312
312
        // Invalid data raises an error.
313
313
        v, err = e.Coerce(42, path)
314
 
        c.Assert(err, ErrorMatches, "<path>: expected map, got 42")
 
314
        c.Assert(err, gc.ErrorMatches, "<path>: expected map, got 42")
315
315
 
316
316
        v, err = e.Coerce(map[string]interface{}{"interface": "http", "optional": nil}, path)
317
 
        c.Assert(err, ErrorMatches, "<path>.optional: expected bool, got nothing")
 
317
        c.Assert(err, gc.ErrorMatches, "<path>.optional: expected bool, got nothing")
318
318
 
319
319
        v, err = e.Coerce(map[string]interface{}{"interface": "http", "limit": "none, really"}, path)
320
 
        c.Assert(err, ErrorMatches, "<path>.limit: unexpected value.*")
 
320
        c.Assert(err, gc.ErrorMatches, "<path>.limit: unexpected value.*")
321
321
 
322
322
        // Can change default limit
323
323
        e = charm.IfaceExpander(1)
324
324
        v, err = e.Coerce(map[string]interface{}{"interface": "http"}, path)
325
 
        c.Assert(err, IsNil)
326
 
        c.Assert(v, DeepEquals, map[string]interface{}{"interface": "http", "limit": int64(1), "optional": false, "scope": string(charm.ScopeGlobal)})
 
325
        c.Assert(err, gc.IsNil)
 
326
        c.Assert(v, gc.DeepEquals, map[string]interface{}{"interface": "http", "limit": int64(1), "optional": false, "scope": string(charm.ScopeGlobal)})
327
327
}
328
328
 
329
 
func (s *MetaSuite) TestMetaHooks(c *C) {
 
329
func (s *MetaSuite) TestMetaHooks(c *gc.C) {
330
330
        meta, err := charm.ReadMeta(repoMeta("wordpress"))
331
 
        c.Assert(err, IsNil)
 
331
        c.Assert(err, gc.IsNil)
332
332
        hooks := meta.Hooks()
333
333
        expectedHooks := map[string]bool{
334
334
                "install":                           true,
357
357
                "url-relation-departed":             true,
358
358
                "url-relation-broken":               true,
359
359
        }
360
 
        c.Assert(hooks, DeepEquals, expectedHooks)
 
360
        c.Assert(hooks, gc.DeepEquals, expectedHooks)
361
361
}
362
362
 
363
 
func (s *MetaSuite) TestCodecRoundTripEmpty(c *C) {
 
363
func (s *MetaSuite) TestCodecRoundTripEmpty(c *gc.C) {
364
364
        for i, codec := range codecs {
365
365
                c.Logf("codec %d", i)
366
366
                empty_input := charm.Meta{}
367
367
                data, err := codec.Marshal(empty_input)
368
 
                c.Assert(err, IsNil)
 
368
                c.Assert(err, gc.IsNil)
369
369
                var empty_output charm.Meta
370
370
                err = codec.Unmarshal(data, &empty_output)
371
 
                c.Assert(err, IsNil)
372
 
                c.Assert(empty_input, DeepEquals, empty_output)
 
371
                c.Assert(err, gc.IsNil)
 
372
                c.Assert(empty_input, gc.DeepEquals, empty_output)
373
373
        }
374
374
}
375
375
 
376
 
func (s *MetaSuite) TestCodecRoundTrip(c *C) {
 
376
func (s *MetaSuite) TestCodecRoundTrip(c *gc.C) {
377
377
        var input = charm.Meta{
378
378
                Name:        "Foo",
379
379
                Summary:     "Bar",
410
410
        for i, codec := range codecs {
411
411
                c.Logf("codec %d", i)
412
412
                data, err := codec.Marshal(input)
413
 
                c.Assert(err, IsNil)
 
413
                c.Assert(err, gc.IsNil)
414
414
                var output charm.Meta
415
415
                err = codec.Unmarshal(data, &output)
416
 
                c.Assert(err, IsNil)
417
 
                c.Assert(input, DeepEquals, output)
 
416
                c.Assert(err, gc.IsNil)
 
417
                c.Assert(input, gc.DeepEquals, output)
418
418
        }
419
419
}
420
420
 
457
457
        {"ifce-peer", "peer", charm.RolePeer, charm.ScopeContainer, true, false},
458
458
}
459
459
 
460
 
func (s *MetaSuite) TestImplementedBy(c *C) {
 
460
func (s *MetaSuite) TestImplementedBy(c *gc.C) {
461
461
        for i, t := range implementedByTests {
462
462
                c.Logf("test %d", i)
463
463
                r := charm.Relation{
466
466
                        Role:      t.role,
467
467
                        Scope:     t.scope,
468
468
                }
469
 
                c.Assert(r.ImplementedBy(&dummyCharm{}), Equals, t.match)
470
 
                c.Assert(r.IsImplicit(), Equals, t.implicit)
 
469
                c.Assert(r.ImplementedBy(&dummyCharm{}), gc.Equals, t.match)
 
470
                c.Assert(r.IsImplicit(), gc.Equals, t.implicit)
471
471
        }
472
472
}
473
473